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.marshall;
022:
023: import com.db4o.*;
024: import com.db4o.internal.*;
025: import com.db4o.internal.slots.*;
026:
027: /**
028: * @exclude
029: */
030: public class ObjectMarshaller1 extends ObjectMarshaller {
031:
032: public void addFieldIndices(final ClassMetadata yc,
033: ObjectHeaderAttributes attributes,
034: final StatefulBuffer writer, final Slot oldSlot) {
035: TraverseFieldCommand command = new TraverseFieldCommand() {
036: public void processField(FieldMetadata field,
037: boolean isNull, ClassMetadata containingClass) {
038: if (isNull) {
039: field.addIndexEntry(writer.getTransaction(), writer
040: .getID(), null);
041: } else {
042: field.addFieldIndex(_family, yc, writer, oldSlot);
043: }
044: }
045: };
046: traverseFields(yc, writer, attributes, command);
047: }
048:
049: public TreeInt collectFieldIDs(TreeInt tree, ClassMetadata yc,
050: ObjectHeaderAttributes attributes,
051: final StatefulBuffer writer, final String name) {
052: final TreeInt[] ret = { tree };
053: TraverseFieldCommand command = new TraverseFieldCommand() {
054: public void processField(FieldMetadata field,
055: boolean isNull, ClassMetadata containingClass) {
056: if (isNull) {
057: return;
058: }
059: if (name.equals(field.getName())) {
060: ret[0] = field.collectIDs(_family, ret[0], writer);
061: } else {
062: field.incrementOffset(writer);
063: }
064: }
065: };
066: traverseFields(yc, writer, attributes, command);
067: return ret[0];
068: }
069:
070: public void deleteMembers(ClassMetadata yc,
071: ObjectHeaderAttributes attributes,
072: final StatefulBuffer writer, int type,
073: final boolean isUpdate) {
074: TraverseFieldCommand command = new TraverseFieldCommand() {
075: public void processField(FieldMetadata field,
076: boolean isNull, ClassMetadata containingClass) {
077: if (isNull) {
078: field.removeIndexEntry(writer.getTransaction(),
079: writer.getID(), null);
080: } else {
081: field.delete(_family, writer, isUpdate);
082: }
083: }
084: };
085: traverseFields(yc, writer, attributes, command);
086: }
087:
088: public boolean findOffset(ClassMetadata yc,
089: FieldListInfo fieldListInfo, final Buffer reader,
090: final FieldMetadata field) {
091: final boolean[] ret = { false };
092: TraverseFieldCommand command = new TraverseFieldCommand() {
093: public void processField(FieldMetadata curField,
094: boolean isNull, ClassMetadata containingClass) {
095: if (curField == field) {
096: ret[0] = !isNull;
097: cancel();
098: return;
099: }
100: if (!isNull) {
101: curField.incrementOffset(reader);
102: }
103: }
104: };
105: traverseFields(yc, reader, fieldListInfo, command);
106: return ret[0];
107: }
108:
109: public ObjectHeaderAttributes readHeaderAttributes(Buffer reader) {
110: return new ObjectHeaderAttributes(reader);
111: }
112:
113: public Object readIndexEntry(ClassMetadata clazz,
114: ObjectHeaderAttributes attributes, FieldMetadata field,
115: StatefulBuffer reader) throws FieldIndexException {
116: if (clazz == null) {
117: return null;
118: }
119:
120: if (!findOffset(clazz, attributes, reader, field)) {
121: return null;
122: }
123:
124: try {
125: return field.readIndexEntry(_family, reader);
126: } catch (CorruptionException exc) {
127: throw new FieldIndexException(exc, field);
128: }
129: }
130:
131: public void readVirtualAttributes(final Transaction trans,
132: ClassMetadata yc, final ObjectReference yo,
133: ObjectHeaderAttributes attributes, final Buffer reader) {
134: TraverseFieldCommand command = new TraverseFieldCommand() {
135: public void processField(FieldMetadata field,
136: boolean isNull, ClassMetadata containingClass) {
137: if (!isNull) {
138: field.readVirtualAttribute(trans, reader, yo);
139: }
140: }
141: };
142: traverseFields(yc, reader, attributes, command);
143: }
144:
145: protected boolean isNull(FieldListInfo fieldList, int fieldIndex) {
146: return fieldList.isNull(fieldIndex);
147: }
148:
149: public void defragFields(ClassMetadata yc, ObjectHeader header,
150: final BufferPair readers) {
151: TraverseFieldCommand command = new TraverseFieldCommand() {
152:
153: public int fieldCount(ClassMetadata yapClass, Buffer reader) {
154: return readers.readInt();
155: }
156:
157: public void processField(FieldMetadata field,
158: boolean isNull, ClassMetadata containingClass) {
159: if (!isNull) {
160: field.defragField(_family, readers);
161: }
162: }
163: };
164: traverseFields(yc, null, header._headerAttributes, command);
165: }
166:
167: public void writeObjectClassID(Buffer reader, int id) {
168: reader.writeInt(-id);
169: }
170:
171: public void skipMarshallerInfo(Buffer reader) {
172: reader.incrementOffset(1);
173: }
174:
175: }
|