KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > data > RecordId


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.RecordId
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.raw.data;
23
24 import org.apache.derby.iapi.store.raw.ContainerKey;
25 import org.apache.derby.iapi.store.raw.PageKey;
26 import org.apache.derby.iapi.services.locks.Latch;
27
28 import org.apache.derby.iapi.store.raw.RowLock;
29 import org.apache.derby.iapi.store.raw.RecordHandle;
30
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32 import org.apache.derby.iapi.services.locks.VirtualLockTable;
33
34 import org.apache.derby.catalog.UUID;
35 import java.util.Hashtable JavaDoc;
36
37 /**
38     Implementation of RecordHandle.
39
40     <BR>
41     MT - Mutable - Immutable identity : Thread Aware -
42     <BR>The methods of RecordHandle only access the identity
43         of the object and so the object appears immutable to them, as required.
44     <BR>The methods of Lockable are single thread required.
45
46 */

47 public final class RecordId implements RecordHandle {
48
49     /**
50         Page the record lives in.
51         MT - Immutable
52     */

53     private final PageKey pageId;
54
55     /**
56         The actual record id.
57         MT - Immutable
58     */

59     private final int recordId;
60
61     /**
62         Last slot number this record id was at.
63     */

64     transient private int slotNumberHint;
65
66     public RecordId(ContainerKey container, long pageNumber, int recordId) {
67         this.pageId = new PageKey(container, pageNumber);
68         this.recordId = recordId;
69
70         // FIRST_SLOT_NUMBER guaranteed to be zero
71
// this.slotNumberHint = Page.FIRST_SLOT_NUMBER;
72
}
73
74     public RecordId(PageKey pageId, int recordId) {
75         this.pageId = pageId;
76         this.recordId = recordId;
77
78         // FIRST_SLOT_NUMBER guaranteed to be zero
79
// this.slotNumberHint = Page.FIRST_SLOT_NUMBER;
80
}
81
82     public RecordId(PageKey pageId, int recordId, int current_slot) {
83         this.pageId = pageId;
84         this.recordId = recordId;
85         this.slotNumberHint = current_slot;
86     }
87
88     /*
89     ** Methods of RecordHandle
90     */

91
92     /**
93         Get my record id.
94
95         <BR>
96         MT - thread safe
97
98         @see RecordHandle#getId
99     */

100     public int getId() {
101         return recordId;
102     }
103
104     /**
105         Get my page number.
106
107         <BR>
108         MT - thread safe
109
110         @see RecordHandle#getPageNumber
111     */

112
113     public long getPageNumber() {
114         return pageId.getPageNumber();
115     }
116
117     public Object JavaDoc getPageId() {
118         return pageId;
119     }
120
121     public ContainerKey getContainerId() {
122         return pageId.getContainerId();
123     }
124
125
126     /**
127      * What slot number might the record be at?
128      * <p>
129      * The raw store guarantees that the record handle of a record will not
130      * change, but its slot number may. When a RecordId is constructed the
131      * caller could have provided a slot number, if so return that slot number
132      * hint here. If a hint was not provided then the default
133      * Page.FIRST_SLOT_NUMBER will be returned.
134      *
135      * @return The slot number the record handle may be at.
136      **/

137     public int getSlotNumberHint()
138     {
139         return(slotNumberHint);
140     }
141
142     /*
143     ** Methods of Lockable (from RecordHandle)
144     */

145
146     /**
147         Lock me.
148
149         <BR>
150         MT - Single thread required (methods of Lockable)
151
152         @see org.apache.derby.iapi.services.locks.Lockable#lockEvent
153     */

154     public void lockEvent(Latch lockInfo) {
155     }
156
157
158     /**
159         Determine if this request can be granted.
160         <p)
161         Implements the grant/wait lock logic for row locks. See the
162         table in RowLock for more information.
163
164         <BR>
165         MT - Single thread required (methods of Lockable)
166
167         @see org.apache.derby.iapi.services.locks.Lockable#requestCompatible
168     */

169     public boolean requestCompatible(
170     Object JavaDoc requestedQualifier,
171     Object JavaDoc grantedQualifier)
172     {
173
174         if (SanityManager.DEBUG) {
175             SanityManager.ASSERT((requestedQualifier == RowLock.RS2) ||
176                                  (requestedQualifier == RowLock.RS3) ||
177                                  (requestedQualifier == RowLock.RU2) ||
178                                  (requestedQualifier == RowLock.RU3) ||
179                                  (requestedQualifier == RowLock.RIP) ||
180                                  (requestedQualifier == RowLock.RI) ||
181                                  (requestedQualifier == RowLock.RX2) ||
182                                  (requestedQualifier == RowLock.RX3));
183             SanityManager.ASSERT((grantedQualifier == RowLock.RS2) ||
184                                  (grantedQualifier == RowLock.RS3) ||
185                                  (grantedQualifier == RowLock.RU2) ||
186                                  (grantedQualifier == RowLock.RU3) ||
187                                  (grantedQualifier == RowLock.RIP) ||
188                                  (grantedQualifier == RowLock.RI) ||
189                                  (grantedQualifier == RowLock.RX2) ||
190                                  (grantedQualifier == RowLock.RX3));
191         }
192
193         RowLock rlRequested = (RowLock) requestedQualifier;
194         RowLock rlGranted = (RowLock) grantedQualifier;
195
196         return(rlRequested.isCompatible(rlGranted));
197     }
198
199     /**
200         Is a caller that holds a lock compatible with themselves?
201         <p>
202         Row locks held in the same transaction are always compatible with
203         themselves.
204
205         <BR>
206         MT - Single thread required (methods of Lockable)
207
208         @see org.apache.derby.iapi.services.locks.Lockable#lockerAlwaysCompatible
209     */

210     public boolean lockerAlwaysCompatible() {
211         return true;
212     }
213
214     /**
215         Unlock me.
216
217         <BR>
218         MT - Single thread required (methods of Lockable)
219
220         @see org.apache.derby.iapi.services.locks.Lockable#unlockEvent
221     */

222     public void unlockEvent(Latch lockInfo) {
223     }
224
225     /*
226     ** Methods of Object
227     */

228
229     /**
230         Implement value equality.
231         <BR>
232         MT - Thread safe
233     */

234     public boolean equals(Object JavaDoc ref) {
235
236         if (!(ref instanceof RecordId))
237             return false;
238
239         RecordId other = (RecordId) ref;
240
241         return ((recordId == other.recordId)
242             && pageId.equals(other.pageId));
243     }
244
245     /**
246         Return a hashcode based on value.
247         <BR>
248         MT - thread safe
249     */

250     public int hashCode() {
251
252         return (int) recordId ^ pageId.hashCode();
253     }
254
255     public String JavaDoc toString()
256     {
257         if (SanityManager.DEBUG)
258         {
259             return "Record id=" + recordId + " " + pageId.toString();
260         }
261         else
262         {
263             return(null);
264         }
265
266     }
267
268     /**
269         This lockable wants to participate in the Virtual Lock table.
270      */

271     public boolean lockAttributes(int flag, Hashtable JavaDoc attributes)
272     {
273
274         if (SanityManager.DEBUG)
275         {
276             SanityManager.ASSERT(attributes != null,
277                 "cannot call lockProperties with null attribute list");
278             SanityManager.ASSERT(pageId != null,
279                 "RecordId PageId is null");
280         }
281
282         if ((flag & VirtualLockTable.TABLE_AND_ROWLOCK) == 0)
283             return false;
284
285         attributes.put(VirtualLockTable.CONTAINERID,
286                        new Long JavaDoc(pageId.getContainerId().getContainerId()));
287
288         attributes.put(VirtualLockTable.LOCKNAME,
289                        "(" + pageId.getPageNumber() + "," + recordId + ")");
290
291         attributes.put(VirtualLockTable.LOCKTYPE, "ROW");
292
293         // don't new unnecessary things for now
294
// attributes.put(VirtualLockTable.SEGMENTID, new Long(pageId.getContainerId().getSegmentId()));
295
// attributes.put(VirtualLockTable.PAGENUM, new Long(pageId.getPageNumber()));
296
// attributes.put(VirtualLockTable.PAGENUM, new Long(pageId.getPageNumber()));
297
// attributes.put(VirtualLockTable.RECID, new Integer(getId()));
298

299         return true;
300     }
301
302 }
303
Popular Tags