You are viewing a plain text version of this content. The canonical link for it is here.
Posted to triplesoup-commits@incubator.apache.org by le...@apache.org on 2007/02/21 21:56:45 UTC

svn commit: r510255 [2/3] - in /incubator/triplesoup/donations/TRIPLES-2-libb: ./ redland-integration/ src/ tests/ utils/

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/bhash.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/bhash.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/bhash.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/bhash.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,206 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+static b_error_t b_hash_new_cache (b_t * data, unsigned char *what,
+				   b_uint64 len, unsigned char *where,
+				   char *status);
+static b_error_t b_hash_destroy_single (b_hash_t ** cache_root,
+					b_uint64 * numb, b_hash_t * cache);
+
+b_error_t
+b_hash_new (b_t * data, unsigned char *what, b_uint64 len,
+	    unsigned char *where)
+{
+  char status;
+  b_hash_t *hash;
+  b_error_t error;
+
+  B_LOG4 ("b_hash_new: %p %p %d %p", data, what, (int) len, where);
+
+  if (!what || !where)
+    return B_ERROR_DATA;
+
+  len = len > 0 ? len : strlen ((char *) what);
+
+  if ((error = b_hash_new_cache (data, what, len, where, &status)) != B_OK)
+    return error;
+
+  if (status)
+    return B_OK;
+
+  b_MD5 (what, len, where);
+
+  if (!(hash = (b_hash_t *) calloc (1, sizeof (b_hash_t))))
+    return B_ERROR_MEMORY;
+
+  hash->what = b_string_alloc (what, len);
+  hash->len = len;
+
+  b_hash_copy (hash->hash, where);
+
+  if (data->hashes_cache_items)
+    {
+      hash->next = data->hashes_cache;
+      data->hashes_cache->prev = hash;
+    }
+
+  data->hashes_cache = hash;
+  data->hashes_cache_items++;
+
+  if (data->hashes_cache_items >= data->max_cache)
+    {
+      hash = data->hashes_cache;
+
+      while (hash->next)
+	hash = hash->next;
+
+      b_hash_destroy_single (&data->hashes_cache, &data->hashes_cache_items,
+			     hash);
+    }
+
+  return B_OK;
+}
+
+static b_error_t
+b_hash_new_cache (b_t * data, unsigned char *what, b_uint64 len,
+		  unsigned char *where, char *status)
+{
+  b_hash_t *h;
+
+  B_LOG5 ("b_hash_new_cache: %p %p %d %p %p", data, what, (int) len, where,
+	  status);
+
+  for (h = data->hashes_cache; h; h = h->next)
+    {
+      if (len == h->len && !memcmp (what, h->what, (size_t) len))
+	{
+	  b_hash_copy (where, h->hash);
+	  *status = 1;
+	  return B_OK;
+	}
+    }
+
+  *status = 0;
+  return B_OK;
+}
+
+b_error_t
+b_hash_copy (unsigned char *dst, unsigned char *src)
+{
+  if (!dst || !src)
+    return B_ERROR_DATA;
+
+  B_LOG2 ("b_hash_copy: %p %p", dst, src);
+
+  memcpy (dst, src, B_HASH_SIZE);
+  return B_OK;
+}
+
+int
+b_hash_compare (unsigned char *a, unsigned char *b)
+{
+  B_LOG2 ("b_hash_compare: %p %p", a, b);
+
+  return memcmp (a, b, B_HASH_SIZE);
+}
+
+int
+b_hash_empty (unsigned char *a)
+{
+  int i;
+
+  B_LOG1 ("b_hash_empty: %p", a);
+
+  if (!a)
+    return 0;
+
+  for (i = 0; i < B_HASH_SIZE; i++)
+    if (a[i])
+      return 1;
+
+  return 0;
+}
+
+b_error_t
+b_hash_zero (unsigned char *a)
+{
+  B_LOG1 ("b_hash_zero: %p", a);
+
+  if (!a)
+    return B_ERROR_DATA;
+
+  memset (a, 0, B_HASH_SIZE);
+  return B_OK;
+}
+
+b_error_t
+b_hash_print (FILE * file, unsigned char *hash)
+{
+  int i;
+
+  B_LOG2 ("b_hash_print: %p %p", file, hash);
+
+  if (!file || !hash)
+    return B_ERROR_DATA;
+
+  for (i = 0; i < B_HASH_SIZE; i++)
+    fprintf (file, "%.2X", hash[i]);
+  fprintf (file, "\n");
+
+  return B_OK;
+}
+
+/* Destroy the hashes cache list: */
+b_error_t
+b_hash_destroy (b_hash_t ** list, b_uint64 * len)
+{
+  B_LOG2 ("b_hash_destroy: %p %p", list, len);
+
+  if (!list || !*list)
+    return B_ERROR_DATA;
+
+  while (*list)
+    b_hash_destroy_single (list, len, *list);
+
+  return B_OK;
+}
+
+/* This function destroies a hashes cache item: */
+static b_error_t
+b_hash_destroy_single (b_hash_t ** cache_root, b_uint64 * numb,
+		       b_hash_t * cache)
+{
+  B_LOG3 ("b_hash_destroy_single: %p %p %p", cache_root, numb, cache);
+
+  if (!cache)
+    return B_ERROR_DATA;
+
+  if (cache == *cache_root)
+    *cache_root = cache->next;
+
+  if (cache->prev)
+    cache->prev->next = cache->next;
+
+  if (cache->next)
+    cache->next->prev = cache->prev;
+
+  if (cache->what)
+    free (cache->what);
+
+  free (cache);
+
+  if (numb)
+    (*numb)--;
+
+  return B_OK;
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/bio.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/bio.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/bio.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/bio.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,337 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+static void *b_io_open (unsigned char *file);
+static int b_io_close (void *fd);
+static int b_io_ftruncate (void *fd, b_uint64 size);
+
+static void *b_io_mmap (void *fd, b_uint64 size, b_uint64 * ret_size);
+static void b_io_munmap (void *fd, b_uint64 size);
+static void b_io_msync (void *fd, b_uint64 size);
+
+#ifdef WIN32
+typedef struct b_io_win32_t b_io_win32_t;
+
+struct b_io_win32_t
+{
+  HANDLE *file;
+  HANDLE *data;
+};
+#endif
+
+b_error_t
+b_io_new (b_io_t ** ret, unsigned char *file, b_uint64 size)
+{
+  b_io_t *io;
+
+  B_LOG3 ("b_io_new: %p %p %d", ret, file, (int) size);
+
+  if (!ret || !file || size <= 0)
+    return B_ERROR_DATA;
+
+  if (!(io = calloc (1, sizeof (b_io_t))))
+    return B_ERROR_MEMORY;
+
+  if (!(io->fd = b_io_open (file)))
+    {
+      b_io_destroy (io);
+      return B_ERROR_POSIX;
+    }
+
+  size = (B_HASH_SIZE + sizeof (b_uint64)) * size + sizeof (b_uint64);
+
+  if (!(io->data = b_io_mmap (io->fd, size, &io->size)))
+    {
+      b_io_destroy (io);
+      return B_ERROR_POSIX;
+    }
+
+  memcpy (&io->end_pointer, io->data, sizeof (b_uint64));
+
+  if (!io->end_pointer)
+    io->end_pointer = sizeof (b_uint64);
+
+  *ret = io;
+  return B_OK;
+}
+
+b_error_t
+b_io_destroy (b_io_t * io)
+{
+  B_LOG1 ("b_io_destroy: %p", io);
+
+  if (!io)
+    return B_ERROR_DATA;
+
+  b_io_sync (io);
+
+  if (io->data && io->size)
+    b_io_munmap (io->data, io->size);
+
+  if (io->fd)
+    b_io_close (io->fd);
+
+  free (io);
+
+  return B_OK;
+}
+
+b_error_t
+b_io_sync (b_io_t * io)
+{
+  B_LOG1 ("b_io_sync: %p", io);
+
+  if (!io)
+    return B_ERROR_DATA;
+
+  b_io_msync (io->data, io->size);
+  return B_OK;
+}
+
+b_int64
+b_io_read (b_io_t * io, void *what, b_uint64 size)
+{
+  B_LOG3 ("b_io_read: %p %p %d", io, what, (int) size);
+
+  if ((io->end_pointer - io->pointer) < size)
+    size = io->end_pointer - io->pointer;
+
+  if (size)
+    {
+      memcpy (what, io->data + io->pointer, (size_t) size);
+      io->pointer += size;
+    }
+
+  return size;
+}
+
+b_int64
+b_io_write (b_io_t * io, void *what, b_uint64 size, b_uint64 buffer_max)
+{
+  B_LOG4 ("b_io_write: %p %p %d %d", io, what, (int) size, (int) buffer_max);
+
+  while ((io->size - io->pointer) < size)
+    {
+      void *new;
+      b_uint64 n, ret;
+
+      b_io_sync (io);
+
+      n = buffer_max * (B_HASH_SIZE + sizeof (b_uint64)) + sizeof (b_uint64);
+
+      /* mremap doesn't work: */
+      b_io_munmap (io->data, io->size);
+      io->data = NULL;
+
+      if (b_io_ftruncate (io->fd, io->size + n))
+	return -1;
+
+      if (!(new = b_io_mmap (io->fd, io->size + n, &ret)))
+	return -1;
+
+      memset (((unsigned char *) new) + io->size, 0, (size_t) n);
+
+      io->size += n;
+      io->data = new;
+    }
+
+  memcpy (io->data + io->pointer, what, (size_t) size);
+  io->pointer += size;
+
+  if (io->pointer > io->end_pointer)
+    {
+      io->end_pointer = io->pointer;
+      memcpy (io->data, &io->end_pointer, sizeof (io->end_pointer));
+    }
+
+  return size;
+}
+
+b_int64
+b_io_seek (b_io_t * io, b_uint64 size, int whence)
+{
+  B_LOG3 ("b_io_seek: %p %d %d", io, (int) size, whence);
+
+  switch (whence)
+    {
+    case SEEK_SET:
+      io->pointer = size <= io->end_pointer ? size : io->end_pointer;
+      return io->pointer;
+
+    case SEEK_END:
+      io->pointer = io->end_pointer;
+      return io->pointer;
+
+    case SEEK_CUR:
+    default:
+      if ((io->end_pointer - io->pointer) < size)
+	{
+	  io->pointer = io->end_pointer;
+	  return io->pointer;
+	}
+
+      io->pointer += size;
+      return io->pointer;
+    }
+}
+
+static void *
+b_io_open (unsigned char *file)
+{
+#ifndef WIN32
+  int fd;
+
+  B_LOG1 ("b_io_open: %p", file);
+
+  if ((fd = open ((char *) file, O_RDWR | O_CREAT, 0644)) == -1)
+    return NULL;
+
+  return (void *) fd;
+#else
+  b_io_win32_t *data;
+
+  B_LOG1 ("b_io_open: %p", file);
+
+  if (!(data = calloc (1, sizeof (b_io_win32_t))))
+    return NULL;
+
+  if ((data->file =
+       CreateFile (file, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0,
+		   0)) == INVALID_HANDLE_VALUE)
+    return NULL;
+
+  return (void *) data;
+#endif
+}
+
+static int
+b_io_close (void *dummy)
+{
+#ifndef WIN32
+  B_LOG1 ("b_io_close: %p", dummy);
+
+  return close ((int) dummy);
+#else
+  b_io_win32_t *data = (b_io_win32_t *) dummy;
+
+  B_LOG1 ("b_io_close: %p", dummy);
+
+  CloseHandle ((HANDLE) data->data);
+  CloseHandle ((HANDLE) data->file);
+
+  free (data);
+  return 0;
+#endif
+}
+
+static int
+b_io_ftruncate (void *dummy, b_uint64 size)
+{
+#ifndef WIN32
+  B_LOG2 ("b_io_ftruncate: %p %d", dummy, (int) size);
+
+  return ftruncate ((int) dummy, size);
+#else
+  b_io_win32_t *win = (b_io_win32_t *) dummy;
+
+  B_LOG2 ("b_io_ftruncate: %p %d", dummy, (int) size);
+
+  SetFilePointer (win->file, (LONG) size, 0, FILE_BEGIN);
+  return 0;
+#endif
+}
+
+static void *
+b_io_mmap (void *dummy, b_uint64 size, b_uint64 * ret_size)
+{
+  unsigned char *data;
+
+#ifndef WIN32
+  struct stat st;
+  int fd;
+
+  B_LOG3 ("b_io_mmap: %p %d %p", dummy, (int) size, ret_size);
+
+  fd = (int) dummy;
+
+  if (fstat (fd, &st))
+    return NULL;
+
+  if ((b_uint64) st.st_size < size)
+    {
+      if (ftruncate (fd, size))
+	return NULL;
+    }
+
+  size = (b_uint64) st.st_size < size ? size : (b_uint64) st.st_size;
+
+  if ((data =
+       mmap (0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
+	     0)) == MAP_FAILED)
+    return NULL;
+
+  if ((b_uint64) st.st_size < size)
+    memset (data + st.st_size, 0, size - st.st_size);
+
+  *ret_size = size;
+  return (void *) data;
+#else
+  b_io_win32_t *win = (b_io_win32_t *) dummy;
+  b_uint64 s;
+
+  B_LOG3 ("b_io_mmap: %p %d %p", dummy, (int) size, ret_size);
+
+  s = GetFileSize (win->file, NULL);
+  size = s < size ? size : s;
+
+  if (!
+      (win->data =
+       CreateFileMapping (win->file, NULL, PAGE_READWRITE, 0, (DWORD) size,
+			  NULL)) || win->data == INVALID_HANDLE_VALUE)
+    return NULL;
+
+  if (!
+      (data = MapViewOfFile (win->data, FILE_MAP_WRITE, 0, 0, (SIZE_T) size)))
+    return NULL;
+
+  if (s < size)
+    memset (data + s, 0, (size_t) (size - s));
+
+  *ret_size = size;
+  return data;
+#endif
+}
+
+static void
+b_io_munmap (void *data, b_uint64 size)
+{
+  B_LOG2 ("b_io_munmap: %p %d", data, (int) size);
+
+#ifndef WIN32
+  munmap (data, size);
+#else
+  UnmapViewOfFile (data);
+#endif
+}
+
+static void
+b_io_msync (void *data, b_uint64 size)
+{
+  B_LOG2 ("b_io_msync: %p %d", data, (int) size);
+
+#ifndef WIN32
+  msync (data, size, MS_ASYNC);
+#else
+  FlushViewOfFile (data, (SIZE_T) size);
+#endif
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/biterator.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/biterator.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/biterator.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/biterator.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,761 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+static b_error_t b_iterator_type_pointer (b_iterator_type_t * iterator,
+					  b_cache_t ** cache);
+static b_error_t b_iterator_type_pointer_init (b_iterator_type_t *
+					       iterator, b_cache_t ** cache);
+static b_error_t b_iterator_type_pointer_right (b_iterator_type_t *
+						iterator, b_cache_t ** cache);
+static b_error_t b_iterator_type_pointer_up (b_iterator_type_t *
+					     iterator, b_cache_t ** cache);
+static b_error_t b_iterator_type_pointer_found (b_iterator_type_t *
+						iterator,
+						b_cache_t ** cache,
+						b_map_t * map,
+						b_uint64 pointer);
+
+static b_error_t b_iterator_triple_pointer (b_iterator_triple_t *
+					    iterator,
+					    b_uint64 tmap_pointer,
+					    unsigned char tmap_index,
+					    b_tcache_t ** cache);
+
+/**
+ * b_iterator_type_new:
+ * @data: The pointer to a b_t data struct.
+ * @type: The type of the elements
+ * @iterator: The return pointer.
+ * @returns: The error code.
+ *
+ * Creates a new Iterator for a type of element. The iterator must be destoyed
+ * with b_iterator_type_destroy(). You should use b_iterator_type_step() to get
+ * the list of results.
+ **/
+b_error_t
+b_iterator_type_new (b_t * data, b_type_t type, b_iterator_type_t ** iterator)
+{
+  b_io_t *io;
+  b_error_t error;
+
+  B_LOG3 ("b_iterator_type_new: %p %d %p", data, type, iterator);
+
+  if (!data || !iterator)
+    return B_ERROR_DATA;
+
+  if ((error = b_get_type_internal (data, type, &io)) != B_OK)
+    return error;
+
+  if (!(*iterator = calloc (1, sizeof (b_iterator_type_t))))
+    return B_ERROR_MEMORY;
+
+  (*iterator)->io = io;
+  (*iterator)->data = data;
+  (*iterator)->type = type;
+
+  (*iterator)->right = -1;
+
+  return B_OK;
+}
+
+/**
+ * b_iterator_type_destroy:
+ * @iterator: The pointer of a b_iterator_t struct.
+ * @returns: The error code.
+ *
+ * Destroies a b_iterator_t struct.
+ **/
+b_error_t
+b_iterator_type_destroy (b_iterator_type_t * iterator)
+{
+  B_LOG1 ("b_iterator_type_destroy: %p", iterator);
+
+  if (!iterator)
+    return B_ERROR_DATA;
+
+  free (iterator);
+
+  return B_OK;
+}
+
+/**
+ * b_iterator_type_step:
+ * @iterator: The pointer of a b_iterator_t struct.
+ * @ret: The return string for the next result.
+ * @ret_size: the len of the ret string. It can be NULL.
+ * @returns: The error code.
+ *
+ * Returns the next element of the storage iterator. If *ret will be NULL
+ * no other results are into the storage. The new *ret string must be freed
+ * with a normal free().
+ **/
+b_error_t
+b_iterator_type_step (b_iterator_type_t * iterator, unsigned char **ret,
+		      b_uint64 * ret_size)
+{
+  b_cache_t *cache;
+  b_error_t error;
+
+  B_LOG3 ("b_iterator_type_step: %p %p %p", iterator, ret, ret_size);
+
+  if (!iterator || !ret)
+    return B_ERROR_DATA;
+
+  error = b_iterator_type_pointer (iterator, &cache);
+
+  if (error != B_OK)
+    return error;
+
+  if (cache)
+    {
+      if (!(*ret = b_string_alloc (cache->what, cache->len)))
+	return B_ERROR_MEMORY;
+
+      if (ret_size)
+	*ret_size = cache->len;
+
+      b_cache_destroy (cache);
+    }
+  else
+    {
+      *ret = NULL;
+
+      if (ret_size)
+	*ret_size = 0;
+    }
+
+  return B_OK;
+}
+
+static b_error_t
+b_iterator_type_pointer (b_iterator_type_t * iterator, b_cache_t ** cache)
+{
+  *cache = NULL;
+
+  B_LOG2 ("b_iterator_type_pointer: %p %p", iterator, cache);
+
+  if (!iterator->pointer)
+    return b_iterator_type_pointer_init (iterator, cache);
+
+  else if (iterator->right == 1)
+    return b_iterator_type_pointer_right (iterator, cache);
+
+  else if (iterator->right == 0)
+    return b_iterator_type_pointer_up (iterator, cache);
+
+  else
+    return B_OK;
+}
+
+static b_error_t
+b_iterator_type_pointer_init (b_iterator_type_t * iterator,
+			      b_cache_t ** cache)
+{
+  b_map_t map;
+  b_uint64 pointer;
+
+  B_LOG2 ("b_iterator_type_pointer_init: %p %p", iterator, cache);
+
+  pointer = b_io_seek (iterator->io, sizeof (b_uint64), SEEK_SET);
+
+  /* Search the first noempty element: */
+  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
+    return B_OK;
+
+  if (map.left)
+    {
+      while (map.left)
+	{
+	  pointer = b_io_seek (iterator->io, map.left, SEEK_SET);
+	  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
+	    return B_ERROR_INTERNAL;
+	}
+
+      if (map.right)
+	iterator->right = 1;
+      else
+	iterator->right = 0;
+
+      iterator->pointer = pointer;
+      return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
+    }
+
+  if (map.right)
+    {
+      iterator->right = 1;
+      iterator->pointer = pointer;
+      return b_iterator_type_pointer (iterator, cache);
+    }
+
+  return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
+}
+
+static b_error_t
+b_iterator_type_pointer_right (b_iterator_type_t * iterator,
+			       b_cache_t ** cache)
+{
+  b_map_t map;
+  b_uint64 pointer;
+
+  B_LOG2 ("b_iterator_type_pointer_right: %p %p", iterator, cache);
+
+  pointer = b_io_seek (iterator->io, iterator->pointer, SEEK_SET);
+  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
+    return B_ERROR_INTERNAL;
+
+  pointer = b_io_seek (iterator->io, map.right, SEEK_SET);
+  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
+    return B_ERROR_INTERNAL;
+
+  if (map.left)
+    {
+      while (map.left)
+	{
+	  pointer = b_io_seek (iterator->io, map.left, SEEK_SET);
+	  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
+	    return B_ERROR_INTERNAL;
+	}
+
+      if (map.right)
+	iterator->right = 1;
+      else
+	iterator->right = 0;
+
+      iterator->pointer = pointer;
+      return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
+    }
+
+  if (map.right)
+    iterator->right = 1;
+  else
+    iterator->right = 0;
+
+  iterator->pointer = pointer;
+  return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
+}
+
+static b_error_t
+b_iterator_type_pointer_up (b_iterator_type_t * iterator, b_cache_t ** cache)
+{
+  b_map_t map;
+  b_uint64 pointer;
+
+  B_LOG2 ("b_iterator_type_pointer_up: %p %p", iterator, cache);
+
+  pointer = b_io_seek (iterator->io, iterator->pointer, SEEK_SET);
+  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
+    return B_ERROR_INTERNAL;
+
+  if (!map.parent)
+    return B_OK;
+
+  pointer = b_io_seek (iterator->io, map.parent, SEEK_SET);
+  if (b_io_read (iterator->io, &map, sizeof (map)) <= 0)
+    return B_ERROR_INTERNAL;
+
+  if (map.left == iterator->pointer)
+    {
+      if (map.right)
+	iterator->right = 1;
+      else
+	iterator->right = 0;
+
+      iterator->pointer = pointer;
+      return b_iterator_type_pointer_found (iterator, cache, &map, pointer);
+    }
+
+  iterator->right = 0;
+  iterator->pointer = pointer;
+  return b_iterator_type_pointer (iterator, cache);
+}
+
+static b_error_t
+b_iterator_type_pointer_found (b_iterator_type_t * iterator,
+			       b_cache_t ** cache, b_map_t * map,
+			       b_uint64 pointer)
+{
+  b_error_t error;
+  unsigned char *what;
+
+  B_LOG4 ("b_iterator_type_pointer_found: %p %p %p %d", iterator, cache, map,
+	  (int) pointer);
+
+  if (pointer == sizeof (b_uint64))
+    return b_iterator_type_pointer (iterator, cache);
+
+  b_io_seek (iterator->io, pointer + sizeof (b_map_t), SEEK_SET);
+
+  if (!(what = malloc (sizeof (unsigned char) * ((size_t) map->len + 1))))
+    return B_ERROR_MEMORY;
+
+  if (map->len && b_io_read (iterator->io, what, map->len) <= 0)
+    {
+      free (what);
+      return B_ERROR_INTERNAL;
+    }
+
+  what[map->len] = 0;
+
+  error = b_cache_new (map->hash, what, map->len, pointer, cache);
+
+  free (what);
+  return error;
+}
+
+/**
+ * b_iterator_triple_new:
+ * @data: The pointer to a b_t data struct.
+ * @iterator: The return pointer for the new iterator.
+ * @compare: If you want have only results from a template, this b_triple_t
+ * describes the template. You can create it with b_triple_new_incomplete().
+ * @returns: The error code.
+ *
+ * Creates a new iterator for triples. It must be destroied with 
+ * b_iterator_triple_destroy(). You can get the list of results with 
+ * b_iterator_triple_step().
+ **/
+b_error_t
+b_iterator_triple_new (b_t * data, b_iterator_triple_t ** iterator,
+		       b_triple_t * compare)
+{
+  B_LOG3 ("b_iterator_triple_new: %p %p %p", data, iterator, compare);
+
+  if (!data || !iterator)
+    return B_ERROR_DATA;
+
+  b_sync (data);
+
+  if (!(*iterator = calloc (1, sizeof (b_iterator_triple_t))))
+    return B_ERROR_MEMORY;
+
+  (*iterator)->data = data;
+  (*iterator)->status = -1;
+
+  if (compare)
+    {
+      if (!((*iterator)->compare = calloc (1, sizeof (b_iterator_compare_t))))
+	{
+	  free (*iterator);
+	  return B_ERROR_MEMORY;
+	}
+
+#define B_ITERATOR_COMPARE( x , len , e ) \
+      if (compare->x) \
+        { \
+  	  b_hash_new (data, compare->x, compare->len, \
+                      (*iterator)->compare->x); \
+          (*iterator)->compare->e = 1; \
+        }
+
+      B_ITERATOR_COMPARE (subject_uri, subject_uri_len, subject_uri_exists);
+      B_ITERATOR_COMPARE (subject_bnode, subject_bnode_len,
+			  subject_bnode_exists);
+
+      B_ITERATOR_COMPARE (property, property_len, property_exists);
+
+      B_ITERATOR_COMPARE (object_uri, object_uri_len, object_uri_exists);
+      B_ITERATOR_COMPARE (object_bnode, object_bnode_len,
+			  object_bnode_exists);
+      B_ITERATOR_COMPARE (object_literal, object_literal_len,
+			  object_literal_exists);
+
+      B_ITERATOR_COMPARE (context, context_len, context_exists);
+
+      B_ITERATOR_COMPARE (datatype, datatype_len, datatype_exists);
+      B_ITERATOR_COMPARE (lang, lang_len, lang_exists);
+    }
+
+  return B_OK;
+}
+
+/**
+ * b_iterator_triple_destroy:
+ * @iterator: The triple iterator.
+ * @returns: The error code.
+ *
+ * Destroies a b_iterator_triple_t data struct.
+ **/
+b_error_t
+b_iterator_triple_destroy (b_iterator_triple_t * iterator)
+{
+  B_LOG1 ("b_iterator_triple_destroy: %p", iterator);
+
+  if (!iterator)
+    return B_ERROR_DATA;
+
+  if (iterator->compare)
+    free (iterator->compare);
+
+  free (iterator);
+
+  return B_OK;
+}
+
+/**
+ * b_iterator_triple_step:
+ * @iterator: The triple iterator.
+ * @triple: The return pointer for the next b_triple_t. If it is NULL, no other
+ * results are into the storage.
+ * @returns: The error code.
+ *
+ * Gets the next results. The new b_triple_t must be freed with
+ * b_triple_destroy().
+ **/
+b_error_t
+b_iterator_triple_step (b_iterator_triple_t * iterator, b_triple_t ** ret)
+{
+  b_tcache_t *cache;
+  b_error_t error;
+
+  B_LOG2 ("b_iterator_triple_step: %p %p", iterator, ret);
+
+  if (!iterator->file)
+    error =
+      b_iterator_triple_pointer (iterator, sizeof (b_uint64), 0, &cache);
+
+  /* Previous results? */
+  else if (iterator->results)
+    {
+      b_el_t *list = iterator->results;
+      cache = list->cache;
+      iterator->results = list->next;
+      free (list);
+      error = B_OK;
+    }
+
+  else if (!iterator->compare && iterator->file->tmap_index < B_TMAP_NUMB - 1)
+    {
+      error =
+	b_iterator_triple_pointer (iterator, iterator->file->tmap_pointer,
+				   iterator->file->tmap_index + 1, &cache);
+    }
+  else
+    {
+      error =
+	b_iterator_triple_pointer (iterator,
+				   iterator->file->tmap_pointer +
+				   sizeof (b_tmap_t) +
+				   B_TRIPLE_SIZE * B_TMAP_NUMB, 0, &cache);
+    }
+
+  if (error != B_OK)
+    return error;
+
+  if (cache)
+    {
+      if (iterator->file)
+	b_tcache_destroy_single (&iterator->file, NULL, iterator->file);
+
+      iterator->file = cache;
+
+      if ((error = b_triple_new_cache (cache, ret)) != B_OK)
+	return error;
+    }
+  else
+    {
+      if (iterator->file)
+	b_tcache_destroy_single (&iterator->file, NULL, iterator->file);
+
+      *ret = NULL;
+    }
+
+  return B_OK;
+}
+
+static b_error_t
+b_iterator_triple_pointer (b_iterator_triple_t * iterator,
+			   b_uint64 tmap_pointer, unsigned char tmap_index,
+			   b_tcache_t ** cache)
+{
+  b_tcache_t *c;
+  b_error_t error;
+  b_tmap_t tmap;
+  b_el_t *list;
+
+  B_LOG4 ("b_iterator_triple_pointer: %p %d %d %p", iterator,
+	  (int) tmap_pointer, tmap_index, cache);
+
+  *cache = NULL;
+
+  if (!iterator->compare)
+    {
+      b_tmap_item_t tmap_item;
+
+      b_io_seek (iterator->data->triples_io, tmap_pointer, SEEK_SET);
+
+      /* This index is full ? */
+      if (b_io_read (iterator->data->triples_io, &tmap, sizeof (b_tmap_t)) <=
+	  0)
+	return B_OK;
+
+      if ((error =
+	   b_tempty_check (iterator->data, &tmap, tmap_pointer, 0)) != B_OK)
+	return error;
+
+      if (!tmap.map[tmap_index])
+	{
+	  for (; !tmap.map[tmap_index] && tmap_index < B_TMAP_NUMB;
+	       tmap_index++);
+
+	  if (tmap_index == B_TMAP_NUMB)
+	    return b_iterator_triple_pointer (iterator,
+					      tmap_pointer +
+					      sizeof (b_tmap_t) +
+					      B_TRIPLE_SIZE * B_TMAP_NUMB, 0,
+					      cache);
+	}
+
+      b_io_seek (iterator->data->triples_io,
+		 tmap_pointer + sizeof (b_tmap_t) +
+		 tmap_index * B_TRIPLE_SIZE, SEEK_SET);
+
+      if (!(c = (b_tcache_t *) calloc (1, sizeof (b_tcache_t))))
+	return B_ERROR_MEMORY;
+
+#define B_TRIPLE_POINTER( x , t ) \
+  if (b_io_read (iterator->data->triples_io, &tmap_item, \
+		 sizeof (b_tmap_item_t)) <= 0) \
+    { \
+      b_tcache_destroy_single (NULL, NULL, c); \
+      return B_ERROR_INTERNAL; \
+    } \
+  \
+  if (tmap_item.pointer) \
+    { \
+      if ((error = b_get_type_pointer (iterator->data, t, \
+				       tmap_item.pointer, &c->x)) \
+          != B_OK) \
+	{ \
+          b_tcache_destroy_single (NULL, NULL, c); \
+	  return error; \
+	} \
+      \
+      if (!c->x) \
+	{ \
+          b_tcache_destroy_single (NULL, NULL, c); \
+	  return B_ERROR_INTERNAL; \
+	} \
+    }
+
+      B_TRIPLE_POINTER (subject_uri, B_TYPE_URI);
+      B_TRIPLE_POINTER (subject_bnode, B_TYPE_BNODE);
+      B_TRIPLE_POINTER (property, B_TYPE_PROPERTY);
+      B_TRIPLE_POINTER (object_uri, B_TYPE_URI);
+      B_TRIPLE_POINTER (object_bnode, B_TYPE_BNODE);
+      B_TRIPLE_POINTER (object_literal, B_TYPE_LITERAL);
+      B_TRIPLE_POINTER (context, B_TYPE_CONTEXT);
+      B_TRIPLE_POINTER (datatype, B_TYPE_DATATYPE);
+      B_TRIPLE_POINTER (lang, B_TYPE_LITERAL);
+
+      c->tmap_index = tmap_index;
+      c->tmap_pointer = tmap_pointer;
+      *cache = c;
+      return B_OK;
+    }
+
+  /* To compare: */
+  else
+    {
+      int tmap_results[B_TMAP_NUMB];
+      int i, j;
+
+      if (iterator->results)
+	return B_ERROR_INTERNAL;
+
+      b_io_seek (iterator->data->triples_io, tmap_pointer, SEEK_SET);
+
+      if (b_io_read (iterator->data->triples_io, &tmap, sizeof (b_tmap_t)) <=
+	  0)
+	return B_OK;
+
+      if ((error =
+	   b_tempty_check (iterator->data, &tmap, tmap_pointer, 0)) != B_OK)
+	return error;
+
+      memset (&tmap_results, 0, sizeof (tmap_results));
+
+      /* Disactive no compatible results: */
+      for (i = 0; i < B_TMAP_NUMB; i++)
+	{
+	  if (!tmap.map[i])
+	    tmap_results[i] = 1;
+	  else
+	    {
+	      if (iterator->compare->subject_uri_exists
+		  && B_TMAP_VALUE (tmap.subjects_uri[i]) ==
+		  B_TMAP_VALUE_EMPTY)
+		tmap_results[B_TMAP_DATA (tmap.subjects_uri[i])] = 1;
+
+	      if (iterator->compare->subject_bnode_exists
+		  && B_TMAP_VALUE (tmap.subjects_bnode[i]) ==
+		  B_TMAP_VALUE_EMPTY)
+		tmap_results[B_TMAP_DATA (tmap.subjects_bnode[i])] = 1;
+
+	      if (iterator->compare->object_uri_exists
+		  && B_TMAP_VALUE (tmap.objects_uri[i]) == B_TMAP_VALUE_EMPTY)
+		tmap_results[B_TMAP_DATA (tmap.objects_uri[i])] = 1;
+
+	      if (iterator->compare->object_bnode_exists
+		  && B_TMAP_VALUE (tmap.objects_bnode[i]) ==
+		  B_TMAP_VALUE_EMPTY)
+		tmap_results[B_TMAP_DATA (tmap.objects_bnode[i])] = 1;
+
+	      if (iterator->compare->object_literal_exists
+		  && B_TMAP_VALUE (tmap.objects_literal[i]) ==
+		  B_TMAP_VALUE_EMPTY)
+		tmap_results[B_TMAP_DATA (tmap.objects_literal[i])] = 1;
+
+	      if (iterator->compare->context_exists
+		  && B_TMAP_VALUE (tmap.contexts[i]) == B_TMAP_VALUE_EMPTY)
+		tmap_results[B_TMAP_DATA (tmap.contexts[i])] = 1;
+
+	      if (iterator->compare->datatype_exists
+		  && B_TMAP_VALUE (tmap.datatypes[i]) == B_TMAP_VALUE_EMPTY)
+		tmap_results[B_TMAP_DATA (tmap.datatypes[i])] = 1;
+
+	      if (iterator->compare->lang_exists
+		  && B_TMAP_VALUE (tmap.langs[i]) == B_TMAP_VALUE_EMPTY)
+		tmap_results[B_TMAP_DATA (tmap.langs[i])] = 1;
+	    }
+	}
+
+#define B_GET_FILE( x , y , n , e ) \
+  if (tmap_results[i]) \
+    continue; \
+  \
+  if (iterator->compare->e) \
+    { \
+      b_io_seek (iterator->data->triples_io, tmap_pointer + \
+                 sizeof (b_tmap_t) + i * B_TRIPLE_SIZE + \
+		 sizeof(b_tmap_item_t) * n, SEEK_SET); \
+      if (b_io_read (iterator->data->triples_io, &x, \
+		     sizeof (b_tmap_item_t)) <= 0) \
+	{ \
+	  return B_ERROR_INTERNAL; \
+	} \
+        \
+      switch (b_hash_compare (x.hash, iterator->compare->x)) \
+	{ \
+          case -1: \
+            for (j=0; j<B_TMAP_NUMB; j++) \
+              { \
+                if (B_TMAP_VALUE (tmap.y[j]) != B_TMAP_VALUE_EMPTY) \
+                  { \
+                    tmap_results[B_TMAP_DATA(tmap.y[j])] = 1; \
+		    if (B_TMAP_DATA(tmap.y[j]) == i) \
+		      break; \
+                  } \
+              } \
+            break; \
+          \
+          case 1: \
+            for (j=B_TMAP_NUMB-1; j>=0; j--) \
+              { \
+                if (B_TMAP_VALUE (tmap.y[j]) != B_TMAP_VALUE_EMPTY) \
+                  { \
+                    tmap_results[B_TMAP_DATA(tmap.y[j])] = 1; \
+		    if (B_TMAP_DATA(tmap.y[j]) == i) \
+		     break; \
+                  } \
+              } \
+            break; \
+          \
+          case 0: \
+          default: \
+            break; \
+	} \
+    }
+
+      for (i = 0; i < B_TMAP_NUMB; i++)
+	{
+	  b_tmap_item_t subject_uri, subject_bnode, property, object_uri,
+	    object_bnode, object_literal, context, datatype, lang;
+
+	  B_GET_FILE (subject_uri, subjects_uri, 0, subject_uri_exists);
+	  B_GET_FILE (subject_bnode, subjects_bnode, 1, subject_bnode_exists);
+
+	  B_GET_FILE (property, properties, 2, property_exists);
+
+	  B_GET_FILE (object_uri, objects_uri, 3, object_uri_exists);
+	  B_GET_FILE (object_bnode, objects_bnode, 4, object_bnode_exists);
+	  B_GET_FILE (object_literal, objects_literal, 5,
+		      object_literal_exists);
+	  B_GET_FILE (context, contexts, 6, context_exists);
+	  B_GET_FILE (datatype, datatypes, 7, datatype_exists);
+	  B_GET_FILE (lang, langs, 8, lang_exists);
+
+	  if (!(c = (b_tcache_t *) calloc (1, sizeof (b_tcache_t))))
+	    return B_ERROR_MEMORY;
+
+	  b_io_seek (iterator->data->triples_io,
+		     tmap_pointer + sizeof (b_tmap_t) + i * B_TRIPLE_SIZE,
+		     SEEK_SET);
+
+#define B_TRIPLE_POINTER2( x , t ) \
+  if (b_io_read (iterator->data->triples_io, &x, \
+		 sizeof (b_tmap_item_t)) <= 0) \
+    { \
+      b_tcache_destroy_single (NULL, NULL, c); \
+      return B_ERROR_INTERNAL; \
+    } \
+  \
+  if (x.pointer) \
+    { \
+      if ((error = b_get_type_pointer (iterator->data, t, \
+			               x.pointer, &c->x)) \
+          != B_OK) \
+	{ \
+          b_tcache_destroy_single (NULL, NULL, c); \
+	  return error; \
+	} \
+      \
+      if (!c->x) \
+	{ \
+          b_tcache_destroy_single (NULL, NULL, c); \
+	  return B_ERROR_INTERNAL; \
+	} \
+    }
+
+	  B_TRIPLE_POINTER2 (subject_uri, B_TYPE_URI);
+	  B_TRIPLE_POINTER2 (subject_bnode, B_TYPE_BNODE);
+	  B_TRIPLE_POINTER2 (property, B_TYPE_PROPERTY);
+	  B_TRIPLE_POINTER2 (object_uri, B_TYPE_URI);
+	  B_TRIPLE_POINTER2 (object_bnode, B_TYPE_BNODE);
+	  B_TRIPLE_POINTER2 (object_literal, B_TYPE_LITERAL);
+	  B_TRIPLE_POINTER2 (context, B_TYPE_CONTEXT);
+	  B_TRIPLE_POINTER2 (datatype, B_TYPE_DATATYPE);
+	  B_TRIPLE_POINTER2 (lang, B_TYPE_LITERAL);
+
+	  b_tcache_destroy_single (NULL, NULL, c);
+
+	}
+
+      if (iterator->results)
+	{
+	  list = iterator->results;
+
+	  *cache = list->cache;
+	  iterator->results = list->next;
+	  free (list);
+	}
+      else
+	{
+	  return b_iterator_triple_pointer (iterator,
+					    tmap_pointer + sizeof (b_tmap_t) +
+					    B_TRIPLE_SIZE * B_TMAP_NUMB, 0,
+					    cache);
+	}
+
+      return B_OK;
+    }
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/bmd5.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/bmd5.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/bmd5.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/bmd5.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,311 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ *
+ * Original copyright message:
+ *
+ * This code implements the MD5 message-digest algorithm.  To compute
+ * the message digest of a chunk of bytes, declare an MD5Context
+ * structure, pass it to MD5Init, call MD5Update as needed on
+ * buffers full of bytes, and then call MD5Final, which will fill a
+ * supplied 16-byte array with the digest.
+ *
+ * Equivalent code is available from RSA Data Security, Inc.  This code
+ * has been tested against that, and is equivalent, except that you
+ * don't need to include two pages of legalese with every copy.
+ *
+ * --
+ * End of original copyright license.
+ */
+
+#include <b.h>
+
+/* original code from header - function names have changed */
+
+struct MD5Context
+{
+  unsigned int buf[4];
+  unsigned int bits[2];
+  unsigned char in[64];
+};
+
+static void MD5Init (struct MD5Context *context);
+static void MD5Update (struct MD5Context *context,
+		       const unsigned char *buf, b_uint64 len);
+static void MD5Final (struct MD5Context *context, unsigned char buf[16]);
+static void MD5Transform (unsigned int buf[4], unsigned int const in[16]);
+
+
+/* original code from C file - GNU configurised */
+
+#ifndef WORDS_BIGENDIAN
+#define byteReverse(buf, len)	/* Nothing */
+#else
+static void byteReverse (unsigned char *buf, unsigned longs);
+
+/* ASM ifdef removed */
+
+/*
+ * Note: this code is harmless on little-endian machines.
+ */
+static void
+byteReverse (unsigned char *buf, unsigned longs)
+{
+  unsigned int t;
+  do
+    {
+      t = (unsigned int) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
+	((unsigned) buf[1] << 8 | buf[0]);
+      *(unsigned int *) buf = t;
+      buf += 4;
+    }
+  while (--longs);
+}
+#endif /* WORDS_BIGENDIAN */
+
+
+/*
+ * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
+ * initialization constants.
+ */
+static void
+MD5Init (struct MD5Context *ctx)
+{
+  ctx->buf[0] = 0x67452301;
+  ctx->buf[1] = 0xefcdab89;
+  ctx->buf[2] = 0x98badcfe;
+  ctx->buf[3] = 0x10325476;
+
+  ctx->bits[0] = 0;
+  ctx->bits[1] = 0;
+}
+
+/*
+ * Update context to reflect the concatenation of another buffer full
+ * of bytes.
+ */
+static void
+MD5Update (struct MD5Context *ctx, const unsigned char *buf, b_uint64 len)
+{
+  unsigned int t;
+
+  /* Update bitcount */
+
+  t = ctx->bits[0];
+  if ((ctx->bits[0] = t + ((unsigned int) len << 3)) < t)
+    ctx->bits[1]++;		/* Carry from low to high */
+  ctx->bits[1] += (unsigned int) len >> 29;
+
+  t = (t >> 3) & 0x3f;		/* Bytes already in shsInfo->data */
+
+  /* Handle any leading odd-sized chunks */
+
+  if (t)
+    {
+      unsigned char *p = (unsigned char *) ctx->in + t;
+
+      t = 64 - t;
+      if (len < t)
+	{
+	  memcpy (p, buf, (size_t) len);
+	  return;
+	}
+      memcpy (p, buf, t);
+      byteReverse (ctx->in, 16);
+      MD5Transform (ctx->buf, (unsigned int *) ctx->in);
+      buf += t;
+      len -= t;
+    }
+
+  /* Process data in 64-byte chunks */
+
+  while (len >= 64)
+    {
+      memcpy (ctx->in, buf, 64);
+      byteReverse (ctx->in, 16);
+      MD5Transform (ctx->buf, (unsigned int *) ctx->in);
+      buf += 64;
+      len -= 64;
+    }
+
+  /* Handle any remaining bytes of data. */
+
+  memcpy (ctx->in, buf, (size_t) len);
+}
+
+/*
+ * Final wrapup - pad to 64-byte boundary with the bit pattern 
+ * 1 0* (64-bit count of bits processed, MSB-first)
+ */
+
+/* Interface altered by DJB to write digest into pre-allocated context */
+static void
+MD5Final (struct MD5Context *ctx, unsigned char *digest)
+{
+  unsigned count;
+  unsigned char *p;
+
+  /* Compute number of bytes mod 64 */
+  count = (ctx->bits[0] >> 3) & 0x3F;
+
+  /* Set the first char of padding to 0x80.  This is safe since there is
+     always at least one byte free */
+  p = ctx->in + count;
+  *p++ = 0x80;
+
+  /* Bytes of padding needed to make 64 bytes */
+  count = 64 - 1 - count;
+
+  /* Pad out to 56 mod 64 */
+  if (count < 8)
+    {
+      /* Two lots of padding:  Pad the first block to 64 bytes */
+      memset (p, 0, count);
+      byteReverse (ctx->in, 16);
+      MD5Transform (ctx->buf, (unsigned int *) ctx->in);
+
+      /* Now fill the next block with 56 bytes */
+      memset (ctx->in, 0, 56);
+    }
+  else
+    {
+      /* Pad block to 56 bytes */
+      memset (p, 0, count - 8);
+    }
+  byteReverse (ctx->in, 14);
+
+  /* Append length in bits and transform */
+  ((unsigned int *) ctx->in)[14] = ctx->bits[0];
+  ((unsigned int *) ctx->in)[15] = ctx->bits[1];
+
+  MD5Transform (ctx->buf, (unsigned int *) ctx->in);
+  byteReverse ((unsigned char *) ctx->buf, 4);
+  memcpy (digest, ctx->buf, 16);
+  memset (ctx, 0, sizeof (ctx));	/* In case it's sensitive */
+}
+
+
+/* The four core functions - F1 is optimized somewhat */
+
+/* #define F1(x, y, z) (x & y | ~x & z) */
+#define F1(x, y, z) (z ^ (x & (y ^ z)))
+#define F2(x, y, z) F1(z, x, y)
+#define F3(x, y, z) (x ^ y ^ z)
+#define F4(x, y, z) (y ^ (x | ~z))
+
+/* This is the central step in the MD5 algorithm. */
+#define MD5STEP(f, w, x, y, z, data, s) \
+	( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
+
+/*
+ * The core of the MD5 algorithm, this alters an existing MD5 hash to
+ * reflect the addition of 16 longwords of new data.  MD5Update blocks
+ * the data and converts bytes into longwords for this routine.
+ */
+static void
+MD5Transform (unsigned int buf[4], unsigned int const in[16])
+{
+  register unsigned int a, b, c, d;
+
+  a = buf[0];
+  b = buf[1];
+  c = buf[2];
+  d = buf[3];
+
+  MD5STEP (F1, a, b, c, d, in[0] + 0xd76aa478, 7);
+  MD5STEP (F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
+  MD5STEP (F1, c, d, a, b, in[2] + 0x242070db, 17);
+  MD5STEP (F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
+  MD5STEP (F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
+  MD5STEP (F1, d, a, b, c, in[5] + 0x4787c62a, 12);
+  MD5STEP (F1, c, d, a, b, in[6] + 0xa8304613, 17);
+  MD5STEP (F1, b, c, d, a, in[7] + 0xfd469501, 22);
+  MD5STEP (F1, a, b, c, d, in[8] + 0x698098d8, 7);
+  MD5STEP (F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
+  MD5STEP (F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
+  MD5STEP (F1, b, c, d, a, in[11] + 0x895cd7be, 22);
+  MD5STEP (F1, a, b, c, d, in[12] + 0x6b901122, 7);
+  MD5STEP (F1, d, a, b, c, in[13] + 0xfd987193, 12);
+  MD5STEP (F1, c, d, a, b, in[14] + 0xa679438e, 17);
+  MD5STEP (F1, b, c, d, a, in[15] + 0x49b40821, 22);
+
+  MD5STEP (F2, a, b, c, d, in[1] + 0xf61e2562, 5);
+  MD5STEP (F2, d, a, b, c, in[6] + 0xc040b340, 9);
+  MD5STEP (F2, c, d, a, b, in[11] + 0x265e5a51, 14);
+  MD5STEP (F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
+  MD5STEP (F2, a, b, c, d, in[5] + 0xd62f105d, 5);
+  MD5STEP (F2, d, a, b, c, in[10] + 0x02441453, 9);
+  MD5STEP (F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
+  MD5STEP (F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
+  MD5STEP (F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
+  MD5STEP (F2, d, a, b, c, in[14] + 0xc33707d6, 9);
+  MD5STEP (F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
+  MD5STEP (F2, b, c, d, a, in[8] + 0x455a14ed, 20);
+  MD5STEP (F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
+  MD5STEP (F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
+  MD5STEP (F2, c, d, a, b, in[7] + 0x676f02d9, 14);
+  MD5STEP (F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
+
+  MD5STEP (F3, a, b, c, d, in[5] + 0xfffa3942, 4);
+  MD5STEP (F3, d, a, b, c, in[8] + 0x8771f681, 11);
+  MD5STEP (F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
+  MD5STEP (F3, b, c, d, a, in[14] + 0xfde5380c, 23);
+  MD5STEP (F3, a, b, c, d, in[1] + 0xa4beea44, 4);
+  MD5STEP (F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
+  MD5STEP (F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
+  MD5STEP (F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
+  MD5STEP (F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
+  MD5STEP (F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
+  MD5STEP (F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
+  MD5STEP (F3, b, c, d, a, in[6] + 0x04881d05, 23);
+  MD5STEP (F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
+  MD5STEP (F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
+  MD5STEP (F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
+  MD5STEP (F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
+
+  MD5STEP (F4, a, b, c, d, in[0] + 0xf4292244, 6);
+  MD5STEP (F4, d, a, b, c, in[7] + 0x432aff97, 10);
+  MD5STEP (F4, c, d, a, b, in[14] + 0xab9423a7, 15);
+  MD5STEP (F4, b, c, d, a, in[5] + 0xfc93a039, 21);
+  MD5STEP (F4, a, b, c, d, in[12] + 0x655b59c3, 6);
+  MD5STEP (F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
+  MD5STEP (F4, c, d, a, b, in[10] + 0xffeff47d, 15);
+  MD5STEP (F4, b, c, d, a, in[1] + 0x85845dd1, 21);
+  MD5STEP (F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
+  MD5STEP (F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
+  MD5STEP (F4, c, d, a, b, in[6] + 0xa3014314, 15);
+  MD5STEP (F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
+  MD5STEP (F4, a, b, c, d, in[4] + 0xf7537e82, 6);
+  MD5STEP (F4, d, a, b, c, in[11] + 0xbd3af235, 10);
+  MD5STEP (F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
+  MD5STEP (F4, b, c, d, a, in[9] + 0xeb86d391, 21);
+
+  buf[0] += a;
+  buf[1] += b;
+  buf[2] += c;
+  buf[3] += d;
+}
+
+/* My code starts here: */
+b_error_t
+b_MD5 (unsigned char *what, b_uint64 len, unsigned char *where)
+{
+  struct MD5Context context;
+
+  B_LOG3 ("b_MD5: %p %d %p", what, (int) len, where);
+
+  if (!what || !where)
+    return B_ERROR_DATA;
+
+  MD5Init (&context);
+  MD5Update (&context, what, len);
+  MD5Final (&context, where);
+
+  return B_OK;
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/bnew.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/bnew.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/bnew.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/bnew.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,82 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+/**
+ * b_new:
+ * @data: The pointer to the b_t struct.
+ * @path: The path the name of the DB.
+ * @returns: the error code.
+ *
+ * Creates a new b_t struct.
+ *
+ * The new b_t will be allocated and it should be destroied with
+ * b_destroy(). The path is name of the DB. You should read the
+ * introduction to understand how the B storage uses this path.
+ **/
+b_error_t
+b_new (b_t ** data, unsigned char *path)
+{
+  b_error_t error;
+  char *file;
+  b_uint64 n;
+
+  B_LOG2 ("b_new: %p %p", data, path);
+
+  if (!data || !path)
+    return B_ERROR_DATA;
+
+  /* allocation: */
+  if (!(*data = (b_t *) calloc (1, sizeof (b_t))))
+    return B_ERROR_MEMORY;
+
+  n = B_MAX_CACHE_ITEMS * (B_HASH_SIZE + sizeof (b_uint64));
+
+#define B_NEW_FD( p , where ) \
+  if (! \
+      (file = \
+       malloc (sizeof (char) * (strlen ((char *)path) + strlen (p) + 1)))) \
+    { \
+      b_destroy (*data); \
+      return B_ERROR_MEMORY; \
+    } \
+  \
+  sprintf (file, "%s%s", path, p); \
+  error = b_io_new (&(*data)->where, (unsigned char *)file, n); \
+  free(file); \
+  \
+  if (error!=B_OK) \
+    { \
+      b_destroy (*data); \
+      return error; \
+    }
+
+  /* Set the max cache: */
+  (*data)->max_cache = B_MAX_CACHE_ITEMS;
+
+  /* Open the file descriptor for the cache systems: */
+  B_NEW_FD (B_PATH_URIS_DATA, uris_io);
+  B_NEW_FD (B_PATH_PROPERTIES_DATA, properties_io);
+  B_NEW_FD (B_PATH_LITERALS_DATA, literals_io);
+  B_NEW_FD (B_PATH_BNODES_DATA, bnodes_io);
+  B_NEW_FD (B_PATH_CONTEXTS_DATA, contexts_io);
+  B_NEW_FD (B_PATH_DATATYPES_DATA, datatypes_io);
+  B_NEW_FD (B_PATH_TRIPLES_DATA, triples_io);
+
+  if ((error = b_count_triple_cache (*data, &(*data)->triples_total)))
+    {
+      b_destroy (*data);
+      return error;
+    }
+
+  return B_OK;
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/bremove.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/bremove.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/bremove.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/bremove.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,302 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+/**
+ * b_remove_type:
+ * @data: The pointer to a b_t data struct.
+ * @type: The type of the element
+ * @what: the value of the element
+ * @len: The len. It can be 0
+ * @returns: The error code.
+ *
+ * Removes a element into the storage. The element is removed if it is not 
+ * used by some triple.
+ **/
+b_error_t
+b_remove_type (b_t * data, b_type_t type, unsigned char *what, b_uint64 len)
+{
+  b_error_t error;
+  b_cache_t *cache;
+  b_io_t *io;
+
+  if ((error = b_get_type_cache (data, type, what, len, &cache)) != B_OK
+      || !cache)
+    return error;
+
+  if ((error = b_get_type_internal (data, type, &io)) != B_OK)
+    return error;
+
+  return b_remove_type_cache (data, io, cache);
+}
+
+b_error_t
+b_remove_type_cache (b_t * data, b_io_t * io, b_cache_t * cache)
+{
+  b_map_t map;
+  b_map_t parent;
+
+  b_io_seek (io, cache->pointer, SEEK_SET);
+
+  if (b_io_read (io, &map, sizeof (b_map_t)) <= 0)
+    return B_ERROR_INTERNAL;
+
+  /* I don't remove used element: */
+  if (map.instances > 0)
+    return B_OK;
+
+  b_io_seek (io, map.parent, SEEK_SET);
+  if (b_io_read (io, &parent, sizeof (b_map_t)) <= 0)
+    return B_ERROR_INTERNAL;
+
+  /* 1 child or 0 children: */
+  if (!map.left)
+    {
+      if (parent.left == cache->pointer)
+	parent.left = map.right;
+      else
+	parent.right = map.right;
+
+      if (map.right)
+	{
+	  b_map_t other;
+
+	  b_io_seek (io, map.right, SEEK_SET);
+	  if (b_io_read (io, &other, sizeof (b_map_t)) <= 0)
+	    return B_ERROR_INTERNAL;
+
+	  other.parent = map.parent;
+
+	  b_io_seek (io, map.right, SEEK_SET);
+	  if (b_io_write (io, &other, sizeof (b_map_t), data->max_cache) <= 0)
+	    return B_ERROR_INTERNAL;
+	}
+    }
+
+  else if (!map.right)
+    {
+      if (parent.left == cache->pointer)
+	parent.left = map.left;
+      else
+	parent.right = map.left;
+
+      if (map.left)
+	{
+	  b_map_t other;
+
+	  b_io_seek (io, map.left, SEEK_SET);
+	  if (b_io_read (io, &other, sizeof (b_map_t)) <= 0)
+	    return B_ERROR_INTERNAL;
+
+	  other.parent = map.parent;
+
+	  b_io_seek (io, map.left, SEEK_SET);
+	  if (b_io_write (io, &other, sizeof (b_map_t), data->max_cache) <= 0)
+	    return B_ERROR_INTERNAL;
+	}
+    }
+
+  /* 2 children: */
+  else
+    {
+      b_map_t other;
+      b_uint64 pointer;
+
+      /* I'm looking for the in-order successor: */
+      pointer = b_io_seek (io, map.right, SEEK_SET);
+      if (b_io_read (io, &other, sizeof (b_map_t)) <= 0)
+	return B_ERROR_INTERNAL;
+
+      while (other.left)
+	{
+	  pointer = b_io_seek (io, other.left, SEEK_SET);
+	  if (b_io_read (io, &other, sizeof (b_map_t)) <= 0)
+	    return B_ERROR_INTERNAL;
+	}
+
+      /* I change the parent link: */
+      if (parent.left == cache->pointer)
+	parent.left = pointer;
+      else
+	parent.right = pointer;
+
+      /* If it is not the previous on the right: */
+      if (other.parent != cache->pointer)
+	{
+	  b_map_t other_p;
+	  b_map_t other_r;
+
+	  b_io_seek (io, other.parent, SEEK_SET);
+	  if (b_io_read (io, &other_p, sizeof (b_map_t)) <= 0)
+	    return B_ERROR_INTERNAL;
+
+	  if (other_p.left == pointer)
+	    other_p.left = other.right;
+	  else
+	    other_p.right = other.right;
+
+	  b_io_seek (io, other.parent, SEEK_SET);
+	  if (b_io_write (io, &other_p, sizeof (b_map_t), data->max_cache) <=
+	      0)
+	    return B_ERROR_INTERNAL;
+
+	  if (other.right)
+	    {
+	      b_io_seek (io, other.right, SEEK_SET);
+	      if (b_io_read (io, &other_r, sizeof (b_map_t)) <= 0)
+		return B_ERROR_INTERNAL;
+
+	      other_r.parent = other.parent;
+
+	      b_io_seek (io, other.right, SEEK_SET);
+	      if (b_io_write (io, &other_r, sizeof (b_map_t), data->max_cache)
+		  <= 0)
+		return B_ERROR_INTERNAL;
+	    }
+
+	  b_io_seek (io, map.right, SEEK_SET);
+	  if (b_io_read (io, &other_r, sizeof (b_map_t)) <= 0)
+	    return B_ERROR_INTERNAL;
+
+	  other_r.parent = pointer;
+
+	  b_io_seek (io, map.right, SEEK_SET);
+	  if (b_io_write (io, &other_r, sizeof (b_map_t), data->max_cache) <=
+	      0)
+	    return B_ERROR_INTERNAL;
+
+	  other.right = map.right;
+	}
+
+      /* Set the parent and the left one: */
+      other.parent = map.parent;
+      other.left = map.left;
+
+      b_io_seek (io, pointer, SEEK_SET);
+      if (b_io_write (io, &other, sizeof (b_map_t), data->max_cache) <= 0)
+	return B_ERROR_INTERNAL;
+
+      /* The left child: */
+      b_io_seek (io, map.left, SEEK_SET);
+      if (b_io_read (io, &other, sizeof (b_map_t)) <= 0)
+	return B_ERROR_INTERNAL;
+
+      other.parent = pointer;
+
+      b_io_seek (io, map.left, SEEK_SET);
+      if (b_io_write (io, &other, sizeof (b_map_t), data->max_cache) <= 0)
+	return B_ERROR_INTERNAL;
+    }
+
+  /* sync the parent: */
+  b_io_seek (io, map.parent, SEEK_SET);
+  if (b_io_write (io, &parent, sizeof (b_map_t), data->max_cache) <= 0)
+    return B_ERROR_INTERNAL;
+
+  /* Sync the node: */
+  b_hash_zero (map.hash);
+
+  b_io_seek (io, cache->pointer, SEEK_SET);
+  if (b_io_write (io, &map, sizeof (b_map_t), data->max_cache) <= 0)
+    return B_ERROR_INTERNAL;
+
+  return B_OK;
+}
+
+/**
+ * b_remove_triple:
+ * @data: The pointer to a b_t data struct.
+ * @triple: The pointer of the triple.
+ * @returns: The error code.
+ *
+ * Removes a triple into the storage.
+ **/
+b_error_t
+b_remove_triple (b_t * data, b_triple_t * triple)
+{
+  b_tcache_t *cache;
+  b_error_t error;
+
+  B_LOG2 ("b_remove_triple: %p %p", data, triple);
+
+  if (!data)
+    return B_ERROR_DATA;
+
+  if (b_triple_check (triple) != B_OK)
+    return B_ERROR_DATA;
+
+  /* if it doesn't exist? */
+  if ((error = b_get_triple_cache (data, triple, &cache)) != B_OK || !cache)
+    {
+      return error;
+    }
+
+  if (cache->tmap_pointer)
+    {
+      b_tmap_t tmap;
+      unsigned char s_uri, s_bnode, p, o_uri, o_bnode, o_literal, c, d, l, i;
+
+      b_io_seek (data->triples_io, cache->tmap_pointer, SEEK_SET);
+
+      if (b_io_read (data->triples_io, &tmap, sizeof (b_tmap_t)) <= 0)
+	return B_ERROR_INTERNAL;
+
+      s_uri = s_bnode = p = o_uri = o_bnode = o_literal = c = d = l = 0;
+
+      for (i = 0; i < B_TMAP_NUMB; i++)
+	{
+
+#define B_REMOVE_SWITCH( x , a ) \
+        if(!x && B_TMAP_DATA (tmap.a[i])==cache->tmap_index) \
+          x++; \
+        \
+        if (x) \
+	  { \
+	    if (i<B_TMAP_NUMB - 1) \
+              tmap.a[i]=tmap.a[i+1]; \
+	    else \
+              tmap.a[i]=0; \
+	  }
+
+	  B_REMOVE_SWITCH (s_uri, subjects_uri);
+	  B_REMOVE_SWITCH (s_bnode, subjects_bnode);
+	  B_REMOVE_SWITCH (p, properties);
+	  B_REMOVE_SWITCH (o_uri, objects_uri);
+	  B_REMOVE_SWITCH (o_bnode, objects_bnode);
+	  B_REMOVE_SWITCH (o_literal, objects_literal);
+	  B_REMOVE_SWITCH (c, contexts);
+	  B_REMOVE_SWITCH (d, datatypes);
+	  B_REMOVE_SWITCH (l, langs);
+	}
+
+      tmap.map[cache->tmap_index] = 0;
+
+      b_io_seek (data->triples_io, cache->tmap_pointer, SEEK_SET);
+
+      if (b_io_write (data->triples_io, &tmap, sizeof (tmap), data->max_cache)
+	  <= 0)
+	return B_ERROR_POSIX;
+
+      if ((error =
+	   b_tempty_check (data, &tmap, cache->tmap_pointer, 0)) != B_OK)
+	return error;
+
+    }
+
+  if ((error = b_sync_instances (data, cache, 0)) != B_OK)
+    return error;
+
+  b_tcache_destroy_single (&data->triples_cache, &data->triples_cache_items,
+			   cache);
+  data->triples_total--;
+  return B_OK;
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/bstring.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/bstring.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/bstring.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/bstring.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,124 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+b_error_t
+b_string_new (b_string_t ** new)
+{
+  B_LOG1 ("b_string_new: %p", new);
+
+  if (!new)
+    return B_ERROR_DATA;
+
+  if (!(*new = (b_string_t *) calloc (1, sizeof (b_string_t))))
+    return B_ERROR_MEMORY;
+
+  return B_OK;
+}
+
+b_error_t
+b_string_new_callback (b_string_t ** new, b_write_func callback,
+		       b_uint64 size, void *data)
+{
+  B_LOG4 ("b_string_new_callback: %p %p %d %p", new, callback, (int) size,
+	  data);
+
+  if (!new || !callback || !size)
+    return B_ERROR_DATA;
+
+  if (!(*new = (b_string_t *) calloc (1, sizeof (b_string_t))))
+    return B_ERROR_MEMORY;
+
+  (*new)->callback = callback;
+  (*new)->callback_data = data;
+  (*new)->callback_size = size;
+
+  return B_OK;
+}
+
+b_error_t
+b_string_destroy (b_string_t * str, b_int64 * ret)
+{
+  B_LOG2 ("b_string_destroy: %p %p", str, ret);
+
+  if (!str)
+    return B_ERROR_DATA;
+
+  if (str->size && str->callback && str->callback_ret >= 0)
+    str->callback_ret =
+      str->callback (str->string, str->size, str->callback_data);
+
+  if (str->string)
+    free (str->string);
+
+  if (ret)
+    *ret = str->callback_ret;
+
+  free (str);
+  return B_OK;
+}
+
+b_error_t
+b_string_add (b_string_t * str, unsigned char *what, b_uint64 size)
+{
+  B_LOG3 ("b_string_add: %p %p %d", str, what, (int) size);
+
+  if (!str || !what)
+    return B_ERROR_DATA;
+
+  size = size > 0 ? size : strlen ((char *) what);
+
+  if (!str->string)
+    str->string = malloc (sizeof (char) * (size_t) (size + 1));
+  else
+    str->string =
+      realloc (str->string, sizeof (char) * (size_t) (size + 1 + str->size));
+
+  if (!str->string)
+    return B_ERROR_MEMORY;
+
+  memcpy (str->string + str->size, what, (size_t) size);
+  str->size += size;
+  str->string[str->size] = 0;
+
+  if (str->callback_ret >= 0 && str->callback
+      && str->size >= str->callback_size)
+    {
+      str->callback_ret =
+	str->callback (str->string, str->size, str->callback_data);
+      free (str->string);
+
+      str->string = NULL;
+      str->size = 0;
+    }
+
+  return B_OK;
+}
+
+unsigned char *
+b_string_alloc (unsigned char *what, b_uint64 len)
+{
+  unsigned char *str;
+
+  B_LOG2 ("b_string_alloc: %p %d", what, (int) len);
+
+  if (!
+      (str =
+       (unsigned char *) malloc (sizeof (unsigned char) *
+				 (size_t) (len + 1))))
+    return NULL;
+
+  memcpy (str, what, (size_t) len);
+  str[len] = 0;
+
+  return str;
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/bsync.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/bsync.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/bsync.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/bsync.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,604 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+struct b_sync_data_t
+{
+  b_io_t *io;
+  b_uint64 max_cache;
+};
+
+static b_int64 b_sync_write (unsigned char *what, b_uint64 size, void *dummy);
+static b_error_t b_sync_cache_empty (b_t * data, b_io_t * io);
+static b_error_t b_sync_instances_cache (b_t * data, b_cache_t * cache,
+					 b_type_t type, char add);
+
+/**
+ * b_sync:
+ * @data: The pointer to a b_t data struct.
+ * @returns: The error code.
+ * 
+ * Syncs the storage. You shouldn't use this function because B storage syncs
+ * the data automaticly.
+ **/
+b_error_t
+b_sync (b_t * data)
+{
+  b_error_t error;
+
+  B_LOG1 ("b_sync: %p", data);
+
+  if (!data)
+    return B_ERROR_DATA;
+
+  /* A different function for the triples: */
+  if ((error = b_sync_tcache (data)) != B_OK)
+    return error;
+
+  return B_OK;
+}
+
+/* This function syncs a single list: */
+b_error_t
+b_sync_cache (b_t * data, b_io_t * io, unsigned char *hash,
+	      unsigned char *what, b_uint64 len)
+{
+  b_map_t m, map;
+  b_uint64 pointer;
+  int cmp;
+
+  B_LOG5 ("b_sync_cache: %p %p %p %p %d", data, io, hash, what, (int) len);
+
+  memset (&map, 0, sizeof (b_map_t));
+  b_hash_copy (map.hash, hash);
+  map.len = len;
+
+  pointer = b_io_seek (io, sizeof (b_uint64), SEEK_SET);
+
+  /* The b-tree starts with a empty elements, so I can't read it doesn't
+   * exist: */
+  if (b_io_read (io, &m, sizeof (m)) <= 0)
+    {
+      b_error_t err;
+
+      if ((err = b_sync_cache_empty (data, io)) != B_OK)
+	return err;
+
+      /* Recursion: */
+      return b_sync_cache (data, io, hash, what, len);
+    }
+
+  pointer = b_io_seek (io, sizeof (b_uint64), SEEK_SET);
+
+  while (1)
+    {
+      /* Try to read but only if it is not the first node 
+       * (I have read it already 10 lines before): */
+      if (pointer != sizeof (b_uint64) && b_io_read (io, &m, sizeof (m)) <= 0)
+	return B_ERROR_INTERNAL;
+
+      cmp = b_hash_compare (map.hash, m.hash);
+
+      if (cmp <= 0)
+	{
+	  if (m.left)
+	    {
+	      pointer = b_io_seek (io, m.left, SEEK_SET);
+	      continue;
+	    }
+	  else
+	    {
+	      b_uint64 end;
+	      map.parent = pointer;
+
+	      /* Write the new element: */
+	      end = b_io_seek (io, 0, SEEK_END);
+
+	      if (b_io_write
+		  (io, &map, sizeof (b_map_t), data->max_cache) <= 0)
+		return B_ERROR_POSIX;
+
+	      if (map.len
+		  && b_io_write (io, what, map.len, data->max_cache) <= 0)
+		return B_ERROR_POSIX;
+
+	      /* Set the left node to the parent: */
+	      b_io_seek (io, pointer, SEEK_SET);
+	      if (b_io_read (io, &m, sizeof (m)) <= 0)
+		return B_ERROR_INTERNAL;
+
+	      m.left = end;
+
+	      b_io_seek (io, pointer, SEEK_SET);
+	      if (b_io_write (io, &m, sizeof (b_map_t), data->max_cache) <= 0)
+		return B_ERROR_POSIX;
+
+	      return B_OK;
+	    }
+	}
+
+      else
+	{
+	  if (m.right)
+	    {
+	      pointer = b_io_seek (io, m.right, SEEK_SET);
+	      continue;
+	    }
+	  else
+	    {
+	      b_uint64 end;
+	      map.parent = pointer;
+
+	      /* Write the new element: */
+	      end = b_io_seek (io, 0, SEEK_END);
+
+	      if (b_io_write
+		  (io, &map, sizeof (b_map_t), data->max_cache) <= 0)
+		return B_ERROR_POSIX;
+
+	      if (map.len
+		  && b_io_write (io, what, map.len, data->max_cache) <= 0)
+		return B_ERROR_POSIX;
+
+	      /* Set the right node to the parent: */
+	      b_io_seek (io, pointer, SEEK_SET);
+	      if (b_io_read (io, &m, sizeof (m)) <= 0)
+		return B_ERROR_INTERNAL;
+
+	      m.right = end;
+
+	      b_io_seek (io, pointer, SEEK_SET);
+	      if (b_io_write (io, &m, sizeof (b_map_t), data->max_cache) <= 0)
+		return B_ERROR_POSIX;
+
+	      return B_OK;
+	    }
+	}
+    }
+
+  return B_OK;
+}
+
+/* Empty cache: */
+static b_error_t
+b_sync_cache_empty (b_t * data, b_io_t * io)
+{
+  b_map_t map;
+  b_error_t err;
+
+  memset (&map, 0, sizeof (b_map_t));
+
+  if ((err = b_hash_new (data, (unsigned char *) "", 0, map.hash)) != B_OK)
+    return err;
+
+  b_io_seek (io, sizeof (b_uint64), SEEK_SET);
+
+  if (b_io_write (io, &map, sizeof (b_map_t), data->max_cache) <= 0)
+    return B_ERROR_POSIX;
+
+  if (map.len && b_io_write (io, "", 1, data->max_cache) <= 0)
+    return B_ERROR_POSIX;
+
+  return B_OK;
+}
+
+/* This function syncs a single triple list: */
+b_error_t
+b_sync_tcache (b_t * data)
+{
+  b_uint64 pointer;
+  b_error_t error;
+  b_tcache_t *cache;
+  unsigned char empty[B_TRIPLE_SIZE];
+  struct b_sync_data_t write_data;
+
+  B_LOG1 ("b_sync_tcache: %p", data);
+
+  if (!data->triples_cache)
+    return B_OK;
+
+  /* Empty element: */
+  memset (empty, 0, sizeof (empty));
+
+  /* Until we have cache: */
+  cache = data->triples_cache;
+  while (cache)
+    {
+      int numb, i, j, b;
+      b_tmap_t tmap;
+      b_tmap_item_t tmap_item;
+      b_tcache_t *caches[B_TMAP_NUMB];
+      char caches_free[B_TMAP_NUMB];
+      char tosync = 0;
+      char fromtempty = 0;
+
+      memset (caches, 0, sizeof (caches));
+      memset (caches_free, 0, sizeof (caches_free));
+
+      /* FIXME: This code doesn't work yet: */
+      if (1 || b_tempty_get (data, &tmap, &pointer))
+	{
+	  /* Creating a empty map: */
+	  memset (&tmap, 0, sizeof (tmap));
+	  pointer = b_io_seek (data->triples_io, 0, SEEK_END);
+	}
+      else
+	fromtempty = 1;
+
+      /* Until the map is not full: */
+      for (numb = 0; cache && numb < B_TMAP_NUMB; cache = cache->next, numb++)
+	{
+	  /* If the cache element is already storage, I restart researc a
+	   * new element: */
+	  if (cache->tmap_pointer)
+	    {
+	      numb -= 1;
+	      continue;
+	    }
+
+	  /* Search a empty space in the current map: */
+	  while (tmap.map[numb] == 1 && numb < B_TMAP_NUMB)
+	    numb++;
+
+	  if (numb >= B_TMAP_NUMB)
+	    break;
+
+	  tmap.map[numb] = 1;
+
+	  /* Set the new values in the cache struct: */
+	  cache->tmap_pointer = pointer;
+	  cache->tmap_index = numb;
+
+	  caches[numb] = cache;
+	  tosync = 1;
+
+	  if (fromtempty)
+	    {
+	      b_tcache_copy (caches[numb], &caches[numb]);
+	      caches_free[numb] = 1;
+	    }
+
+#define B_SYNC_GET( x , l , t ) \
+  if (b_io_read (data->triples_io, &tmap_item, \
+		 sizeof (b_tmap_item_t)) <= 0) \
+    { \
+      b_tcache_destroy_single (NULL, NULL, caches[i]); \
+      return B_ERROR_INTERNAL; \
+    } \
+  \
+  if (tmap_item.pointer) \
+    { \
+      if ((error = b_get_type_pointer (data, t, tmap_item.pointer, \
+	                               &caches[value]->x)) \
+          != B_OK) \
+	{ \
+          b_tcache_destroy_single (NULL, NULL, caches[value]); \
+	  return error; \
+	} \
+      \
+      if (!caches[value]->x) \
+	{ \
+          b_tcache_destroy_single (NULL, NULL, caches[value]); \
+	  return B_ERROR_INTERNAL; \
+	} \
+    }
+
+	  /* This macro set a item in the correct order. I use it for
+	   * the different elements (subject, property, ...): */
+#define B_SYNC_TCACHE( x , y ) \
+	  if (!caches[numb]->x) \
+	    { \
+	      for (i = numb; i > 0; i--) \
+		tmap.y[i] = tmap.y[i - 1]; \
+              \
+	      tmap.y[0] = B_TMAP_SET(numb, B_TMAP_VALUE_EMPTY); \
+	    } \
+	  else \
+	    { \
+	      for (i = b = 0; i < B_TMAP_NUMB; i++) \
+		{ \
+	          int value; \
+                  \
+		  value = B_TMAP_DATA (tmap.y[i]); \
+                  \
+		  if (!tmap.map[value] || B_TMAP_VALUE (tmap.y[i]) == \
+		      B_TMAP_VALUE_EMPTY) \
+		    continue; \
+                  \
+		  if (!caches[value]) \
+		    { \
+                      if (!(caches[value] = \
+			  (b_tcache_t *) calloc (1, sizeof (b_tcache_t)))) \
+	                return B_ERROR_MEMORY; \
+                      \
+                      b_io_seek (data->triples_io, pointer + value * \
+				 B_TRIPLE_SIZE + sizeof(b_tmap_t), \
+				 SEEK_SET); \
+		      \
+                      B_SYNC_GET (subject_uri, subject_uri_len, \
+	 			     B_TYPE_URI); \
+                      B_SYNC_GET (subject_bnode, subject_bnode_len, \
+				     B_TYPE_BNODE); \
+                      B_SYNC_GET (property, property_len, B_TYPE_PROPERTY); \
+                      B_SYNC_GET (object_uri, object_uri_len, \
+				     B_TYPE_URI); \
+                      B_SYNC_GET (object_bnode, object_bnode_len, \
+				     B_TYPE_BNODE); \
+                      B_SYNC_GET (object_literal, object_literal_len, \
+				     B_TYPE_LITERAL); \
+                      B_SYNC_GET (context, context_len, B_TYPE_CONTEXT); \
+                      B_SYNC_GET (datatype, datatype_len, B_TYPE_DATATYPE); \
+                      B_SYNC_GET (lang, lang_len, B_TYPE_LITERAL); \
+                      \
+		      caches_free[value]=1; \
+		    } \
+                  \
+		  switch (b_hash_compare \
+			  (caches[numb]->x->hash, caches[value]->x->hash)) \
+		    { \
+		    case -1: \
+		      for (j = numb; j > i; j--) \
+			tmap.y[j] = tmap.y[j - 1]; \
+                      \
+		      tmap.y[i] = B_TMAP_SET(numb, B_TMAP_VALUE_FULL); \
+		      b = 1; \
+		      break; \
+                      \
+		    case 0: \
+		      for (j = numb; j > i + 1; j--) \
+			tmap.y[j] = tmap.y[j - 1]; \
+                      \
+		      tmap.y[i + 1] = B_TMAP_SET(numb, B_TMAP_VALUE_FULL); \
+		      b = 1; \
+		      break; \
+                    \
+		    default: \
+		      break; \
+		    } \
+                  \
+		  if (b) \
+		    break; \
+		} \
+              \
+	      if (!b) \
+		tmap.y[numb] = B_TMAP_SET(numb, B_TMAP_VALUE_FULL); \
+	    }
+
+	  /* Correct position in the map: */
+	  B_SYNC_TCACHE (subject_uri, subjects_uri);
+	  B_SYNC_TCACHE (subject_bnode, subjects_bnode);
+
+	  B_SYNC_TCACHE (property, properties);
+
+	  B_SYNC_TCACHE (object_uri, objects_uri);
+	  B_SYNC_TCACHE (object_bnode, objects_bnode);
+	  B_SYNC_TCACHE (object_literal, objects_literal);
+
+	  B_SYNC_TCACHE (context, contexts);
+	  B_SYNC_TCACHE (datatype, datatypes);
+	  B_SYNC_TCACHE (lang, langs);
+	}
+
+      /* If I don't need a sync for this map: */
+      if (!tosync)
+	continue;
+
+      /* Map in the head... */
+      if (fromtempty == 0)
+	{
+	  b_string_t *str;
+
+	  /* Creating of the b_string_new_callback: */
+	  write_data.io = data->triples_io;
+	  write_data.max_cache = data->max_cache;
+
+	  if ((error =
+	       b_string_new_callback (&str, b_sync_write, data->max_cache,
+				      &write_data)) != B_OK)
+	    return error;
+
+	  b_string_add (str, (unsigned char *) &tmap, sizeof (tmap));
+
+	  /* ...and later the triples: */
+	  for (i = 0; i < B_TMAP_NUMB; i++)
+	    {
+	      /* If I have it: */
+	      if (i < numb && caches[i])
+		{
+#define B_SYNC_TCACHE2( x ) \
+		if (caches[i]->x) \
+		  { \
+		    b_hash_copy (tmap_item.hash, caches[i]->x->hash); \
+		    tmap_item.pointer = caches[i]->x->pointer; \
+		  } \
+		else \
+		  memset (&tmap_item, 0, sizeof (b_tmap_item_t)); \
+                \
+		b_string_add (str, (unsigned char *)&tmap_item, \
+                              sizeof (b_tmap_item_t));
+
+		  B_SYNC_TCACHE2 (subject_uri);
+		  B_SYNC_TCACHE2 (subject_bnode);
+		  B_SYNC_TCACHE2 (property);
+		  B_SYNC_TCACHE2 (object_uri);
+		  B_SYNC_TCACHE2 (object_bnode);
+		  B_SYNC_TCACHE2 (object_literal);
+		  B_SYNC_TCACHE2 (context);
+		  B_SYNC_TCACHE2 (datatype);
+		  B_SYNC_TCACHE2 (lang);
+		}
+
+	      /* else empty value: */
+	      else
+		b_string_add (str, empty, sizeof (empty));
+	    }
+
+	  /* Destroy of the b_string_t: */
+	  b_string_destroy (str, NULL);
+	}
+      else
+	{
+	  b_io_seek (data->triples_io, pointer, SEEK_SET);
+
+	  if (b_io_write
+	      (data->triples_io, &tmap, sizeof (b_tmap_t),
+	       data->max_cache) <= 0)
+	    return B_ERROR_INTERNAL;
+
+	  for (i = 0; i < B_TMAP_NUMB; i++)
+	    {
+	      if (i < numb && caches[i] && caches_free[i])
+		{
+#define B_SYNC_TCACHE3( x ) \
+		if (caches[i]->x) \
+		  { \
+		    b_hash_copy (tmap_item.hash, caches[i]->x->hash); \
+		    tmap_item.pointer = caches[i]->x->pointer; \
+		  } \
+		else \
+		  memset (&tmap_item, 0, sizeof (b_tmap_item_t)); \
+                \
+	        if (b_io_write (data->triples_io, &tmap_item, \
+			        sizeof (b_tmap_item_t), data->max_cache) \
+		    <= 0) \
+	          return B_ERROR_INTERNAL;
+
+		  B_SYNC_TCACHE3 (subject_uri);
+		  B_SYNC_TCACHE3 (subject_bnode);
+		  B_SYNC_TCACHE3 (property);
+		  B_SYNC_TCACHE3 (object_uri);
+		  B_SYNC_TCACHE3 (object_bnode);
+		  B_SYNC_TCACHE3 (object_literal);
+		  B_SYNC_TCACHE3 (context);
+		  B_SYNC_TCACHE3 (datatype);
+		  B_SYNC_TCACHE3 (lang);
+		}
+	      else
+		b_io_seek (data->triples_io, B_TRIPLE_SIZE, SEEK_CUR);
+	    }
+	}
+
+      for (i = 0; i < B_TMAP_NUMB; i++)
+	if (caches_free[i])
+	  b_tcache_destroy_single (NULL, NULL, caches[i]);
+
+      if ((error = b_tempty_check (data, &tmap, pointer, 1)) != B_OK)
+	return error;
+    }
+
+  return B_OK;
+}
+
+/* This function is the callback for the b_string_new_with_callback */
+static b_int64
+b_sync_write (unsigned char *what, b_uint64 size, void *dummy)
+{
+  struct b_sync_data_t *data = (struct b_sync_data_t *) dummy;
+
+  B_LOG3 ("b_sync_write: %p %d %p", what, (int) size, dummy);
+
+  return b_io_write (data->io, what, size, data->max_cache);
+}
+
+/* This function change the instances value */
+b_error_t
+b_sync_instances (b_t * data, b_tcache_t * cache, char add)
+{
+  b_error_t ret;
+
+  if (cache->subject_uri
+      && (ret =
+	  b_sync_instances_cache (data, cache->subject_uri, B_TYPE_URI,
+				  add)) != B_OK)
+    return ret;
+
+  if (cache->subject_bnode
+      && (ret =
+	  b_sync_instances_cache (data, cache->subject_bnode, B_TYPE_BNODE,
+				  add)) != B_OK)
+    return ret;
+
+  if (cache->property
+      && (ret =
+	  b_sync_instances_cache (data, cache->property, B_TYPE_PROPERTY,
+				  add)) != B_OK)
+    return ret;
+
+  if (cache->object_uri
+      && (ret =
+	  b_sync_instances_cache (data, cache->object_uri, B_TYPE_URI,
+				  add)) != B_OK)
+    return ret;
+
+  if (cache->object_bnode
+      && (ret =
+	  b_sync_instances_cache (data, cache->object_bnode, B_TYPE_BNODE,
+				  add)) != B_OK)
+    return ret;
+
+  if (cache->object_literal
+      && (ret =
+	  b_sync_instances_cache (data, cache->object_literal, B_TYPE_LITERAL,
+				  add)) != B_OK)
+    return ret;
+
+  if (cache->context
+      && (ret =
+	  b_sync_instances_cache (data, cache->context, B_TYPE_CONTEXT,
+				  add)) != B_OK)
+    return ret;
+
+  if (cache->datatype
+      && (ret =
+	  b_sync_instances_cache (data, cache->datatype, B_TYPE_DATATYPE,
+				  add)) != B_OK)
+    return ret;
+
+  if (cache->lang
+      && (ret =
+	  b_sync_instances_cache (data, cache->lang, B_TYPE_LITERAL,
+				  add)) != B_OK)
+    return ret;
+
+  return B_OK;
+}
+
+static b_error_t
+b_sync_instances_cache (b_t * data, b_cache_t * cache, b_type_t type,
+			char add)
+{
+  b_error_t error;
+  b_io_t *io;
+  b_uint64 pointer;
+  b_uint64 instances;
+
+  if ((error = b_get_type_internal (data, type, &io)) != B_OK)
+    return error;
+
+  pointer = cache->pointer + B_HASH_SIZE + sizeof (b_uint64);
+  b_io_seek (io, pointer, SEEK_SET);
+
+  if (b_io_read (io, &instances, sizeof (instances)) <= 0)
+    return B_ERROR_INTERNAL;
+
+  instances += (add > 0 ? 1 : -1);
+
+  b_io_seek (io, pointer, SEEK_SET);
+
+  if (b_io_write (io, &instances, sizeof (instances), data->max_cache) <= 0)
+    return B_ERROR_INTERNAL;
+
+  if (instances <= 0)
+    return b_remove_type_cache (data, io, cache);
+
+  return B_OK;
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/btcache.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/btcache.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/btcache.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/btcache.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,157 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+/* Destroy the triple cache list: */
+b_error_t
+b_tcache_destroy (b_tcache_t ** list, b_uint64 * len)
+{
+  B_LOG2 ("b_tcache_destroy: %p %p", list, len);
+
+  if (!list || !*list)
+    return B_ERROR_DATA;
+
+  while (*list)
+    b_tcache_destroy_single (list, len, *list);
+
+  return B_OK;
+}
+
+/* This function destroies a triple cache item: */
+b_error_t
+b_tcache_destroy_single (b_tcache_t ** cache_root, b_uint64 * numb,
+			 b_tcache_t * cache)
+{
+  B_LOG3 ("b_tcache_destroy_single: %p %p %p", cache_root, numb, cache);
+
+  if (!cache)
+    return B_ERROR_DATA;
+
+  if (cache_root && cache == *cache_root)
+    *cache_root = cache->next;
+
+  if (cache->prev)
+    cache->prev->next = cache->next;
+
+  if (cache->next)
+    cache->next->prev = cache->prev;
+
+  if (cache->subject_uri)
+    b_cache_destroy (cache->subject_uri);
+
+  if (cache->subject_bnode)
+    b_cache_destroy (cache->subject_bnode);
+
+  if (cache->property)
+    b_cache_destroy (cache->property);
+
+  if (cache->object_uri)
+    b_cache_destroy (cache->object_uri);
+
+  if (cache->object_bnode)
+    b_cache_destroy (cache->object_bnode);
+
+  if (cache->object_literal)
+    b_cache_destroy (cache->object_literal);
+
+  if (cache->context)
+    b_cache_destroy (cache->context);
+
+  if (cache->datatype)
+    b_cache_destroy (cache->datatype);
+
+  if (cache->lang)
+    b_cache_destroy (cache->lang);
+
+  free (cache);
+
+  if (numb)
+    (*numb)--;
+
+  return B_OK;
+}
+
+b_error_t
+b_tcache_copy (b_tcache_t * src, b_tcache_t ** dst)
+{
+  b_tcache_t *new;
+  b_error_t error;
+
+  B_LOG2 ("b_tcache_copy: %p %p", src, dst);
+
+  if (!src)
+    return B_ERROR_DATA;
+
+  if (!(new = (b_tcache_t *) calloc (1, sizeof (b_tcache_t))))
+    return B_ERROR_MEMORY;
+
+#define B_COPY_TCACHE( x ) \
+  if (src->x) \
+    { \
+      if ((error = b_cache_copy (src->x, &new->x)) != B_OK) \
+	return error; \
+    }
+
+  B_COPY_TCACHE (subject_uri);
+  B_COPY_TCACHE (subject_bnode);
+  B_COPY_TCACHE (property);
+  B_COPY_TCACHE (object_uri);
+  B_COPY_TCACHE (object_bnode);
+  B_COPY_TCACHE (object_literal);
+  B_COPY_TCACHE (context);
+  B_COPY_TCACHE (datatype);
+  B_COPY_TCACHE (lang);
+
+  new->tmap_index = src->tmap_index;
+  new->tmap_pointer = src->tmap_pointer;
+
+  *dst = new;
+  return B_OK;
+}
+
+b_error_t
+b_tcache_copy_easy (b_tcache_t * src, b_tcache_t ** dst)
+{
+  b_tcache_t *new;
+  b_error_t error;
+
+  B_LOG2 ("b_tcache_copy_easy: %p %p", src, dst);
+
+  if (!src)
+    return B_ERROR_DATA;
+
+  if (!(new = (b_tcache_t *) calloc (1, sizeof (b_tcache_t))))
+    return B_ERROR_MEMORY;
+
+#define B_COPY_TCACHE_EASY( x ) \
+  if (src->x) \
+    { \
+      if ((error = b_cache_copy_easy (src->x, &new->x)) != B_OK) \
+	return error; \
+    }
+
+  B_COPY_TCACHE_EASY (subject_uri);
+  B_COPY_TCACHE_EASY (subject_bnode);
+  B_COPY_TCACHE_EASY (property);
+  B_COPY_TCACHE_EASY (object_uri);
+  B_COPY_TCACHE_EASY (object_bnode);
+  B_COPY_TCACHE_EASY (object_literal);
+  B_COPY_TCACHE_EASY (context);
+  B_COPY_TCACHE_EASY (datatype);
+  B_COPY_TCACHE_EASY (lang);
+
+  new->tmap_index = src->tmap_index;
+  new->tmap_pointer = src->tmap_pointer;
+
+  *dst = new;
+  return B_OK;
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/btempty.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/btempty.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/btempty.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/btempty.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,132 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+b_error_t
+b_tempty_check (b_t * data, b_tmap_t * tmap, b_uint64 pointer, char remove)
+{
+  b_tempty_t *te;
+  b_uint64 i, value;
+
+  /* Check the number of the empty elements: */
+  for (value = i = 0; i < B_TMAP_NUMB; i++)
+    if (!tmap->map[i])
+      value++;
+
+  if (!value)
+    {
+      if (data->triples_empty && remove)
+	{
+	  b_tempty_t *old;
+	  for (te = data->triples_empty, old = NULL; te; te = te->next)
+	    {
+	      if (te->pointer == pointer)
+		{
+		  if (old)
+		    old->next = te->next;
+		  else
+		    data->triples_empty = te->next;
+
+		  free (te);
+		  data->triples_empty_size--;
+		  break;
+		}
+
+	      old = te;
+	    }
+	}
+
+      return B_OK;
+    }
+  /* Is it already in the list? */
+  for (te = data->triples_empty; te; te = te->next)
+    {
+      if (te->pointer == pointer)
+	break;
+
+      else if (te->pointer > pointer)
+	{
+	  te = NULL;
+	  break;
+	}
+    }
+
+  /* I must create a new elements: */
+  if (!te)
+    {
+      if (data->triples_empty_size >= data->max_cache)
+	return B_OK;
+
+      if (!(te = calloc (1, sizeof (b_tempty_t))))
+	return B_ERROR_MEMORY;
+
+      te->pointer = pointer;
+
+      if (!data->triples_empty)
+	data->triples_empty = te;
+
+      else if (pointer <= data->triples_empty->pointer)
+	{
+	  te->next = data->triples_empty;
+	  data->triples_empty = te;
+	}
+      else
+	{
+	  b_tempty_t *tmp;
+
+	  for (tmp = data->triples_empty; tmp->next; tmp = tmp->next)
+	    {
+	      if (tmp->pointer >= pointer)
+		{
+		  te->next = tmp->next;
+		  tmp->next = te;
+		  break;
+		}
+	    }
+
+	  if (!tmp->next)
+	    tmp->next = te;
+	}
+
+      data->triples_empty_size++;
+    }
+
+  return B_OK;
+}
+
+char
+b_tempty_get (b_t * data, b_tmap_t * tmap, b_uint64 * pointer)
+{
+  if (!data->triples_empty)
+    return 1;
+
+  b_io_seek (data->triples_io, data->triples_empty->pointer, SEEK_SET);
+  if (b_io_read (data->triples_io, tmap, sizeof (b_tmap_t)) <= 0)
+    return 1;
+
+  *pointer = data->triples_empty->pointer;
+
+  return 0;
+}
+
+void
+b_tempty_destroy (b_tempty_t ** list)
+{
+  b_tempty_t *next;
+
+  while (*list)
+    {
+      next = (*list)->next;
+      free (*list);
+      *list = next;
+    }
+}
+
+/* EOF */

Added: incubator/triplesoup/donations/TRIPLES-2-libb/src/btimer.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/src/btimer.c?view=auto&rev=510255
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/src/btimer.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/src/btimer.c Wed Feb 21 13:56:43 2007
@@ -0,0 +1,143 @@
+/* (c) 2007 Satis Superque Merce B.V. All rights reserved.
+ *
+ *   Licensed 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
+ */
+
+#include <b.h>
+
+#ifdef WIN32
+int
+gettimeofday (struct timeval *tv, void *tz)
+{
+  struct _timeb timebuffer;
+
+  _ftime (&timebuffer);
+  tv->tv_sec = (long) timebuffer.time;
+  tv->tv_usec = timebuffer.millitm * 1000;
+
+  return 0;
+}
+#endif
+
+/**
+ * b_timer_new:
+ * @timer: The return pointer to a b_timer_t struct.
+ * @returns: The error code.
+ *
+ * Creates a timer. This function execs the b_timer_start().
+ **/
+b_error_t
+b_timer_new (b_timer_t ** timer)
+{
+  if (!timer)
+    return B_ERROR_DATA;
+
+  if (!(*timer = (b_timer_t *) calloc (1, sizeof (b_timer_t))))
+    return B_ERROR_MEMORY;
+
+  b_timer_start (*timer);
+  return B_OK;
+}
+
+/**
+ * b_timer_destroy:
+ * @timer: The timer you want destroy.
+ * @nanosec: If it != NULL, It will contain the number of nanosec from the
+ * start moment and the stop event. If no stop was execed, the b_timer_stop() 
+ * will be executed.
+ * @returns: The error code.
+ *
+ * Destroies the b_timer_t data struct and returns the nanonsec from the start 
+ * moment.
+ **/
+b_error_t
+b_timer_destroy (b_timer_t * timer, b_uint64 * nanosec)
+{
+  if (!timer)
+    return B_ERROR_DATA;
+
+  if (nanosec)
+    {
+      if (!timer->stopped)
+	b_timer_stop (timer);
+
+      b_timer_diff (timer, nanosec);
+    }
+
+  free (timer);
+  return B_OK;
+}
+
+/**
+ * b_timer_start:
+ * @timer: The b_timer_t data struct.
+ * @returns: The error code.
+ *
+ * starts the timer.
+ **/
+b_error_t
+b_timer_start (b_timer_t * timer)
+{
+  if (!timer)
+    return B_ERROR_DATA;
+
+  timer->stopped = 0;
+  gettimeofday (&timer->start, NULL);
+
+  return B_OK;
+}
+
+/**
+ * b_timer_stop:
+ * @timer: The b_timer_t data struct.
+ * @returns: The error code.
+ *
+ * stops the timer.
+ **/
+b_error_t
+b_timer_stop (b_timer_t * timer)
+{
+  if (!timer)
+    return B_ERROR_DATA;
+
+  if (!timer->stopped)
+    {
+      timer->stopped = 1;
+      gettimeofday (&timer->stop, NULL);
+    }
+
+  return B_OK;
+}
+
+/**
+ * b_timer_diff:
+ * @timer: The b_timer_t data struct.
+ * @nanosec: The nanosec variable'll contain the nanosec from the start moment
+ * and the stop moment. If no b_timer_stop() was executed, the stop moment is
+ * now.
+ * @returns: The error code.
+ *
+ * checks and returns the differences from the start and the stop (or now).
+ **/
+b_error_t
+b_timer_diff (b_timer_t * timer, b_uint64 * nanosec)
+{
+  b_uint64 start, stop;
+  if (!timer || !nanosec)
+    return B_ERROR_DATA;
+
+  if (!timer->stopped)
+    gettimeofday (&timer->stop, NULL);
+
+  start = timer->start.tv_sec * 1000000 + timer->start.tv_usec;
+  stop = timer->stop.tv_sec * 1000000 + timer->stop.tv_usec;
+  *nanosec = stop - start;
+
+  return B_OK;
+}
+
+/* EOF */