/* 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>

/* Destroy the triple cache list: */
b_error_t
b_tcache_destroy (b_tcache_t ** list, b_uint64 * len)
{
  B_LOG2 ("b_tcache_destroy: %p %p", list, len);

  if (!list || !*list)
    return B_ERROR_DATA;

  while (*list)
    b_tcache_destroy_single (list, len, *list);

  return B_OK;
}

/* This function destroies a triple cache item: */
b_error_t
b_tcache_destroy_single (b_tcache_t ** cache_root, b_uint64 * numb,
			 b_tcache_t * cache)
{
  B_LOG3 ("b_tcache_destroy_single: %p %p %p", cache_root, numb, cache);

  if (!cache)
    return B_ERROR_DATA;

  if (cache_root && cache == *cache_root)
    *cache_root = cache->next;

  if (cache->prev)
    cache->prev->next = cache->next;

  if (cache->next)
    cache->next->prev = cache->prev;

  if (cache->subject_uri)
    b_cache_destroy (cache->subject_uri);

  if (cache->subject_bnode)
    b_cache_destroy (cache->subject_bnode);

  if (cache->property)
    b_cache_destroy (cache->property);

  if (cache->object_uri)
    b_cache_destroy (cache->object_uri);

  if (cache->object_bnode)
    b_cache_destroy (cache->object_bnode);

  if (cache->object_literal)
    b_cache_destroy (cache->object_literal);

  if (cache->context)
    b_cache_destroy (cache->context);

  if (cache->datatype)
    b_cache_destroy (cache->datatype);

  if (cache->lang)
    b_cache_destroy (cache->lang);

  free (cache);

  if (numb)
    (*numb)--;

  return B_OK;
}

b_error_t
b_tcache_copy (b_tcache_t * src, b_tcache_t ** dst)
{
  b_tcache_t *new;
  b_error_t error;

  B_LOG2 ("b_tcache_copy: %p %p", src, dst);

  if (!src)
    return B_ERROR_DATA;

  if (!(new = (b_tcache_t *) calloc (1, sizeof (b_tcache_t))))
    return B_ERROR_MEMORY;

#define B_COPY_TCACHE( x ) \
  if (src->x) \
    { \
      if ((error = b_cache_copy (src->x, &new->x)) != B_OK) \
        { \
	  b_tcache_destroy_single (NULL, NULL, new); \
	  return error; \
        } \
    }

  B_COPY_TCACHE (subject_uri);
  B_COPY_TCACHE (subject_bnode);
  B_COPY_TCACHE (property);
  B_COPY_TCACHE (object_uri);
  B_COPY_TCACHE (object_bnode);
  B_COPY_TCACHE (object_literal);
  B_COPY_TCACHE (context);
  B_COPY_TCACHE (datatype);
  B_COPY_TCACHE (lang);

  new->tmap_index = src->tmap_index;
  new->tmap_pointer = src->tmap_pointer;

  *dst = new;
  return B_OK;
}

b_error_t
b_tcache_copy_easy (b_tcache_t * src, b_tcache_t ** dst)
{
  b_tcache_t *new;
  b_error_t error;

  B_LOG2 ("b_tcache_copy_easy: %p %p", src, dst);

  if (!src)
    return B_ERROR_DATA;

  if (!(new = (b_tcache_t *) calloc (1, sizeof (b_tcache_t))))
    return B_ERROR_MEMORY;

#define B_COPY_TCACHE_EASY( x ) \
  if (src->x) \
    { \
      if ((error = b_cache_copy_easy (src->x, &new->x)) != B_OK) \
        { \
	  b_tcache_destroy_single (NULL, NULL, new); \
	  return error; \
        } \
    }

  B_COPY_TCACHE_EASY (subject_uri);
  B_COPY_TCACHE_EASY (subject_bnode);
  B_COPY_TCACHE_EASY (property);
  B_COPY_TCACHE_EASY (object_uri);
  B_COPY_TCACHE_EASY (object_bnode);
  B_COPY_TCACHE_EASY (object_literal);
  B_COPY_TCACHE_EASY (context);
  B_COPY_TCACHE_EASY (datatype);
  B_COPY_TCACHE_EASY (lang);

  new->tmap_index = src->tmap_index;
  new->tmap_pointer = src->tmap_pointer;

  *dst = new;
  return B_OK;
}

/* EOF */
