/* 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_INTERNAL_H__
#define __B_INTERNAL_H__

#define B_PATH_URIS_DATA	"_uris.data"
#define B_PATH_PROPERTIES_DATA	"_properties.data"
#define B_PATH_LITERALS_DATA	"_literals.data"
#define B_PATH_BNODES_DATA	"_bnodes.data"
#define B_PATH_CONTEXTS_DATA	"_contexts.data"
#define B_PATH_DATATYPES_DATA	"_datatypes.data"
#define B_PATH_TRIPLES_DATA	"_triples.data"
#define B_PATH_JOURNALING_DATA	".journaling"

#define B_HASH_SIZE		16
#define B_TRIPLE_SIZE		(sizeof(b_tmap_item_t) * 9)

#define B_MAX_CACHE_ITEMS	512
#define B_MIN_CACHE_ITEMS	32

#define B_TMAP_NUMB		128 /* <= 127 = 0xFE >> 0x1 -> 01111111 */

#define B_TMAP_VALUE( x )	(x & 0x01)
#define B_TMAP_DATA( x )	((x & 0xFE) >> 0x1)
#define B_TMAP_SET( x , y )     ((x << 0x1 ) | y)

#define B_TMAP_VALUE_EMPTY	0x0
#define B_TMAP_VALUE_FULL	0x1

#ifdef WIN32
#  define snprintf	sprintf_s
#endif

#ifdef B_DEBUG
#  define B_LOG( x ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x); \
   fprintf (stderr, "\n");

#  define B_LOG1( x , a ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a); \
   fprintf (stderr, "\n");

#  define B_LOG2( x , a , b ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a, b); \
   fprintf (stderr, "\n");

#  define B_LOG3( x , a , b , c ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a, b, c); \
   fprintf (stderr, "\n");

#  define B_LOG4( x , a , b , c , d ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a, b, c, d); \
   fprintf (stderr, "\n");

#  define B_LOG5( x , a , b , c , d , e ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a, b, c, d, e); \
   fprintf (stderr, "\n");

#  define B_LOG6( x , a , b , c , d , e , f) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a, b, c, d, e, f); \
   fprintf (stderr, "\n");

#  define B_LOG7( x , a , b , c , d , e , f , g ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a, b, c, d, e, f, g); \
   fprintf (stderr, "\n");

#  define B_LOG8( x , a , b , c , d , e , f , g , h ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a, b, c, d, e, f, g, h); \
   fprintf (stderr, "\n");

#  define B_LOG9( x , a , b , c , d , e , f , g , h , i ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a, b, c, d, e, f, g, h, i); \
   fprintf (stderr, "\n");

#  define B_LOG19( x , a , b , c , d , e , f , g , h , i , l , m , n , o , \
                   p , q , r , s , t , u ) \
   fprintf (stderr, "%s (%d) - ", __FILE__, __LINE__); \
   fprintf (stderr, x, a, b, c, d, e, f, g, h, i , l , m , n , o , p , q , \
                    r , s , t , u ); \
   fprintf (stderr, "\n");
#else
#  define B_LOG( x )
#  define B_LOG1( x , a )
#  define B_LOG2( x , a , b )
#  define B_LOG3( x , a , b , c )
#  define B_LOG4( x , a , b , c , d )
#  define B_LOG5( x , a , b , c , d , e )
#  define B_LOG6( x , a , b , c , d , e , f )
#  define B_LOG7( x , a , b , c , d , e , f , g )
#  define B_LOG8( x , a , b , c , d , e , f , g , h )
#  define B_LOG9( x , a , b , c , d , e , f , g , h , i )
#  define B_LOG19( x , a , b , c , d , e , f , g , h , i , l , m , n , o , \
                   p , q , r , s , t , u )
#endif

typedef struct b_io_t		b_io_t;
typedef struct b_el_t		b_el_t;
typedef struct b_hash_t		b_hash_t;
typedef struct b_cache_t	b_cache_t;
typedef struct b_tcache_t	b_tcache_t;
typedef struct b_string_t	b_string_t;

typedef struct b_tmap_t		b_tmap_t;
typedef struct b_tmap_item_t	b_tmap_item_t;

typedef struct b_map_t		b_map_t;

typedef struct b_tempty_t	b_tempty_t;

typedef struct b_iterator_compare_t
				b_iterator_compare_t;

typedef b_int64 (*b_write_func)	(unsigned char *	what,
				 b_uint64		size,
				 void *			data);

struct b_cache_t
{
  unsigned char		hash[B_HASH_SIZE];

  unsigned char *	what;
  b_uint64		len;

  b_uint64		pointer;
};

struct b_tcache_t
{
  b_uint64		tmap_pointer;
  unsigned char		tmap_index;

  b_cache_t *		subject_uri;
  b_cache_t *		subject_bnode;

  b_cache_t *		property;

  b_cache_t *		object_uri;
  b_cache_t *		object_bnode;
  b_cache_t *		object_literal;

  b_cache_t *		context;

