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.foundation.*;
024: import com.db4o.internal.*;
025: import com.db4o.internal.btree.*;
026:
027: /**
028: * @exclude
029: */
030: public class BTreeClassIndexStrategy extends AbstractClassIndexStrategy {
031:
032: private BTree _btreeIndex;
033:
034: public BTreeClassIndexStrategy(ClassMetadata yapClass) {
035: super (yapClass);
036: }
037:
038: public BTree btree() {
039: return _btreeIndex;
040: }
041:
042: public int entryCount(Transaction ta) {
043: return _btreeIndex != null ? _btreeIndex.size(ta) : 0;
044: }
045:
046: public void initialize(ObjectContainerBase stream) {
047: createBTreeIndex(stream, 0);
048: }
049:
050: public void purge() {
051: }
052:
053: public void read(ObjectContainerBase stream, int indexID) {
054: readBTreeIndex(stream, indexID);
055: }
056:
057: public int write(Transaction trans) {
058: if (_btreeIndex == null) {
059: return 0;
060: }
061: _btreeIndex.write(trans);
062: return _btreeIndex.getID();
063: }
064:
065: public void traverseAll(Transaction ta, Visitor4 command) {
066: // better alternatives for this null check? (has been moved as is from YapFile)
067: if (_btreeIndex != null) {
068: _btreeIndex.traverseKeys(ta, command);
069: }
070: }
071:
072: private void createBTreeIndex(final ObjectContainerBase stream,
073: int btreeID) {
074: if (stream.isClient()) {
075: return;
076: }
077: _btreeIndex = ((LocalObjectContainer) stream)
078: .createBTreeClassIndex(btreeID);
079: _btreeIndex.setRemoveListener(new Visitor4() {
080: public void visit(Object obj) {
081: int id = ((Integer) obj).intValue();
082: stream.referenceSystemRegistry().removeId(id);
083: }
084: });
085: }
086:
087: private void readBTreeIndex(ObjectContainerBase stream, int indexId) {
088: if (!stream.isClient() && _btreeIndex == null) {
089: createBTreeIndex(stream, indexId);
090: }
091: }
092:
093: protected void internalAdd(Transaction trans, int id) {
094: _btreeIndex.add(trans, new Integer(id));
095: }
096:
097: protected void internalRemove(Transaction ta, int id) {
098: _btreeIndex.remove(ta, new Integer(id));
099: }
100:
101: public void dontDelete(Transaction transaction, int id) {
102: }
103:
104: public void defragReference(ClassMetadata yapClass,
105: BufferPair readers, int classIndexID) {
106: int newID = -classIndexID;
107: readers.writeInt(newID);
108: }
109:
110: public int id() {
111: return _btreeIndex.getID();
112: }
113:
114: public Iterator4 allSlotIDs(Transaction trans) {
115: return _btreeIndex.allNodeIds(trans);
116: }
117:
118: public void defragIndex(BufferPair readers) {
119: _btreeIndex.defragIndex(readers);
120: }
121:
122: public static BTree btree(ClassMetadata clazz) {
123: ClassIndexStrategy index = clazz.index();
124: if (!(index instanceof BTreeClassIndexStrategy)) {
125: throw new IllegalStateException();
126: }
127: return ((BTreeClassIndexStrategy) index).btree();
128: }
129:
130: public static Iterator4 iterate(ClassMetadata clazz,
131: Transaction trans) {
132: return btree(clazz).asRange(trans).keys();
133: }
134:
135: }
|