/* 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>
#include <stdarg.h>

static char b_journaling_io (b_t * data, b_io_t * io);
static b_error_t b_journaling_recover_cache_empty (b_t * data, b_io_t * io,
						   b_uint64 * done);
static b_error_t b_journaling_recover_end_write (b_t * data, b_io_t * io,
						 b_uint64 * done);
static b_error_t b_journaling_recover_write (b_t * data, b_io_t * io,
					     b_uint64 * done);

/* Journaling procedure: */
void
b_journaling_new (b_t * data, b_io_t * io, b_journaling_type_t type, ...)
{
  va_list va;
  b_uint64 pointer;
  size_t size;
  unsigned char *what;
  char freed;
  char cio;
  b_string_t *str;

  /* Witch filedescriptor? */
  cio = b_journaling_io (data, io);

  va_start (va, type);

  /* Witch operation? */
  switch (type)
    {
    case B_JOURNALING_CACHE_EMPTY:
      /* No argument */

      if (b_string_new (&str) != B_OK)
	return;

      b_string_add (str, (unsigned char *) &cio, sizeof (cio));
      b_string_add (str, (unsigned char *) &type, sizeof (type));

      b_io_write (data->journaling_io, str->string, str->size,
		  data->max_cache);
      b_string_destroy (str, NULL);
      break;

    case B_JOURNALING_END_WRITE:
      /* 1th argument: the pointer */
      if (!(pointer = va_arg (va, b_uint64)))
	return;

      if (b_string_new (&str) != B_OK)
	return;

      b_string_add (str, (unsigned char *) &cio, sizeof (cio));
      b_string_add (str, (unsigned char *) &type, sizeof (type));
      b_string_add (str, (unsigned char *) &pointer, sizeof (pointer));

      b_io_write (data->journaling_io, str->string, str->size,
		  data->max_cache);
      b_string_destroy (str, NULL);
      break;

    case B_JOURNALING_WRITE:
      /* 1th argument: the pointer */
      if (!(pointer = va_arg (va, b_uint64)))
	return;

      /* 2nd argument: the size */
      if (!(size = va_arg (va, size_t)))
	return;

      /* 3rd argument: what */
      if (!(what = va_arg (va, void *)))
	{
	  freed = 1;

	  if (!(what = malloc (size + 1)))
	    return;

	  if (b_io_read (io, what, size) <= 0)
	    return;
	}
      else
	freed = 0;

      if (b_string_new (&str) != B_OK)
	{
	  if (freed)
	    free (what);
	  return;
	}

      b_string_add (str, (unsigned char *) &cio, sizeof (cio));
      b_string_add (str, (unsigned char *) &type, sizeof (type));
      b_string_add (str, (unsigned char *) &pointer, sizeof (pointer));
      b_string_add (str, (unsigned char *) &size, sizeof (size));
      b_string_add (str, (unsigned char *) what, size);

      b_io_write (data->journaling_io, str->string, str->size,
		  data->max_cache);
      b_string_destroy (str, NULL);

      if (freed)
	free (what);
      break;
    }

  va_end (va);
}

/* The destroing procedure removes the file: */
void
b_journaling_destroy (b_t * data)
{
  data->journaling_io->end_pointer = sizeof (b_uint64);
  memcpy (data->journaling_io->data, &data->journaling_io->end_pointer,
	  sizeof (data->journaling_io->end_pointer));
}

/* Witch io element? */
static char
b_journaling_io (b_t * data, b_io_t * io)
{
  if (data->uris_io == io)
    return B_TYPE_URI;

  if (data->properties_io == io)
    return B_TYPE_PROPERTY;

  if (data->literals_io == io)
    return B_TYPE_LITERAL;

  if (data->bnodes_io == io)
    return B_TYPE_BNODE;

  if (data->contexts_io == io)
    return B_TYPE_CONTEXT;

  if (data->datatypes_io == io)
    return B_TYPE_DATATYPE;

  return -1;
}

