001: /*
002:
003: Derby - Class org.apache.derby.impl.store.raw.xact.XactId
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.store.raw.xact;
023:
024: import org.apache.derby.iapi.services.io.FormatIdUtil;
025: import org.apache.derby.iapi.services.io.StoredFormatIds;
026: import org.apache.derby.iapi.services.sanity.SanityManager;
027:
028: import org.apache.derby.iapi.error.StandardException;
029:
030: import org.apache.derby.iapi.store.raw.xact.TransactionId;
031:
032: import org.apache.derby.iapi.services.io.CompressedNumber;
033:
034: import java.io.ObjectOutput;
035: import java.io.ObjectInput;
036: import java.io.IOException;
037:
038: /**
039: Use this class for a short hand representation of the transaction. This
040: value is only guarentee to be unique within one continuous operation of the
041: raw store, in other words, every reboot may reuse the same value.
042:
043: Whereas GlobalXactId is unique for all times across all raw store, a XactId
044: is only unique within a particular rawstore and may be reused.
045:
046: XactId keeps track of the outstanding transactionId and is responsible
047: for dispensing new transactionIds
048: */
049: public class XactId implements TransactionId {
050: /*
051: ** Fields
052: */
053: private long id; // immutable
054:
055: /*
056: ** Constructor
057: */
058: public XactId(long id) {
059: this .id = id;
060: }
061:
062: /*
063: * Formatable methods
064: */
065:
066: // no-arg constructor, required by Formatable
067: public XactId() {
068: super ();
069: }
070:
071: /**
072: Write this out.
073: @exception IOException error writing to log stream
074: */
075: public void writeExternal(ObjectOutput out) throws IOException {
076: CompressedNumber.writeLong(out, id);
077: }
078:
079: /**
080: Read this in
081: @exception IOException error reading from log stream
082: */
083: public void readExternal(ObjectInput in) throws IOException {
084: id = CompressedNumber.readLong(in);
085: }
086:
087: /**
088: Return my format identifier.
089: */
090: public int getTypeFormatId() {
091: return StoredFormatIds.RAW_STORE_XACT_ID;
092: }
093:
094: /**
095: TransactionId method
096: */
097:
098: public int getMaxStoredSize() {
099: return FormatIdUtil
100: .getFormatIdByteLength(StoredFormatIds.RAW_STORE_XACT_ID)
101: + CompressedNumber.MAX_LONG_STORED_SIZE;
102: }
103:
104: public boolean equals(Object other) {
105: if (other == this )
106: return true;
107:
108: // assume cast will be successful rather than waste time doing an
109: // instanceof first. Catch the exception if it failed.
110: try {
111: XactId oxid = (XactId) other;
112: return (id == oxid.id);
113: } catch (ClassCastException cce) {
114: return false;
115: }
116: }
117:
118: public int hashCode() {
119: return (int) id;
120: }
121:
122: /**
123: Methods specific to this class
124: */
125:
126: /**
127: Return 0 if a == b,
128: +ve number if a > b
129: -ve number if a < b
130: */
131: public static long compare(TransactionId a, TransactionId b) {
132: if (a == null || b == null) {
133: if (a == null)
134: return -1;
135: else if (b == null)
136: return 1;
137: else
138: return 0;
139: }
140:
141: if (SanityManager.DEBUG) {
142: SanityManager.ASSERT(a instanceof XactId);
143: SanityManager.ASSERT(b instanceof XactId);
144: }
145: XactId A = (XactId) a;
146: XactId B = (XactId) b;
147:
148: return A.id - B.id;
149: }
150:
151: protected long getId() {
152: return id;
153: }
154:
155: public String toString() {
156: // needed for virtual lock table
157: return Long.toString(id);
158: }
159:
160: }
|