1 19 20 package org.netbeans.mdr.persistence.btreeimpl.btreestorage; 21 22 import java.text.MessageFormat ; 23 import java.util.Collection ; 24 import java.util.Set ; 25 import org.netbeans.mdr.persistence.MOFID; 26 import org.netbeans.mdr.persistence.SinglevaluedIndex; 27 import org.netbeans.mdr.persistence.Storage; 28 import org.netbeans.mdr.persistence.StorageBadRequestException; 29 import org.netbeans.mdr.persistence.StorageException; 30 31 32 36 public class PrimaryIndex implements SinglevaluedIndex { 37 38 private FileCache fileCache; private int fileId; private Storage.EntryType keyType; 42 private Storage.EntryType dataType; 43 private String indexName; 44 private int valuesInPage; 45 private final long firstMofIdSerial; 46 private static final int NO_VALUE=0; 47 private static final int sizeof_int=4; 48 private static final int reservedValues=FileHeader.HEADER_SIZE/sizeof_int; 49 50 private class ValInfo { 51 private int index; 52 private CachedPage page; 53 54 ValInfo(Object mofId) throws StorageException { 55 MOFID mid=(MOFID)mofId; 56 long serial=mid.getSerialNumber(); 57 58 if (serial>=BtreeFactory.FIRST_EXTERNAL_ID) { 59 assert serial>=firstMofIdSerial; 60 serial-=firstMofIdSerial-BtreeFactory.FIRST_EXTERNAL_ID; 61 } 62 serial+=reservedValues; 63 long pageNum=serial/valuesInPage; 64 page=fileCache.getPage(fileId, (int)pageNum); 65 66 index=(int)(serial%valuesInPage)*sizeof_int; 67 } 68 } 69 70 PrimaryIndex(String name, int id, FileCache cache, long firstSerial, Storage.EntryType key, Storage.EntryType data) { 71 indexName=name; 72 fileId=id; 73 fileCache=cache; 74 firstMofIdSerial=firstSerial; 75 keyType=key; 76 dataType=data; 77 valuesInPage=cache.getPageSize()/sizeof_int; 78 79 } 80 81 public void add(Object key, Object value) throws StorageException { 82 put(key, value); 83 } 84 85 public Object get(Object key) throws StorageException, StorageBadRequestException { 86 Object result = getIfExists(key); 87 if (result == null) { 88 throw new StorageBadRequestException ( 89 MessageFormat.format("Key {0} not found in index", 90 new Object [] {key})); 91 } 92 return result; 93 } 94 95 public Object getIfExists(Object key) throws StorageException { 96 ValInfo info=new ValInfo(key); 97 int value = Converter.readInt(info.page.contents,info.index); 98 99 fileCache.unpin(info.page); 100 if (value==NO_VALUE) 101 return null; 102 return new Integer (value); 103 } 104 105 public Storage.EntryType getKeyType() throws StorageException { 106 return keyType; 107 } 108 109 public String getName() throws StorageException { 110 return indexName; 111 } 112 113 public Object getObject(Object key, SinglevaluedIndex repos) throws StorageException { 114 Object result = getObjectIfExists(key, repos); 115 if (result == null) { 116 throw new StorageBadRequestException ( 117 MessageFormat.format("Key {0} not found in index", 118 new Object [] {key})); 119 } 120 return result; 121 } 122 123 public Object getObjectIfExists(Object key, SinglevaluedIndex repos) throws StorageException { 124 throw new UnsupportedOperationException (); 125 } 126 127 public Storage.EntryType getValueType() throws StorageException { 128 return dataType; 129 } 130 131 public Set keySet() throws StorageException { 132 throw new UnsupportedOperationException (); 133 } 134 135 public boolean put(Object key, Object value) throws StorageException { 136 ValInfo info=new ValInfo(key); 137 int oldVal = Converter.readInt(info.page.contents,info.index); 138 int val=((Integer )value).intValue(); 139 140 if (oldVal!=val) { 141 fileCache.setWritable(info.page); 142 Converter.writeInt(info.page.contents,info.index, val); 143 } 144 fileCache.unpin(info.page); 145 return oldVal!=NO_VALUE; 146 } 147 148 public Collection queryByKeyPrefix(Object prefix, SinglevaluedIndex repos) throws StorageException { 149 throw new UnsupportedOperationException (); 150 } 151 152 public boolean remove(Object key) throws StorageException { 153 ValInfo info=new ValInfo(key); 154 int val=Converter.readInt(info.page.contents,info.index); 155 156 if (val!=NO_VALUE) { 157 fileCache.setWritable(info.page); 158 Converter.writeInt(info.page.contents,info.index, NO_VALUE); 159 } 160 fileCache.unpin(info.page); 161 return val!=NO_VALUE; 162 } 163 164 public void replace(Object key, Object value) throws StorageException, StorageBadRequestException { 165 ValInfo info=new ValInfo(key); 166 int oldVal = Converter.readInt(info.page.contents,info.index); 167 168 if (oldVal==NO_VALUE) { 169 throw new StorageBadRequestException ( 170 MessageFormat.format("Key {0} not found in index", 171 new Object [] {key})); 172 } 173 int val=((Integer )value).intValue(); 174 fileCache.setWritable(info.page); 175 Converter.writeInt(info.page.contents,info.index, val); 176 fileCache.unpin(info.page); 177 } 178 179 public Collection values() throws StorageException { 180 throw new UnsupportedOperationException (); 181 } 182 183 } 184 | Popular Tags |