Source code: org/apache/derby/iapi/store/raw/ContainerKey.java
1 /*
2
3 Derby - Class org.apache.derby.iapi.store.raw.ContainerKey
4
5 Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19 */
20
21 package org.apache.derby.iapi.store.raw;
22
23 import org.apache.derby.iapi.util.Matchable;
24 import org.apache.derby.iapi.services.io.CompressedNumber;
25
26 import java.io.ObjectOutput;
27 import java.io.ObjectInput;
28 import java.io.IOException;
29
30 import org.apache.derby.iapi.services.sanity.SanityManager;
31 import org.apache.derby.iapi.services.locks.Lockable;
32 import org.apache.derby.iapi.services.locks.Latch;
33 import org.apache.derby.iapi.services.locks.VirtualLockTable;
34
35 import java.util.Hashtable;
36
37 /**
38 A key that identifies a Container within the RawStore.
39 <BR> MT - Immutable
40 */
41 public final class ContainerKey implements Matchable, Lockable
42 {
43 private final long segmentId; // segment identifier
44 private final long containerId; // container identifier
45
46 /**
47 Create a new ContainerKey
48 */
49 public ContainerKey(long segmentId, long containerId) {
50 this.segmentId = segmentId;
51 this.containerId = containerId;
52 }
53
54 /**
55 Return my identifier within the segment
56 */
57 public long getContainerId() {
58 return containerId;
59 }
60
61 /**
62 Return my segment identifier
63 */
64 public long getSegmentId() {
65 return segmentId;
66 }
67
68 /*
69 ** Methods to read and write ContainerKeys.
70 */
71
72 public void writeExternal(ObjectOutput out) throws IOException
73 {
74 CompressedNumber.writeLong(out, segmentId);
75 CompressedNumber.writeLong(out, containerId);
76 }
77
78 public static ContainerKey read(ObjectInput in) throws IOException
79 {
80 long sid = CompressedNumber.readLong(in);
81 long cid = CompressedNumber.readLong(in);
82
83 return new ContainerKey(sid, cid);
84 }
85
86 /*
87 ** Methods of Object
88 */
89
90 public boolean equals(Object other) {
91 if (other == this)
92 return true;
93
94 if (other instanceof ContainerKey) {
95 ContainerKey otherKey = (ContainerKey) other;
96
97 return (containerId == otherKey.containerId) &&
98 (segmentId == otherKey.segmentId);
99 } else {
100 return false;
101 }
102 }
103
104 public int hashCode() {
105
106 return (int) (segmentId ^ containerId);
107 }
108
109 public String toString() {
110
111 return "Container(" + segmentId + ", " + containerId + ")";
112 }
113
114 /*
115 ** methods of Matchable
116 */
117
118 public boolean match(Object key) {
119 // instance of ContainerKey?
120 if (equals(key))
121 return true;
122
123 if (key instanceof PageKey)
124 return equals(((PageKey) key).getContainerId());
125
126 if (key instanceof RecordHandle) {
127 return equals(((RecordHandle) key).getContainerId());
128 }
129 return false;
130 }
131 /*
132 ** Methods of Lockable
133 */
134
135 public void lockEvent(Latch lockInfo) {
136 }
137
138
139 public boolean requestCompatible(Object requestedQualifier, Object grantedQualifier) {
140 if (SanityManager.DEBUG) {
141 SanityManager.ASSERT(requestedQualifier instanceof ContainerLock);
142 SanityManager.ASSERT(grantedQualifier instanceof ContainerLock);
143 }
144
145 ContainerLock clRequested = (ContainerLock) requestedQualifier;
146 ContainerLock clGranted = (ContainerLock) grantedQualifier;
147
148 return clRequested.isCompatible(clGranted);
149 }
150
151 /**
152 This method will only be called if requestCompatible returned false.
153 This results from two cases, some other compatabilty space has some
154 lock that would conflict with the request, or this compatability space
155 has a lock tha
156 */
157 public boolean lockerAlwaysCompatible() {
158 return true;
159 }
160
161 public void unlockEvent(Latch lockInfo) {
162 }
163
164 /**
165 This lockable wants to participate in the Virtual Lock table.
166 */
167 public boolean lockAttributes(int flag, Hashtable attributes)
168 {
169 if (SanityManager.DEBUG)
170 {
171 SanityManager.ASSERT(attributes != null,
172 "cannot call lockProperties with null attribute list");
173 }
174
175 if ((flag & VirtualLockTable.TABLE_AND_ROWLOCK) == 0)
176 return false;
177
178 attributes.put(VirtualLockTable.CONTAINERID,
179 new Long(getContainerId()));
180 attributes.put(VirtualLockTable.LOCKNAME, "Tablelock");
181 attributes.put(VirtualLockTable.LOCKTYPE, "TABLE");
182
183 // attributes.put(VirtualLockTable.SEGMENTID, new Long(identity.getSegmentId()));
184
185 return true;
186 }
187 }