1 8 9 package com.sleepycat.persist.impl; 10 11 import com.sleepycat.bind.EntityBinding; 12 import com.sleepycat.bind.tuple.TupleBase; 13 import com.sleepycat.je.DatabaseEntry; 14 import com.sleepycat.persist.raw.RawObject; 15 16 21 public class PersistEntityBinding implements EntityBinding { 22 23 Catalog catalog; 24 Format entityFormat; 25 boolean rawAccess; 26 PersistKeyAssigner keyAssigner; 27 28 31 public PersistEntityBinding(Catalog catalog, 32 String entityClassName, 33 boolean rawAccess) { 34 this.catalog = catalog; 35 if (rawAccess) { 36 entityFormat = catalog.getFormat(entityClassName); 37 if (entityFormat == null || !entityFormat.isEntity()) { 38 throw new IllegalArgumentException 39 ("Not an entity class: " + entityClassName); 40 } 41 } else { 42 Class entityCls; 43 try { 44 entityCls = Class.forName(entityClassName); 45 } catch (ClassNotFoundException e) { 46 throw new IllegalArgumentException (e); 47 } 48 entityFormat = catalog.getFormat(entityCls); 49 } 50 this.rawAccess = rawAccess; 51 } 52 53 public PersistKeyAssigner getKeyAssigner() { 54 return keyAssigner; 55 } 56 57 public Object entryToObject(DatabaseEntry key, DatabaseEntry data) { 58 return readEntity(catalog, key, data, rawAccess); 59 } 60 61 73 static Object readEntity(Catalog catalog, 74 DatabaseEntry key, 75 DatabaseEntry data, 76 boolean rawAccess) { 77 RecordInput keyInput = new RecordInput 78 (catalog, rawAccess, null, 0, 79 key.getData(), key.getOffset(), key.getSize()); 80 RecordInput dataInput = new RecordInput 81 (catalog, rawAccess, null, 0, 82 data.getData(), data.getOffset(), data.getSize()); 83 int formatId = dataInput.readPackedInt(); 84 Format format = catalog.getFormat(formatId); 85 Reader reader = format.getReader(); 86 Object entity = reader.newInstance(dataInput, rawAccess); 87 reader.readPriKey(entity, keyInput, rawAccess); 88 return reader.readObject(entity, dataInput, rawAccess); 89 } 90 91 public void objectToData(Object entity, DatabaseEntry data) { 92 Format format = getValidFormat(entity); 93 writeEntity(format, catalog, entity, data, rawAccess); 94 } 95 96 106 static void writeEntity(Format format, 107 Catalog catalog, 108 Object entity, 109 DatabaseEntry data, 110 boolean rawAccess) { 111 RecordOutput output = new RecordOutput(catalog, rawAccess); 112 output.writePackedInt(format.getId()); 113 format.writeObject(entity, output, rawAccess); 114 TupleBase.outputToEntry(output, data); 115 } 116 117 public void objectToKey(Object entity, DatabaseEntry key) { 118 119 123 Format format = getValidFormat(entity); 124 RecordOutput output = new RecordOutput(catalog, rawAccess); 125 126 127 format.writePriKey(entity, output, rawAccess); 128 TupleBase.outputToEntry(output, key); 129 } 130 131 135 private Format getValidFormat(Object entity) { 136 137 138 if (entity == null) { 139 throw new IllegalArgumentException ("An entity may not be null"); 140 } 141 142 146 Format format; 147 if (rawAccess) { 148 if (!(entity instanceof RawObject)) { 149 throw new IllegalArgumentException 150 ("Entity must be a RawObject"); 151 } 152 format = (Format) ((RawObject) entity).getType(); 153 } else { 154 format = catalog.getFormat(entity.getClass()); 155 } 156 157 158 if (format.getEntityFormat() != entityFormat) { 159 throw new IllegalArgumentException 160 ("The entity class (" + format.getClassName() + 161 ") must be this entity class or a subclass of it: " + 162 entityFormat.getClassName()); 163 } 164 165 return format; 166 } 167 } 168 | Popular Tags |