KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > query > btree > BTree


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.query.btree;
25
26 import org.objectweb.jalisto.se.impl.LogicalOid;
27 import org.objectweb.jalisto.se.impl.InFileAddress;
28 import org.objectweb.jalisto.se.api.query.Index;
29 import org.objectweb.jalisto.se.api.ClassDescription;
30 import org.objectweb.jalisto.se.api.query.FieldDescription;
31 import org.objectweb.jalisto.se.api.query.*;
32 import org.objectweb.jalisto.se.query.IndexManagerImpl;
33
34 import java.io.Serializable JavaDoc;
35 import java.util.*;
36
37 public class BTree implements Serializable JavaDoc {
38
39     public BTree(Index index, int fieldIndex, ClassDescription meta,
40                  IndexManagerImpl indexManager) {
41         this.index = index;
42         this.comparator = meta.getComparator(fieldIndex);
43         this.fieldIndex = fieldIndex;
44         this.indexManager = indexManager;
45         this.meta = meta;
46         loadedElements = new HashMap();
47         BTreeNode root = new BTreeNode(this, null);
48         rootIfa = root.getIfa();
49         firstIfa = rootIfa;
50     }
51
52     public Set getOidsFor(Object JavaDoc value) {
53         BTreeNode root = indexManager.readNodeInBase(this, rootIfa);
54         OidCollection oids = indexManager.readOidsInBase(this, root.getOidsIfa(value, comparator));
55         return oids.asSet();
56     }
57
58     public void addOid(Object JavaDoc value, LogicalOid oid) {
59         BTreeNode root = indexManager.readNodeInBase(this, rootIfa);
60         root.insertOid(comparator, value, oid);
61     }
62
63     public void removeOid(Object JavaDoc value, LogicalOid oid) {
64         BTreeNode root = indexManager.readNodeInBase(this, rootIfa);
65         root.removeOid(value, oid, comparator);
66     }
67
68     public Set getKeys() {
69         Set set = new HashSet();
70         InFileAddress nextIfa = firstIfa;
71         while (nextIfa != null) {
72             BTreeNode node = indexManager.readNodeInBase(this, nextIfa);
73             nextIfa = node.getKeys(set);
74         }
75         return set;
76     }
77
78     public IndexManagerImpl getIndexManager() {
79         return indexManager;
80     }
81
82     public void setIndexManager(IndexManagerImpl indexManager) {
83         this.indexManager = indexManager;
84         loadedElements = new HashMap();
85     }
86
87     public void setRootIfa(InFileAddress rootIfa) {
88         this.rootIfa = rootIfa;
89         updateInBase();
90     }
91
92     public FielComparator getComparator() {
93         return comparator;
94     }
95
96     public InFileAddress getFirstIfa() {
97         return firstIfa;
98     }
99
100     public void setFirstIfa(InFileAddress firstIfa) {
101         this.firstIfa = firstIfa;
102         updateInBase();
103     }
104
105     public InFileAddress getLastIfa() {
106         return lastIfa;
107     }
108
109     public void setLastIfa(InFileAddress lastIfa) {
110         this.lastIfa = lastIfa;
111         updateInBase();
112     }
113
114     public FieldDescription getFieldDescription() {
115         return meta.getFieldDescription(fieldIndex);
116     }
117
118     public Map getLoadedElements() {
119         return loadedElements;
120     }
121
122     public void deleteTree() {
123         BTreeNode root = indexManager.readNodeInBase(this, rootIfa);
124         root.deleteYourself();
125     }
126
127     private void updateInBase() {
128         indexManager.updateIndexInBase(index);
129     }
130
131     public String JavaDoc toString() {
132         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
133         sb.append("BTree on ").append(indexManager).append(" with comparator ").append(comparator);
134         sb.append("\n root ifa : ").append(rootIfa);
135         sb.append("\n firstIfa ifa : ").append(firstIfa);
136         sb.append("\n lastIfa ifa : ").append(lastIfa);
137         if (loadedElements != null) {
138             sb.append("\n loadedElements size : ").append(loadedElements.size());
139         }
140         sb.append("\n");
141         if (indexManager != null) {
142             LinkedList toPrintNode = new LinkedList();
143             LinkedList toPrintOids = new LinkedList();
144             toPrintNode.add(rootIfa);
145             while (!toPrintNode.isEmpty()) {
146                 BTreeNode node = indexManager.readNodeInBase(this, (InFileAddress) toPrintNode.removeFirst());
147                 sb.append(node.toFullString(toPrintNode, toPrintOids));
148             }
149             while (!toPrintOids.isEmpty()) {
150                 OidCollection oids = indexManager.readOidsInBase(this, (InFileAddress) toPrintOids.removeFirst());
151                 sb.append(oids.toFullString()).append("\n");
152             }
153         }
154         return sb.toString();
155     }
156
157     private Index index;
158     private ClassDescription meta;
159     private int fieldIndex;
160     private InFileAddress rootIfa;
161     private InFileAddress firstIfa;
162     private InFileAddress lastIfa;
163     private FielComparator comparator;
164
165     private transient IndexManagerImpl indexManager;
166     private transient Map loadedElements;
167
168
169     static final long serialVersionUID = -7589377901514761459L;
170
171 }
172
Popular Tags