1 8 9 package com.sleepycat.persist.impl; 10 11 import java.util.Collection ; 12 import java.util.Set ; 13 14 import com.sleepycat.bind.tuple.TupleBase; 15 import com.sleepycat.je.DatabaseEntry; 16 import com.sleepycat.je.DatabaseException; 17 import com.sleepycat.je.ForeignMultiKeyNullifier; 18 import com.sleepycat.je.SecondaryDatabase; 19 import com.sleepycat.je.SecondaryKeyCreator; 20 import com.sleepycat.je.SecondaryMultiKeyCreator; 21 import com.sleepycat.persist.model.EntityMetadata; 22 import com.sleepycat.persist.model.Relationship; 23 import com.sleepycat.persist.model.SecondaryKeyMetadata; 24 import com.sleepycat.persist.raw.RawObject; 25 26 32 public class PersistKeyCreator implements SecondaryKeyCreator, 33 SecondaryMultiKeyCreator, 34 ForeignMultiKeyNullifier { 35 36 static boolean isManyType(Class cls) { 37 return cls.isArray() || Collection .class.isAssignableFrom(cls); 38 } 39 40 private Catalog catalog; 41 private int priKeyFormatId; 42 private String keyName; 43 private Format keyFormat; 44 private boolean toMany; 45 46 49 public PersistKeyCreator(Catalog catalog, 50 EntityMetadata entityMeta, 51 String keyClassName, 52 SecondaryKeyMetadata secKeyMeta) { 53 this.catalog = catalog; 54 Format priKeyFormat = 55 catalog.getFormat(entityMeta.getPrimaryKey().getClassName()); 56 priKeyFormatId = priKeyFormat.getId(); 57 keyName = secKeyMeta.getKeyName(); 58 keyFormat = catalog.getFormat(keyClassName); 59 if (keyFormat == null) { 60 throw new IllegalArgumentException 61 ("Not a key class: " + keyClassName); 62 } 63 if (keyFormat.isPrimitive()) { 64 throw new IllegalArgumentException 65 ("Use a primitive wrapper class instead of class: " + 66 keyFormat.getClassName()); 67 } 68 Relationship rel = secKeyMeta.getRelationship(); 69 toMany = (rel == Relationship.ONE_TO_MANY || 70 rel == Relationship.MANY_TO_MANY); 71 } 72 73 public boolean createSecondaryKey(SecondaryDatabase secondary, 74 DatabaseEntry key, 75 DatabaseEntry data, 76 DatabaseEntry result) 77 throws DatabaseException { 78 79 if (toMany) { 80 throw new IllegalStateException (); 81 } 82 KeyLocation loc = moveToKey(key, data); 83 if (loc != null) { 84 RecordOutput output = new RecordOutput 85 (catalog, true ); 86 loc.format.copySecKey(loc.input, output); 87 TupleBase.outputToEntry(output, result); 88 return true; 89 } else { 90 91 return false; 92 } 93 } 94 95 public void createSecondaryKeys(SecondaryDatabase secondary, 96 DatabaseEntry key, 97 DatabaseEntry data, 98 Set results) 99 throws DatabaseException { 100 101 if (!toMany) { 102 throw new IllegalStateException (); 103 } 104 KeyLocation loc = moveToKey(key, data); 105 if (loc != null) { 106 loc.format.copySecMultiKey(loc.input, keyFormat, results); 107 } 108 109 } 110 111 public boolean nullifyForeignKey(SecondaryDatabase secondary, 112 DatabaseEntry key, 113 DatabaseEntry data, 114 DatabaseEntry secKey) 115 throws DatabaseException { 116 117 118 RawObject entity = (RawObject) PersistEntityBinding.readEntity 119 (catalog, key, data, true ); 120 Format entityFormat = (Format) entity.getType(); 121 122 126 Object secKeyObject = null; 127 if (toMany) { 128 secKeyObject = PersistKeyBinding.readKey 129 (keyFormat, catalog, secKey.getData(), secKey.getOffset(), 130 secKey.getSize(), true ); 131 } 132 if (entityFormat.nullifySecKey 133 (catalog, entity, keyName, secKeyObject)) { 134 135 139 PersistEntityBinding.writeEntity 140 (entityFormat, catalog, entity, data, true ); 141 return true; 142 } else { 143 144 return false; 145 } 146 } 147 148 151 private KeyLocation moveToKey(DatabaseEntry priKey, DatabaseEntry data) { 152 153 RecordInput input = new RecordInput 154 (catalog, true , priKey, priKeyFormatId, 155 data.getData(), data.getOffset(), data.getSize()); 156 int formatId = input.readPackedInt(); 157 Format entityFormat = catalog.getFormat(formatId); 158 Format fieldFormat = entityFormat.skipToSecKey(input, keyName); 159 if (fieldFormat != null) { 160 161 return input.getKeyLocation(fieldFormat); 162 } else { 163 164 return null; 165 } 166 } 167 } 168 | Popular Tags |