KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > store > Page


1 /*
2  * Created on 14-Mar-2006
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.jofti.store;
8
9 import java.nio.ByteBuffer JavaDoc;
10 import java.util.Arrays JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import com.jofti.btree.IPage;
16 import com.jofti.btree.LeafNodeEntry;
17 import com.jofti.core.IStoreManager;
18 import com.jofti.exception.JoftiException;
19
20 /**
21  * @author xenephon
22  *
23  */

24   public class Page implements Cloneable JavaDoc, IPage{
25     
26       private static Log log = LogFactory.getLog(Page.class);
27       
28     public int[] pointers =null;
29     public ByteBuffer JavaDoc buf = null;
30     IStoreManager manager =null;
31     IEntrySerializer serializer=null;
32     int bufSize =0;
33     
34     int reference =0;
35     
36     public Page(int[] pointers, ByteBuffer JavaDoc buf, IStoreManager manager, IEntrySerializer serializer){
37         this.pointers = pointers;
38         this.buf =buf;
39         bufSize = buf.capacity();
40         this.manager = manager;
41         this.serializer = serializer;
42     }
43     
44     
45     
46     public LeafNodeEntry getEntry(int position){
47         LeafNodeEntry obj =null;
48         
49         
50
51             
52         if (position >= pointers.length){
53             log.warn("error in page for position "+ position);
54             return null;
55         }else{
56             int pos = pointers[position];
57             if (pos != -1){
58                 ByteBuffer JavaDoc tempBuffer = buf.duplicate();
59                 tempBuffer.position(pos);
60                 tempBuffer.getInt();
61                 try{
62                 obj= serializer.convertFromStorage(tempBuffer);
63                 } catch (Throwable JavaDoc e){
64                     e.printStackTrace();
65                     throw new RuntimeException JavaDoc(e);
66                 }
67             // nodeEntries[position]=obj;
68
}else{
69                 log.warn("expected entry in page found -1 at pos "+ position);
70                 
71                 for (int i=0;i<position;i++){
72                     log.warn(i + "["+pointers[i] + "],");
73                 }
74                 throw new RuntimeException JavaDoc("expected entry found -1 "+ position +" "+ this);
75             }
76             if (obj ==null){
77                 log.warn("returning null for "+ position + " on buf "+ buf);
78             }
79             return obj;
80         }
81     }
82     
83     public void setEntry(int position, LeafNodeEntry entry){
84         
85         byte[] bytes = null;
86         try {
87             bytes = serializer.convertForStorage(entry);
88         } catch(JoftiException e){
89             throw new RuntimeException JavaDoc("unable to convert entry to bytes "+ entry,e);
90         }
91         //see if we can fit this into the buffer
92
if (buf.capacity() < buf.limit() + (bytes.length+4)){
93             // we need to reallocate the buffer
94
ByteBuffer JavaDoc temp = ByteBuffer.allocate((int)(buf.capacity() *1.5));
95             buf.position(0);
96             temp.put(buf);
97             temp.flip();
98             ByteBuffer JavaDoc oldBuf =buf;
99             //temp.limit(temp.position());
100
buf = temp;
101             log.info("new buffer assigned in page "+ buf + " " + oldBuf);
102         }
103     
104         
105         
106         
107         // find out where we put this
108
int pos = pointers[position];
109
110         
111         
112         //length of insert
113
int length = 4 + bytes.length;
114         
115         // first insert - or at end -
116
if (pos == -1 ){
117             //we can just add at end
118
if (position ==0){
119                 pos =0;
120             }else{
121                 pos = buf.limit();
122             }
123         //are we adding at end
124
//reset the limt
125
buf.limit(buf.limit()+length);
126             buf.position(pos);
127             buf.putInt(bytes.length);
128             buf.put(bytes);
129             //new limit and position should match
130

131         }else{
132             //set where we are going to put the new data
133
buf.position(pos);
134             buf.mark();
135             // create a copy - not a copy of the backing data
136
ByteBuffer JavaDoc copy = buf.duplicate();
137             // reset limit on buffer so we do not overflow
138
buf.limit(buf.capacity());
139             //set the position we are writing to
140
buf.position(buf.position() + length);
141             // copy the moved data
142
try {
143                 buf.put(copy);
144             } catch (Throwable JavaDoc t){
145                 log.warn("inserting failure for " + buf,t);
146                 throw new RuntimeException JavaDoc("inserting failure for " + buf,t);
147             }
148             // reset the limt to the current position
149
buf.limit(buf.position());
150     
151             
152             // reset back to the previous mark
153
buf.reset();
154             
155             //write the data
156
buf.putInt(bytes.length);
157             buf.put(bytes);
158         }
159
160             // reset the pointers
161
if (pointers[position] != -1){
162                 System.arraycopy(pointers,position,pointers,position+1,(pointers.length-1) -position);
163     
164                 for (int i=position+1;i<pointers.length && pointers[i]!=-1;i++){
165                     pointers[i]=pointers[i]+ length;
166                 }
167             }
168             pointers[position]=pos;
169
170
171         
172     }
173     
174     
175     
176
177     
178     public void removeEntry(int position){
179         
180         if (position >= pointers.length || pointers[position] ==-1){
181             return;
182         }else{
183
184             int pos = pointers[position];
185             // set up the start of the next entry
186
int overWriteStart =0;
187             // if it is not the last entry then use the next entry
188
if (position != pointers.length-1){
189                 overWriteStart = pointers[position+1];
190             }
191             if (overWriteStart ==-1 || position == pointers.length-1){
192                 //we can just truncate using the limit
193
buf.position(pos);
194                 buf.limit(pos);
195                 pointers[position]=-1;
196             }else{
197             // set the position of the buffer to the pos
198
buf.position(pos);
199                 
200                 // mark where the write is going to be put
201
buf.mark();
202                 // duplicate the buffer
203
ByteBuffer JavaDoc temp = buf.duplicate();
204                 //get the size of the array plus the
205
int length = temp.getInt()+4;
206                 // set the pos for the overwriteStart
207
temp.position(overWriteStart);
208                 
209                 // reset back to the mark
210
buf.reset();
211                 // put the bytes in
212
try {
213                     buf.put(temp);
214                 } catch (Exception JavaDoc e){
215                     log.warn("problem moving buffer posoition ",e );
216
217                 }
218                 
219                 // reset the limt
220
buf.limit(buf.position());
221                 
222
223                 
224                 //copy them back over the previous entries
225
int start =position+1;
226                 int arrayLength = (pointers.length) -(position+1);
227                 System.arraycopy(pointers,start,pointers,position,arrayLength);
228                 
229                 // reset the last one to -1 - sort of like bit shift
230

231                 pointers[pointers.length-1]=-1;
232                     
233                 
234                 
235                 // take off the removed entries
236
for (int i=position;i<pointers.length && pointers[i]!=-1;i++){
237                     if (position == pointers.length-1 || pointers[i]==-1){
238                         pointers[i]=-1;
239                     }else{
240                         pointers[i]=pointers[i]- length;
241                     }
242                 }
243                 }
244         
245             
246         }
247     }
248     
249     public void updateEntry(int location, LeafNodeEntry entry){
250         removeEntry(location);
251         setEntry(location, entry);
252     }
253     
254     
255     
256     public ByteBuffer JavaDoc copyBuffer(ByteBuffer JavaDoc newBuf){
257         
258         buf.rewind();
259         newBuf.clear();
260         newBuf.put(buf);
261         newBuf.flip();
262         return newBuf;
263     }
264     
265     public void reset(){
266         Arrays.fill(pointers,-1);
267         
268         buf.clear();
269         buf.flip();
270         
271     }
272
273
274
275     /* (non-Javadoc)
276      * @see com.jofti.store.IPage#getBuffer()
277      */

278     public ByteBuffer JavaDoc getBuffer() {
279         
280         return buf;
281     }
282
283
284
285     /* (non-Javadoc)
286      * @see com.jofti.store.IPage#getPointers()
287      */

288     public int[] getPointers() {
289         return pointers;
290     }
291
292
293
294     /* (non-Javadoc)
295      * @see com.jofti.store.IPage#setManager(com.jofti.core.IStoreManager)
296      */

297     public void setManager(IStoreManager manager) {
298         this.manager = manager;
299         
300     }
301     
302 }
303
304
Popular Tags