001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.core;
025:
026: public final class IdentifierManager extends AbstractManager {
027: public final static String XID = "myoodb.xid";
028: public final static long BLOCK_SIZE = 100;
029:
030: private static volatile long s_incLocalTime = 0;
031: private static volatile long s_incConnectTime = 0;
032: private static volatile long s_incTransactionTime = 0;
033:
034: private long m_objectIdentifierBlock;
035: private long m_objectIdentifierBegin;
036: private long m_objectIdentifierEnd;
037: private long m_connectionIdentifier;
038:
039: public static synchronized long getLocalTime() {
040: long localtime = System.currentTimeMillis();
041:
042: if (s_incLocalTime > localtime) {
043: localtime = s_incLocalTime;
044: }
045:
046: return localtime;
047: }
048:
049: public static synchronized long getSynchronizedLocalTime() {
050: long localtime = System.currentTimeMillis();
051:
052: if (s_incLocalTime >= localtime) {
053: localtime = s_incLocalTime + 1;
054: }
055:
056: s_incLocalTime = localtime;
057:
058: return localtime;
059: }
060:
061: private static synchronized long getSynchronizedConnectTime() {
062: long localtime = System.currentTimeMillis();
063:
064: if (s_incConnectTime >= localtime) {
065: localtime = s_incConnectTime + 1;
066: }
067:
068: s_incConnectTime = localtime;
069:
070: return localtime;
071: }
072:
073: private static synchronized long getSynchronizedTransactionTime() {
074: long localtime = System.currentTimeMillis();
075:
076: if (s_incTransactionTime >= localtime) {
077: localtime = s_incTransactionTime + 1;
078: }
079:
080: s_incTransactionTime = localtime;
081:
082: return localtime;
083: }
084:
085: public IdentifierManager() {
086: reset();
087: }
088:
089: private void store() {
090: try {
091: MyOodbManager.getTheManager().getIdentifierProperties()
092: .setLongProperty(XID, m_objectIdentifierBlock);
093: MyOodbManager.getTheManager().storeIdentifierProperties();
094: } catch (Exception e) {
095: MyOodbManager.getTheManager().fatalError(
096: this ,
097: "Identifier Manager unable to store identifier: "
098: + e, e);
099: }
100: }
101:
102: public void startup() throws Exception {
103: }
104:
105: public void shutdown() throws Exception {
106: }
107:
108: public void reset() {
109: m_objectIdentifierBlock = -1;
110: m_objectIdentifierBegin = 0;
111: m_objectIdentifierEnd = 0;
112: }
113:
114: public synchronized long getNextObjectId() {
115: if (m_objectIdentifierBlock == -1) {
116: m_objectIdentifierBlock = MyOodbManager.getTheManager()
117: .getIdentifierProperties().getLongProperty(XID, -1);
118:
119: if (m_objectIdentifierBlock == -1) {
120: MyOodbManager.getTheManager().fatalError(
121: this ,
122: "Identifier Manager unable to access the "
123: + XID + " property.", null);
124: } else if (m_objectIdentifierBlock != 0) {
125: m_objectIdentifierEnd = m_objectIdentifierBlock;
126: }
127:
128: m_objectIdentifierBlock += BLOCK_SIZE;
129:
130: store();
131: }
132:
133: if (m_objectIdentifierBegin >= BLOCK_SIZE) {
134: m_objectIdentifierBegin = 0;
135: m_objectIdentifierEnd = m_objectIdentifierBlock;
136:
137: m_objectIdentifierBlock += BLOCK_SIZE;
138:
139: store();
140: }
141:
142: return ++m_objectIdentifierBegin + m_objectIdentifierEnd;
143: }
144:
145: public synchronized long getNextConnectionId() {
146: return getSynchronizedConnectTime();
147: }
148:
149: public synchronized long getNextTransactionId() {
150: return getSynchronizedTransactionTime();
151: }
152: }
|