KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > freespace > FreeSlotNode


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.freespace;
22
23 import com.db4o.*;
24 import com.db4o.foundation.Tree;
25
26 /**
27  * @exclude
28  */

29 public final class FreeSlotNode extends TreeInt {
30     public static int sizeLimit;
31
32     FreeSlotNode _peer;
33
34     FreeSlotNode(int a_key) {
35         super(a_key);
36     }
37
38     public Object JavaDoc shallowClone() {
39         FreeSlotNode frslot = new FreeSlotNode(_key);
40         frslot._peer = _peer;
41         return super.shallowCloneInternal(frslot);
42     }
43
44     final void createPeer(int a_key) {
45         _peer = new FreeSlotNode(a_key);
46         _peer._peer = this;
47     }
48
49     public boolean duplicates() {
50         return true;
51     }
52
53     public final int ownLength() {
54         return YapConst.INT_LENGTH * 2;
55     }
56
57     final static Tree removeGreaterOrEqual(FreeSlotNode a_in,
58             TreeIntObject a_finder) {
59         if (a_in == null) {
60             return null;
61         }
62         int cmp = a_in._key - a_finder._key;
63         if (cmp == 0) {
64             a_finder._object = a_in; // the highest node in the hierarchy !!!
65
return a_in.remove();
66         }
67         if (cmp > 0) {
68             a_in._preceding = removeGreaterOrEqual(
69                     (FreeSlotNode) a_in._preceding, a_finder);
70             if (a_finder._object != null) {
71                 a_in._size--;
72                 return a_in;
73             }
74             a_finder._object = a_in;
75             return a_in.remove();
76         }
77         a_in._subsequent = removeGreaterOrEqual(
78                 (FreeSlotNode) a_in._subsequent, a_finder);
79         if (a_finder._object != null) {
80             a_in._size--;
81         }
82         return a_in;
83     }
84
85     public Object JavaDoc read(YapReader a_reader) {
86         int size = a_reader.readInt();
87         int address = a_reader.readInt();
88         if (size > sizeLimit) {
89             FreeSlotNode node = new FreeSlotNode(size);
90             node.createPeer(address);
91             if (Deploy.debug) {
92                 if (a_reader instanceof YapWriter) {
93                     Transaction trans = ((YapWriter) a_reader).getTransaction();
94                     if (trans.stream() instanceof YapRandomAccessFile) {
95                         YapWriter checker = trans.stream().getWriter(trans,
96                                 node._peer._key, node._key);
97                         checker.read();
98                         for (int i = 0; i < node._key; i++) {
99                             if (checker.readByte() != (byte) 'X') {
100                                 System.out
101                                         .println("!!! Free space corruption at:"
102                                                 + node._peer._key);
103                                 break;
104                             }
105                         }
106                     }
107                 }
108             }
109             return node;
110         }
111         return null;
112     }
113
114     public final void write(YapReader a_writer) {
115         // byte order: size, address
116
a_writer.writeInt(_key);
117         a_writer.writeInt(_peer._key);
118     }
119
120     // public static final void debug(FreeSlotNode a_node){
121
// if(a_node == null){
122
// return;
123
// }
124
// System.out.println("Address:" + a_node.i_key);
125
// System.out.println("Length:" + a_node.i_peer.i_key);
126
// debug((FreeSlotNode)a_node.i_preceding);
127
// debug((FreeSlotNode)a_node.i_subsequent);
128
// }
129

130     public String JavaDoc toString() {
131         if (!Debug.freespace) {
132             return super.toString();
133
134         }
135         String JavaDoc str = "FreeSlotNode " + _key;
136         if (_peer != null) {
137             str += " peer: " + _peer._key;
138         }
139         return str;
140     }
141 }
142
Popular Tags