KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > ix > IxFileRange


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.inside.ix;
22
23 import com.db4o.*;
24 import com.db4o.foundation.*;
25 import com.db4o.inside.freespace.*;
26
27 /**
28  * A range of index entries in the database file.
29  */

30 class IxFileRange extends IxTree{
31     
32     final int _address;
33     int _addressOffset;
34     int _entries;
35     private int[] _lowerAndUpperMatches;
36     
37     public IxFileRange(IndexTransaction a_ft, int a_address, int addressOffset, int a_entries){
38         super(a_ft);
39         _address = a_address;
40         _addressOffset = addressOffset;
41         _entries = a_entries;
42         _size = a_entries;
43     }
44     
45     public Tree add(final Tree a_new){
46         return reader().add(this, a_new);
47     }
48
49     public int compare(Tree a_to) {
50         _lowerAndUpperMatches = new int[2];
51         return reader().compare(this, _lowerAndUpperMatches);
52     }
53     
54     int[] lowerAndUpperMatch(){
55         return _lowerAndUpperMatches;
56     }
57     
58     private final IxFileRangeReader reader(){
59         return _fieldTransaction.i_index.fileRangeReader();
60     }
61     
62     public void incrementAddress(int length) {
63         _addressOffset += length;
64     }
65     
66     public int ownSize(){
67         return _entries;
68     }
69     
70     public String JavaDoc toString(){
71         if(! Debug4.prettyToStrings){
72             return super.toString();
73         }
74         YapReader fileReader = new YapReader(slotLength());
75         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76         sb.append("IxFileRange");
77         visitAll(new IntObjectVisitor() {
78             public void visit(int anInt, Object JavaDoc anObject) {
79                 sb.append("\n ");
80                 sb.append("Parent: " + anInt);
81                 sb.append("\n ");
82                 sb.append(anObject);
83             }
84         });
85        return sb.toString();
86     }
87     
88     public void visit(Object JavaDoc obj){
89         visit((Visitor4)obj, null);
90     }
91
92     public void visit(Visitor4 visitor, int[] lowerUpper){
93         IxFileRangeReader frr = reader();
94         if (lowerUpper == null) {
95             lowerUpper = new int[] { 0, _entries - 1};
96         }
97         int count = lowerUpper[1] - lowerUpper[0] + 1;
98         if (count > 0) {
99             YapReader fileReader = new YapReader(count * frr._slotLength);
100             fileReader.read(stream(), _address, _addressOffset + (lowerUpper[0] * frr._slotLength));
101             for (int i = lowerUpper[0]; i <= lowerUpper[1]; i++) {
102                 fileReader.incrementOffset(frr._linkLegth);
103                 visitor.visit(new Integer JavaDoc(fileReader.readInt()));
104             }
105         }
106     }
107
108     public int write(Indexable4 a_handler, YapWriter a_writer) {
109         YapFile yf = (YapFile)a_writer.getStream();
110         int length = _entries * slotLength();
111         yf.copy(_address, _addressOffset, a_writer.getAddress(), a_writer.addressOffset(), length);
112         a_writer.moveForward(length);
113         return _entries;
114     }
115     
116     public void visitAll(IntObjectVisitor visitor) {
117         YapFile yf = stream();
118         Transaction transaction = trans();
119         YapReader fileReader = new YapReader(slotLength());
120         for (int i = 0; i < _entries; i++) {
121             int address = _address + (i * slotLength());
122             fileReader.read(yf, address, _addressOffset);
123             fileReader._offset = 0;
124             Object JavaDoc obj = handler().comparableObject(transaction, handler().readIndexEntry(fileReader));
125             visitor.visit(fileReader.readInt(), obj);
126         }
127     }
128     
129     public void visitFirst(FreespaceVisitor visitor){
130         if(_preceding != null){
131             ((IxTree)_preceding).visitFirst(visitor);
132             if(visitor.visited()){
133                 return;
134             }
135         }
136         freespaceVisit(visitor, 0);
137     }
138     
139     public void visitLast(FreespaceVisitor visitor){
140         if(_subsequent != null){
141             ((IxTree)_subsequent).visitLast(visitor);
142             if(visitor.visited()){
143                 return;
144             }
145         }
146         freespaceVisit(visitor, _entries - 1);
147     }
148     
149     public void freespaceVisit(FreespaceVisitor visitor, int index){
150         IxFileRangeReader frr = reader();
151         YapReader fileReader = new YapReader(frr._slotLength);
152         fileReader.read(stream(), _address, _addressOffset + (index * frr._slotLength));
153         int val = fileReader.readInt();
154         int parentID = fileReader.readInt();
155         visitor.visit(parentID, val);
156     }
157
158     public Object JavaDoc shallowClone() {
159         IxFileRange range=new IxFileRange(_fieldTransaction,_address,_addressOffset,_entries);
160         super.shallowCloneInternal(range);
161         if(_lowerAndUpperMatches!=null) {
162             range._lowerAndUpperMatches=new int[]{_lowerAndUpperMatches[0],_lowerAndUpperMatches[1]};
163         }
164         return range;
165     }
166 }
167
Popular Tags