KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > store > raw > ContainerKey


1 /*
2
3    Derby - Class org.apache.derby.iapi.store.raw.ContainerKey
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.iapi.store.raw;
23
24 import org.apache.derby.iapi.util.Matchable;
25 import org.apache.derby.iapi.services.io.CompressedNumber;
26
27 import java.io.ObjectOutput JavaDoc;
28 import java.io.ObjectInput JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32 import org.apache.derby.iapi.services.locks.Lockable;
33 import org.apache.derby.iapi.services.locks.Latch;
34 import org.apache.derby.iapi.services.locks.VirtualLockTable;
35
36 import java.util.Hashtable JavaDoc;
37
38 /**
39     A key that identifies a Container within the RawStore.
40     <BR> MT - Immutable
41 */

42 public final class ContainerKey implements Matchable, Lockable
43 {
44     private final long segmentId; // segment identifier
45
private final long containerId; // container identifier
46

47     /**
48         Create a new ContainerKey
49     */

50     public ContainerKey(long segmentId, long containerId) {
51         this.segmentId = segmentId;
52         this.containerId = containerId;
53     }
54
55     /**
56         Return my identifier within the segment
57     */

58     public long getContainerId() {
59         return containerId;
60     }
61
62     /**
63         Return my segment identifier
64     */

65     public long getSegmentId() {
66         return segmentId;
67     }
68
69     /*
70     ** Methods to read and write ContainerKeys.
71     */

72
73     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
74     {
75         CompressedNumber.writeLong(out, segmentId);
76         CompressedNumber.writeLong(out, containerId);
77     }
78
79     public static ContainerKey read(ObjectInput JavaDoc in) throws IOException JavaDoc
80     {
81         long sid = CompressedNumber.readLong(in);
82         long cid = CompressedNumber.readLong(in);
83
84         return new ContainerKey(sid, cid);
85     }
86
87     /*
88     ** Methods of Object
89     */

90
91     public boolean equals(Object JavaDoc other) {
92         if (other == this)
93             return true;
94
95         if (other instanceof ContainerKey) {
96             ContainerKey otherKey = (ContainerKey) other;
97
98             return (containerId == otherKey.containerId) &&
99                     (segmentId == otherKey.segmentId);
100         } else {
101             return false;
102         }
103     }
104
105     public int hashCode() {
106
107         return (int) (segmentId ^ containerId);
108     }
109
110     public String JavaDoc toString() {
111
112         return "Container(" + segmentId + ", " + containerId + ")";
113     }
114
115     /*
116     ** methods of Matchable
117     */

118
119     public boolean match(Object JavaDoc key) {
120         // instance of ContainerKey?
121
if (equals(key))
122             return true;
123
124         if (key instanceof PageKey)
125             return equals(((PageKey) key).getContainerId());
126
127         if (key instanceof RecordHandle) {
128             return equals(((RecordHandle) key).getContainerId());
129         }
130         return false;
131     }
132     /*
133     ** Methods of Lockable
134     */

135
136     public void lockEvent(Latch lockInfo) {
137     }
138      
139
140     public boolean requestCompatible(Object JavaDoc requestedQualifier, Object JavaDoc grantedQualifier) {
141         if (SanityManager.DEBUG) {
142             SanityManager.ASSERT(requestedQualifier instanceof ContainerLock);
143             SanityManager.ASSERT(grantedQualifier instanceof ContainerLock);
144         }
145
146         ContainerLock clRequested = (ContainerLock) requestedQualifier;
147         ContainerLock clGranted = (ContainerLock) grantedQualifier;
148
149         return clRequested.isCompatible(clGranted);
150     }
151
152     /**
153         This method will only be called if requestCompatible returned false.
154         This results from two cases, some other compatabilty space has some
155         lock that would conflict with the request, or this compatability space
156         has a lock tha
157     */

158     public boolean lockerAlwaysCompatible() {
159         return true;
160     }
161
162     public void unlockEvent(Latch lockInfo) {
163     }
164
165     /**
166         This lockable wants to participate in the Virtual Lock table.
167      */

168     public boolean lockAttributes(int flag, Hashtable JavaDoc attributes)
169     {
170         if (SanityManager.DEBUG)
171         {
172             SanityManager.ASSERT(attributes != null,
173                 "cannot call lockProperties with null attribute list");
174         }
175
176         if ((flag & VirtualLockTable.TABLE_AND_ROWLOCK) == 0)
177             return false;
178
179         attributes.put(VirtualLockTable.CONTAINERID,
180                        new Long JavaDoc(getContainerId()));
181         attributes.put(VirtualLockTable.LOCKNAME, "Tablelock");
182         attributes.put(VirtualLockTable.LOCKTYPE, "TABLE");
183
184         // attributes.put(VirtualLockTable.SEGMENTID, new Long(identity.getSegmentId()));
185

186         return true;
187     }
188 }
189
Popular Tags