PersistEntityBinding.java [plain text]
package com.sleepycat.persist.impl;
import com.sleepycat.bind.EntityBinding;
import com.sleepycat.bind.tuple.TupleBase;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.persist.model.EntityModel;
import com.sleepycat.persist.raw.RawObject;
public class PersistEntityBinding implements EntityBinding {
PersistCatalog catalog;
Format entityFormat;
boolean rawAccess;
PersistKeyAssigner keyAssigner;
public PersistEntityBinding(PersistCatalog catalog,
String entityClassName,
boolean rawAccess) {
this.catalog = catalog;
if (rawAccess) {
entityFormat = catalog.getFormat(entityClassName);
if (entityFormat == null || !entityFormat.isEntity()) {
throw new IllegalArgumentException
("Not an entity class: " + entityClassName);
}
} else {
Class entityCls;
try {
entityCls = EntityModel.classForName(entityClassName);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
entityFormat = catalog.getFormat(entityCls);
}
this.rawAccess = rawAccess;
}
public PersistKeyAssigner getKeyAssigner() {
return keyAssigner;
}
public Object entryToObject(DatabaseEntry key, DatabaseEntry data) {
return readEntity(catalog, key, data, rawAccess);
}
static Object readEntity(Catalog catalog,
DatabaseEntry key,
DatabaseEntry data,
boolean rawAccess) {
RecordInput keyInput = new RecordInput
(catalog, rawAccess, null, 0,
key.getData(), key.getOffset(), key.getSize());
RecordInput dataInput = new RecordInput
(catalog, rawAccess, null, 0,
data.getData(), data.getOffset(), data.getSize());
int formatId = dataInput.readPackedInt();
Format format = catalog.getFormat(formatId);
Reader reader = format.getReader();
Object entity = reader.newInstance(dataInput, rawAccess);
reader.readPriKey(entity, keyInput, rawAccess);
return reader.readObject(entity, dataInput, rawAccess);
}
public void objectToData(Object entity, DatabaseEntry data) {
Format format = getValidFormat(entity);
writeEntity(format, catalog, entity, data, rawAccess);
}
static void writeEntity(Format format,
Catalog catalog,
Object entity,
DatabaseEntry data,
boolean rawAccess) {
RecordOutput output = new RecordOutput(catalog, rawAccess);
output.writePackedInt(format.getId());
format.writeObject(entity, output, rawAccess);
TupleBase.outputToEntry(output, data);
}
public void objectToKey(Object entity, DatabaseEntry key) {
Format format = getValidFormat(entity);
RecordOutput output = new RecordOutput(catalog, rawAccess);
format.writePriKey(entity, output, rawAccess);
TupleBase.outputToEntry(output, key);
}
private Format getValidFormat(Object entity) {
if (entity == null) {
throw new IllegalArgumentException("An entity may not be null");
}
Format format;
if (rawAccess) {
if (!(entity instanceof RawObject)) {
throw new IllegalArgumentException
("Entity must be a RawObject");
}
format = (Format) ((RawObject) entity).getType();
} else {
format = catalog.getFormat(entity.getClass());
}
if (format.getEntityFormat() != entityFormat) {
throw new IllegalArgumentException
("The entity class (" + format.getClassName() +
") must be this entity class or a subclass of it: " +
entityFormat.getClassName());
}
return format;
}
}