/* 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_set_max_cache:
 * @data: The pointer to the b_t struct
 * @max_cache: The new max_cache.
 * @returns: the error code.
 *
 * Changes the MaxCache.
 *
 * If you want change the max cache value in any moment you must use this
 * function.
 */
b_error_t
b_set_max_cache (b_t * data, b_uint64 max_cache)
{
  b_error_t error;

  B_LOG2 ("b_set_max_cache: %p %d", data, (int) max_cache);

  if (!data)
    return B_ERROR_DATA;

  if (max_cache < B_MIN_CACHE_ITEMS)
    max_cache = B_MIN_CACHE_ITEMS;

  data->max_cache = max_cache;

  if ((error = b_sync_tcache (data)) != B_OK)
    return error;

  while (data->triples_cache_items > data->max_cache)
    b_tcache_destroy_single (&data->triples_cache, &data->triples_cache_items,
			     data->triples_cache);

  return B_OK;
}

/**
 * b_get_max_cache:
 * @data: The pointer to the b_t struct
 * @max_cache: A pointer to a b_uint64 variable.
 * @returns: The error code;
 *
 * Returns the value of the MaXCache.
 */
b_error_t
b_get_max_cache (b_t * data, b_uint64 * max_cache)
{
  B_LOG2 ("b_get_max_cache: %p %p", data, max_cache);

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

  *max_cache = data->max_cache;
  return B_OK;
}

b_error_t
b_cache_new (unsigned char *hash, unsigned char *what, b_uint64 len,
	     b_uint64 pointer, b_cache_t ** ret)
{
  b_cache_t *new;

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

  b_hash_copy (new->hash, hash);
  new->what = b_string_alloc (what, len);
  new->len = len;
  new->pointer = pointer;

  *ret = new;
  return B_OK;
}

/* Destroy a cache list: */
b_error_t
b_cache_destroy (b_cache_t * cache)
{
  B_LOG1 ("b_cache_destroy: %p", cache);

  if (!cache)
    return B_ERROR_DATA;

  if (cache->what)
    free (cache->what);

  free (cache);

  return B_OK;
}

b_error_t
b_cache_copy (b_cache_t * src, b_cache_t ** dst)
{
  b_cache_t *new;

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

  if (!src)
    return B_ERROR_DATA;

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

  b_hash_copy (new->hash, src->hash);

  if (src->what)
    {
      new->len = src->len;

      if (!(new->what = b_string_alloc (src->what, src->len)))
	{
	  free (new);
	  return B_ERROR_MEMORY;
	}
    }

  new->pointer = src->pointer;

  *dst = new;
  return B_OK;
}

b_error_t
b_cache_copy_easy (b_cache_t * src, b_cache_t ** dst)
{
  b_cache_t *new;

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

  if (!src)
    return B_ERROR_DATA;

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

  b_hash_copy (new->hash, src->hash);

  new->len = src->len;
  new->pointer = src->pointer;

  *dst = new;
  return B_OK;
}

/* EOF */
