KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > slots > SlotChange


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

28 public class SlotChange extends TreeInt {
29
30     private int _action;
31
32     private Slot _newSlot;
33
34     private ReferencedSlot _shared;
35
36     private static final int FREE_ON_COMMIT_BIT = 1;
37
38     private static final int FREE_ON_ROLLBACK_BIT = 2;
39
40     private static final int SET_POINTER_BIT = 3;
41
42     public SlotChange(int id) {
43         super(id);
44     }
45
46     public Object JavaDoc shallowClone() {
47         SlotChange sc = new SlotChange(0);
48         sc._action = _action;
49         sc._newSlot = _newSlot;
50         sc._shared = _shared;
51         return super.shallowCloneInternal(sc);
52     }
53
54     private final void doFreeOnCommit() {
55         setBit(FREE_ON_COMMIT_BIT);
56     }
57
58     private final void doFreeOnRollback() {
59         setBit(FREE_ON_ROLLBACK_BIT);
60     }
61
62     private final void doSetPointer() {
63         setBit(SET_POINTER_BIT);
64     }
65
66     public void freeDuringCommit(YapFile file) {
67         if (isFreeOnCommit()) {
68             file.freeDuringCommit(_shared, _newSlot);
69         }
70     }
71
72     public void freeOnCommit(YapFile file, Slot slot) {
73
74         if (_shared != null) {
75
76             // second call or later.
77
// The object has already been rewritten once, so we can free
78
// directly
79

80             file.free(slot);
81             return;
82         }
83
84         doFreeOnCommit();
85
86         ReferencedSlot refSlot = file.produceFreeOnCommitEntry(_key);
87
88         if (refSlot.addReferenceIsFirst()) {
89             refSlot.pointTo(slot);
90         }
91
92         _shared = refSlot;
93     }
94
95     public void freeOnRollback(int address, int length) {
96         doFreeOnRollback();
97         _newSlot = new Slot(address, length);
98     }
99
100     public void freeOnRollbackSetPointer(int address, int length) {
101         doSetPointer();
102         freeOnRollback(address, length);
103     }
104
105     private final boolean isBitSet(int bitPos) {
106         return (_action | (1 << bitPos)) == _action;
107     }
108
109     public boolean isDeleted() {
110         return isSetPointer() && (_newSlot._address == 0);
111     }
112
113     private final boolean isFreeOnCommit() {
114         return isBitSet(FREE_ON_COMMIT_BIT);
115     }
116
117     private final boolean isFreeOnRollback() {
118         return isBitSet(FREE_ON_ROLLBACK_BIT);
119     }
120
121     public final boolean isSetPointer() {
122         return isBitSet(SET_POINTER_BIT);
123     }
124
125     public Slot newSlot() {
126         return _newSlot;
127     }
128     
129     public Slot oldSlot() {
130         if(_shared == null){
131             return null;
132         }
133         return _shared.slot();
134     }
135
136     public Object JavaDoc read(YapReader reader) {
137         SlotChange change = new SlotChange(reader.readInt());
138         change._newSlot = new Slot(reader.readInt(), reader.readInt());
139         change.doSetPointer();
140         return change;
141     }
142
143     public void rollback(YapFile yapFile) {
144         if (_shared != null) {
145             yapFile.reduceFreeOnCommitReferences(_shared);
146         }
147         if (isFreeOnRollback()) {
148             yapFile.free(_newSlot);
149         }
150     }
151
152     private final void setBit(int bitPos) {
153         _action |= (1 << bitPos);
154     }
155
156     public void setPointer(int address, int length) {
157         doSetPointer();
158         _newSlot = new Slot(address, length);
159     }
160
161     public void write(YapReader writer) {
162         if (isSetPointer()) {
163             writer.writeInt(_key);
164             writer.writeInt(_newSlot._address);
165             writer.writeInt(_newSlot._length);
166         }
167     }
168
169     public void writePointer(Transaction trans) {
170         if (isSetPointer()) {
171             trans.writePointer(_key, _newSlot._address, _newSlot._length);
172         }
173     }
174 }
175
Popular Tags