001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: DatabaseId.java,v 1.33.2.4 2008/01/07 15:14:09 cwl Exp $
007: */
008:
009: package com.sleepycat.je.dbi;
010:
011: import java.io.UnsupportedEncodingException;
012: import java.nio.ByteBuffer;
013:
014: import com.sleepycat.je.DatabaseException;
015: import com.sleepycat.je.log.LogUtils;
016: import com.sleepycat.je.log.Loggable;
017:
018: /**
019: * DatabaseImpl Ids are wrapped in a class so they can be logged.
020: */
021: public class DatabaseId implements Comparable, Loggable {
022:
023: /**
024: * The unique id of this database.
025: */
026: private int id;
027:
028: /**
029: *
030: */
031: public DatabaseId(int id) {
032: this .id = id;
033: }
034:
035: /**
036: * Uninitialized database id, for logging.
037: */
038: public DatabaseId() {
039: }
040:
041: /**
042: * @return id value
043: */
044: public int getId() {
045: return id;
046: }
047:
048: /**
049: * @return id as bytes, for use as a key
050: */
051: public byte[] getBytes() throws DatabaseException {
052:
053: try {
054: return toString().getBytes("UTF-8");
055: } catch (UnsupportedEncodingException UEE) {
056: throw new DatabaseException(UEE);
057: }
058: }
059:
060: /**
061: * Compare two DatabaseImpl Id's.
062: */
063: public boolean equals(Object obj) {
064: if (this == obj) {
065: return true;
066: }
067:
068: if (!(obj instanceof DatabaseId)) {
069: return false;
070: }
071:
072: return ((DatabaseId) obj).id == id;
073: }
074:
075: public int hashCode() {
076: return id;
077: }
078:
079: public String toString() {
080: return Integer.toString(id);
081: }
082:
083: /**
084: * see Comparable#compareTo
085: */
086: public int compareTo(Object o) {
087: if (o == null) {
088: throw new NullPointerException();
089: }
090:
091: DatabaseId argId = (DatabaseId) o;
092: if (id == argId.id) {
093: return 0;
094: } else if (id > argId.id) {
095: return 1;
096: } else {
097: return -1;
098: }
099: }
100:
101: /*
102: * Logging support.
103: */
104:
105: /**
106: * @see Loggable#getLogSize
107: */
108: public int getLogSize() {
109: return LogUtils.INT_BYTES;
110: }
111:
112: /**
113: * @see Loggable#writeToLog
114: */
115: public void writeToLog(ByteBuffer logBuffer) {
116: LogUtils.writeInt(logBuffer, id);
117: }
118:
119: /**
120: * @see Loggable#readFromLog
121: */
122: public void readFromLog(ByteBuffer itemBuffer, byte entryTypeVersion) {
123: id = LogUtils.readInt(itemBuffer);
124: }
125:
126: /**
127: * @see Loggable#dumpLog
128: */
129: public void dumpLog(StringBuffer sb, boolean verbose) {
130: sb.append("<dbId id=\"");
131: sb.append(id);
132: sb.append("\"/>");
133: }
134:
135: /**
136: * @see Loggable#getTransactionId
137: */
138: public long getTransactionId() {
139: return 0;
140: }
141: }
|