KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > localstore > HistoryStoreEntry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
20  * HistoryStoreEntry objects perform all required conversion operations
21  * when performing insertion and retrieval from the history store.
22  * Following retrieval, removal from the store is also performed
23  * through the entry objects.
24  * <p>
25  * For insertion:
26  * - Create an entry object using create(IndexedStore, IndexCursor).
27  * - Insert the entry to the store using the keyToBytes() and
28  * valueToBytes() methods.
29  * <p>
30  * For retrieval:
31  * - Create an entry object by providing the indexed store and
32  * the cursor location from which version information can be read.
33  * <p>
34  * A static method keyPrefixToBytes() is provided to perform the
35  * conversion of key prefix values to byte array representation. The
36  * returned value is intended to be used when traversing the indexed
37  * store on partial key matches (path + timestamp) is required.
38  *
39  */

40 public class HistoryStoreEntry implements ILocalStoreConstants {
41     private IndexCursor cursor;
42     private UniversalUniqueIdentifier uuid;
43     private byte[] key;
44
45     /**
46      * Constructs an entry object for retrieval from the history store.
47      */

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     /**
55      * Constructs an entry object to perform insertion to the history store.
56      */

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     /**
72      * Returns an entry object containing the information retrieved from the history store.
73      *
74      * @param store Indexed history store from which data is to be read.
75      * @param cursor Position from which data is to be read.
76      */

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     /**
110      * Converts the provided parameters into a single byte array representation.
111      * Format:
112      * path + lastModified.
113      *
114      * @return Converted byte array.
115      */

116     public static byte[] keyPrefixToBytes(IPath path, long lastModified) {
117         // Retrieve byte array representations of values.
118
byte[] pathBytes = Convert.toUTF8(path.toString());
119         byte[] lastModifiedBytes = Convert.longToBytes(lastModified);
120         // Byte array to hold key prefix.
121
byte[] keyPrefixBytes = new byte[pathBytes.length + lastModifiedBytes.length];
122         // Copy values.
123
System.arraycopy(pathBytes, 0, keyPrefixBytes, 0, pathBytes.length);
124         System.arraycopy(lastModifiedBytes, 0, keyPrefixBytes, pathBytes.length, lastModifiedBytes.length);
125         return keyPrefixBytes;
126     }
127
128     /**
129      * Converts the key for this entry object into byte array representation.
130      * Format:
131      * path + lastModified + count.
132      *
133      * Note that the count variable consists of a single byte. All other portions
134      * of the entry consist of multiple bytes.
135      *
136      * @return Key as a byte array.
137      */

138     protected byte[] keyToBytes(IPath path, long lastModified, byte count) {
139         // Get beginning portion of key.
140
byte[] keyPrefix = keyPrefixToBytes(path, lastModified);
141         // Byte array to hold full key. The count value is 1 byte in length.
142
byte[] keyBytes = new byte[keyPrefix.length + 1];
143         // Copy all values into full key.
144
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     /**
152      * Removes this entry from the store.
153      */

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     /**
171      * Used for debug.
172      */

173     public String JavaDoc toString() {
174         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
175         s.append("Path: ").append(getPath()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
176
s.append("Last Modified: ").append(getLastModified()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
177
s.append("Count: ").append(getCount()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
178
s.append("UUID: ").append(uuid.toStringAsBytes()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
179
return s.toString();
180     }
181
182     /**
183      * Converts the value for this entry object into byte array representation.
184      * Format:
185      * uuid.
186      *
187      * @return Value as a byte array.
188      */

189     public byte[] valueToBytes() {
190         return uuid.toBytes();
191     }
192 }
193
Popular Tags