KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > ReaderPair


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;
22
23 import com.db4o.foundation.*;
24 import com.db4o.inside.mapping.*;
25 import com.db4o.inside.marshall.*;
26
27 /**
28  * @exclude
29  */

30 public final class ReaderPair implements SlotReader {
31     private YapReader _source;
32     private YapReader _target;
33     private DefragContext _mapping;
34     private Transaction _systemTrans;
35     
36     public ReaderPair(YapReader source,DefragContext mapping,Transaction systemTrans) {
37         _source = source;
38         _mapping=mapping;
39         _target = new YapReader(source.getLength());
40         _source.copyTo(_target, 0, 0, _source.getLength());
41         _systemTrans=systemTrans;
42     }
43     
44     public int offset() {
45         return _source.offset();
46     }
47
48     public void offset(int offset) {
49         _source.offset(offset);
50         _target.offset(offset);
51     }
52
53     public void incrementOffset(int numBytes) {
54         _source.incrementOffset(numBytes);
55         _target.incrementOffset(numBytes);
56     }
57
58     public void incrementIntSize() {
59         incrementOffset(YapConst.INT_LENGTH);
60     }
61
62     public int copyUnindexedID() {
63         int orig=_source.readInt();
64         int mapped=-1;
65         try {
66             mapped=_mapping.mappedID(orig);
67         } catch (MappingNotFoundException exc) {
68             mapped=_mapping.allocateTargetSlot(YapConst.POINTER_LENGTH);
69             _mapping.mapIDs(orig,mapped, false);
70             _mapping.registerUnindexed(orig);
71         }
72         _target.writeInt(mapped);
73         return mapped;
74     }
75
76     public int copyID() {
77         // This code is slightly redundant.
78
// The profiler shows it's a hotspot.
79
// The following would be non-redudant.
80
// return copy(false, false);
81

82         int mapped=_mapping.mappedID(_source.readInt(),false);
83         _target.writeInt(mapped);
84         return mapped;
85     }
86
87     public int copyID(boolean flipNegative,boolean lenient) {
88         int id=_source.readInt();
89         return internalCopyID(flipNegative, lenient, id);
90     }
91
92     public MappedIDPair copyIDAndRetrieveMapping() {
93         int id=_source.readInt();
94         return new MappedIDPair(id,internalCopyID(false, false, id));
95     }
96
97     private int internalCopyID(boolean flipNegative, boolean lenient, int id) {
98         if(flipNegative&&id<0) {
99             id=-id;
100         }
101         int mapped=_mapping.mappedID(id,lenient);
102         if(flipNegative&&id<0) {
103             mapped=-mapped;
104         }
105         _target.writeInt(mapped);
106         return mapped;
107     }
108     
109     public void readBegin(byte identifier) {
110         _source.readBegin(identifier);
111         _target.readBegin(identifier);
112     }
113     
114     public byte readByte() {
115         byte value=_source.readByte();
116         _target.incrementOffset(1);
117         return value;
118     }
119
120     public int readInt() {
121         int value=_source.readInt();
122         _target.incrementOffset(YapConst.INT_LENGTH);
123         return value;
124     }
125
126     public void writeInt(int value) {
127         _source.incrementOffset(YapConst.INT_LENGTH);
128         _target.writeInt(value);
129     }
130     
131     public void write(YapFile file,int address) {
132         file.writeBytes(_target,address,0);
133     }
134     
135     public String JavaDoc readShortString(YapStringIO sio) throws CorruptionException {
136         String JavaDoc value=StringMarshaller.readShort(sio,false,_source);
137         StringMarshaller.readShort(sio,false,_target);
138         return value;
139     }
140     
141     public YapReader source() {
142         return _source;
143     }
144
145     public YapReader target() {
146         return _target;
147     }
148     
149     public IDMapping mapping() {
150         return _mapping;
151     }
152
153     public Transaction systemTrans() {
154         return _systemTrans;
155     }
156
157     public DefragContext context() {
158         return _mapping;
159     }
160
161     public static void processCopy(DefragContext context, int sourceID,SlotCopyHandler command) throws CorruptionException {
162         processCopy(context, sourceID, command, false);
163     }
164
165     public static void processCopy(DefragContext context, int sourceID,SlotCopyHandler command,boolean registerAddressMapping) throws CorruptionException {
166         YapReader sourceReader=(
167                 registerAddressMapping
168                     ? context.sourceWriterByID(sourceID)
169                     : context.sourceReaderByID(sourceID));
170         int targetID=context.mappedID(sourceID);
171     
172         int targetLength = sourceReader.getLength();
173         int targetAddress=context.allocateTargetSlot(targetLength);
174         
175         if(registerAddressMapping) {
176             int sourceAddress=((YapWriter)sourceReader).getAddress();
177             context.mapIDs(sourceAddress, targetAddress, false);
178         }
179         
180         YapReader targetPointerReader=new YapReader(YapConst.POINTER_LENGTH);
181         if(Deploy.debug) {
182             targetPointerReader.writeBegin(YapConst.YAPPOINTER);
183         }
184         targetPointerReader.writeInt(targetAddress);
185         targetPointerReader.writeInt(targetLength);
186         if(Deploy.debug) {
187             targetPointerReader.writeEnd();
188         }
189         context.targetWriteBytes(targetPointerReader,targetID);
190         
191         ReaderPair readers=new ReaderPair(sourceReader,context,context.systemTrans());
192         command.processCopy(readers);
193         context.targetWriteBytes(readers,targetAddress);
194     }
195
196     public void append(byte value) {
197         _source.incrementOffset(1);
198         _target.append(value);
199     }
200
201     public long readLong() {
202         long value=_source.readLong();
203         _target.incrementOffset(YapConst.LONG_LENGTH);
204         return value;
205     }
206
207     public void writeLong(long value) {
208         _source.incrementOffset(YapConst.LONG_LENGTH);
209         _target.writeLong(value);
210     }
211
212     public BitMap4 readBitMap(int bitCount) {
213         BitMap4 value=_source.readBitMap(bitCount);
214         _target.incrementOffset(value.marshalledLength());
215         return value;
216     }
217
218     public void copyBytes(byte[] target, int sourceOffset,int targetOffset, int length) {
219         _source.copyBytes(target, sourceOffset, targetOffset, length);
220     }
221
222     public void readEnd() {
223         _source.readEnd();
224         _target.readEnd();
225     }
226
227     public int preparePayloadRead() {
228         int newPayLoadOffset = readInt();
229         readInt();
230         int linkOffSet = offset();
231         offset(newPayLoadOffset);
232         return linkOffSet;
233     }
234 }
235
Popular Tags