  b_cache_t *		datatype;
  b_cache_t *		lang;

  b_tcache_t *		prev;
  b_tcache_t *		next;
};

struct b_t
{
  b_uint64		max_cache;

  b_hash_t *		hashes_cache;
  b_uint64		hashes_cache_items;

  b_io_t *		uris_io;
  b_io_t *		properties_io;
  b_io_t *		literals_io;
  b_io_t *		bnodes_io;
  b_io_t *		contexts_io;
  b_io_t *		datatypes_io;

  b_io_t *		triples_io;
  b_tcache_t *		triples_cache;
  b_uint64		triples_cache_items;
  b_uint64		triples_total;

  b_tempty_t *		triples_empty;
  b_uint64		triples_empty_size;

  unsigned char *	journaling_file;
  b_io_t *		journaling_io;
};

struct b_hash_t
{
  unsigned char		hash[B_HASH_SIZE];
  unsigned char *	what;
  b_uint64		len;

  b_hash_t *		next;
  b_hash_t *		prev;
};

struct b_string_t
{
  unsigned char *	string;
  b_uint64		size;

  b_write_func		callback;
  b_uint64		callback_size;
  void *		callback_data;
  b_int64		callback_ret;
};

struct b_io_t
{
  void *		fd;

  b_uint64		size;
  b_uint64		pointer;
  b_uint64		end_pointer;

  unsigned char		*data;
};

struct b_el_t
{
  b_tcache_t *		cache;
  b_el_t *		next;
};

struct b_iterator_type_t
{
  b_t *			data;
  b_type_t		type;

  b_io_t *		io;

  char			right;
  b_uint64		pointer;
};

struct b_iterator_compare_t
{
  char			subject_uri_exists;
  unsigned char		subject_uri[B_HASH_SIZE];
  char			subject_bnode_exists;
  unsigned char		subject_bnode[B_HASH_SIZE];

  char			property_exists;
  unsigned char		property[B_HASH_SIZE];

  char			object_uri_exists;
  unsigned char		object_uri[B_HASH_SIZE];
  char			object_bnode_exists;
  unsigned char		object_bnode[B_HASH_SIZE];
  char			object_literal_exists;
  unsigned char		object_literal[B_HASH_SIZE];

  char			context_exists;
  unsigned char		context[B_HASH_SIZE];

  char			datatype_exists;
  unsigned char		datatype[B_HASH_SIZE];
  char			lang_exists;
  unsigned char		lang[B_HASH_SIZE];
};

struct b_iterator_triple_t
{
  b_t *			data;
  char			status;
  b_iterator_compare_t *compare;

  b_tcache_t *		file;
  b_el_t *		results;
};

struct b_map_t
{
  /* This 2 elements must be in the top: */
  unsigned char		hash[B_HASH_SIZE];
  b_uint64		len;

  /* how many triples linked with this item? */
  b_uint64		instances;

  b_uint64		left;
  b_uint64		right;
  b_uint64		parent;
};

struct b_timer_t
{
  struct timeval	start;
  struct timeval	stop;
  char			stopped;
};

struct b_tempty_t
{
  b_uint64		pointer;
  b_tempty_t *		next;
};

struct b_tmap_t
{
  /* Maybe tomorrow a single bit: */
  unsigned char		map[B_TMAP_NUMB];

  unsigned char		subjects_uri[B_TMAP_NUMB];
  unsigned char		subjects_bnode[B_TMAP_NUMB];

  unsigned char		properties[B_TMAP_NUMB];

  unsigned char		objects_uri[B_TMAP_NUMB];
  unsigned char		objects_bnode[B_TMAP_NUMB];
  unsigned char		objects_literal[B_TMAP_NUMB];

  unsigned char		contexts[B_TMAP_NUMB];

  unsigned char		datatypes[B_TMAP_NUMB];
  unsigned char		langs[B_TMAP_NUMB];
};

struct b_tmap_item_t
{
  unsigned char		hash[B_HASH_SIZE];
  b_uint64		pointer;
};

typedef enum
{
  B_JOURNALING_CACHE_EMPTY,
  B_JOURNALING_END_WRITE,
  B_JOURNALING_WRITE
} b_journaling_type_t;

/* b_string: */
b_error_t	b_string_new		(b_string_t **		string);

b_error_t	b_string_new_callback	(b_string_t **		string,
					 b_write_func		callback,
					 b_uint64		size,
					 void *			data);

b_error_t	b_string_destroy	(b_string_t *		string,
					 b_int64 *		ret);

b_error_t	b_string_add		(b_string_t *		string,
					 unsigned char *	what,
				         b_uint64		size);

b_error_t	b_string_callback	(b_string_t *		string,
					 b_int64 *		ret);

unsigned char *	b_string_alloc		(unsigned char *	what,
					 b_uint64		len);

/* io: */
b_error_t	b_io_new		(b_io_t **		ret,
					 unsigned char *	file,
					 b_uint64		size);

