Java Code Examples for com.sleepycat.je.LockMode

The following examples show how to use com.sleepycat.je.LockMode. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: qpid-broker-j   Author: apache   File: AbstractBDBMessageStore.java    License: Apache License 2.0 6 votes vote down vote up
private void visitMessagesInternal(MessageHandler handler, EnvironmentFacade environmentFacade)
{
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    MessageMetaDataBinding valueBinding = MessageMetaDataBinding.getInstance();

    try(Cursor cursor = getMessageMetaDataDb().openCursor(null, null))
    {
        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS)
        {
            long messageId = LongBinding.entryToLong(key);
            StorableMessageMetaData metaData = valueBinding.entryToObject(value);
            StoredBDBMessage message = createStoredBDBMessage(messageId, metaData, true);
            if (!handler.handle(message))
            {
                break;
            }
        }
    }
    catch (RuntimeException e)
    {
        throw environmentFacade.handleDatabaseException("Cannot visit messages", e);
    }
}
 
Example #2
Source Project: qpid-broker-j   Author: apache   File: AbstractBDBMessageStore.java    License: Apache License 2.0 6 votes vote down vote up
private StoredBDBMessage<?> getMessageInternal(long messageId, EnvironmentFacade environmentFacade)
{
    try
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        MessageMetaDataBinding valueBinding = MessageMetaDataBinding.getInstance();
        LongBinding.longToEntry(messageId, key);
        if(getMessageMetaDataDb().get(null, key, value, LockMode.READ_COMMITTED) == OperationStatus.SUCCESS)
        {
            StorableMessageMetaData metaData = valueBinding.entryToObject(value);
            StoredBDBMessage message = createStoredBDBMessage(messageId, metaData, true);
            return message;
        }
        else
        {
            return null;
        }

    }
    catch (RuntimeException e)
    {
        throw environmentFacade.handleDatabaseException("Cannot visit messages", e);
    }
}
 
Example #3
Source Project: qpid-broker-j   Author: apache   File: UpgradeFrom7To8.java    License: Apache License 2.0 6 votes vote down vote up
private int getConfigVersion(Database configVersionDb)
{
    Cursor cursor = null;
    try
    {
        cursor = configVersionDb.openCursor(null, null);
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        while (cursor.getNext(key, value, LockMode.RMW) == OperationStatus.SUCCESS)
        {
            return IntegerBinding.entryToInt(value);
        }
        return -1;
    }
    finally
    {
        if (cursor != null)
        {
            cursor.close();
        }
    }
}
 
Example #4
Source Project: qpid-broker-j   Author: apache   File: CursorTemplate.java    License: Apache License 2.0 6 votes vote down vote up
public void processEntries()
{
    _cursor = _database.openCursor(_transaction, CursorConfig.READ_COMMITTED);
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();

    try
    {
        _iterating = true;
        while (_iterating && _cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
        {
            _databaseEntryCallback.processEntry(_database, _transaction, key, value);
        }
    }
    finally
    {
        _cursor.close();
    }
}
 
Example #5
Source Project: qpid-broker-j   Author: apache   File: AbstractBDBPreferenceStore.java    License: Apache License 2.0 6 votes vote down vote up
private Collection<PreferenceRecord> getPreferenceRecords(final EnvironmentFacade environmentFacade)
{
    Collection<PreferenceRecord> records = new LinkedHashSet<>();

    try(Cursor cursor = getPreferencesDb().openCursor(null, null))
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        UUIDTupleBinding keyBinding = UUIDTupleBinding.getInstance();
        MapBinding valueBinding = MapBinding.getInstance();

        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS)
        {
            UUID preferenceId = keyBinding.entryToObject(key);
            Map<String, Object> preferenceAttributes = valueBinding.entryToObject(value);
            PreferenceRecord record = new PreferenceRecordImpl(preferenceId, preferenceAttributes);
            records.add(record);
        }
    }
    catch (RuntimeException e)
    {
        throw environmentFacade.handleDatabaseException("Cannot visit preferences", e);
    }
    return records;
}
 
