KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > marshall > FieldMarshaller1


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.inside.marshall;
22
23 import com.db4o.*;
24 import com.db4o.inside.btree.*;
25
26 /**
27  * @exclude
28  */

29 public class FieldMarshaller1 extends FieldMarshaller0 {
30     
31     private boolean hasBTreeIndex(YapField field){
32         return ! field.isVirtual();
33     }
34
35     public void write(Transaction trans, YapClass clazz, YapField field, YapReader writer) {
36         super.write(trans, clazz, field, writer);
37         if(! hasBTreeIndex(field)){
38             return;
39         }
40         writer.writeIDOf(trans, field.getIndex(trans));
41     }
42
43     public RawFieldSpec readSpec(YapStream stream, YapReader reader) {
44         RawFieldSpec spec=super.readSpec(stream, reader);
45         if(spec==null) {
46             return null;
47         }
48         if (spec.isVirtual()) {
49             return spec;
50         }
51         int indexID = reader.readInt();
52         spec.indexID(indexID);
53         return spec;
54     }
55     
56     protected YapField fromSpec(RawFieldSpec spec, YapStream stream, YapField field) {
57         YapField actualField = super.fromSpec(spec, stream, field);
58         if(spec==null) {
59             return field;
60         }
61         if (spec.indexID() != 0) {
62             actualField.initIndex(stream.getSystemTransaction(), spec.indexID());
63         }
64         return actualField;
65     }
66     
67     public int marshalledLength(YapStream stream, YapField field) {
68         int len = super.marshalledLength(stream, field);
69         if(! hasBTreeIndex(field)){
70             return len;
71         }
72         final int BTREE_ID = YapConst.ID_LENGTH;
73         return len + BTREE_ID;
74     }
75
76     public void defrag(YapClass yapClass, YapField yapField, YapStringIO sio,
77             final ReaderPair readers)
78             throws CorruptionException {
79         super.defrag(yapClass, yapField, sio, readers);
80         if(yapField.isVirtual()) {
81             return;
82         }
83         if(yapField.hasIndex()) {
84             BTree index = yapField.getIndex(readers.systemTrans());
85             int targetIndexID=readers.copyID();
86             if(targetIndexID!=0) {
87                 index.defragBTree(readers.context());
88             }
89         }
90         else {
91             readers.writeInt(0);
92         }
93     }
94 }
95
Popular Tags