KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreestorage > VirtualList


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.btreestorage;
20
21 import java.io.*;
22 import java.util.*;
23 import org.netbeans.mdr.persistence.*;
24 import org.netbeans.mdr.persistence.btreeimpl.btreeindex.*;
25
26 /**
27 * An implementation of List which uses a btree database as a backing store.
28 * This allows arrays which which not otherwise fit in memory. To use a
29 * Virtual List:
30 * <ol>
31 * <li>
32 * First create it with VirtualList(String name). This will create
33 * a btree database with the given name to hold the list elements.
34 * <li>
35 * After an entry changes, call VirtualList.changed(index) to ensure that the
36 * changed version is written out to the repository.
37 * <li>
38 * When done, call VirtualArray.delete() to delete the backing repository
39 * </ol>
40 * Note that objects in the list must be Streamable.
41 */

42 public class VirtualList extends AbstractList {
43     private ArrayList keys = new ArrayList();
44     private BtreeDatabase store;
45     private String JavaDoc name;
46     private BtreeStorage storage;
47
48     /** Create a VirtualList
49     * @param nm The name of the btree repository to create
50     */

51     public VirtualList (String JavaDoc fileName, ObjectResolver resolver) {
52         this (defaultMap (fileName), resolver);
53     }
54     
55     public VirtualList(Map properties, ObjectResolver resolver) {
56     try {
57         this.storage = (BtreeStorage) (new BtreeFactory()).createStorage(properties);
58         this.storage.create(true, resolver);
59         this.store = (BtreeDatabase)this.storage.getPrimaryIndex();
60     }
61     catch (StorageException ex) {
62         throw new RuntimeStorageException(ex);
63     }
64     }
65
66     /** Delete the VirtualList's backing repository
67     */

68     public void delete() {
69     try {
70         store.close();
71         store.delete(name);
72     }
73     catch (StorageException ex) {
74         throw new RuntimeStorageException(ex);
75     }
76     }
77
78     /** Get the specified element
79     * @param index The index of the element to get
80     * @return the element at the specified index
81     */

82     public synchronized Object JavaDoc get(int index) {
83     try {
84         MOFID key = (MOFID)keys.get(index);
85         return store.get(key);
86     }
87     catch (StorageException ex) {
88         throw new RuntimeStorageException(ex);
89     }
90     }
91
92     /** Get the number of elements in the list
93     */

94
95     public int size() {
96         return keys.size();
97     }
98
99     /** Set the element at the specified index
100     * @param index the index to set
101     * @param element the element to set the index to
102     * @return the element formerly at that index
103     */

104     public synchronized Object JavaDoc set(int index, Object JavaDoc element) {
105     try {
106         MOFID key = (MOFID)keys.get(index);
107         Object JavaDoc retval = store.getIfExists(key);
108         store.put(key, element);
109         return retval;
110     }
111     catch (StorageException ex) {
112         throw new RuntimeStorageException(ex);
113     }
114     }
115
116     /** Add a new element to the list
117     * @param the index at which to ass it
118     * @param element the element to add
119     */

120     public synchronized void add(int index, Object JavaDoc element) {
121     try {
122         MOFID key = new MOFID(this.storage);
123         keys.add(index, key);
124         store.add(key, element);
125     }
126     catch (StorageException ex) {
127         keys.remove(index);
128         throw new RuntimeStorageException(ex);
129     }
130     }
131
132     /** Remove ana element from the list
133     * @param index Where to remove the element
134     * @return the element removed.
135     */

136     public synchronized Object JavaDoc remove(int index) {
137     try {
138         MOFID key = (MOFID)keys.get(index);
139         Object JavaDoc o = store.get(key);
140         store.remove(key);
141         keys.remove(index);
142         return o;
143     }
144     catch (StorageException ex) {
145         throw new RuntimeStorageException(ex);
146     }
147     }
148
149     /** mark the elemenet at the given index changed
150     * @param the index of the changed element.
151     */

152     public synchronized void changed(int index) {
153     try {
154         store.objectStateChanged((MOFID)keys.get(index));
155     }
156     catch (StorageException ex) {
157         throw new RuntimeStorageException(ex);
158     }
159     }
160     
161     
162     private static Map defaultMap (String JavaDoc fileName) {
163         java.util.Hashtable JavaDoc map = new java.util.Hashtable JavaDoc ();
164         map.put (org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory.STORAGE_FILE_NAME,fileName);
165         return map;
166     }
167 }
168
Popular Tags