KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > access > heap > HeapRowLocation


1 /*
2
3    Derby - Class org.apache.derby.impl.store.access.heap.HeapRowLocation
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.access.heap;
23
24 /**
25
26   A heap row location represents the location of a row in the heap.
27   <P>
28   It's implementad as a wrapper around a raw store record handle.
29
30 **/

31
32 import org.apache.derby.iapi.services.io.ArrayInputStream;
33 import org.apache.derby.iapi.services.io.CompressedNumber;
34
35 import org.apache.derby.iapi.error.StandardException;
36 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager;
37 import org.apache.derby.iapi.types.CloneableObject;
38 import org.apache.derby.iapi.types.Orderable;
39 import org.apache.derby.iapi.types.RowLocation;
40 import org.apache.derby.iapi.store.raw.RecordHandle;
41 import org.apache.derby.iapi.store.raw.ContainerHandle;
42 import org.apache.derby.iapi.store.raw.Transaction;
43
44 import org.apache.derby.iapi.services.io.FormatIdUtil;
45 import org.apache.derby.iapi.services.io.StoredFormatIds;
46
47 import org.apache.derby.iapi.services.sanity.SanityManager;
48
49 import org.apache.derby.iapi.types.DataValueDescriptor;
50
51 import org.apache.derby.iapi.services.cache.ClassSize;
52
53 import org.apache.derby.iapi.types.DataType;
54
55
56 import java.io.ObjectOutput JavaDoc;
57 import java.io.ObjectInput JavaDoc;
58 import java.io.IOException JavaDoc;
59
60 /**
61  * @format_id ACCESS_HEAP_ROW_LOCATION_V1_ID
62  *
63  * @purpose Object used to store the location of a row within a Heap table.
64  * One of these is stored in every row of a btree secondary index
65  * built on a heap base table.
66  *
67  * @upgrade The type of the btree determines the type of rowlocation stored.
68  * In current btree implementations only one type of rowlocation can
69  * be stored per tree, and it's type is stored in the format id
70  * array stored in the Conglomerate object.
71  *
72  * @disk_layout
73  * page number(CompressedNumber.writeLong())
74  * record id(CompressedNumber.writeInt())
75  **/

