/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <b.h>

/**
 * b_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_journaling_new (data, io, B_JOURNALING_WRITE, cache->pointer,
		    sizeof (b_map_t), &map);

  b_io_seek (io, map.parent, SEEK_SET);
  if (b_io_read (io, &parent, sizeof (b_map_t)) <= 0)
    {
      b_journaling_destroy (data);
      return B_ERROR_INTERNAL;
    }

  b_journaling_new (data, io, B_JOURNALING_WRITE, map.parent,
		    sizeof (b_map_t), &parent);

  /* 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)
	    {
	      b_journaling_destroy (data);
	      return B_ERROR_INTERNAL;
	    }

	  b_journaling_new (data, io, B_JOURNALING_WRITE, map.right,
			    sizeof (b_map_t), &other);

	  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)
	    {
	      b_journaling_destroy (data);
	      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)
	    {
	      b_journaling_destroy (data);
	      return B_ERROR_INTERNAL;
	    }

	  b_journaling_new (data, io, B_JOURNALING_WRITE, map.left,
			    sizeof (b_map_t), &other);

	  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)
	    {
	      b_journaling_destroy (data);
	      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)
	{
	  b_journaling_destroy (data);
	  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)
	    {
	      b_journaling_destroy (data);
	      return B_ERROR_INTERNAL;
	    }
	}

      b_journaling_new (data, io, B_JOURNALING_WRITE, pointer,
			sizeof (b_map_t), &other);

      /* 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)
	    {
	      b_journaling_destroy (data);
	      return B_ERROR_INTERNAL;
	    }

	  b_journaling_new (data, io, B_JOURNALING_WRITE, other.parent,
			    sizeof (b_map_t), &other_p);

	  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)
	    {
	      b_journaling_destroy (data);
	      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)
		{
		  b_journaling_destroy (data);
		  return B_ERROR_INTERNAL;
		}

	      b_journaling_new (data, io, B_JOURNALING_WRITE,
				other.right, sizeof (b_map_t), &other_r);

	      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)
		{
		  b_journaling_destroy (data);
		  return B_ERROR_INTERNAL;
		}
	    }

	  b_io_seek (io, map.right, SEEK_SET);
	  if (b_io_read (io, &other_r, sizeof (b_map_t)) <= 0)
	    {
	      b_journaling_destroy (data);
	      return B_ERROR_INTERNAL;
	    }

	  b_journaling_new (data, io, B_JOURNALING_WRITE, map.right,
			    sizeof (b_map_t), &other_r);

	  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)
	    {
	      b_journaling_destroy (data);
	      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)
	{
	  b_journaling_destroy (data);
	  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)
	{
	  b_journaling_destroy (data);
	  return B_ERROR_INTERNAL;
	}

      b_journaling_new (data, io, B_JOURNALING_WRITE, map.left,
			sizeof (b_map_t), &other);

      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)
	{
	  b_journaling_destroy (data);
	  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)
    {
      b_journaling_destroy (data);
      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)
    {
      b_journaling_destroy (data);
      return B_ERROR_INTERNAL;
    }

  b_journaling_destroy (data);
  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;

      b_journaling_new (data, data->triples_io, B_JOURNALING_WRITE,
			cache->tmap_pointer, sizeof (b_tmap_t), &tmap);

      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 (b_tmap_t), data->max_cache) <= 0)
	{
	  b_journaling_destroy (data);
	  return B_ERROR_POSIX;
	}

      b_journaling_destroy (data);

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