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

#ifndef __B_H__
#define __B_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef WIN32
#  include <sys/mman.h>
#  include <unistd.h>
#  include <errno.h>
#  include <sys/time.h>
#else
#  include <Windows.h>
#  include <Winbase.h>
#  include <Winsock2.h>
#  include <sys/timeb.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>

#include <time.h>

#include <fcntl.h>

/**
 * b_t:
 *
 * The principal data struct. You must have one of this for any B storage.
 **/
typedef struct b_t b_t;

/**
 * b_iterator_type_t:
 *
 * The data struct for a Type Iterator. You should use this data struct
 * only with b_iterator_type_*() functions.
 **/
typedef struct b_iterator_type_t b_iterator_type_t;

/**
 * b_iterator_triple_t:
 *
 * The data struct for a Triple Iterator. You should use this data struct
 * only with b_iterator_triple_*() functions.
 **/
typedef struct b_iterator_triple_t b_iterator_triple_t;

/**
 * b_timer_t:
 *
 * The data struct for a Timer. You should use this data struct only with 
 * b_timer_*() functions.
 **/
typedef struct b_timer_t b_timer_t;

/**
 * b_int64:
 *
 * Variable 64bit signed.
 **/

/**
 * b_uint64:
 *
 * Variable 64bit unsigned.
 **/

#ifdef B_INT64_TYPE
typedef B_INT64_TYPE b_int64;
typedef unsigned B_INT64_TYPE b_uint64;
#elif defined(_MSC_VER) || defined(__BORLANDC__)
typedef __int64 b_int64;
typedef unsigned __int64 b_uint64;
#else
typedef long long int b_int64;
typedef unsigned long long int b_uint64;
#endif

/**
 * b_error_t:
 * @B_OK: Success.
 * @B_ERROR_MEMORY: Error in a alloc operation.
 * @B_ERROR_POSIX: System error. If you are using a Posix system, read the 
 * errno variable.
 * @B_ERROR_INTERNAL: Internal error. It shouldn't be.
 * @B_ERROR_DATA: Pls, read the documentation.
 *
 * The list of errors for this library.
 **/
typedef enum
{
  B_OK = 0,

  B_ERROR_MEMORY,
  B_ERROR_POSIX,
  B_ERROR_INTERNAL,

  B_ERROR_DATA
} b_error_t;

/**
 * b_type_t:
 * @B_TYPE_URI: The URI elements.
 * @B_TYPE_PROPERTY: The properties are in a separated B-Tree.
 * @B_TYPE_LITERAL: The Literal elements.
 * @B_TYPE_BNODE: The BNode elements.
 * @B_TYPE_CONTEXT: The Contexts are URI but they are into a separated B-Tree.
 * @B_TYPE_DATATYPE: Like the Contexts the Datatypes are into a separated
 * B-Tree.
 *
 * The types of simple elements.
 */
typedef enum
{
  B_TYPE_URI,
  B_TYPE_PROPERTY,
  B_TYPE_LITERAL,
  B_TYPE_BNODE,
  B_TYPE_CONTEXT,
  B_TYPE_DATATYPE
} b_type_t;

/**
 * b_triple_t:
 * @subject_uri: subject of this triple if the subject is a URI.
 * @subject_uri_len: The length of the subject_uri.
 * @subject_bnode: subject of this triple if the subject is a BNODE.
 * @subject_bnode_len: The length of the subject_bnode.
 * @property: property of this triple.
 * @property_len: The length of the property.
 * @object_uri: object of this triple if the object is a URI.
 * @object_uri_len: The length of the object_uri.
 * @object_bnode: object of this triple if the object is a BNODE.
 * @object_bnode_len: The length of the object_bnode.
 * @object_literal: object of this triple if the object is a LITERAL.
 * @object_literal_len: The length of the object_literal.
 * @context: context of this triple. It can be NULL.
 * @context_len: The length of the context.
 * @datatype: datatype of this triple. It can be NULL.
 * @datatype_len: The length of the datatype.
 * @lang: lang of this triple. It can be NULL.
 * @lang_len: The length of the lang.
 *
 * This struct describes a RDF triple.
 */
typedef struct
{
  unsigned char *	subject_uri;
  b_uint64		subject_uri_len;

  unsigned char *	subject_bnode;
  b_uint64		subject_bnode_len;

  unsigned char *	property;
  b_uint64		property_len;

  unsigned char *	object_uri;
  b_uint64		object_uri_len;

  unsigned char *	object_bnode;
  b_uint64		object_bnode_len;

  unsigned char *	object_literal;
  b_uint64		object_literal_len;

  unsigned char *	context;
  b_uint64		context_len;

  unsigned char *	datatype;
  b_uint64		datatype_len;

  unsigned char *	lang;
  b_uint64		lang_len;
} b_triple_t;

