KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > indexing > Index


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.indexing;
12
13 import java.util.Vector JavaDoc;
14
15 /**
16  * This class provides the public interface to an index.
17  */

18 public class Index {
19
20     private IndexedStore store;
21     private ObjectAddress anchorAddress;
22
23     /**
24      * Index constructor.
25      */

26     Index(IndexedStore store, ObjectAddress anchorAddress) {
27         this.store = store;
28         this.anchorAddress = anchorAddress;
29     }
30
31     /**
32      * Returns a vector of ObjectIDs whose keys match the key given in the index.
33      * This assumes that the underlying index has values that can be converted
34      * to ObjectIDs.
35      */

36     public synchronized Vector JavaDoc getObjectIdentifiersMatching(byte[] key) throws IndexedStoreException {
37         IndexCursor cursor = open();
38         cursor.find(key);
39         Vector JavaDoc vector = new Vector JavaDoc(20);
40         while (cursor.keyMatches(key)) {
41             vector.addElement(cursor.getValueAsObjectID());
42             cursor.next();
43         }
44         cursor.close();
45         return vector;
46     }
47
48     /**
49      * Inserts an entry into an index. The key and the value are byte arrays.
50      * Keys cannot be more than 1024 bytes in length. Values must not
51      * be greater than 2048 bytes in length. The other insert methods are
52      * convenience methods that use this for their implementation.
53      */

54     public synchronized void insert(byte[] key, byte[] value) throws IndexedStoreException {
55         if (key.length > 1024)
56             throw new IndexedStoreException(IndexedStoreException.EntryKeyLengthError);
57         if (value.length > 2048)
58             throw new IndexedStoreException(IndexedStoreException.EntryValueLengthError);
59         IndexAnchor anchor = store.acquireAnchor(anchorAddress);
60         anchor.insert(key, value);
61         anchor.release();
62     }
63
64     public synchronized void insert(byte[] key, Insertable value) throws IndexedStoreException {
65         insert(key, value.toByteArray());
66     }
67
68     public synchronized void insert(String JavaDoc key, byte[] value) throws IndexedStoreException {
69         insert(Convert.toUTF8(key), value);
70     }
71
72     /**
73      * Returns a cursor for this index. The cursor is initially in the unset state
74      * and should be positioned using "find" before being used.
75      */

76     public synchronized IndexCursor open() throws IndexedStoreException {
77         IndexCursor c = new IndexCursor(store, anchorAddress);
78         return c;
79     }
80 }
81
Popular Tags