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.btree;
022:
023: import com.db4o.foundation.*;
024: import com.db4o.internal.*;
025: import com.db4o.internal.handlers.*;
026:
027: /**
028: * @exclude
029: */
030: public class FieldIndexKeyHandler implements Indexable4 {
031:
032: private final Indexable4 _valueHandler;
033:
034: private final IntHandler _parentIdHandler;
035:
036: public FieldIndexKeyHandler(ObjectContainerBase stream,
037: Indexable4 delegate_) {
038: _parentIdHandler = new IDHandler(stream);
039: _valueHandler = delegate_;
040: }
041:
042: public int linkLength() {
043: return _valueHandler.linkLength() + Const4.INT_LENGTH;
044: }
045:
046: public Object readIndexEntry(Buffer a_reader) {
047: // TODO: could read int directly here with a_reader.readInt()
048: int parentID = readParentID(a_reader);
049: Object objPart = _valueHandler.readIndexEntry(a_reader);
050: if (parentID < 0) {
051: objPart = null;
052: parentID = -parentID;
053: }
054: return new FieldIndexKey(parentID, objPart);
055: }
056:
057: private int readParentID(Buffer a_reader) {
058: return ((Integer) _parentIdHandler.readIndexEntry(a_reader))
059: .intValue();
060: }
061:
062: public void writeIndexEntry(Buffer writer, Object obj) {
063: FieldIndexKey composite = (FieldIndexKey) obj;
064: int parentID = composite.parentID();
065: Object value = composite.value();
066: if (value == null) {
067: parentID = -parentID;
068: }
069: _parentIdHandler.write(parentID, writer);
070: _valueHandler.writeIndexEntry(writer, composite.value());
071: }
072:
073: public Indexable4 valueHandler() {
074: return _valueHandler;
075: }
076:
077: public Comparable4 prepareComparison(Object obj) {
078: FieldIndexKey composite = (FieldIndexKey) obj;
079: _valueHandler.prepareComparison(composite.value());
080: _parentIdHandler.prepareComparison(composite.parentID());
081: return this ;
082: }
083:
084: public int compareTo(Object obj) {
085: if (null == obj) {
086: throw new ArgumentNullException();
087: }
088: FieldIndexKey composite = (FieldIndexKey) obj;
089: try {
090: int delegateResult = _valueHandler.compareTo(composite
091: .value());
092: if (delegateResult != 0) {
093: return delegateResult;
094: }
095: } catch (IllegalComparisonException ex) {
096: // can happen, is expected
097: }
098: return _parentIdHandler.compareTo(composite.parentID());
099: }
100:
101: public void defragIndexEntry(BufferPair readers) {
102: _parentIdHandler.defragIndexEntry(readers);
103: _valueHandler.defragIndexEntry(readers);
104: }
105: }
|