PersistKeyBinding.java [plain text]
package com.sleepycat.persist.impl;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.tuple.TupleBase;
import com.sleepycat.db.DatabaseEntry;
public class PersistKeyBinding implements EntryBinding {
Catalog catalog;
Format keyFormat;
boolean rawAccess;
public PersistKeyBinding(Catalog catalog,
String clsName,
boolean rawAccess) {
this.catalog = catalog;
keyFormat = catalog.getFormat(clsName);
if (keyFormat == null) {
throw new IllegalArgumentException
("Class is not persistent: " + clsName);
}
if (!keyFormat.isSimple() &&
(keyFormat.getClassMetadata() == null ||
keyFormat.getClassMetadata().getCompositeKeyFields() == null)) {
throw new IllegalArgumentException
("Key class is not a simple type or a composite key class " +
"(composite keys must include @KeyField annotations): " +
clsName);
}
this.rawAccess = rawAccess;
}
PersistKeyBinding(Class cls, String[] compositeFieldOrder) {
catalog = SimpleCatalog.getInstance();
if (compositeFieldOrder != null) {
assert !SimpleCatalog.isSimpleType(cls);
keyFormat = new CompositeKeyFormat(cls, null, compositeFieldOrder);
} else {
assert SimpleCatalog.isSimpleType(cls);
keyFormat = catalog.getFormat(cls);
}
keyFormat.initializeIfNeeded(catalog);
}
Object bytesToObject(byte[] bytes, int offset, int length) {
return readKey(keyFormat, catalog, bytes, offset, length, rawAccess);
}
static Object readKey(Format keyFormat,
Catalog catalog,
byte[] bytes,
int offset,
int length,
boolean rawAccess) {
EntityInput input = new RecordInput
(catalog, rawAccess, null, 0, bytes, offset, length);
return input.readKeyObject(keyFormat);
}
public Object entryToObject(DatabaseEntry entry) {
return bytesToObject
(entry.getData(), entry.getOffset(), entry.getSize());
}
public void objectToEntry(Object object, DatabaseEntry entry) {
RecordOutput output = new RecordOutput(catalog, rawAccess);
output.writeKeyObject(object, keyFormat);
TupleBase.outputToEntry(output, entry);
}
}