KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > cs > TransactionClient


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.cs;
22
23 import com.db4o.*;
24 import com.db4o.cs.messages.*;
25 import com.db4o.foundation.*;
26
27 final class TransactionClient extends Transaction {
28
29     private final YapClient i_client;
30     
31     private Tree i_yapObjectsToGc;
32     
33     TransactionClient(YapClient a_stream, Transaction a_parent) {
34         super(a_stream, a_parent);
35         i_client = a_stream;
36     }
37     
38     public void commit() {
39         commitTransactionListeners();
40         clearAll();
41         i_client.writeMsg(Msg.COMMIT);
42         
43     }
44     
45     protected void clearAll() {
46         removeYapObjectReferences();
47         super.clearAll();
48     }
49
50     private void removeYapObjectReferences() {
51         if(i_yapObjectsToGc != null){
52             i_yapObjectsToGc.traverse(new Visitor4() {
53                 public void visit(Object JavaDoc a_object) {
54                     YapObject yo = (YapObject)((TreeIntObject) a_object)._object;
55                     stream().removeReference(yo);
56                 }
57             });
58         }
59         i_yapObjectsToGc = null;
60     }
61
62     public boolean delete(YapObject ref, int id, int cascade) {
63         if (! super.delete(ref, id, cascade)){
64             return false;
65         }
66         i_client.writeMsg(Msg.TA_DELETE.getWriterForInts(this, new int[] {id, cascade}));
67         return true;
68     }
69
70     public boolean isDeleted(int a_id) {
71
72         // This one really is a hack.
73
// It only helps to get information about the current
74
// transaction.
75

76         // We need a better strategy for C/S concurrency behaviour.
77

78         i_client.writeMsg(Msg.TA_IS_DELETED.getWriterForInt(this, a_id));
79         int res = i_client.expectedByteResponse(Msg.TA_IS_DELETED).readInt();
80         return res == 1;
81     }
82     
83     public Object JavaDoc[] objectAndYapObjectBySignature(final long a_uuid, final byte[] a_signature) {
84         int messageLength = YapConst.LONG_LENGTH + YapConst.INT_LENGTH + a_signature.length;
85         MsgD message = Msg.OBJECT_BY_UUID.getWriterForLength(this, messageLength);
86         message.writeLong(a_uuid);
87         message.writeBytes(a_signature);
88         i_client.writeMsg(message);
89         message = (MsgD)i_client.expectedResponse(Msg.OBJECT_BY_UUID);
90         int id = message.readInt();
91         if(id > 0){
92             return stream().getObjectAndYapObjectByID(this, id);
93         }
94         return new Object JavaDoc[2];
95     }
96     
97     
98     public void processDeletes() {
99         if (i_delete != null) {
100             i_delete.traverse(new Visitor4() {
101                 public void visit(Object JavaDoc a_object) {
102                     DeleteInfo info = (DeleteInfo) a_object;
103                     if (info._reference != null) {
104                         i_yapObjectsToGc = Tree.add(i_yapObjectsToGc, new TreeIntObject(info._key, info._reference));
105                     }
106                 }
107             });
108         }
109         i_delete = null;
110         i_writtenUpdateDeletedMembers = null;
111         i_client.writeMsg(Msg.PROCESS_DELETES);
112     }
113     
114     public void rollback() {
115         i_yapObjectsToGc = null;
116         rollBackTransactionListeners();
117         clearAll();
118     }
119
120     public void writeUpdateDeleteMembers(int a_id, YapClass a_yc, int a_type,
121         int a_cascade) {
122         i_client.writeMsg(Msg.WRITE_UPDATE_DELETE_MEMBERS.getWriterForInts(this,
123             new int[]{
124             a_id,
125             a_yc.getID(),
126             a_type,
127             a_cascade
128         }));
129     }
130
131     public void setPointer(int a_id, int a_address, int a_length) {
132     }
133 }
Popular Tags