1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.store.raw.xact.XactId |
4 | |
5 | Copyright 1997, 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.impl.store.raw.xact; |
22 | |
23 | import org.apache.derby.iapi.services.io.FormatIdUtil; |
24 | import org.apache.derby.iapi.services.io.StoredFormatIds; |
25 | import org.apache.derby.iapi.services.sanity.SanityManager; |
26 | |
27 | import org.apache.derby.iapi.error.StandardException; |
28 | |
29 | import org.apache.derby.iapi.store.raw.xact.TransactionId; |
30 | |
31 | import org.apache.derby.iapi.services.io.CompressedNumber; |
32 | |
33 | |
34 | import java.io.ObjectOutput; |
35 | import java.io.ObjectInput; |
36 | import java.io.IOException; |
37 | |
38 | /** |
39 | Use this class for a short hand representation of the transaction. This |
40 | value is only guarentee to be unique within one continuous operation of the |
41 | raw store, in other words, every reboot may reuse the same value. |
42 | |
43 | Whereas GlobalXactId is unique for all times across all raw store, a XactId |
44 | is only unique within a particular rawstore and may be reused. |
45 | |
46 | XactId keeps track of the outstanding transactionId and is responsible |
47 | for dispensing new transactionIds |
48 | */ |
49 | public class XactId implements TransactionId |
50 | { |
51 | /* |
52 | ** Fields |
53 | */ |
54 | private long id; // immutable |
55 | |
56 | /* |
57 | ** Constructor |
58 | */ |
59 | public XactId(long id) { |
60 | this.id = id; |
61 | } |
62 | |
63 | /* |
64 | * Formatable methods |
65 | */ |
66 | |
67 | // no-arg constructor, required by Formatable |
68 | public XactId() { super(); } |
69 | |
70 | /** |
71 | Write this out. |
72 | @exception IOException error writing to log stream |
73 | */ |
74 | public void writeExternal(ObjectOutput out) throws IOException |
75 | { |
76 | CompressedNumber.writeLong(out, id); |
77 | } |
78 | |
79 | /** |
80 | Read this in |
81 | @exception IOException error reading from log stream |
82 | */ |
83 | public void readExternal(ObjectInput in) throws IOException |
84 | { |
85 | id = CompressedNumber.readLong(in); |
86 | } |
87 | |
88 | /** |
89 | Return my format identifier. |
90 | */ |
91 | public int getTypeFormatId() { |
92 | return StoredFormatIds.RAW_STORE_XACT_ID; |
93 | } |
94 | |
95 | /** |
96 | TransactionId method |
97 | */ |
98 | |
99 | public int getMaxStoredSize() |
100 | { |
101 | return FormatIdUtil.getFormatIdByteLength(StoredFormatIds.RAW_STORE_XACT_ID) + |
102 | CompressedNumber.MAX_LONG_STORED_SIZE; |
103 | } |
104 | |
105 | public boolean equals(Object other) { |
106 | if (other == this) |
107 | return true; |
108 | |
109 | // assume cast will be successful rather than waste time doing an |
110 | // instanceof first. Catch the exception if it failed. |
111 | try |
112 | { |
113 | XactId oxid = (XactId)other; |
114 | return (id == oxid.id); |
115 | } |
116 | catch (ClassCastException cce) |
117 | { |
118 | return false; |
119 | } |
120 | } |
121 | |
122 | public int hashCode() |
123 | { |
124 | return (int)id; |
125 | } |
126 | |
127 | /** |
128 | Methods specific to this class |
129 | */ |
130 | |
131 | |
132 | /** |
133 | Return 0 if a == b, |
134 | +ve number if a > b |
135 | -ve number if a < b |
136 | */ |
137 | public static long compare(TransactionId a, TransactionId b) |
138 | { |
139 | if (a == null || b == null) |
140 | { |
141 | if (a == null) |
142 | return -1; |
143 | else if (b == null) |
144 | return 1; |
145 | else |
146 | return 0; |
147 | } |
148 | |
149 | if (SanityManager.DEBUG) |
150 | { |
151 | SanityManager.ASSERT(a instanceof XactId); |
152 | SanityManager.ASSERT(b instanceof XactId); |
153 | } |
154 | XactId A = (XactId)a; |
155 | XactId B = (XactId)b; |
156 | |
157 | return A.id - B.id; |
158 | } |
159 | |
160 | protected long getId() |
161 | { |
162 | return id; |
163 | } |
164 | |
165 | |
166 | public String toString() |
167 | { |
168 | // needed for virtual lock table |
169 | return Long.toString(id); |
170 | } |
171 | |
172 | |
173 | } |
174 | |
175 | |