001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.key.impl;
010:
011: import com.completex.objective.components.log.Log;
012: import com.completex.objective.components.persistency.OdalPersistencyException;
013: import com.completex.objective.components.persistency.Persistency;
014: import com.completex.objective.components.persistency.PersistentEntry;
015: import com.completex.objective.components.persistency.Record;
016: import com.completex.objective.components.persistency.core.DatabasePolicy;
017: import com.completex.objective.components.persistency.key.ComplexSequenceKeyGenerator;
018: import com.completex.objective.components.persistency.transact.Transaction;
019:
020: /**
021: * Provides basic functionaly for bulk sequence generation.
022: * Reserves next bulkSize sequence numbers with single call to the delegateKeyGenerator.
023: * It is assumed that delegateKeyGenerator takes the sequence from the database.
024: * Once chosen bulkSize must not change for the life of the application since it can cause
025: * duplicate sequences to be generated
026: *
027: * @author Gennady Krizhevsky
028: */
029: public abstract class BulkSequenceKeyGeneratorImpl implements
030: ComplexSequenceKeyGenerator {
031:
032: public static final int DEFAULT_BULK_SIZE = 100;
033: private int bulkSize = DEFAULT_BULK_SIZE;
034: private long cur = 0;
035: private long offset = 0;
036: private int count = 0;
037: protected ComplexSequenceKeyGenerator delegateKeyGenerator;
038:
039: public BulkSequenceKeyGeneratorImpl(
040: ComplexSequenceKeyGenerator delegateKeyGenerator,
041: DatabasePolicy databasePolicy, int bulkSize, long offset,
042: Log log) {
043: this .delegateKeyGenerator = delegateKeyGenerator;
044: setDatabasePolicy(databasePolicy);
045: setLogger(log);
046: this .bulkSize = bulkSize;
047: this .offset = offset;
048: this .count = this .bulkSize;
049: }
050:
051: protected BulkSequenceKeyGeneratorImpl() {
052: }
053:
054: /**
055: * Get number of sequences generated between calls to a delegateKeyGenerator
056: *
057: * @return number of sequences generated between calls to a delegateKeyGenerator
058: */
059: public int getBulkSize() {
060: return bulkSize;
061: }
062:
063: /**
064: * Set number of sequences generated between calls to a delegateKeyGenerator
065: *
066: * @param bulkSize
067: */
068: protected void setBulkSize(int bulkSize) {
069: this .bulkSize = bulkSize;
070: }
071:
072: public long getOffset() {
073: return offset;
074: }
075:
076: /**
077: * Set offset which defines the 1st sequnce number to be generated
078: *
079: * @param offset
080: */
081: protected void setOffset(long offset) {
082: this .offset = offset;
083: }
084:
085: /**
086: * Get current sequence value
087: *
088: * @return current sequence value
089: */
090: public long getCurrentValue() {
091: return cur;
092: }
093:
094: /**
095: * @see ComplexSequenceKeyGenerator#setSeqName(String)
096: */
097: public void setSeqName(String seqName) {
098: delegateKeyGenerator.setSeqName(seqName);
099: }
100:
101: /**
102: * @see ComplexSequenceKeyGenerator#getSeqName()
103: */
104: public String getSeqName() {
105: return delegateKeyGenerator.getSeqName();
106: }
107:
108: /**
109: * Get next sequence number
110: *
111: * @param transaction
112: * @param persistency
113: * @param object
114: * @return next sequnce number as long
115: * @throws OdalPersistencyException
116: */
117: public synchronized Long getNextKeyLong(Transaction transaction,
118: Persistency persistency, Object object)
119: throws OdalPersistencyException {
120: long seq;
121:
122: if (count == bulkSize || count == 0) {
123: seq = ((Long) delegateKeyGenerator.getNextKeyPlain(
124: transaction, persistency, (String) object))
125: .longValue();
126: cur = (seq - 1) * bulkSize + 1 + offset;
127: count = 0;
128: } else {
129: cur++;
130: }
131: count++;
132: return new Long(cur);
133: }
134:
135: /**
136: * Get next sequence number
137: *
138: * @param transaction
139: * @param persistency
140: * @return next key as Long
141: * @throws OdalPersistencyException
142: */
143: public synchronized Long getNextKeyLong(Transaction transaction,
144: Persistency persistency) throws OdalPersistencyException {
145: return getNextKeyLong(transaction, persistency,
146: delegateKeyGenerator.getSeqName());
147: }
148:
149: /**
150: * @see com.completex.objective.components.persistency.key.KeyGenerator#getNextKey(Transaction transaction, Persistency persistency, Record record)
151: */
152: public Object getNextKey(Transaction transaction,
153: Persistency persistency, Record record)
154: throws OdalPersistencyException {
155: return getNextKeyLong(transaction, persistency);
156: }
157:
158: /**
159: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#getStaticParameters()
160: */
161: public Object getStaticParameters() {
162: return delegateKeyGenerator.getStaticParameters();
163: }
164:
165: /**
166: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#setLogger(com.completex.objective.components.log.Log)
167: */
168: public void setLogger(Log log) {
169: delegateKeyGenerator.setLogger(log);
170: }
171:
172: /**
173: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#setDatabasePolicy(com.completex.objective.components.persistency.core.DatabasePolicy)
174: */
175: public void setDatabasePolicy(DatabasePolicy databasePolicy) {
176: delegateKeyGenerator.setDatabasePolicy(databasePolicy);
177: }
178:
179: /**
180: * @see com.completex.objective.components.persistency.key.ComplexSequenceKeyGenerator#getNextKeyPlain(Transaction transaction, Persistency persistency, String objectName)
181: */
182: public Object getNextKeyPlain(Transaction transaction,
183: Persistency persistency, String objectName)
184: throws OdalPersistencyException {
185: return delegateKeyGenerator.getNextKeyPlain(transaction,
186: persistency, objectName);
187: }
188:
189: /**
190: * @see com.completex.objective.components.persistency.key.KeyGenerator#getNextKey(Transaction transaction, Persistency persistency, Record record, Object dynamicParameters)
191: */
192: public Object getNextKey(Transaction transaction,
193: Persistency persistency, Record record,
194: Object dynamicParameters) throws OdalPersistencyException {
195: return getNextKeyLong(transaction, persistency, record);
196: }
197:
198: /**
199: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#insertValue(com.completex.objective.components.persistency.transact.Transaction, com.completex.objective.components.persistency.Persistency, com.completex.objective.components.persistency.PersistentEntry)
200: */
201: public void insertValue(Transaction transaction,
202: Persistency persistency, PersistentEntry persistentEntry)
203: throws OdalPersistencyException {
204: delegateKeyGenerator.insertValue(transaction, persistency,
205: persistentEntry);
206: }
207:
208: /**
209: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#updateValue(com.completex.objective.components.persistency.transact.Transaction, com.completex.objective.components.persistency.Persistency, com.completex.objective.components.persistency.PersistentEntry, boolean)
210: */
211: public void updateValue(Transaction transaction,
212: Persistency persistency, PersistentEntry persistentEntry,
213: boolean complexDirty) throws OdalPersistencyException {
214: delegateKeyGenerator.updateValue(transaction, persistency,
215: persistentEntry, complexDirty);
216: }
217:
218: /**
219: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#setStaticParameters(Object)
220: */
221: public void setStaticParameters(Object staticParameters) {
222: delegateKeyGenerator.setStaticParameters(staticParameters);
223: }
224: }
|