/* Recovering procedure: */
b_error_t
b_journaling_recover (b_t * data)
{
  b_uint64 done = 0;
  char b;
  b_io_t *io;

  /* Until the file exists: */
  while (b_io_read (data->journaling_io, &b, sizeof (char)) <= 0)
    {
      /* The first byte means the io element: */
      switch (b)
	{
	case B_TYPE_URI:
	  io = data->uris_io;
	  break;

	case B_TYPE_PROPERTY:
	  io = data->properties_io;
	  break;

	case B_TYPE_LITERAL:
	  io = data->literals_io;
	  break;

	case B_TYPE_BNODE:
	  io = data->bnodes_io;
	  break;

	case B_TYPE_CONTEXT:
	  io = data->contexts_io;
	  break;

	case B_TYPE_DATATYPE:
	  io = data->datatypes_io;
	  break;

	case -1:
	  io = data->triples_io;
	  break;

	default:
	  /* Error in the file: */
	  b_journaling_destroy (data);
	  return B_OK;
	}

      /* The second byte means the operation: */
      if (b_io_read (data->journaling_io, &b, sizeof (char)) <= 0)
	{
	  b_journaling_destroy (data);
	  return B_OK;
	}

      switch (b)
	{
	case B_JOURNALING_CACHE_EMPTY:
	  if (b_journaling_recover_cache_empty (data, io, &done) != B_OK)
	    {
	      b_journaling_destroy (data);
	      return B_OK;
	    }

	  break;

	case B_JOURNALING_END_WRITE:
	  if (b_journaling_recover_end_write (data, io, &done) != B_OK)
	    {
	      b_journaling_destroy (data);
	      return B_OK;
	    }

	  break;

	case B_JOURNALING_WRITE:
	  if (b_journaling_recover_write (data, io, &done) != B_OK)
	    {
	      b_journaling_destroy (data);
	      return B_OK;
	    }

	  break;

	default:
	  /* Error in the file: */
	  b_journaling_destroy (data);
	  return B_OK;
	}
    }

  b_journaling_destroy (data);
  return B_OK;
}

/* If the crashed procedure was creating the empty element of a b-tree, I can
 * reset the end_pointer: */
static b_error_t
b_journaling_recover_cache_empty (b_t * data, b_io_t * io, b_uint64 * done)
{
  io->end_pointer = sizeof (b_uint64);
  memcpy (io->data, &io->end_pointer, sizeof (io->end_pointer));
  return B_OK;
}

/* If the crashed procedure was writting something in the end of the file, I
 * reset the end_pointer to the previous value: */
static b_error_t
b_journaling_recover_end_write (b_t * data, b_io_t * io, b_uint64 * done)
{
  b_uint64 pointer;

  if (b_io_read (data->journaling_io, &pointer, sizeof (pointer)) <= 0)
    return B_ERROR_POSIX;

  io->end_pointer = pointer;
  memcpy (io->data, &io->end_pointer, sizeof (io->end_pointer));

  (*done) += sizeof (pointer);
  return B_OK;
}

/* if the crashed procedure was writting in the middle of the file, I must
 * write the previous value: */
static b_error_t
b_journaling_recover_write (b_t * data, b_io_t * io, b_uint64 * done)
{
  b_uint64 pointer;
  size_t size;
  unsigned char *what;

  if (b_io_read (data->journaling_io, &pointer, sizeof (pointer)) <= 0)
    return B_ERROR_POSIX;

  if (b_io_read (data->journaling_io, &size, sizeof (size)) <= 0)
    return B_ERROR_POSIX;

  if (!(what = malloc (size)))
    return B_ERROR_POSIX;

  if (b_io_read (data->journaling_io, what, size) <= 0)
    {
      free (what);
      return B_ERROR_POSIX;
    }

  b_io_seek (io, pointer, SEEK_SET);
  b_io_write (io, what, size, data->max_cache);
  free (what);

  (*done) += size + sizeof (size) + sizeof (pointer);
  return B_OK;
}

/* EOF */