Example #6
Source Project: qpid-broker-j   Author: apache   File: UpgradeFrom5To6Test.java    License: Apache License 2.0 6 votes vote down vote up
private void assertXidEntries(Environment environment)
{
    final DatabaseEntry value = new DatabaseEntry();
    final DatabaseEntry key = getXidKey();
    new DatabaseTemplate(environment, NEW_XID_DB_NAME, null).run(new DatabaseRunnable()
    {

        @Override
        public void run(Database xidDatabase, Database nullDatabase, Transaction transaction)
        {
            xidDatabase.get(null, key, value, LockMode.DEFAULT);
        }
    });
    NewPreparedTransactionBinding newBinding = new NewPreparedTransactionBinding();
    NewPreparedTransaction newTransaction = newBinding.entryToObject(value);
    NewRecordImpl[] newEnqueues = newTransaction.getEnqueues();
    NewRecordImpl[] newDequeues = newTransaction.getDequeues();
    assertEquals("Unxpected new enqueus number", 1, newEnqueues.length);
    NewRecordImpl enqueue = newEnqueues[0];
    assertEquals("Unxpected queue id", UUIDGenerator.generateQueueUUID("TEST1", getVirtualHost().getName()), enqueue.getId());
    assertEquals("Unxpected message id", 1, enqueue.getMessageNumber());
    assertEquals("Unxpected new dequeues number", 1, newDequeues.length);
    NewRecordImpl dequeue = newDequeues[0];
    assertEquals("Unxpected queue id", UUIDGenerator.generateQueueUUID("TEST2", getVirtualHost().getName()), dequeue.getId());
    assertEquals("Unxpected message id", 2, dequeue.getMessageNumber());
}
 
Example #7
Source Project: SPADE   Author: ashish-gehani   File: BerkeleyDB.java    License: GNU General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
public void readData(int limit)
{
    Cursor cursor = scaffoldDatabase.openCursor(null, null);
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry data = new DatabaseEntry();
    int i = 0;
    while(cursor.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS)
    {
        if(i >= limit)
            break;
        String keyString = new String(key.getData());
        System.out.println("hash: " + keyString);
        i++;
    }
    cursor.close();
}
 
Example #8
Source Project: bboxdb   Author: jnidzwetzki   File: BDBTupleStore.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Tuple readTuple(final String key) throws IOException {
	final DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes());
    final DatabaseEntry value = new DatabaseEntry();
    
	Transaction txn = null;

	if(USE_TRANSACTIONS) {
		txn = environment.beginTransaction(null, null);
	}
	
    final OperationStatus result = database.get(null, keyEntry, value, LockMode.DEFAULT);
    
    if (result != OperationStatus.SUCCESS) {
        throw new RuntimeException("Data fetch got status " + result + " for " + key);
    }
    
       if(txn != null) {
       	txn.commit();
       }
       
       final ByteBuffer byteBuffer = ByteBuffer.wrap(value.getData());
       
       return TupleHelper.decodeTuple(byteBuffer);
}
 
Example #9
Source Project: bboxdb   Author: jnidzwetzki   File: FileLineIndex.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get the content of the line
 * @param lineNumber
 * @return
 */
public long locateLine(final long line) {

	if(database == null) {
		throw new IllegalArgumentException("No database is open, please index file first");
	}

	if(line >= indexedLines) {
		throw new IllegalArgumentException("Line " + line + " is higher then indexedLines: " + (indexedLines - 1));
	}

	final DatabaseEntry key = buildDatabaseEntry(line);
    final DatabaseEntry value = new DatabaseEntry();

    final OperationStatus result = database.get(null, key, value, LockMode.DEFAULT);

    if (result != OperationStatus.SUCCESS) {
        throw new RuntimeException("Data fetch got status " + result + " for " + line);
    }

    final long bytePos = DataEncoderHelper.readLongFromByte(value.getData());

    return bytePos;
}
 
