/* 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_add_type:
 * @data: The pointer to the b_t struct.
 * @type: The type of the item.
 * @what: The data as string.
 * @len: The len of what. If it is 0, this function uses strlen on what.
 * @returns: the error code.
 *
 * Adds a item into the storage. No dublicate items will be inserted.
 **/
b_error_t
b_add_type (b_t * data, b_type_t type, unsigned char *what, b_uint64 len)
{
  b_io_t *io;
  b_error_t error;
  b_cache_t *exist = NULL;
  unsigned char hash[B_HASH_SIZE];

  B_LOG4 ("b_add_type: %p %d %p %d", data, type, what, (int) len);

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

  len = len > 0 ? len : strlen ((char *) what);
  b_hash_new (data, what, len, hash);

  /* does it already exist? */
  if ((error =
       b_get_type_cache_hash (data, type, hash, what, len, &exist)) != B_OK)
    return error;

  if (exist)
    {
      b_cache_destroy (exist);
      return B_OK;
    }

  if ((error = b_get_type_internal (data, type, &io)) != B_OK)
    return error;

  return b_sync_cache (data, io, hash, what, len);
}

/**
 * b_add_triple:
 * @data: The pointer to the b_t struct.
 * @triple: The triple. You should use b_triple_new() to create it.
 * @returns: the error code.
 *
 * Adds a triple and the internal elements into the storage. This function
 * doesn't insert a triple if it exists already in the storage.
 **/
b_error_t
b_add_triple (b_t * data, b_triple_t * triple)
{
  b_tcache_t *status;
  b_error_t error;

  B_LOG2 ("b_add_triple: %p %p", data, triple);

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

  if (b_triple_check (triple) != B_OK)
    return B_ERROR_DATA;

  /* does it already exist? */
  if ((error = b_get_triple_cache (data, triple, &status)) != B_OK)
    return error;

  if (status)
    return B_OK;

  /* Add to the cache list: */
  return b_add_triple_cache (data, triple, 0, 0);
}

/* This function adds a triple to cache: */
b_error_t
b_add_triple_cache (b_t * data, b_triple_t * triple, b_uint64 tmap_pointer,
		    unsigned char tmap_index)
{
  b_tcache_t *cache;
  b_error_t error;

  B_LOG4 ("b_add_triple_cache: %p %p %d %d", data, triple, (int) tmap_pointer,
	  (int) tmap_index);

  /* Allocation: */
  if (!(cache = (b_tcache_t *) calloc (1, sizeof (b_tcache_t))))
    return B_ERROR_MEMORY;

#define B_ADD_TRIPLES_CACHE( str, len , type , where ) \
    if ((error = b_add_type (data, type, str, len)) != B_OK) \
      { \
        b_tcache_destroy_single (NULL, NULL, cache); \
        return error; \
      } \
    \
    if ((error = b_get_type_cache (data, type, str, len, &cache->where)) \
		 != B_OK) \
      { \
        b_tcache_destroy_single (NULL, NULL, cache); \
        return error; \
      } \
    \
    if (!cache->where) \
      { \
        b_tcache_destroy_single (NULL, NULL, cache); \
        return B_ERROR_INTERNAL; \
      }

  if (triple->subject_uri)
    {
      B_ADD_TRIPLES_CACHE (triple->subject_uri, triple->subject_uri_len,
			   B_TYPE_URI, subject_uri);
    }
  else if (triple->subject_bnode)
    {
      B_ADD_TRIPLES_CACHE (triple->subject_bnode, triple->subject_bnode_len,
			   B_TYPE_BNODE, subject_bnode);
    }

  B_ADD_TRIPLES_CACHE (triple->property, triple->property_len,
		       B_TYPE_PROPERTY, property);

  if (triple->object_uri)
    {
      B_ADD_TRIPLES_CACHE (triple->object_uri, triple->object_uri_len,
			   B_TYPE_URI, object_uri);
    }

  else if (triple->object_bnode)
    {
      B_ADD_TRIPLES_CACHE (triple->object_bnode, triple->object_bnode_len,
			   B_TYPE_BNODE, object_bnode);
    }

  else if (triple->object_literal)
    {
      B_ADD_TRIPLES_CACHE (triple->object_literal, triple->object_literal_len,
			   B_TYPE_LITERAL, object_literal);
    }

  if (triple->context)
    {
      B_ADD_TRIPLES_CACHE (triple->context, triple->context_len,
			   B_TYPE_CONTEXT, context);
    }

  if (triple->datatype)
    {
      B_ADD_TRIPLES_CACHE (triple->datatype, triple->datatype_len,
			   B_TYPE_DATATYPE, datatype);
    }

  if (triple->lang)
    {
      B_ADD_TRIPLES_CACHE (triple->lang, triple->lang_len, B_TYPE_LITERAL,
			   lang);
    }

  cache->tmap_index = tmap_index;
  cache->tmap_pointer = tmap_pointer;

  if (data->triples_cache_items)
    {
      cache->next = data->triples_cache;
      data->triples_cache->prev = cache;
    }

  data->triples_cache = cache;
  data->triples_cache_items++;

  if (!tmap_pointer)
    {
      /* Incrementing the instances for the elements: */
      if (b_sync_instances (data, cache, 1))
	return B_ERROR_INTERNAL;

      data->triples_total++;
    }

  if (data->triples_cache_items > data->max_cache)
    {
      b_error_t error;
      b_uint64 half = data->triples_cache_items / 2;
      b_tcache_t *next;

      /* Sync all the list because any item will active
       * this routine so, 1 write is better then 1024. */
      if ((error = b_sync_tcache (data)) != B_OK)
	return error;

      for (cache = data->triples_cache; cache && half;
	   cache = cache->next, half--);

      /* I destroy half cache (totaly random - the random is always the better
       * solution: */
      while (cache)
	{
	  next = cache->next;

	  b_tcache_destroy_single (&data->triples_cache,
				   &data->triples_cache_items, cache);
	  cache = next;
	}
    }

  return B_OK;
}

/* EOF */
