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.classindex;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.*;
026: import com.db4o.internal.slots.*;
027:
028: /**
029: * representation to collect and hold all IDs of one class
030: */
031: public class ClassIndex extends PersistentBase implements ReadWriteable {
032:
033: private final ClassMetadata _clazz;
034:
035: /**
036: * contains TreeInt with object IDs
037: */
038: private TreeInt i_root;
039:
040: ClassIndex(ClassMetadata yapClass) {
041: _clazz = yapClass;
042: }
043:
044: public void add(int a_id) {
045: i_root = TreeInt.add(i_root, a_id);
046: }
047:
048: public final int marshalledLength() {
049: return Const4.INT_LENGTH * (Tree.size(i_root) + 1);
050: }
051:
052: public final void clear() {
053: i_root = null;
054: }
055:
056: void ensureActive(Transaction trans) {
057: if (!isActive()) {
058: setStateDirty();
059: read(trans);
060: }
061: }
062:
063: int entryCount(Transaction ta) {
064: if (isActive() || isNew()) {
065: return Tree.size(i_root);
066: }
067: Slot slot = ((LocalTransaction) ta).getCurrentSlotOfID(getID());
068: int length = Const4.INT_LENGTH;
069: if (Deploy.debug) {
070: length += Const4.LEADING_LENGTH;
071: }
072: Buffer reader = new Buffer(length);
073: reader.readEncrypt(ta.container(), slot.address());
074: if (Deploy.debug) {
075: reader.readBegin(getIdentifier());
076: }
077: return reader.readInt();
078: }
079:
080: public final byte getIdentifier() {
081: return Const4.YAPINDEX;
082: }
083:
084: TreeInt getRoot() {
085: return i_root;
086: }
087:
088: public final int ownLength() {
089: return Const4.OBJECT_LENGTH + marshalledLength();
090: }
091:
092: public final Object read(Buffer a_reader) {
093: throw Exceptions4.virtualException();
094: }
095:
096: public final void readThis(Transaction a_trans, Buffer a_reader) {
097: i_root = (TreeInt) new TreeReader(a_reader, new TreeInt(0))
098: .read();
099: }
100:
101: public void remove(int a_id) {
102: i_root = TreeInt.removeLike(i_root, a_id);
103: }
104:
105: void setDirty(ObjectContainerBase a_stream) {
106: // TODO: get rid of the setDirty call
107: a_stream.setDirtyInSystemTransaction(this );
108: }
109:
110: public void write(Buffer a_writer) {
111: writeThis(null, a_writer);
112: }
113:
114: public final void writeThis(Transaction trans, final Buffer a_writer) {
115: TreeInt.write(a_writer, i_root);
116: }
117:
118: public String toString() {
119: if (!Debug4.prettyToStrings) {
120: return super .toString();
121: }
122: return _clazz + " index";
123: }
124: }
|