/* List of functions */

b_error_t	b_new		(b_t **			data,
				 unsigned char *	path);

b_error_t	b_set_max_cache	(b_t *			data,
				 b_uint64		max_cache);

b_error_t	b_get_max_cache	(b_t *			data,
				 b_uint64 *		max_cache);

b_error_t	b_destroy	(b_t *			data);

b_error_t	b_sync		(b_t *			data);

b_error_t	b_add_type	(b_t *			data,
				 b_type_t		type,
				 unsigned char *	what,
				 b_uint64		len); 

b_error_t	b_add_triple	(b_t *			data,
				 b_triple_t *		triple);

b_error_t	b_get_type	(b_t *			data,
				 b_type_t		type,
				 unsigned char *	what,
				 b_uint64		len,
				 char *			exist);

b_error_t	b_get_triple	(b_t *			data,
				 b_triple_t *		triple,
				 char *			exist);

b_error_t	b_iterator_type_new
				(b_t *			data,
				 b_type_t		type,
				 b_iterator_type_t **	iterator);

b_error_t	b_iterator_type_step
				(b_iterator_type_t *	iterator,
				 unsigned char **	ret,
				 b_uint64 *		ret_size);

b_error_t	b_iterator_type_destroy
				(b_iterator_type_t *	iterator);

b_error_t	b_iterator_triple_new
				(b_t *			data,
				 b_iterator_triple_t **	iterator,
				 b_triple_t *		compare);

b_error_t	b_iterator_triple_step
				 (b_iterator_triple_t *	iterator,
				 b_triple_t **		triple);

b_error_t	b_iterator_triple_destroy
				(b_iterator_triple_t *	iterator);

b_error_t	b_remove_triple	(b_t *			data,
				 b_triple_t *		triple);

b_error_t	b_remove_type	(b_t *			data,
				 b_type_t		type,
				 unsigned char *	what,
				 b_uint64		len);

b_error_t	b_count_type	(b_t *			data,
				 b_type_t		type,
				 b_uint64 *		count);

b_error_t	b_count_triple	(b_t *			data,
				 b_uint64 *		count);

char *		b_strerror	(b_error_t		error);

b_error_t	b_triple_new	(b_triple_t **		triple,
				 unsigned char *	subject_uri,
				 b_uint64		subject_uri_len,
				 unsigned char *	subject_bnode,
				 b_uint64		subject_bnode_len,
				 unsigned char *	property,
				 b_uint64		property_len,
				 unsigned char *	object_uri,
				 b_uint64		object_uri_len,
				 unsigned char *	object_bnode,
				 b_uint64		object_bnode_len,
				 unsigned char *	object_literal,
				 b_uint64		object_literal_len,
				 unsigned char *	context,
				 b_uint64		context_len,
				 unsigned char *	datatype,
				 b_uint64		datatype_len,
				 unsigned char *	lang,
				 b_uint64		lang_len);

b_error_t	b_triple_new_incomplete
				(b_triple_t **		triple,
				 unsigned char *	subject_uri,
				 b_uint64		subject_uri_len,
				 unsigned char *	subject_bnode,
				 b_uint64		subject_bnode_len,
				 unsigned char *	property,
				 b_uint64		property_len,
				 unsigned char *	object_uri,
				 b_uint64		object_uri_len,
				 unsigned char *	object_bnode,
				 b_uint64		object_bnode_len,
				 unsigned char *	object_literal,
				 b_uint64		object_literal_len,
				 unsigned char *	context,
				 b_uint64		context_len,
				 unsigned char *	datatype,
				 b_uint64		datatype_len,
				 unsigned char *	lang,
				 b_uint64		lang_len);

b_error_t	b_triple_destroy
				(b_triple_t *		triple);

b_error_t	b_triple_print	(FILE *			file,
				 b_triple_t *		triple);

b_error_t	b_timer_new	(b_timer_t **		timer);

b_error_t	b_timer_destroy	(b_timer_t *		timer,
				 b_uint64 *		nanosec);

b_error_t	b_timer_start	(b_timer_t *		timer);

b_error_t	b_timer_stop	(b_timer_t *		timer);

b_error_t	b_timer_diff	(b_timer_t *		timer,
				 b_uint64 *		nanosec);

#ifdef B_INTERNAL
#  include <b_internal.h>
#endif

#endif

/* EOF */
