KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > ext > Db4oDatabase


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.ext;
22
23 import com.db4o.*;
24 import com.db4o.foundation.*;
25 import com.db4o.query.*;
26 import com.db4o.types.*;
27
28
29 /**
30  * Class to identify a database by it's signature.
31  * <br><br>db4o UUID handling uses a reference to the Db4oDatabase object, that
32  * represents the database an object was created on.
33  *
34  * @persistent
35  * @exclude
36  */

37 public class Db4oDatabase implements Db4oType, Internal4{
38     
39
40     /**
41      * Field is public for implementation reasons, DO NOT TOUCH!
42      */

43     public byte[] i_signature;
44     
45     /**
46      * Field is public for implementation reasons, DO NOT TOUCH!
47      *
48      * This field is badly named, it really is the creation time.
49      */

50     // TODO: change to _creationTime with PersistentFormatUpdater
51
public long i_uuid;
52     
53     private static final String JavaDoc CREATIONTIME_FIELD = "i_uuid";
54
55     
56     /**
57      * cached ObjectContainer for getting the own ID.
58      */

59     private transient YapStream i_stream;
60     
61     /**
62      * cached ID, only valid in combination with i_objectContainer
63      */

64     private transient int i_id;
65     
66     /**
67      * constructor for persistence
68      */

69     public Db4oDatabase(){
70     }
71     
72     /**
73      * constructor for comparison and to store new ones
74      */

75     public Db4oDatabase(byte[] signature, long creationTime){
76         // FIXME: make sure signature is null
77
i_signature = signature;
78         i_uuid = creationTime;
79     }
80     
81     /**
82      * generates a new Db4oDatabase object with a unique signature.
83      */

84     public static Db4oDatabase generate() {
85         return new Db4oDatabase(
86                 Unobfuscated.generateSignature(),
87                 System.currentTimeMillis());
88     }
89     
90     /**
91      * comparison by signature.
92      */

93     public boolean equals(Object JavaDoc obj) {
94         if(obj==this) {
95             return true;
96         }
97         if(obj==null||this.getClass()!=obj.getClass()) {
98             return false;
99         }
100         Db4oDatabase other = (Db4oDatabase)obj;
101         if (null == other.i_signature || null == this.i_signature) {
102             return false;
103         }
104         return Arrays4.areEqual(other.i_signature, this.i_signature);
105     }
106
107     /**
108      * gets the db4o ID, and may cache it for performance reasons.
109      *
110      * @return the db4o ID for the ObjectContainer
111      */

112     public int getID(Transaction trans) {
113         YapStream stream = trans.stream();
114         if(stream != i_stream) {
115             i_stream = stream;
116             i_id = bind(trans);
117         }
118         return i_id;
119     }
120     
121     public long getCreationTime(){
122         return i_uuid;
123     }
124     
125     /**
126      * returns the unique signature
127      */

128     public byte[] getSignature(){
129         return i_signature;
130     }
131     
132     public String JavaDoc toString(){
133         return "db " + i_signature;
134     }
135     
136     public boolean isOlderThan(Db4oDatabase peer){
137         
138         if(peer == this)
139             throw new IllegalArgumentException JavaDoc();
140         
141         if(i_uuid != peer.i_uuid){
142             return i_uuid < peer.i_uuid;
143         }
144         
145         // the above logic has failed, both are the same
146
// age but we still want to distinguish in some
147
// way, to have an order in the ReplicationRecord
148

149         // The following is arbitrary, it only needs to
150
// be repeatable.
151

152         // Let's distinguish by signature length
153

154         if(i_signature.length != peer.i_signature.length ){
155             return i_signature.length < peer.i_signature.length;
156         }
157         
158         for (int i = 0; i < i_signature.length; i++) {
159             if(i_signature[i] != peer.i_signature[i]){
160                 return i_signature[i] < peer.i_signature[i];
161             }
162         }
163         
164         // This should never happen.
165

166         // FIXME: Add a message and move to Messages.
167
//
168
throw new RuntimeException JavaDoc();
169     }
170     
171     /**
172      * make sure this Db4oDatabase is stored. Return the ID.
173      */

174     public int bind(Transaction trans){
175         YapStream stream = trans.stream();
176         Db4oDatabase stored = (Db4oDatabase)stream.db4oTypeStored(trans,this);
177         if (stored == null) {
178             stream.showInternalClasses(true);
179             stream.set3(trans,this, 2, false);
180             int newID = stream.getID1(this);
181             stream.showInternalClasses(false);
182             return newID;
183         }
184         if(stored == this){
185             return stream.getID1(this);
186         }
187         if(i_uuid == 0){
188             i_uuid = stored.i_uuid;
189         }
190         stream.showInternalClasses(true);
191         int id = stream.getID1(stored);
192         stream.bind(this, id);
193         stream.showInternalClasses(false);
194         return id;
195     }
196     
197     /**
198      * find a Db4oDatabase with the same signature as this one
199      */

200     public Db4oDatabase query(Transaction trans){
201         // showInternalClasses(true); has to be set for this method to be successful
202
if(i_uuid > 0){
203             // try fast query over uuid (creation time) first
204
Db4oDatabase res = query(trans, true);
205             if(res != null){
206                 return res;
207             }
208         }
209         // if not found, try to find with signature
210
return query(trans, false);
211     }
212     
213     private Db4oDatabase query(Transaction trans, boolean constrainByUUID){
214         YapStream stream = trans.stream();
215         Query q = stream.query(trans);
216         q.constrain(getClass());
217         if(constrainByUUID){
218             q.descend(CREATIONTIME_FIELD).constrain(new Long JavaDoc(i_uuid));
219         }
220         ObjectSet objectSet = q.execute();
221         while (objectSet.hasNext()) {
222             Db4oDatabase storedDatabase = (Db4oDatabase) objectSet.next();
223             stream.activate1(null, storedDatabase, 4);
224             if (storedDatabase.equals(this)) {
225                 return storedDatabase;
226             }
227         }
228         return null;
229     }
230     
231     
232 }
Popular Tags