KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > xact > XactId


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.xact.XactId
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.xact;
23
24 import org.apache.derby.iapi.services.io.FormatIdUtil;
25 import org.apache.derby.iapi.services.io.StoredFormatIds;
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27
28 import org.apache.derby.iapi.error.StandardException;
29
30 import org.apache.derby.iapi.store.raw.xact.TransactionId;
31
32 import org.apache.derby.iapi.services.io.CompressedNumber;
33
34
35 import java.io.ObjectOutput JavaDoc;
36 import java.io.ObjectInput JavaDoc;
37 import java.io.IOException JavaDoc;
38
39 /**
40     Use this class for a short hand representation of the transaction. This
41     value is only guarentee to be unique within one continuous operation of the
42     raw store, in other words, every reboot may reuse the same value.
43
44     Whereas GlobalXactId is unique for all times across all raw store, a XactId
45     is only unique within a particular rawstore and may be reused.
46
47     XactId keeps track of the outstanding transactionId and is responsible
48     for dispensing new transactionIds
49 */

50 public class XactId implements TransactionId
51 {
52     /*
53     ** Fields
54     */

55     private long id; // immutable
56

57     /*
58     ** Constructor
59     */

60     public XactId(long id) {
61         this.id = id;
62     }
63
64     /*
65      * Formatable methods
66      */

67
68     // no-arg constructor, required by Formatable
69
public XactId() { super(); }
70
71     /**
72         Write this out.
73         @exception IOException error writing to log stream
74     */

75     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
76     {
77         CompressedNumber.writeLong(out, id);
78     }
79
80     /**
81         Read this in
82         @exception IOException error reading from log stream
83     */

84     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc
85     {
86         id = CompressedNumber.readLong(in);
87     }
88
89     /**
90         Return my format identifier.
91     */

92     public int getTypeFormatId() {
93         return StoredFormatIds.RAW_STORE_XACT_ID;
94     }
95
96     /**
97         TransactionId method
98     */

99       
100     public int getMaxStoredSize()
101     {
102         return FormatIdUtil.getFormatIdByteLength(StoredFormatIds.RAW_STORE_XACT_ID) +
103             CompressedNumber.MAX_LONG_STORED_SIZE;
104     }
105
106     public boolean equals(Object JavaDoc other) {
107         if (other == this)
108             return true;
109
110         // assume cast will be successful rather than waste time doing an
111
// instanceof first. Catch the exception if it failed.
112
try
113         {
114             XactId oxid = (XactId)other;
115             return (id == oxid.id);
116         }
117         catch (ClassCastException JavaDoc cce)
118         {
119             return false;
120         }
121     }
122
123     public int hashCode()
124     {
125         return (int)id;
126     }
127
128     /**
129         Methods specific to this class
130     */

131
132     
133     /**
134         Return 0 if a == b,
135                 +ve number if a > b
136                 -ve number if a < b
137     */

138     public static long compare(TransactionId a, TransactionId b)
139     {
140         if (a == null || b == null)
141         {
142             if (a == null)
143                 return -1;
144             else if (b == null)
145                 return 1;
146             else
147                 return 0;
148         }
149
150         if (SanityManager.DEBUG)
151         {
152             SanityManager.ASSERT(a instanceof XactId);
153             SanityManager.ASSERT(b instanceof XactId);
154         }
155         XactId A = (XactId)a;
156         XactId B = (XactId)b;
157
158         return A.id - B.id;
159     }
160
161     protected long getId()
162     {
163         return id;
164     }
165
166
167     public String JavaDoc toString()
168     {
169         // needed for virtual lock table
170
return Long.toString(id);
171     }
172
173
174 }
175
176
177
Popular Tags