PersistKeyAssigner.java [plain text]
package com.sleepycat.persist.impl;
import com.sleepycat.bind.tuple.TupleBase;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Sequence;
public class PersistKeyAssigner {
private Catalog catalog;
private Format keyFieldFormat;
private Format entityFormat;
private boolean rawAccess;
private Sequence sequence;
PersistKeyAssigner(PersistKeyBinding keyBinding,
PersistEntityBinding entityBinding,
Sequence sequence) {
catalog = keyBinding.catalog;
keyFieldFormat = keyBinding.keyFormat.getSequenceKeyFormat();
entityFormat = entityBinding.entityFormat;
rawAccess = entityBinding.rawAccess;
this.sequence = sequence;
}
public boolean assignPrimaryKey(Object entity, DatabaseEntry key)
throws DatabaseException {
if (entityFormat.isPriKeyNullOrZero(entity, rawAccess)) {
Long value = sequence.get(null, 1);
RecordOutput output = new RecordOutput(catalog, rawAccess);
keyFieldFormat.writeObject(value, output, rawAccess);
TupleBase.outputToEntry(output, key);
EntityInput input = new RecordInput
(catalog, rawAccess, null, 0,
key.getData(), key.getOffset(), key.getSize());
entityFormat.getReader().readPriKey(entity, input, rawAccess);
return true;
} else {
return false;
}
}
}