Example #10
Source Project: tddl5   Author: loye168   File: JE_Table.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public CloneableRecord get(ExecutionContext context, CloneableRecord key, IndexMeta indexMeta, String dbName) {

    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    keyEntry.setData(indexCodecMap.get(indexMeta.getName()).getKey_codec().encode(key));
    OperationStatus status = getDatabase(dbName).get(context.getTransaction() == null ? null : ((JE_Transaction) context.getTransaction()).txn,
        keyEntry,
        valueEntry,
        LockMode.DEFAULT);
    if (OperationStatus.SUCCESS != status) {
        return null;
    }
    if (valueEntry.getSize() != 0) {
        return indexCodecMap.get(indexMeta.getName()).getValue_codec().decode(valueEntry.getData());
    } else {
        return null;
    }
}
 
Example #11
Source Project: hypergraphdb   Author: hypergraphdb   File: DefaultIndexImpl.java    License: Apache License 2.0 6 votes vote down vote up
public ValueType getData(KeyType key)
{
	checkOpen();
	DatabaseEntry keyEntry = new DatabaseEntry(keyConverter.toByteArray(key));
	DatabaseEntry value = new DatabaseEntry();
	ValueType result = null;

	try
	{
		OperationStatus status = db.get(txn().getBJETransaction(), keyEntry, value, LockMode.DEFAULT);
		if (status == OperationStatus.SUCCESS)
			result = valueConverter.fromByteArray(value.getData(), value.getOffset(), value.getSize());
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to lookup index '" + name + "': " + ex.toString(), ex);
	}
	return result;
}
 
Example #12
Source Project: timbuctoo   Author: HuygensING   File: BdbDumpTask.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void execute(ImmutableMultimap<String, String> parameters, PrintWriter output) throws Exception {
  if (!parameters.keySet().containsAll(REQUIRED_PARAMS)) {
    output.write("Make sure you provide the following parameters: " + REQUIRED_PARAMS);
    return;
  }

  BdbWrapper<String, String> database = environmentCreator.getDatabase(
    getParam(parameters, OWNER),
    getParam(parameters, DATA_SET),
    getParam(parameters, DATABASE),
    true,
    STRING_BINDER,
    STRING_BINDER,
    new StringStringIsCleanHandler()
  );

  String prefix = parameters.containsKey("prefix") ? getParam(parameters, "prefix") : "";
  int start = parameters.containsKey("start") ? Integer.parseInt(getParam(parameters, "start")) : 0;
  int count = parameters.containsKey("count") ? Integer.parseInt(getParam(parameters, "count")) : 10;
  output.write("uncommitted data: " + database.dump(prefix, start, count, LockMode.READ_UNCOMMITTED));
  output.write("committed data: " + database.dump(prefix, start, count, LockMode.READ_COMMITTED));
}
 
Example #13
Source Project: hypergraphdb   Author: hypergraphdb   File: IndexResultSet.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * <p>
 * Construct a result set matching a specific key.
 * </p>
 * 
 * @param cursor
 * @param key
 */
public IndexResultSet(BJETxCursor cursor, DatabaseEntry keyIn, ByteArrayConverter<T> converter) {
	/*
	 * id = idcounter++; System.out.println("Constructing index set with id " + id); StackTraceElement
	 * e[]=Thread.currentThread().getStackTrace(); for (int i=0; i <e.length; i++) { System.out.println(e[i]);
	 * }
	 */
	this.converter = converter;
	this.cursor = cursor;
	this.key = new DatabaseEntry();
	
	if (keyIn != null) {
		assignData(this.key, keyIn.getData());
	}
	
	try {
		cursor.cursor().getCurrent(key, data, LockMode.DEFAULT);
		next = converter.fromByteArray(data.getData(), data.getOffset(), data.getSize());
		lookahead = 1;
	}
	catch (Throwable t) {
		throw new HGException(t);
	}
}
 
