package gw.api.util uses java.util.concurrent.locks.ReentrantLock uses gw.api.system.database.SequenceUtil uses java.util.Map /** * Utilizing Platform's SequenceUtil, BatchSequenceUtil will create 1000 unique IDs for each sequence number pulled from * the database. These IDs are per VM use, improving throughput in use, but potentially leaving gaps in the ID sequence. */ class BatchSequenceUtil { final static var CACHE_SIZE = 10000 // Needs to be a power of 10 to work properly private static var _lock = new ReentrantLock() private static var _sequenceMap : Map private var _sequenceName : String as Name private var _seq : long = 0 private var _topseq : long = 0 protected static property get CacheSize() : int { return CACHE_SIZE } static function next(sequenceName : String) : long { return getSequence(sequenceName).next() } private static function getSequence(sequenceName : String) : BatchSequenceUtil { ensureSequenceMapInitialized() var sequence = _sequenceMap.get(sequenceName) if (sequence == null) { sequence = new BatchSequenceUtil() sequence.Name = sequenceName _sequenceMap.put(sequenceName, sequence) } return sequence } private static function ensureSequenceMapInitialized() { if (_sequenceMap == null) { _sequenceMap = {} } } private function next() : long { using(_lock) { if(_seq == _topseq) { _seq = SequenceUtil.next(1, this.Name) * CACHE_SIZE _topseq = _seq + CACHE_SIZE } var next = _seq _seq = _seq + 1 return next } } }