1 11 package org.eclipse.core.internal.localstore; 12 13 import org.eclipse.core.internal.indexing.*; 14 import org.eclipse.core.internal.properties.IndexedStoreWrapper; 15 import org.eclipse.core.internal.utils.Convert; 16 import org.eclipse.core.internal.utils.UniversalUniqueIdentifier; 17 import org.eclipse.core.runtime.*; 18 19 40 public class HistoryStoreEntry implements ILocalStoreConstants { 41 private IndexCursor cursor; 42 private UniversalUniqueIdentifier uuid; 43 private byte[] key; 44 45 48 private HistoryStoreEntry(byte[] key, byte[] value, IndexCursor cursor) { 49 this.cursor = cursor; 50 this.key = key; 51 this.uuid = new UniversalUniqueIdentifier(value); 52 } 53 54 57 public HistoryStoreEntry(IPath path, UniversalUniqueIdentifier uuid, long lastModified, byte count) { 58 this.key = keyToBytes(path, lastModified, count); 59 this.uuid = uuid; 60 } 61 62 protected boolean compare(byte[] one, byte[] another) { 63 if (one.length != another.length) 64 return false; 65 for (int i = 0; i < one.length; i++) 66 if (one[i] != another[i]) 67 return false; 68 return true; 69 } 70 71 77 public static HistoryStoreEntry create(IndexedStoreWrapper store, IndexCursor cursor) throws CoreException, IndexedStoreException { 78 byte[] key = cursor.getKey(); 79 ObjectID valueID = cursor.getValueAsObjectID(); 80 byte[] value = store.getObject(valueID); 81 return new HistoryStoreEntry(key, value, cursor); 82 } 83 84 public byte getCount() { 85 return key[key.length - 1]; 86 } 87 88 public byte[] getKey() { 89 return key; 90 } 91 92 public long getLastModified() { 93 byte[] lastModifiedBytes = new byte[SIZE_LASTMODIFIED]; 94 int position = (key.length - SIZE_KEY_SUFFIX); 95 System.arraycopy(key, position, lastModifiedBytes, 0, SIZE_LASTMODIFIED); 96 return Convert.bytesToLong(lastModifiedBytes); 97 } 98 99 public IPath getPath() { 100 byte[] pathBytes = new byte[key.length - SIZE_KEY_SUFFIX]; 101 System.arraycopy(key, 0, pathBytes, 0, pathBytes.length); 102 return new Path(Convert.fromUTF8(pathBytes)); 103 } 104 105 public UniversalUniqueIdentifier getUUID() { 106 return uuid; 107 } 108 109 116 public static byte[] keyPrefixToBytes(IPath path, long lastModified) { 117 byte[] pathBytes = Convert.toUTF8(path.toString()); 119 byte[] lastModifiedBytes = Convert.longToBytes(lastModified); 120 byte[] keyPrefixBytes = new byte[pathBytes.length + lastModifiedBytes.length]; 122 System.arraycopy(pathBytes, 0, keyPrefixBytes, 0, pathBytes.length); 124 System.arraycopy(lastModifiedBytes, 0, keyPrefixBytes, pathBytes.length, lastModifiedBytes.length); 125 return keyPrefixBytes; 126 } 127 128 138 protected byte[] keyToBytes(IPath path, long lastModified, byte count) { 139 byte[] keyPrefix = keyPrefixToBytes(path, lastModified); 141 byte[] keyBytes = new byte[keyPrefix.length + 1]; 143 int destPosition = 0; 145 System.arraycopy(keyPrefix, 0, keyBytes, destPosition, keyPrefix.length); 146 destPosition += keyPrefix.length; 147 keyBytes[destPosition] = count; 148 return keyBytes; 149 } 150 151 154 public void remove() throws IndexedStoreException { 155 if (cursor == null) 156 return; 157 reposition(); 158 if (!cursor.isSet()) 159 return; 160 cursor.remove(); 161 } 162 163 protected void reposition() throws IndexedStoreException { 164 if (cursor.isSet()) 165 if (compare(cursor.getKey(), key)) 166 return; 167 cursor.find(key); 168 } 169 170 173 public String toString() { 174 StringBuffer s = new StringBuffer (); 175 s.append("Path: ").append(getPath()).append("\n"); s.append("Last Modified: ").append(getLastModified()).append("\n"); s.append("Count: ").append(getCount()).append("\n"); s.append("UUID: ").append(uuid.toStringAsBytes()).append("\n"); return s.toString(); 180 } 181 182 189 public byte[] valueToBytes() { 190 return uuid.toBytes(); 191 } 192 } 193 | Popular Tags |