/* 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_count_type:
 * @data: The pointer to a b_t struct
 * @type: The type of elements you want count.
 * @count: The pointer to a b_uint64. It'll contain the number of elements.
 * @returns: The error code.
 *
 * counts the elements of a b_type_t into the storage.
 **/
b_error_t
b_count_type (b_t * data, b_type_t type, b_uint64 * count)
{
  b_map_t map;
  b_error_t error;
  b_io_t *io;
  b_uint64 pointer;

  B_LOG3 ("b_count_type_cache: %p %d %p", data, type, count);

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

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

  *count = 0;
  pointer = b_io_seek (io, sizeof (b_uint64), SEEK_SET);

  while (1)
    {
      /* Hash and len is the second variable of the b_map_t struct: */
      if (b_io_read (io, map.hash, sizeof (map.hash)) <= 0 ||
	  b_io_read (io, &map.len, sizeof (map.len)) <= 0)
	break;

      if (pointer != sizeof (b_uint64) && b_hash_empty (map.hash))
	(*count)++;

      pointer =
	b_io_seek (io,
		   sizeof (map) - sizeof (map.hash) - sizeof (map.len) +
		   map.len, SEEK_CUR);

    }

  return B_OK;
}

/**
 * b_count_triple:
 * @data: The pointer to a b_t struct
 * @count: The pointer to a b_uint64. It'll contain the number of triples.
 * @returns: The error code.
 *
 * counts the triples into the Storage.
 **/
b_error_t
b_count_triple (b_t * data, b_uint64 * count)
{
  B_LOG2 ("b_count_triple: %p %p", data, count);

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

  if (data->triples_total == data->triples_cache_items)
    {
      *count = data->triples_cache_items;
      return B_OK;
    }

  return b_count_triple_cache (data, count);
}

b_error_t
b_count_triple_cache (b_t * data, b_uint64 * count)
{
  b_tcache_t *c;
  b_tmap_t tmap;
  b_error_t error;
  b_uint64 pointer;
  int i;

  B_LOG2 ("b_count_triple_cache: %p %p", data, count);

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

  (*count) = 0;
  pointer = b_io_seek (data->triples_io, sizeof (b_uint64), SEEK_SET);

  while (1)
    {
      if (b_io_read (data->triples_io, &tmap.map, sizeof (tmap.map)) <= 0)
	break;

      if ((error = b_tempty_check (data, &tmap, pointer, 0)) != B_OK)
	return error;

      for (i = 0; i < B_TMAP_NUMB; i++)
	if (tmap.map[i])
	  (*count)++;

      pointer =
	b_io_seek (data->triples_io,
		   sizeof (tmap) - sizeof (tmap.map) +
		   B_TRIPLE_SIZE * B_TMAP_NUMB, SEEK_CUR);
    }

  /* No sync items: */
  for (c = data->triples_cache; c; c = c->next)
    if (!c->tmap_pointer)
      (*count)++;

  return B_OK;
}

/* EOF */
