/* 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_destroy:
 * @data: The pointer to the b_t you want destroy.
 * @returns: The error code.
 *
 * Destroies a b_t data struct and closes the storage. This must be the
 * last operation with that data.
 **/
b_error_t
b_destroy (b_t * data)
{
  B_LOG1 ("b_destroy: %p", data);

  if (!data)
    return B_ERROR_DATA;

  /* Sync datas: */
  b_sync (data);

  /* Remove the cache: */
  if (data->triples_cache)
    b_tcache_destroy (&data->triples_cache, &data->triples_cache_items);

  if (data->hashes_cache)
    b_hash_destroy (&data->hashes_cache, &data->hashes_cache_items);

  if (data->triples_empty)
    b_tempty_destroy (&data->triples_empty);

  /* Close the IO: */
  if (data->uris_io)
    b_io_destroy (data->uris_io);

  if (data->properties_io)
    b_io_destroy (data->properties_io);

  if (data->literals_io)
    b_io_destroy (data->literals_io);

  if (data->bnodes_io)
    b_io_destroy (data->bnodes_io);

  if (data->contexts_io)
    b_io_destroy (data->contexts_io);

  if (data->datatypes_io)
    b_io_destroy (data->datatypes_io);

  if (data->triples_io)
    b_io_destroy (data->triples_io);

  if (data->journaling_io)
    b_io_destroy (data->journaling_io);

  if (data->journaling_file)
    {
      remove ((char *) data->journaling_file);
      free (data->journaling_file);
    }

  /* Free memory: */
  free (data);
  return B_OK;
}

/* EOF */
