KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreeindex > MultivaluedOrderedBtree


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex;
20
21 import org.netbeans.mdr.persistence.*;
22 import java.util.*;
23 import java.text.*;
24
25 /**
26  * Btree implementation of the MultivaluedOrderedIndex interface.
27  *
28  * @author Dana Bergen
29  * @version 1.0
30  */

31 public class MultivaluedOrderedBtree extends MultivaluedBtree
32                 implements MultivaluedOrderedIndex {
33
34     public MultivaluedOrderedBtree(String JavaDoc name, Storage.EntryType keyType,
35                      Storage.EntryType dataType,
36                  boolean uniqueValues,
37                  BtreePageSource pageSource)
38                  throws StorageException {
39     super(name, keyType, dataType, uniqueValues, pageSource);
40     }
41
42     public MultivaluedOrderedBtree() {}
43
44     /** Returns a list view of the values associated in the index
45      * with specified key.
46      * Returned collection is read only and may not be modified.
47      * If there are no values associated with the key empty collection is returned.
48      * @param key
49      * @return
50      */

51     public List getItemsOrdered(Object JavaDoc key) throws StorageException {
52         return (List) getItems (key); // supertype always returns List, typecasting is legal
53
}
54
55       /** Like getItemsOrdered, but if the index contains keys, this returns the objects
56      * corresponding to the key
57      * @return
58      * @param key
59      * @throws StorageException
60      */

61    public java.util.Collection JavaDoc getObjectsOrdered (Object JavaDoc key, SinglevaluedIndex repos) throws StorageException {
62     return getObjects (key, repos);
63     }
64
65     /** Inserts the specified element at the specified position in the list of values
66      * associated with the specified key.
67      * Throws StorageBadRequestException if the index is out of range.
68      * @param key
69      * @param index
70      * @param value
71      */

72     public void add(Object JavaDoc key, int index, Object JavaDoc data) throws StorageException {
73         beginWrite();
74     try {
75             if (index < 0) {
76                 failed = true;
77             } else {
78                 failed = false;
79                 btreePut(key, data, ADD, index);
80                 updateKeyModCount (key);
81             }
82         if (failed) {
83         throw new StorageBadRequestException(
84            MessageFormat.format("Index {0} is out of range for key {1}",
85             new Object JavaDoc[] {new Integer JavaDoc(index), key}));
86         }
87     } finally {
88         endWrite();
89     }
90     }
91
92     /**
93      * Add an item to the end of the list of values for this key.
94      *
95      * @param key
96      * @param value
97      */

98     public void add(Object JavaDoc key, Object JavaDoc data) throws StorageException {
99         /* An index of Integer.MAX_VALUE signifies the end of the list */
100         add(key, Integer.MAX_VALUE, data);
101     }
102
103     /** Removes the element at the specified position in the list of values
104      * associated with the specified key.
105      * @return true if this index changed as a result of this call
106      * @param key
107      * @param index
108      */

109     public boolean remove(Object JavaDoc key, int index) throws StorageException {
110         beginWrite();
111     try {
112         boolean result;
113         byte[] keyBuffer;
114
115         if ((keyBuffer = keyInfo.toBuffer(key)) == null) {
116         throw new StorageBadRequestException(
117           MessageFormat.format(
118           "Invalid key type for this index: {0} received, {1} expected",
119                 new Object JavaDoc[] {
120                 key.getClass().getName(),
121                 keyInfo.typeName()} ));
122         }
123
124         BtreePage root = pageSource.getPage(rootPageId, this);
125         result = root.remove(keyBuffer, index);
126             if (result)
127                 updateKeyModCount (key);
128         pageSource.unpinPage(root);
129         return result;
130     } finally {
131         endWrite();
132     }
133     }
134
135     /** Replaces the element at the specified position in the list of values
136      * associated with the specified key with the specified element.
137      * Throws StorageBadRequestException if the index is out of range.
138      * @param key
139      * @param index
140      * @param element
141      * @throws StorageException
142      */

143     public void replace(Object JavaDoc key, int index, Object JavaDoc data)
144                             throws StorageException {
145         beginWrite();
146     try {
147         failed = false;
148         btreePut(key, data, REPLACE, index);
149             updateKeyModCount (key);
150         if (failed) {
151         throw new StorageBadRequestException(
152            MessageFormat.format("Index {0} is out of range for key {1}",
153             new Object JavaDoc[] {new Integer JavaDoc(index), key}));
154         }
155     } finally {
156         endWrite();
157     }
158     }
159 }
160
Popular Tags