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;
022:
023: import com.db4o.*;
024: import com.db4o.ext.*;
025: import com.db4o.foundation.*;
026: import com.db4o.internal.btree.*;
027: import com.db4o.internal.handlers.*;
028: import com.db4o.internal.marshall.*;
029: import com.db4o.internal.slots.*;
030: import com.db4o.marshall.*;
031:
032: /**
033: * @exclude
034: */
035: public class UUIDFieldMetadata extends VirtualFieldMetadata {
036:
037: private static final int LINK_LENGTH = Const4.LONG_LENGTH
038: + Const4.ID_LENGTH;
039:
040: UUIDFieldMetadata(ObjectContainerBase container) {
041: super (Handlers4.LONG_ID, new LongHandler(container));
042: setName(Const4.VIRTUAL_FIELD_PREFIX + "uuid");
043: }
044:
045: public void addFieldIndex(MarshallerFamily mf,
046: ClassMetadata yapClass, StatefulBuffer writer, Slot oldSlot)
047: throws FieldIndexException {
048:
049: boolean isnew = (oldSlot == null);
050:
051: int offset = writer._offset;
052: int db4oDatabaseIdentityID = writer.readInt();
053: long uuid = writer.readLong();
054: writer._offset = offset;
055:
056: LocalObjectContainer yf = (LocalObjectContainer) writer
057: .getStream();
058:
059: if ((uuid == 0 || db4oDatabaseIdentityID == 0)
060: && writer.getID() > 0 && !isnew) {
061: DatabaseIdentityIDAndUUID identityAndUUID = readDatabaseIdentityIDAndUUID(
062: yf, yapClass, oldSlot, false);
063: db4oDatabaseIdentityID = identityAndUUID.databaseIdentityID;
064: uuid = identityAndUUID.uuid;
065: }
066:
067: if (db4oDatabaseIdentityID == 0) {
068: db4oDatabaseIdentityID = yf.identity().getID(
069: writer.getTransaction());
070: }
071:
072: if (uuid == 0) {
073: uuid = yf.generateTimeStampId();
074: }
075:
076: writer.writeInt(db4oDatabaseIdentityID);
077: writer.writeLong(uuid);
078:
079: if (isnew) {
080: addIndexEntry(writer, new Long(uuid));
081: }
082: }
083:
084: static class DatabaseIdentityIDAndUUID {
085: public int databaseIdentityID;
086: public long uuid;
087:
088: public DatabaseIdentityIDAndUUID(int databaseIdentityID_,
089: long uuid_) {
090: databaseIdentityID = databaseIdentityID_;
091: uuid = uuid_;
092: }
093: }
094:
095: private DatabaseIdentityIDAndUUID readDatabaseIdentityIDAndUUID(
096: ObjectContainerBase stream, ClassMetadata classMetadata,
097: Slot oldSlot, boolean checkClass) throws Db4oIOException {
098: if (DTrace.enabled) {
099: DTrace.REREAD_OLD_UUID.logLength(oldSlot.address(), oldSlot
100: .length());
101: }
102: Buffer reader = stream.bufferByAddress(oldSlot.address(),
103: oldSlot.length());
104: if (checkClass) {
105: ClassMetadata realClass = ClassMetadata.readClass(stream,
106: reader);
107: if (realClass != classMetadata) {
108: return null;
109: }
110: }
111: if (classMetadata.findOffset(reader, this ) == HandlerVersion.INVALID) {
112: return null;
113: }
114: return new DatabaseIdentityIDAndUUID(reader.readInt(), reader
115: .readLong());
116: }
117:
118: public void delete(MarshallerFamily mf, StatefulBuffer a_bytes,
119: boolean isUpdate) {
120: if (isUpdate) {
121: a_bytes.incrementOffset(linkLength());
122: return;
123: }
124: a_bytes.incrementOffset(Const4.INT_LENGTH);
125: long longPart = a_bytes.readLong();
126: if (longPart > 0) {
127: ObjectContainerBase stream = a_bytes.getStream();
128: if (stream.maintainsIndices()) {
129: removeIndexEntry(a_bytes.getTransaction(), a_bytes
130: .getID(), new Long(longPart));
131: }
132: }
133: }
134:
135: public boolean hasIndex() {
136: return true;
137: }
138:
139: public BTree getIndex(Transaction transaction) {
140: ensureIndex(transaction);
141: return super .getIndex(transaction);
142: }
143:
144: protected void rebuildIndexForObject(LocalObjectContainer stream,
145: ClassMetadata yapClass, int objectId)
146: throws FieldIndexException {
147: DatabaseIdentityIDAndUUID data = readDatabaseIdentityIDAndUUID(
148: stream, yapClass, ((LocalTransaction) stream
149: .systemTransaction())
150: .getCurrentSlotOfID(objectId), true);
151: if (null == data) {
152: return;
153: }
154: addIndexEntry(stream.getLocalSystemTransaction(), objectId,
155: new Long(data.uuid));
156: }
157:
158: private void ensureIndex(Transaction transaction) {
159: if (null == transaction) {
160: throw new ArgumentNullException();
161: }
162: if (null != super .getIndex(transaction)) {
163: return;
164: }
165: LocalObjectContainer file = ((LocalObjectContainer) transaction
166: .container());
167: SystemData sd = file.systemData();
168: if (sd == null) {
169: // too early, in new file, try again later.
170: return;
171: }
172: initIndex(transaction, sd.uuidIndexId());
173: if (sd.uuidIndexId() == 0) {
174: sd.uuidIndexId(super .getIndex(transaction).getID());
175: file.getFileHeader().writeVariablePart(file, 1);
176: }
177: }
178:
179: void instantiate1(Transaction a_trans, ObjectReference a_yapObject,
180: Buffer a_bytes) {
181: int dbID = a_bytes.readInt();
182: ObjectContainerBase stream = a_trans.container();
183: stream.showInternalClasses(true);
184: try {
185: Db4oDatabase db = (Db4oDatabase) stream.getByID2(a_trans,
186: dbID);
187: if (db != null && db.i_signature == null) {
188: stream.activate(a_trans, db, 2);
189: }
190: VirtualAttributes va = a_yapObject.virtualAttributes();
191: va.i_database = db;
192: va.i_uuid = a_bytes.readLong();
193: } finally {
194: stream.showInternalClasses(false);
195: }
196: }
197:
198: protected int linkLength() {
199: return LINK_LENGTH;
200: }
201:
202: void marshall(Transaction trans, ObjectReference ref,
203: WriteBuffer buffer, boolean isMigrating, boolean isNew) {
204: VirtualAttributes attr = ref.virtualAttributes();
205: ObjectContainerBase container = trans.container();
206: boolean doAddIndexEntry = isNew && container.maintainsIndices();
207: int dbID = 0;
208: boolean linkToDatabase = (attr != null && attr.i_database == null) ? true
209: : !isMigrating;
210: if (linkToDatabase) {
211: Db4oDatabase db = ((InternalObjectContainer) container)
212: .identity();
213: if (db == null) {
214: // can happen on early classes like Metaxxx, no problem
215: attr = null;
216: } else {
217: if (attr.i_database == null) {
218: attr.i_database = db;
219:
220: // TODO: Should be check for ! client instead of instanceof
221: if (container instanceof LocalObjectContainer) {
222: attr.i_uuid = container.generateTimeStampId();
223: doAddIndexEntry = true;
224: }
225: }
226: db = attr.i_database;
227: if (db != null) {
228: dbID = db.getID(trans);
229: }
230: }
231: } else {
232: if (attr != null) {
233: dbID = attr.i_database.getID(trans);
234: }
235: }
236: buffer.writeInt(dbID);
237: if (attr == null) {
238: buffer.writeLong(0);
239: return;
240: }
241: buffer.writeLong(attr.i_uuid);
242: if (doAddIndexEntry) {
243: addIndexEntry(trans, ref.getID(), new Long(attr.i_uuid));
244: }
245: }
246:
247: void marshallIgnore(WriteBuffer buffer) {
248: buffer.writeInt(0);
249: buffer.writeLong(0);
250: }
251:
252: public final HardObjectReference getHardObjectReferenceBySignature(
253: final Transaction transaction, final long longPart,
254: final byte[] signature) {
255: final BTreeRange range = search(transaction, new Long(longPart));
256: final Iterator4 keys = range.keys();
257: while (keys.moveNext()) {
258: final FieldIndexKey current = (FieldIndexKey) keys
259: .current();
260: final HardObjectReference hardRef = getHardObjectReferenceById(
261: transaction, current.parentID(), signature);
262: if (null != hardRef) {
263: return hardRef;
264: }
265: }
266: return HardObjectReference.INVALID;
267: }
268:
269: protected final HardObjectReference getHardObjectReferenceById(
270: Transaction transaction, int parentId, byte[] signature) {
271: HardObjectReference hardRef = transaction.container()
272: .getHardObjectReferenceById(transaction, parentId);
273: if (hardRef._reference == null) {
274: return null;
275: }
276: VirtualAttributes vad = hardRef._reference
277: .virtualAttributes(transaction);
278: if (!Arrays4.areEqual(signature, vad.i_database.i_signature)) {
279: return null;
280: }
281: return hardRef;
282: }
283:
284: public void defragField(MarshallerFamily mf, BufferPair readers) {
285: // database id
286: readers.copyID();
287: // uuid
288: readers.incrementOffset(Const4.LONG_LENGTH);
289: }
290: }
|