76
77 public class HeapRowLocation extends DataType implements RowLocation
78 {
79     /**
80     The HeapRowLocation simply maintains a raw store record handle.
81     **/

82     private long pageno;
83     private int recid;
84     private RecordHandle rh;
85
86     private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( HeapRowLocation.class);
87     private static final int RECORD_HANDLE_MEMORY_USAGE
88     = ClassSize.estimateBaseFromCatalog( org.apache.derby.impl.store.raw.data.RecordId.class);
89
90     public int estimateMemoryUsage()
91     {
92         int sz = BASE_MEMORY_USAGE;
93
94         if( null != rh)
95             sz += RECORD_HANDLE_MEMORY_USAGE;
96         return sz;
97     } // end of estimateMemoryUsage
98

99     public String JavaDoc getTypeName() {
100         return "RowLocation";
101     }
102
103     public void setValueFromResultSet(java.sql.ResultSet JavaDoc resultSet, int colNumber,
104         boolean isNullable) {
105     }
106
107     public DataValueDescriptor getNewNull() {
108         return new HeapRowLocation();
109     }
110
111     public Object JavaDoc getObject() {
112         return null;
113     }
114
115     /*
116     ** Methods of CloneableObject.
117     */

118     public Object JavaDoc cloneObject()
119     {
120         return getClone();
121         
122     }
123
124     public DataValueDescriptor getClone() {
125         return new HeapRowLocation(this);
126     }
127
128     public int getLength() {
129         return 10;
130     }
131
132     public String JavaDoc getString() {
133         return toString();
134     }
135
136     /*
137     ** Methods of Orderable (from RowLocation)
138     **
139     ** see description in
140     ** protocol/Database/Storage/Access/Interface/Orderable.java
141     **
142     */

143
144     public boolean compare(int op,
145                            DataValueDescriptor other,
146                            boolean orderedNulls,
147                            boolean unknownRV)
148     {
149         // HeapRowLocation should not be null, ignore orderedNulls
150
int result = compare(other);
151
152         switch(op)
153         {
154         case ORDER_OP_LESSTHAN:
155             return (result < 0); // this < other
156
case ORDER_OP_EQUALS:
157             return (result == 0); // this == other
158
case ORDER_OP_LESSOREQUALS:
159             return (result <= 0); // this <= other
160
default:
161
162             if (SanityManager.DEBUG)
163                 SanityManager.THROWASSERT("Unexpected operation");
164             return false;
165         }
166     }
167
168     public int compare(DataValueDescriptor other)
169     {
170         // REVISIT: do we need this check?
171
if (SanityManager.DEBUG)
172             SanityManager.ASSERT(other instanceof HeapRowLocation);
173
174         HeapRowLocation arg = (HeapRowLocation) other;
175         
176         // XXX (nat) assumption is that these HeapRowLocations are
177
// never null. However, if they ever become null, need
178
// to add null comparison logic.
179
//
180
// RESOLVE - change these to be state based
181
/*
182         if (SanityManager.DEBUG)
183             SanityManager.ASSERT(getRecordHandle() != null);
184         if (SanityManager.DEBUG)
185             SanityManager.ASSERT(arg.getRecordHandle() != null);
186         */

187
188         long myPage = this.pageno;
189         long otherPage = arg.pageno;
190
191         if (myPage < otherPage)
192             return -1;
193         else if (myPage > otherPage)
194             return 1;
195
196         int myRecordId = this.recid;
197         int otherRecordId = arg.recid;
198
199         if (myRecordId == otherRecordId)
200             return 0;
201         else if (myRecordId < otherRecordId)
202             return -1;
203         else
204             return 1;
205     }
206
207     /*
208     ** Methods of HeapRowLocation
209     */

210
211     HeapRowLocation(RecordHandle rh)
212     {
213         setFrom(rh);
214     }
215
216     public HeapRowLocation()
217     {
218         this.pageno = 0;
219         this.recid = RecordHandle.INVALID_RECORD_HANDLE;
220     }
221
222     /* For cloning */
223     private HeapRowLocation(HeapRowLocation other)
224     {
225         this.pageno = other.pageno;
226         this.recid = other.recid;
227         this.rh = other.rh;
228     }
229
230     public RecordHandle getRecordHandle(ContainerHandle ch)
231         throws StandardException
232     {
233         if (rh != null)
234             return rh;
235
236         return rh = ch.makeRecordHandle(this.pageno, this.recid);
237     }
238
239     void setFrom(RecordHandle rh)
240     {
241         this.pageno = rh.getPageNumber();
242         this.recid = rh.getId();
243         this.rh = rh;
244     }
245
246     //public void setFrom(long pageno, int recid)
247
//{
248
// this.pageno = pageno;
249
// this.recid = recid;
250
//}
251

252     /*
253      * InternalRowLocation interface
254      */

255
256     /**
257      * Return a RecordHandle built from current RowLocation.
258      * <p>
259      * Build a RecordHandle from the current RowLocation. The main client
260      * of this interface is row level locking secondary indexes which read
261      * the RowLocation field from a secondary index row, and then need a
262      * RecordHandle built from this RowLocation.
263      * <p>
264      * The interface is not as generic as one may have wanted in order to
265      * store as compressed a version of a RowLocation as possible. So
266      * if an implementation of a RowLocation does not have the segmentid,
267      * and containerid stored, use the input parameters instead. If the
268      * RowLocation does have the values stored use them and ignore the
269      * input parameters.
270      * <p>
271      * Example:
272      * <p>
273      * The HeapRowLocation implementation of RowLocation generated by the
274      * Heap class, only stores the page and record id. The B2I conglomerate
275      * implements a secondary index on top of a Heap class. B2I knows the
276      * segmentid and containerid of it's base table, and knows that it can
277      * find an InternalRowLocation in a particular column of it's rows. It
278      * uses InternalRowLocation.getRecordHandle() to build a RecordHandle
279      * from the InternalRowLocation, and uses it to set a row lock on that
280      * row in the btree.
281      *
282      * @return The newly allocated RecordHandle.
283      *
284      * @param segmentid The segment id to store in RecordHandle.
285      * @param containerid The segment id to store in RecordHandle.
286      *
287      * @exception StandardException Standard exception policy.
288      **/

289     /*public RecordHandle getRecordHandle(
290     TransactionManager tran,
291     long segmentid,
292     long containerid)
293         throws StandardException
294     {
295         return(
296             this.getRecordHandle(
297                 tran.getRawStoreXact(), segmentid, containerid));
298     }
299 */

300
301     /*
302      * Storable interface, implies Externalizable, TypedFormat
303      */

304
305     /**
306         Return my format identifier.
307
308         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
309     */

310     public int getTypeFormatId() {
311         return StoredFormatIds.ACCESS_HEAP_ROW_LOCATION_V1_ID;
312     }
313
314     public boolean isNull()
315     {
316         return false;
317     }
318
319     public void writeExternal(ObjectOutput JavaDoc out)
320         throws IOException JavaDoc
321     {
322         // Write the page number, compressed
323
CompressedNumber.writeLong(out, this.pageno);
324
325         // Write the record id
326
CompressedNumber.writeInt(out, this.recid);
327     }
328
329     /**
330       @exception java.lang.ClassNotFoundException A class needed to read the
331       stored form of this object could not be found.
332       @see java.io.Externalizable#readExternal
333       */

334     public void readExternal(ObjectInput JavaDoc in)
335         throws IOException JavaDoc, ClassNotFoundException JavaDoc
336     {
337         this.pageno = CompressedNumber.readLong(in);
338
339         this.recid = CompressedNumber.readInt(in);
340
341         rh = null;
342     }
343     public void readExternalFromArray(ArrayInputStream in)
344         throws IOException JavaDoc, ClassNotFoundException JavaDoc
345     {
346         this.pageno = in.readCompressedLong();
347
348         this.recid = in.readCompressedInt();
349
350         rh = null;
351     }
352
353     public void restoreToNull()
354     {
355         if (SanityManager.DEBUG)
356             SanityManager.THROWASSERT("HeapRowLocation is never null");
357     }
358     protected void setFrom(DataValueDescriptor theValue) {
359         if (SanityManager.DEBUG)
360             SanityManager.THROWASSERT("SHOULD NOT BE CALLED");
361     }
362     /*
363     ** Methods of Object
364     */

365
366     /**
367         Implement value equality.
368         <BR>
369         MT - Thread safe
370     */

371     public boolean equals(Object JavaDoc ref)
372     {
373
374         if ((ref instanceof HeapRowLocation))
375         {
376             HeapRowLocation other = (HeapRowLocation) ref;
377
378             return(
379                 (this.pageno == other.pageno) && (this.recid == other.recid));
380         }
381         else
382         {
383             return false;
384         }
385
386     }
387
388     /**
389         Return a hashcode based on value.
390         <BR>
391         MT - thread safe
392     */

393     public int hashCode()
394     {
395         return ((int) this.pageno) ^ this.recid;
396     }
397
398     /*
399      * Standard toString() method.
400      */

401     public String JavaDoc toString()
402     {
403         String JavaDoc string =
404            "(" + this.pageno + "," + this.recid + ")";
405         return(string);
406     }
407 }
408
Popular Tags