/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <b.h>

/**
 * b_new:
 * @data: The pointer to the b_t struct.
 * @path: The path the name of the DB.
 * @returns: the error code.
 *
 * Creates a new b_t struct.
 *
 * The new b_t will be allocated and it should be destroied with
 * b_destroy(). The path is name of the DB. You should read the
 * introduction to understand how the B storage uses this path.
 **/
b_error_t
b_new (b_t ** data, unsigned char *path)
{
  b_error_t error;
  char *file;
  b_uint64 n;

  B_LOG2 ("b_new: %p %p", data, path);

  if (!data || !path)
    return B_ERROR_DATA;

  /* allocation: */
  if (!(*data = (b_t *) calloc (1, sizeof (b_t))))
    return B_ERROR_MEMORY;

  n = B_MAX_CACHE_ITEMS * (B_HASH_SIZE + sizeof (b_uint64));

#define B_NEW_FD( p , where ) \
  if (! \
      (file = \
       malloc (sizeof (char) * (strlen ((char *)path) + strlen (p) + 1)))) \
    { \
      b_destroy (*data); \
      return B_ERROR_MEMORY; \
    } \
  \
  sprintf (file, "%s%s", path, p); \
  error = b_io_new (&(*data)->where, (unsigned char *)file, n); \
  free(file); \
  \
  if (error!=B_OK) \
    { \
      b_destroy (*data); \
      return error; \
    }

  /* Set the max cache: */
  (*data)->max_cache = B_MAX_CACHE_ITEMS;

  /* Open the file descriptor for the cache systems: */
  B_NEW_FD (B_PATH_URIS_DATA, uris_io);
  B_NEW_FD (B_PATH_PROPERTIES_DATA, properties_io);
  B_NEW_FD (B_PATH_LITERALS_DATA, literals_io);
  B_NEW_FD (B_PATH_BNODES_DATA, bnodes_io);
  B_NEW_FD (B_PATH_CONTEXTS_DATA, contexts_io);
  B_NEW_FD (B_PATH_DATATYPES_DATA, datatypes_io);
  B_NEW_FD (B_PATH_TRIPLES_DATA, triples_io);

  if (!
      (file =
       malloc (sizeof (char) *
	       (strlen ((char *) path) + strlen (B_PATH_JOURNALING_DATA) +
		1))))
    {
      b_destroy (*data);
      return B_ERROR_MEMORY;
    }

  sprintf (file, "%s%s", path, B_PATH_JOURNALING_DATA);
  error = b_io_new (&(*data)->journaling_io, (unsigned char *) file, n);
  (*data)->journaling_file = (unsigned char *) file;

  if (error != B_OK)
    {
      b_destroy (*data);
      return error;
    }

  /* if it return a strange value, the DB is broken... */
  if ((error = b_journaling_recover (*data)) != B_OK)
    {
      b_destroy (*data);
      return error;
    }

  if ((error = b_count_triple_cache (*data, &(*data)->triples_total)))
    {
      b_destroy (*data);
      return error;
    }

  return B_OK;
}

/* EOF */
