1 19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex; 20 21 import org.netbeans.mdr.persistence.*; 22 import org.netbeans.mdr.util.MapEntryImpl; 23 import java.util.*; 24 import java.text.*; 25 26 32 public class SinglevaluedBtree extends Btree implements SinglevaluedIndex { 33 34 public SinglevaluedBtree(String name, Storage.EntryType keyType, 35 Storage.EntryType dataType, 36 BtreePageSource pageSource) 37 throws StorageException { 38 super(name, keyType, dataType, pageSource); 39 } 40 41 protected void init() throws StorageException { 42 uniqueKeys = true; 43 uniqueValues = false; 44 super.init(); 45 } 46 47 51 public SinglevaluedBtree() { 52 } 53 54 59 60 71 public Object getIfExists(Object key) 72 throws StorageException { 73 return getIfExistsInternal(key, null); 74 } 75 76 82 public Object getObjectIfExists(Object key, SinglevaluedIndex repos) 83 throws StorageException { 84 return getIfExistsInternal(key, repos); 85 } 86 87 88 private Object getIfExistsInternal(Object key, SinglevaluedIndex repos) 89 throws StorageException { 90 91 beginRead(); 92 try { 93 byte[] keyBuffer; 94 byte[] dataBuffer; 95 Object result; 96 97 if ((keyBuffer = keyInfo.toBuffer(key)) == null) { 98 throw new StorageBadRequestException( 99 MessageFormat.format( 100 "Invalid key type for this index: {0} received, {1} expected", 101 new Object [] { 102 key.getClass().getName(), 103 keyInfo.typeName()} )); 104 } 105 106 BtreePage root = pageSource.getPage(rootPageId, this); 107 dataBuffer = root.get(keyBuffer); 108 if (dataBuffer != null) { 109 if (repos == null) { 110 result = dataInfo.fromBuffer(dataBuffer); 111 } 112 else { 113 result = dataInfo.objectFromBuffer(dataBuffer, repos); 114 } 115 } else { 116 result = null; 117 } 118 pageSource.unpinPage(root); 119 return result; 120 } finally { 121 endRead(); 122 } 123 } 124 125 135 public Object get(Object key) throws StorageException { 136 beginRead(); 137 try { 138 139 Object result = getIfExists(key); 140 if (result == null) { 141 throw new StorageBadRequestException ( 142 MessageFormat.format("Key {0} not found in index", 143 new Object [] {key})); 144 } 145 return result; 146 } finally { 147 endRead(); 148 } 149 } 150 151 157 public Object getObject (Object key, SinglevaluedIndex repos) throws StorageException { 158 beginRead(); 159 try { 160 161 Object result = getObjectIfExists(key, repos); 162 if (result == null) { 163 throw new StorageBadRequestException ( 164 MessageFormat.format("Key {0} not found in index", 165 new Object [] {key})); 166 } 167 return result; 168 } finally { 169 endRead(); 170 } 171 } 172 173 184 public boolean put(Object key, Object data) throws StorageException { 185 beginWrite(); 186 try { 187 replaced = false; 188 btreePut(key, data, REPLACE_IF_EXISTS, 0); 189 return replaced; 190 } finally { 191 endWrite(); 192 } 193 } 194 195 207 public void replace(Object key, Object data) throws StorageException { 208 beginWrite(); 209 try { 210 failed = false; 211 btreePut(key, data, REPLACE, 0); 212 if (failed) { 213 throw new StorageBadRequestException ( 214 MessageFormat.format("Key {0} not found in index", 215 new Object [] {key})); 216 } 217 } finally { 218 endWrite(); 219 } 220 } 221 222 228 public Collection values() throws StorageException { 229 return new BtreeCollection(this); 230 } 231 232 235 public synchronized Collection queryByKeyPrefix (Object prefix, SinglevaluedIndex primaryIndex) throws StorageException { 236 if (keyType != Storage.EntryType.STRING) { 237 throw new UnsupportedOperationException ("Key type must be EntryType.STRING"); 238 } 239 240 List result = new LinkedList (); 241 byte [] prefixBytes = keyInfo.toBuffer (prefix); 242 SearchResult location = getLocation (prefixBytes); 243 if (location.entryNum == location.page.numEntries()) 244 BtreePage.getNext (null, location); 245 246 while ((location.entryNum < location.page.numEntries()) && 247 isPrefix (prefixBytes, location.page.getKey (location.entryNum))) { 248 byte [] key = location.page.getKey (location.entryNum); 249 byte [] data = location.page.getData (location.entryNum); 250 Object entry = new MapEntryImpl ( 251 keyInfo.objectFromBuffer (key, primaryIndex), 252 dataInfo.objectFromBuffer (data, primaryIndex) 253 ); 254 result.add (entry); 255 BtreePage.getNext (null, location); 256 } return result; 258 } 259 260 static boolean isPrefix (byte [] prefix, byte [] key) { 261 if (prefix.length > key.length) 262 return false; 263 for (int x = 0; x < prefix.length; x++) { 264 if (prefix [x] != key [x]) 265 return false; 266 } 267 return true; 268 } 269 270 } 271 | Popular Tags |