b_error_t	b_io_destroy		(b_io_t *		io);

b_error_t	b_io_sync		(b_io_t *		io);

b_int64		b_io_read		(b_io_t *		io,
					 void *			what,
					 b_uint64		size);

b_int64		b_io_write		(b_io_t *		io,
					 void *			what,
					 b_uint64		size,
					 b_uint64		buffer_max);

b_int64		b_io_seek		(b_io_t *		io,
					 b_uint64		size,
					 int			whence);

/* count: */
b_error_t	b_count_triple_cache	(b_t *			data,
					 b_uint64 *		count);

/* sync: */
b_error_t	b_sync_cache		(b_t *			data,
					 b_io_t *		io,
					 unsigned char *	hash,
					 unsigned char *	what,
					 b_uint64		len);

b_error_t	b_sync_tcache		(b_t *			data);

b_error_t	b_sync_instances	(b_t *			data,
					 b_tcache_t *		cache,
					 char 			add);

/* add: */
b_error_t	b_add_triple_cache	(b_t *			data,
					 b_triple_t *		triple,
					 b_uint64		tmap_pointer,
					 unsigned char		tmap_index);

/* get: */
b_error_t	b_get_type_cache	(b_t *			data,
					 b_type_t		type,
					 unsigned char *	what,
					 b_uint64		len,
					 b_cache_t **		exist);

b_error_t	b_get_type_pointer	(b_t *			data,
					 b_type_t		type,
					 b_uint64		pointer,
					 b_cache_t **		exist);

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

b_error_t	b_get_type_internal	(b_t *			data,
					 b_type_t		type,
					 b_io_t **		io);

b_error_t	b_get_triple_cache	(b_t *			data,
					 b_triple_t *		triple,
					 b_tcache_t **		exist);

/* tcache: */
b_error_t	b_tcache_destroy	(b_tcache_t **		cache,
					 b_uint64 *		numb);

b_error_t	b_tcache_destroy_single	(b_tcache_t **		cache_root,
					 b_uint64 *		numb,
					 b_tcache_t *		cache);

b_error_t	b_tcache_copy		(b_tcache_t *		src,
					 b_tcache_t **		dest);

b_error_t	b_tcache_copy_easy	(b_tcache_t *		src,
					 b_tcache_t **		dest);

/* triple: */
b_error_t	b_triple_check		(b_triple_t *		triple);

b_error_t	b_triple_new_cache	(b_tcache_t *		cache,
					 b_triple_t **		ret);

b_error_t	b_triple_copy		(b_triple_t *		src,
					 b_triple_t **		dest);

/* hash: */
b_error_t	b_hash_new		(b_t *			data,
					 unsigned char *	what,
					 b_uint64		len,
					 unsigned char *	where);

b_error_t	b_hash_copy		(unsigned char *	dst,
					 unsigned char *	src);

int		b_hash_compare		(unsigned char *	a,
					 unsigned char *	b);

int		b_hash_empty		(unsigned char *	a);

b_error_t	b_hash_zero		(unsigned char *	a);

b_error_t	b_hash_print		(FILE *			file,
					 unsigned char *	hash);

b_error_t	b_hash_destroy		(b_hash_t **		cache,
					 b_uint64 *		numb);

/* md5: */
b_error_t	b_MD5			(unsigned char *	what,
					 b_uint64		len,
					 unsigned char *	where);

/* graph: */
b_error_t	b_graph			(b_t *			data,
					 b_type_t		type,
					 FILE *			file);

/* tempty: */
b_error_t	b_tempty_check		(b_t *			data,
					 b_tmap_t *		tmap,
					 b_uint64		pointer,
					 char			remove);

char		b_tempty_get		(b_t *			data,
					 b_tmap_t *		tmap,
					 b_uint64 *		pointer);

void		b_tempty_destroy	(b_tempty_t **		list);

/* cache: */
b_error_t	b_cache_new		(unsigned char *	hash,
					 unsigned char *	what,
					 b_uint64		len,
					 b_uint64		pointer,
					 b_cache_t **		ret);

b_error_t	b_cache_destroy		(b_cache_t *		cache);

b_error_t	b_cache_copy		(b_cache_t *		src,
					 b_cache_t **		dest);

b_error_t	b_cache_copy_easy	(b_cache_t *		src,
					 b_cache_t **		dest);

/* remove: */
b_error_t	b_remove_type_cache	(b_t *			data,
					 b_io_t *		io,
					 b_cache_t *		cache);

/* journaling: */
void		b_journaling_new	(b_t *			data,
					 b_io_t *		io,
					 b_journaling_type_t	type,
					 ...);

b_error_t	b_journaling_recover	(b_t *			data);

void		b_journaling_destroy	(b_t *			data);

/* b_el_t functions: */

b_error_t	b_el_new		(b_tcache_t *		cache,
					 b_el_t **		list);

b_tcache_t *	b_el_first		(b_el_t **		list);

void		b_el_free		(b_el_t **		list);

#endif

/* EOF */
