001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.freespace;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.Tree;
025: import com.db4o.internal.*;
026:
027: /**
028: * @exclude
029: */
030: public final class FreeSlotNode extends TreeInt {
031: static int sizeLimit;
032:
033: FreeSlotNode _peer;
034:
035: FreeSlotNode(int a_key) {
036: super (a_key);
037: }
038:
039: public Object shallowClone() {
040: FreeSlotNode frslot = new FreeSlotNode(_key);
041: frslot._peer = _peer;
042: return super .shallowCloneInternal(frslot);
043: }
044:
045: final void createPeer(int a_key) {
046: _peer = new FreeSlotNode(a_key);
047: _peer._peer = this ;
048: }
049:
050: public boolean duplicates() {
051: return true;
052: }
053:
054: public final int ownLength() {
055: return Const4.INT_LENGTH * 2;
056: }
057:
058: final static Tree removeGreaterOrEqual(FreeSlotNode a_in,
059: TreeIntObject a_finder) {
060: if (a_in == null) {
061: return null;
062: }
063: int cmp = a_in._key - a_finder._key;
064: if (cmp == 0) {
065: a_finder._object = a_in; // the highest node in the hierarchy !!!
066: return a_in.remove();
067: }
068: if (cmp > 0) {
069: a_in._preceding = removeGreaterOrEqual(
070: (FreeSlotNode) a_in._preceding, a_finder);
071: if (a_finder._object != null) {
072: a_in._size--;
073: return a_in;
074: }
075: a_finder._object = a_in;
076: return a_in.remove();
077: }
078: a_in._subsequent = removeGreaterOrEqual(
079: (FreeSlotNode) a_in._subsequent, a_finder);
080: if (a_finder._object != null) {
081: a_in._size--;
082: }
083: return a_in;
084: }
085:
086: public Object read(Buffer buffer) {
087: int size = buffer.readInt();
088: int address = buffer.readInt();
089: if (size > sizeLimit) {
090: FreeSlotNode node = new FreeSlotNode(size);
091: node.createPeer(address);
092: if (Deploy.debug && Debug.xbytes) {
093: debugCheckBuffer(buffer, node);
094: }
095: return node;
096: }
097: return null;
098: }
099:
100: private void debugCheckBuffer(Buffer buffer, FreeSlotNode node) {
101: if (!(buffer instanceof StatefulBuffer)) {
102: return;
103: }
104: Transaction trans = ((StatefulBuffer) buffer).getTransaction();
105: if (!(trans.container() instanceof IoAdaptedObjectContainer)) {
106: return;
107: }
108: StatefulBuffer checker = trans.container().getWriter(trans,
109: node._peer._key, node._key);
110: checker.read();
111: for (int i = 0; i < node._key; i++) {
112: if (checker.readByte() != (byte) 'X') {
113: System.out.println("!!! Free space corruption at:"
114: + node._peer._key);
115: break;
116: }
117: }
118: }
119:
120: public final void write(Buffer a_writer) {
121: // byte order: size, address
122: a_writer.writeInt(_key);
123: a_writer.writeInt(_peer._key);
124: }
125:
126: // public static final void debug(FreeSlotNode a_node){
127: // if(a_node == null){
128: // return;
129: // }
130: // System.out.println("Address:" + a_node.i_key);
131: // System.out.println("Length:" + a_node.i_peer.i_key);
132: // debug((FreeSlotNode)a_node.i_preceding);
133: // debug((FreeSlotNode)a_node.i_subsequent);
134: // }
135:
136: public String toString() {
137: if (!Debug.freespace) {
138: return super .toString();
139:
140: }
141: String str = "FreeSlotNode " + _key;
142: if (_peer != null) {
143: str += " peer: " + _peer._key;
144: }
145: return str;
146: }
147: }
|