com.amazon.carbonado.sequence
Class SequenceValueGenerator

java.lang.Object
  extended by com.amazon.carbonado.sequence.AbstractSequenceValueProducer
      extended by com.amazon.carbonado.sequence.SequenceValueGenerator
All Implemented Interfaces:
SequenceValueProducer

public class SequenceValueGenerator
extends AbstractSequenceValueProducer

General purpose implementation of a sequence value generator.

Since:
1.2
Author:
Brian S O'Neill, bcastill
See Also:
Sequence, StoredSequence

Field Summary
static int DEFAULT_INCREMENT
           
static int DEFAULT_INITIAL_VALUE
           
static int DEFAULT_RESERVE_AMOUNT
           
 
Constructor Summary
SequenceValueGenerator(Repository repo, String name)
          Construct a new SequenceValueGenerator which might create persistent sequence data if it does not exist.
SequenceValueGenerator(Repository repo, String name, long initialValue, int increment)
          Construct a new SequenceValueGenerator which might create persistent sequence data if it does not exist.
SequenceValueGenerator(Repository repo, String name, long initialValue, int increment, int reserveAmount)
          Construct a new SequenceValueGenerator which might create persistent sequence data if it does not exist.
 
Method Summary
 int nextIntValue()
          Returns the next value from the sequence, which may wrap negative if all positive values are exhausted.
 long nextLongValue()
          Returns the next value from the sequence, which may wrap negative if all positive values are exhausted.
 void reset(int initialValue)
          Reset the sequence.
 boolean returnReservedValues()
          Allow any unused reserved values to be returned for re-use.
 
Methods inherited from class com.amazon.carbonado.sequence.AbstractSequenceValueProducer
nextDecimalValue, nextNumericalValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_RESERVE_AMOUNT

public static final int DEFAULT_RESERVE_AMOUNT
See Also:
Constant Field Values

DEFAULT_INITIAL_VALUE

public static final int DEFAULT_INITIAL_VALUE
See Also:
Constant Field Values

DEFAULT_INCREMENT

public static final int DEFAULT_INCREMENT
See Also:
Constant Field Values
Constructor Detail

SequenceValueGenerator

public SequenceValueGenerator(Repository repo,
                              String name)
                       throws RepositoryException
Construct a new SequenceValueGenerator which might create persistent sequence data if it does not exist. The initial sequence value is one, and the increment is one.

Parameters:
repo - repository to persist sequence data
name - name of sequence
Throws:
RepositoryException

SequenceValueGenerator

public SequenceValueGenerator(Repository repo,
                              String name,
                              long initialValue,
                              int increment)
                       throws RepositoryException
Construct a new SequenceValueGenerator which might create persistent sequence data if it does not exist.

Parameters:
repo - repository to persist sequence data
name - name of sequence
initialValue - initial sequence value, if sequence needs to be created
increment - amount to increment sequence by
Throws:
RepositoryException

SequenceValueGenerator

public SequenceValueGenerator(Repository repo,
                              String name,
                              long initialValue,
                              int increment,
                              int reserveAmount)
                       throws RepositoryException
Construct a new SequenceValueGenerator which might create persistent sequence data if it does not exist.

Parameters:
repo - repository to persist sequence data
name - name of sequence
initialValue - initial sequence value, if sequence needs to be created
increment - amount to increment sequence by
reserveAmount - amount of sequence values to reserve
Throws:
RepositoryException
Method Detail

reset

public void reset(int initialValue)
           throws FetchException,
                  PersistException
Reset the sequence.

Parameters:
initialValue - first value produced by sequence
Throws:
FetchException
PersistException

nextLongValue

public long nextLongValue()
                   throws PersistException
Returns the next value from the sequence, which may wrap negative if all positive values are exhausted. When sequence wraps back to initial value, the sequence is fully exhausted, and an exception is thrown to indicate this.

Note: this method throws PersistException even for fetch failures since this method is called by insert operations. Insert operations can only throw a PersistException.

Throws:
PersistException - for fetch/persist failure or if sequence is exhausted.

nextIntValue

public int nextIntValue()
                 throws PersistException
Returns the next value from the sequence, which may wrap negative if all positive values are exhausted. When sequence wraps back to initial value, the sequence is fully exhausted, and an exception is thrown to indicate this.

Note: this method throws PersistException even for fetch failures since this method is called by insert operations. Insert operations can only throw a PersistException.

Specified by:
nextIntValue in interface SequenceValueProducer
Overrides:
nextIntValue in class AbstractSequenceValueProducer
Throws:
PersistException - for fetch/persist failure or if sequence is exhausted for int values.

returnReservedValues

public boolean returnReservedValues()
                             throws FetchException,
                                    PersistException
Allow any unused reserved values to be returned for re-use. If the repository is shared by other processes, then reserved values might not be returnable.

This method should be called during the shutdown process of a repository, although calling it does not invalidate this SequenceValueGenerator. If getNextValue is called again, it will reserve values again.

Returns:
true if reserved values were returned
Throws:
FetchException
PersistException


Copyright © 2006-2009 Amazon Technologies, Inc.. All Rights Reserved.