/******************************************************************************
  @header BDBCursor
 
  @availability OS X, GNUstep
  @copyright (C) 2004, 2005, 2006 Oliver Langer
 
  Author: Oliver Langer

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  28.12.2004 ola     initial version
  22.08.2006 ola     license changed
  -------------------------------------------------------------------------
  </pre>
******************************************************************************/
#include <Encore/ECExceptions.h>
#include <BDBErrorHandling.h>
#include <BDB/BDBCursor.h>


@implementation BDBCursor

- initWithCursor: (DBC *) aCursor config: (BDBCursorConfig *) aConfig {
  self = [super init];
  self->cursor = aCursor;
  self->config = [config retain];
  
  return self;
}


- (void) dealloc {
  if( NULL != self->cursor ) {
    cursor->c_close( cursor );
	cursor = NULL;
	if( nil != config ) {
	  [ config release ];
	  config = nil;
	}
  }
  
  [super dealloc];
}


- (BDBOperationStatus) entryWithKey: (BDBDatabaseEntryData *) aKey
  value: (BDBDatabaseEntryData *) aValue flags: (u_int32_t) flags {

  NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
  NSData *valueData;
  DBT dbKey, dbValue;
  int dbResult = BDB_STATUS_UNKNOWN;
  
  if( nil == aKey || nil == aValue ) {
    [[[ECIllegalArgumentException alloc ] 
      initWithArgumentInfo: @"key or data equals nil"] raise];
  }
  
  memset( &dbKey, 0, sizeof( dbKey ) );
  memset( &dbValue, 0, sizeof( dbValue ) );

  dbKey.data = (void *) [ [ aKey data] bytes ];
  dbKey.size = [ [ aKey data ] length ];

  dbValue.flags = DB_DBT_MALLOC;
  
  dbResult = cursor->c_get(cursor, &dbKey, &dbValue, flags );

  BDB_ERROR_CODE_EVAL_BEGIN(dbResult);
    BDB_ERROR_CODE_EVAL_ON_THROW_BEGIN
	  [ pool release ];
	  if( NULL != dbValue.data ) {
        free( dbValue.data );
      }
	BDB_ERROR_CODE_EVAL_ON_THROW_END
  BDB_ERROR_CODE_EVAL_END;

  if( BDB_STATUS_SUCCESS == dbResult ) {
    /* at present we do not support user buffers, so :*/
    valueData = [ [ [ NSData alloc ] initWithBytes:dbValue.data 
      length:dbValue.size ]  autorelease ] ;
	[ aValue setData: valueData ];
  }
  
  if( NULL != dbValue.data ) {
    free( dbValue.data );
  }
  
  [ pool release ];

  return dbResult;
}


- (BDBOperationStatus) firstWithKey: (BDBDatabaseEntryData *) aKey
  value: (BDBDatabaseEntryData *) aValue lockMode: (BDBLockMode *) aLockMode {
  return [self entryWithKey: aKey value:aValue 
    flags:[aLockMode flags] | DB_FIRST ];
}


- (BDBOperationStatus) lastWithKey: (BDBDatabaseEntryData *) aKey
  value: (BDBDatabaseEntryData *) aValue lockMode: (BDBLockMode *) aLockMode {
  return [self entryWithKey: aKey value:aValue 
    flags:[aLockMode flags] | DB_LAST ];
}


- (BDBOperationStatus) nextWithKey: (BDBDatabaseEntryData *) aKey
  value: (BDBDatabaseEntryData *) aValue lockMode: (BDBLockMode *) aLockMode {
  return [self entryWithKey: aKey value:aValue
    flags:[aLockMode flags] | DB_NEXT ];
}


- (BDBOperationStatus) previousWithKey: (BDBDatabaseEntryData *) aKey
  value: (BDBDatabaseEntryData *) aValue lockMode: (BDBLockMode *) aLockMode {
  return [self entryWithKey: aKey value:aValue
    flags:[aLockMode flags] | DB_PREV ];
}
@end