Example #14
Source Project: hypergraphdb   Author: hypergraphdb   File: IndexResultSet.java    License: Apache License 2.0 6 votes vote down vote up
public void goBeforeFirst() {
	try {
		if (cursor.cursor().getFirst(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
			current = UNKNOWN;
			prev = null;
			next = converter.fromByteArray(data.getData(), data.getOffset(), data.getSize());
			lookahead = 1;
		}
		else {
			prev = next = null;
			current = UNKNOWN;
			lookahead = 0;
		}
	}
	catch (Throwable t) {
		closeNoException();
		throw new HGException(t);
	}
}
 
Example #15
Source Project: hypergraphdb   Author: hypergraphdb   File: IndexResultSet.java    License: Apache License 2.0 6 votes vote down vote up
public void goAfterLast() {
	try {
		if (cursor.cursor().getLast(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
			current = UNKNOWN;
			next = null;
			prev = converter.fromByteArray(data.getData(), data.getOffset(), data.getSize());
			lookahead = -1;
		}
		else {
			prev = next = null;
			current = UNKNOWN;
			lookahead = 0;
		}
	}
	catch (Throwable t) {
		closeNoException();
		throw new HGException(t);
	}
}
 
Example #16
Source Project: timbuctoo   Author: HuygensING   File: BdbWrapper.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void replace(KeyT key, ValueT initialValue, Function<ValueT, ValueT> replacer) throws DatabaseWriteException {
  synchronized (keyEntry) {
    try (Cursor cursor = database.openCursor(transaction, CursorConfig.DEFAULT)) {
      keyBinder.objectToEntry(key, keyEntry);
      OperationStatus searchResult = cursor.getSearchKey(keyEntry, valueEntry, LockMode.DEFAULT);
      ValueT newValue = initialValue;
      if (searchResult.equals(OperationStatus.SUCCESS)) {
        newValue = replacer.apply(valueBinder.entryToObject(valueEntry));
      }
      valueBinder.objectToEntry(newValue, valueEntry);
      cursor.putCurrent(valueEntry);
    } catch (Exception e) {
      throw new DatabaseWriteException(e);
    }
  }
}
 
Example #17
Source Project: hypergraphdb   Author: hypergraphdb   File: SingleValueResultSet.java    License: Apache License 2.0 6 votes vote down vote up
public SingleValueResultSet(BJETxCursor cursor, DatabaseEntry keyIn, ByteArrayConverter<T> converter) {
	//
	// The following is bit hacky because we want to avoid some of the default behavior
	// of the super constructor, which is incorrect when the "values" we are interested in 
	// are the DB's primary keys. So we duplicate its bebavior and override instantiation
	// of the current value.
	this.converter = converter;
	this.cursor = cursor;
	this.key = new DatabaseEntry();
	if (keyIn != null) {
		assignData(key, keyIn.getData());
	}
	
	try {
		((SecondaryCursor)cursor.cursor()).getCurrent(key, pkey, data, LockMode.DEFAULT);
		next = converter.fromByteArray(pkey.getData(), pkey.getOffset(), pkey.getSize());
		lookahead = 1;
	}
	catch (Throwable t) {
		throw new HGException(t);
	}

}
 
Example #18
Source Project: multimedia-indexing   Author: MKLab-ITI   File: IVFPQ.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This is a utility method that can be used to dump the contents of the iidToIvfpqDB to a txt file.<br>
 * Currently, only the list id of each item is dumped.
 * 
 * @param dumpFilename
 *            Full path to the file where the dump will be written.
 * @throws Exception
 */
public void dumpIidToIvfpqDB(String dumpFilename) throws Exception {
	DatabaseEntry foundKey = new DatabaseEntry();
	DatabaseEntry foundData = new DatabaseEntry();

	ForwardCursor cursor = iidToIvfpqDB.openCursor(null, null);
	BufferedWriter out = new BufferedWriter(new FileWriter(new File(dumpFilename)));
	while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
		int iid = IntegerBinding.entryToInt(foundKey);
		TupleInput input = TupleBinding.entryToInput(foundData);
		int listId = input.readInt();
		out.write(iid + " " + listId + "\n");
	}
	cursor.close();
	out.close();
}
 
Example #19
Source Project: timbuctoo   Author: HuygensING   File: BdbWrapper.java    License: GNU General Public License v3.0 6 votes vote down vote up
public boolean delete(KeyT key, ValueT value) throws DatabaseWriteException {
  boolean wasChange = false;
  synchronized (keyEntry) {
    try (Cursor cursor = database.openCursor(transaction, CursorConfig.DEFAULT)) {
      keyBinder.objectToEntry(key, keyEntry);
      valueBinder.objectToEntry(value, valueEntry);
      OperationStatus searchResult = cursor.getSearchBoth(keyEntry, valueEntry, LockMode.DEFAULT);
      if (searchResult.equals(OperationStatus.SUCCESS)) {
        wasChange = cursor.delete() == OperationStatus.SUCCESS;
      }
    } catch (Exception e) {
      throw new DatabaseWriteException(e);
    }
  }
  return wasChange;
}
 
Example #20
Source Project: hypergraphdb   Author: hypergraphdb   File: KeyScanResultSet.java    License: Apache License 2.0 6 votes vote down vote up
public KeyScanResultSet(BJETxCursor cursor, DatabaseEntry keyIn, ByteArrayConverter<T> converter) {
	this.converter = converter;
	this.cursor = cursor;
	this.key = new DatabaseEntry();
	
	if (keyIn != null) {
		assignData(key, keyIn.getData());
	}
	
	try {
		cursor.cursor().getCurrent(key, data, LockMode.DEFAULT);
		next = converter.fromByteArray(key.getData(), key.getOffset(), key.getSize());
		lookahead = 1;
	}
	catch (Throwable t) {
		throw new HGException(t);
	}
}
 
Example #21
Source Project: hypergraphdb   Author: hypergraphdb   File: DefaultBiIndexImpl.java    License: Apache License 2.0 6 votes vote down vote up
public long countKeys(ValueType value) {
	DatabaseEntry keyEntry = new DatabaseEntry(valueConverter.toByteArray(value));
	DatabaseEntry valueEntry = new DatabaseEntry();
	SecondaryCursor cursor = null;
	
	try {
		cursor = secondaryDb.openCursor(txn().getBJETransaction(), cursorConfig);
		OperationStatus status = cursor.getSearchKey(keyEntry, valueEntry, dummy, LockMode.DEFAULT);
		if (status == OperationStatus.SUCCESS)
			return cursor.count();
		else
			return 0;
	}
	catch (DatabaseException ex) {
		throw new HGException(ex);
	}
	finally {
		if (cursor != null) {
			try {
				cursor.close();
			}
			catch (Throwable t) {
			}
		}
	}
}
 
Example #22
Source Project: hypergraphdb   Author: hypergraphdb   File: BJEStorageImplementation.java    License: Apache License 2.0 6 votes vote down vote up
public boolean containsLink(HGPersistentHandle handle)
{
	DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
	DatabaseEntry value = new DatabaseEntry();
	value.setPartial(0, 0, true);
	try
	{
		if (data_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
		{
			// System.out.println(value.toString());
			return true;
		}
	}
	catch (DatabaseException ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle + ": " + ex.toString(), ex);
	}

	return false;
}
 
Example #23
Source Project: hypergraphdb   Author: hypergraphdb   File: BJEStorageImplementation.java    License: Apache License 2.0 6 votes vote down vote up
public boolean containsData(HGPersistentHandle handle)
{
	DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
	DatabaseEntry value = new DatabaseEntry();
	value.setPartial(0, 0, true);
	try
	{
		if (primitive_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
		{
			return true;
		}
	}
	catch (DatabaseException ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle + ": " + ex.toString(), ex);
	}
	return false;
}
 
Example #24
Source Project: hypergraphdb   Author: hypergraphdb   File: BJEStorageImplementation.java    License: Apache License 2.0 6 votes vote down vote up
public byte[] getData(HGPersistentHandle handle)
{
	try
	{
		DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
		DatabaseEntry value = new DatabaseEntry();
		if (primitive_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
			return value.getData();
		else
			return null;
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle, ex);
	}
}
 
Example #25
Source Project: hypergraphdb   Author: hypergraphdb   File: BJEStorageImplementation.java    License: Apache License 2.0 6 votes vote down vote up
public HGPersistentHandle[] getLink(HGPersistentHandle handle)
{
	try
	{
		DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
		DatabaseEntry value = new DatabaseEntry();
		if (data_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
			return (HGPersistentHandle[]) linkBinding.entryToObject(value);
		else
			return null;
	}
	catch (Exception ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle, ex);
	}
}
 
Example #26
Source Project: hypergraphdb   Author: hypergraphdb   File: BerkeleyDBStore.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public V load(K key) {
    try {
        DatabaseEntry keyEntry = objectToEntry(key);
        DatabaseEntry valueEntry = new DatabaseEntry();
        OperationStatus status = _db.get(null, keyEntry, valueEntry, LockMode.DEFAULT);
        if (status == OperationStatus.SUCCESS) {
            return (V) entryToObject(valueEntry);
        } else {
            return null;
        }
    } catch (Exception e) {
        _logger.log(Level.SEVERE, e.getMessage(), e);
        return null;
    }
}
 
Example #27
Source Project: tddl   Author: cpsing   File: JE_Table.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public CloneableRecord get(ExecutionContext context, CloneableRecord key, IndexMeta indexMeta, String dbName) {

    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    keyEntry.setData(indexCodecMap.get(indexMeta.getName()).getKey_codec().encode(key));
    OperationStatus status = getDatabase(dbName).get(context.getTransaction() == null ? null : ((JE_Transaction) context.getTransaction()).txn,
        keyEntry,
        valueEntry,
        LockMode.DEFAULT);
    if (OperationStatus.SUCCESS != status) {
        return null;
    }
    if (valueEntry.getSize() != 0) {
        return indexCodecMap.get(indexMeta.getName()).getValue_codec().decode(valueEntry.getData());
    } else {
        return null;
    }
}
 
Example #28
Source Project: Rel   Author: DaveVoorhis   File: TempTableDisk.java    License: Apache License 2.0 6 votes vote down vote up
public boolean hasNext() {
	if (current != null)
		return true;
	try {
		if (cursor == null)
			cursor = storage.openCursor(null, null);
	    DatabaseEntry foundKey = new DatabaseEntry();
	    DatabaseEntry foundData = new DatabaseEntry();	
		if (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
			current = (ValueTuple)database.getTupleBinding().entryToObject(foundKey); 
			return true;
		}
	} catch (DatabaseException exp) {
		throw new ExceptionFatal("RS0321: Unable to get next tuple: " + exp.getMessage());					
	}
	return false;
}
 
Example #29
Source Project: Rel   Author: DaveVoorhis   File: TempIndexDisk.java    License: Apache License 2.0 6 votes vote down vote up
public void put(ValueTuple keyTuple, ValueTuple valueTuple) throws DatabaseException {
	DatabaseEntry theKey = new DatabaseEntry();
	database.getTupleBinding().objectToEntry(keyTuple, theKey);

	DatabaseEntry theID = new DatabaseEntry();
	
	OperationStatus status = keys.get(null, theKey, theID, LockMode.DEFAULT);
	if (status == OperationStatus.NOTFOUND) {
		LongBinding.longToEntry(id++, theID);
		keys.put(null, theKey, theID);
	} else if (status != OperationStatus.SUCCESS)
		throw new ExceptionFatal("RS0318: Insertion failure in put().");
	
	DatabaseEntry theData = new DatabaseEntry();
	database.getTupleBinding().objectToEntry(valueTuple, theData);
	
	values.put(null, theID, theData);
}
 
Example #30
Source Project: WebCollector   Author: CrawlScript   File: BerkeleyDBManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void list() throws Exception {
        if (env == null) {
            open();
        }
        Cursor cursor = null;
        Database crawldbDatabase = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
        cursor = crawldbDatabase.openCursor(null, CursorConfig.DEFAULT);
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();

        while (cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
            CrawlDatum datum = BerkeleyDBUtils.createCrawlDatum(key, value);
            System.out.println(CrawlDatumFormater.datumToString(datum));
//            try {
//                CrawlDatum datum = BerkeleyDBUtils.createCrawlDatum(key, value);
//                System.out.println(CrawlDatumFormater.datumToString(datum));
//            } catch (Exception ex) {
//                LOG.info("Exception when generating", ex);
//                continue;
//            }
        }

    }