KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > btree > BTreeUpdate


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.btree;
22
23 /**
24  * @exclude
25  */

26 import com.db4o.Transaction;
27 import com.db4o.foundation.No4;
28
29 public abstract class BTreeUpdate extends BTreePatch {
30
31     protected BTreeUpdate _next;
32
33     public BTreeUpdate(Transaction transaction, Object JavaDoc obj) {
34         super(transaction, obj);
35     }
36
37     protected boolean hasNext() {
38         return _next != null;
39     }
40
41     public BTreePatch forTransaction(Transaction trans) {
42         if(_transaction == trans){
43             return this;
44         }
45         if(_next == null){
46             return null;
47         }
48         return _next.forTransaction(trans);
49     }
50
51     public BTreeUpdate removeFor(Transaction trans) {
52         if (_transaction == trans) {
53             return _next;
54         }
55         if (_next == null) {
56             return this;
57         }
58         return _next.removeFor(trans);
59     }
60
61     public void append(BTreeUpdate patch) {
62         if(_transaction == patch._transaction){
63             // don't allow two patches for the same transaction
64
throw new IllegalArgumentException JavaDoc();
65         }
66         if(!hasNext()){
67             _next = patch;
68         }else{
69             _next.append(patch);
70         }
71     }
72     
73     protected void applyKeyChange(Object JavaDoc obj) {
74         _object = obj;
75         if (hasNext()) {
76             _next.applyKeyChange(obj);
77         }
78     }
79
80     protected abstract void committed(BTree btree);
81
82     public Object JavaDoc commit(Transaction trans, BTree btree) {
83         final BTreeUpdate patch = (BTreeUpdate) forTransaction(trans);
84         if (patch instanceof BTreeCancelledRemoval) {
85             Object JavaDoc obj = patch.getCommittedObject();
86             applyKeyChange(obj);
87         }
88         return internalCommit(trans, btree);
89     }
90
91     protected Object JavaDoc internalCommit(Transaction trans, BTree btree) {
92         if(_transaction == trans){
93             committed(btree);
94             if (hasNext()){
95                 return _next;
96             }
97             return getCommittedObject();
98         }
99         if(hasNext()){
100             setNextIfPatch(_next.internalCommit(trans, btree));
101         }
102         return this;
103     }
104
105     private void setNextIfPatch(Object JavaDoc newNext) {
106         if(newNext instanceof BTreeUpdate){
107             _next = (BTreeUpdate)newNext;
108         } else {
109             _next = null;
110         }
111     }
112
113     protected abstract Object JavaDoc getCommittedObject();
114
115     public Object JavaDoc rollback(Transaction trans, BTree btree) {
116         if(_transaction == trans){
117             if(hasNext()){
118                 return _next;
119             }
120             return getObject();
121         }
122         if(hasNext()){
123             setNextIfPatch(_next.rollback(trans, btree));
124         }
125         return this;
126     }
127     
128     public Object JavaDoc key(Transaction trans) {
129         BTreePatch patch = forTransaction(trans);
130         if (patch == null) {
131             return getObject();
132         }
133         if (patch.isRemove()) {
134             return No4.INSTANCE;
135         }
136         return patch.getObject();
137     }
138
139 }
Popular Tags