diff options
author | Dwayne Boone <dwayne@zeroc.com> | 2015-11-13 16:53:57 -0330 |
---|---|---|
committer | Dwayne Boone <dwayne@zeroc.com> | 2015-11-13 16:53:57 -0330 |
commit | 99c3941638f735825b1bfa9f2df764255351242b (patch) | |
tree | fdde43c6580b785e5edc9ae90c26487d9e16be13 /java/src | |
parent | adding registerIceBT (diff) | |
download | ice-99c3941638f735825b1bfa9f2df764255351242b.tar.bz2 ice-99c3941638f735825b1bfa9f2df764255351242b.tar.xz ice-99c3941638f735825b1bfa9f2df764255351242b.zip |
Moved Freeze to its own repository
Diffstat (limited to 'java/src')
33 files changed, 0 insertions, 12159 deletions
diff --git a/java/src/Freeze/build.gradle b/java/src/Freeze/build.gradle deleted file mode 100644 index ca91c1885b5..00000000000 --- a/java/src/Freeze/build.gradle +++ /dev/null @@ -1,43 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -sourceCompatibility = iceSourceCompatibility -targetCompatibility = iceTargetCompatibility - -slice { - java { - args = "--ice" - files = files(fileTree(dir: "$sliceDir/Freeze", includes:['*.ice'], excludes:["*F.ice"]), - file("./src/main/java/Freeze/PingObject.ice")) - } - freezej { - files = [file("${sliceDir}/Ice/BuiltinSequences.ice"), - file("${sliceDir}/Freeze/CatalogData.ice")] - args = "--ice" - dict { - CatalogData { - javaType = "Freeze.Catalog" - key = "string" - value = "Freeze::CatalogData" - } - CatalogIndexList { - javaType = "Freeze.CatalogIndexList" - key = "string" - value = "Ice::StringSeq" - } - } - } -} - -dependencies { - compile project(':ice') - compile files(dbJar) -} - -apply from: "$rootProject.projectDir/gradle/library.gradle" diff --git a/java/src/Freeze/src/main/java/Freeze/BackgroundSaveEvictorI.java b/java/src/Freeze/src/main/java/Freeze/BackgroundSaveEvictorI.java deleted file mode 100644 index 44d6e191d76..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/BackgroundSaveEvictorI.java +++ /dev/null @@ -1,1475 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -// -// A Freeze evictor implementation that saves updates asynchronously, -// in a "saving" thread -// - -class BackgroundSaveEvictorI extends EvictorI implements BackgroundSaveEvictor, Runnable -{ - // - // Clean object; can become modified or destroyed - // - static final byte clean = 0; - - // - // New object; can become clean, dead or destroyed - // - static final byte created = 1; - - // - // Modified object; can become clean or destroyed - // - static final byte modified = 2; - - // - // Being saved. Can become dead or created - // - static final byte destroyed = 3; - - // - // Exists only in the SaveAsyncEvictor; for example the object was created - // and later destroyed (without a save in between), or it was - // destroyed on disk but is still in use. Can become created. - // - static final byte dead = 4; - - BackgroundSaveEvictorI(Ice.ObjectAdapter adapter, String envName, String filename, - ServantInitializer initializer, Index[] indices, boolean createDb) - { - this(adapter, envName, null, filename, initializer, indices, createDb); - } - - BackgroundSaveEvictorI(Ice.ObjectAdapter adapter, String envName, com.sleepycat.db.Environment dbEnv, - String filename, ServantInitializer initializer, Index[] indices, boolean createDb) - { - super(adapter, envName, dbEnv, filename, null, initializer, indices, createDb); - - String propertyPrefix = "Freeze.Evictor." + envName + '.' + filename; - - // - // By default, we save every minute or when the size of the modified - // queue reaches 10. - // - - _saveSizeTrigger = - _communicator.getProperties().getPropertyAsIntWithDefault(propertyPrefix + ".SaveSizeTrigger", 10); - - _savePeriod = - _communicator.getProperties().getPropertyAsIntWithDefault(propertyPrefix + ".SavePeriod", 60 * 1000); - - // - // By default, we save at most 10 * SaveSizeTrigger objects per transaction - // - _maxTxSize = _communicator.getProperties().getPropertyAsIntWithDefault( - propertyPrefix + ".MaxTxSize", 10 * _saveSizeTrigger); - - if(_maxTxSize <= 0) - { - _maxTxSize = 100; - } - - // - // By default, no stream timeout - // - _streamTimeout = - _communicator.getProperties().getPropertyAsIntWithDefault(propertyPrefix + ".StreamTimeout", 0) * 1000; - if(_streamTimeout > 0) - { - _timer = IceInternal.Util.getInstance(_communicator).timer(); - } - - // - // Start threads - // - String savingThreadName; - - String programName = _communicator.getProperties().getProperty("Ice.ProgramName"); - if(programName.length() > 0) - { - savingThreadName = programName + "-"; - } - else - { - savingThreadName = ""; - } - savingThreadName += "FreezeEvictorThread(" + envName + '.' + _filename + ")"; - _thread = new Thread(this, savingThreadName); - _thread.start(); - } - - @Override - public Ice.ObjectPrx - addFacet(Ice.Object servant, Ice.Identity ident, String facet) - { - checkIdentity(ident); - checkServant(servant); - - if(facet == null) - { - facet = ""; - } - - _deactivateController.lock(); - try - { - ObjectStore store = findStore(facet, _createDb); - - if(store == null) - { - NotFoundException ex = new NotFoundException(); - ex.message = _errorPrefix + "addFacet: could not open database for facet '" + facet + "'"; - throw ex; - } - - boolean alreadyThere = false; - - for(;;) - { - // - // Create a new entry - // - - EvictorElement element = new EvictorElement(ident, store); - element.status = dead; - element.rec = new ObjectRecord(); - - if(store.keepStats()) - { - element.rec.stats = new Statistics(); - } - - Object o = store.cache().putIfAbsent(ident, element); - - if(o != null) - { - element = (EvictorElement)o; - } - - synchronized(this) - { - if(element.stale) - { - // - // Try again - // - continue; - } - fixEvictPosition(element); - - synchronized(element) - { - switch(element.status) - { - case clean: - case created: - case modified: - { - alreadyThere = true; - break; - } - case destroyed: - { - element.status = modified; - element.rec.servant = servant; - - // - // No need to push it on the modified queue, as a destroyed object - // is either already on the queue or about to be saved. When saved, - // it becomes dead. - // - break; - } - case dead: - { - element.status = created; - ObjectRecord rec = element.rec; - - rec.servant = servant; - - if(store.keepStats()) - { - rec.stats.creationTime = IceInternal.Time.currentMonotonicTimeMillis(); - rec.stats.lastSaveTime = 0; - rec.stats.avgSaveTime = 0; - } - - addToModifiedQueue(element); - break; - } - default: - { - assert false; - break; - } - } - } - } - break; // for(;;) - } - - if(alreadyThere) - { - Ice.AlreadyRegisteredException ex = new Ice.AlreadyRegisteredException(); - ex.kindOfObject = "servant"; - ex.id = _communicator.identityToString(ident); - if(facet.length() > 0) - { - ex.id += " -f " + IceUtilInternal.StringUtil.escapeString(facet, ""); - } - throw ex; - } - - if(_trace >= 1) - { - String objString = "object \"" + _communicator.identityToString(ident) + "\""; - if(!facet.equals("")) - { - objString += " with facet \"" + facet + "\""; - } - - _communicator.getLogger().trace("Freeze.Evictor", "added " + objString + " to Db \"" + _filename + - "\""); - } - - Ice.ObjectPrx obj = _adapter.createProxy(ident); - if(facet.length() > 0) - { - obj = obj.ice_facet(facet); - } - return obj; - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public Ice.Object - removeFacet(Ice.Identity ident, String facet) - { - checkIdentity(ident); - if(facet == null) - { - facet = ""; - } - - _deactivateController.lock(); - - try - { - ObjectStore store = findStore(facet, false); - Ice.Object servant = null; - - if(store != null) - { - for(;;) - { - // - // Retrieve object - // - EvictorElement element = (EvictorElement)store.cache().pin(ident); - if(element != null) - { - synchronized(this) - { - if(element.stale) - { - // - // Try again - // - continue; - } - - fixEvictPosition(element); - synchronized(element) - { - switch(element.status) - { - case clean: - { - servant = element.rec.servant; - element.status = destroyed; - element.rec.servant = null; - addToModifiedQueue(element); - break; - } - case created: - { - servant = element.rec.servant; - element.status = dead; - element.rec.servant = null; - break; - } - case modified: - { - servant = element.rec.servant; - element.status = destroyed; - element.rec.servant = null; - // - // Not necessary to push it on the modified queue, as a modified - // element is either on the queue already or about to be saved - // (at which point it becomes clean) - // - break; - } - case destroyed: - case dead: - { - break; - } - default: - { - assert false; - break; - } - } - } - - if(element.keepCount > 0) - { - assert servant != null; - - element.keepCount = 0; - // - // Add to front of evictor queue - // - // Note that save evicts dead objects - // - _evictorList.addFirst(element); - element.evictPosition = _evictorList.iterator(); - // - // Position the iterator "on" the element. - // - element.evictPosition.next(); - _currentEvictorSize++; - } - } - } - break; // for(;;) - } - } - - if(servant == null) - { - Ice.NotRegisteredException ex = new Ice.NotRegisteredException(); - ex.kindOfObject = "servant"; - ex.id = _communicator.identityToString(ident); - if(facet.length() > 0) - { - ex.id += " -f " + IceUtilInternal.StringUtil.escapeString(facet, ""); - } - throw ex; - } - - if(_trace >= 1) - { - String objString = "object \"" + _communicator.identityToString(ident) + "\""; - if(!facet.equals("")) - { - objString += " with facet \"" + facet + "\""; - } - - _communicator.getLogger().trace("Freeze.Evictor", "removed " + objString + " from Db \"" + _filename + - "\""); - } - return servant; - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public void - keep(Ice.Identity ident) - { - keepFacet(ident, ""); - } - - @Override - public void - keepFacet(Ice.Identity ident, String facet) - { - checkIdentity(ident); - if(facet == null) - { - facet = ""; - } - - _deactivateController.lock(); - try - { - boolean notThere = false; - - ObjectStore store = findStore(facet, false); - if(store == null) - { - notThere = true; - } - else - { - for(;;) - { - EvictorElement element = (EvictorElement)store.cache().pin(ident); - if(element == null) - { - notThere = true; - break; - } - - synchronized(this) - { - if(element.stale) - { - // - // try again - // - continue; - } - - synchronized(element) - { - if(element.status == destroyed || element.status == dead) - { - notThere = true; - break; - } - } - - // - // Found! - // - - if(element.keepCount == 0) - { - if(element.usageCount < 0) - { - // - // New object - // - element.usageCount = 0; - } - else - { - assert element.evictPosition != null; - element.evictPosition.remove(); - element.evictPosition = null; - _currentEvictorSize--; - } - element.keepCount = 1; - } - else - { - element.keepCount++; - } - break; - } - } - } - - if(notThere) - { - Ice.NotRegisteredException ex = new Ice.NotRegisteredException(); - ex.kindOfObject = "servant"; - ex.id = _communicator.identityToString(ident); - if(facet.length() > 0) - { - ex.id += " -f " + IceUtilInternal.StringUtil.escapeString(facet, ""); - } - throw ex; - } - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public void - release(Ice.Identity ident) - { - releaseFacet(ident, ""); - } - - @Override - public void - releaseFacet(Ice.Identity ident, String facet) - { - checkIdentity(ident); - if(facet == null) - { - facet = ""; - } - - _deactivateController.lock(); - try - { - - ObjectStore store = findStore(facet, false); - - if(store != null) - { - synchronized(this) - { - EvictorElement element = (EvictorElement)store.cache().getIfPinned(ident); - if(element != null) - { - assert !element.stale; - if(element.keepCount > 0) - { - if(--element.keepCount == 0) - { - // - // Add to front of evictor queue - // - // Note that the element cannot be destroyed or dead since - // its keepCount was > 0. - // - assert element.evictPosition == null; - _evictorList.addFirst(element); - element.evictPosition = _evictorList.iterator(); - // - // Position the iterator "on" the element. - // - element.evictPosition.next(); - _currentEvictorSize++; - } - // - // Success - // - return; - } - } - } - } - - Ice.NotRegisteredException ex = new Ice.NotRegisteredException(); - ex.kindOfObject = "servant"; - ex.id = _communicator.identityToString(ident); - if(facet.length() > 0) - { - ex.id += " -f " + IceUtilInternal.StringUtil.escapeString(facet, ""); - } - - throw ex; - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public boolean - hasFacet(Ice.Identity ident, String facet) - { - checkIdentity(ident); - if(facet == null) - { - facet = ""; - } - - _deactivateController.lock(); - try - { - - ObjectStore store = findStore(facet, false); - - if(store == null) - { - return false; - } - - synchronized(this) - { - EvictorElement element = (EvictorElement)store.cache().getIfPinned(ident); - if(element != null) - { - assert !element.stale; - - synchronized(element) - { - return element.status != dead && element.status != destroyed; - } - } - } - return store.dbHasObject(ident, null); - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - protected boolean - hasAnotherFacet(Ice.Identity ident, String facet) - { - _deactivateController.lock(); - try - { - // - // If the object exists in another store, throw FacetNotExistException - // instead of returning null (== ObjectNotExistException) - // - java.util.Map<String, ObjectStore> storeMapCopy; - synchronized(this) - { - storeMapCopy = new java.util.HashMap<String, ObjectStore>(_storeMap); - } - - for(java.util.Map.Entry<String, ObjectStore> entry : storeMapCopy.entrySet()) - { - // - // Do not check facet - // - if(!facet.equals(entry.getKey())) - { - ObjectStore store = entry.getValue(); - boolean inCache = false; - - synchronized(this) - { - EvictorElement element = (EvictorElement)store.cache().getIfPinned(ident); - if(element != null) - { - inCache = true; - assert !element.stale; - - synchronized(element) - { - if(element.status != dead && element.status != destroyed) - { - return true; - } - } - } - } - if(!inCache) - { - if(store.dbHasObject(ident, null)) - { - return true; - } - } - } - } - return false; - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - protected Object - createEvictorElement(Ice.Identity ident, ObjectRecord rec, ObjectStore store) - { - EvictorElement elt = new EvictorElement(ident, store); - elt.rec = rec; - return elt; - } - - @Override - protected Ice.Object - locateImpl(Ice.Current current, Ice.LocalObjectHolder cookie) - { - _deactivateController.lock(); - try - { - cookie.value = null; - - ObjectStore store = findStore(current.facet, false); - if(store == null) - { - if(_trace >= 2) - { - _communicator.getLogger().trace("Freeze.Evictor", "locate could not find a database for facet \"" + - current.facet + "\""); - } - return null; - } - - for(;;) - { - EvictorElement element = (EvictorElement)store.cache().pin(current.id); - if(element == null) - { - if(_trace >= 2) - { - _communicator.getLogger().trace("Freeze.Evictor", "locate could not find \"" + - _communicator.identityToString(current.id) + "\" in Db \"" + - _filename + "\""); - } - return null; - } - - synchronized(this) - { - if(element.stale) - { - // - // try again - // - continue; - } - - synchronized(element) - { - if(element.status == destroyed || element.status == dead) - { - if(_trace >= 2) - { - _communicator.getLogger().trace("Freeze.Evictor", "locate found \"" + - _communicator.identityToString(current.id) + - "\" in the cache for Db \"" + _filename + - "\" but it was dead or destroyed"); - } - return null; - } - - // - // It's a good one! - // - - if(_trace >= 2) - { - _communicator.getLogger().trace("Freeze.Evictor", "locate found \"" + - _communicator.identityToString(current.id) + "\" in Db \"" + - _filename + "\""); - } - - fixEvictPosition(element); - element.usageCount++; - cookie.value = element; - assert element.rec.servant != null; - return element.rec.servant; - } - } - } - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public void - finished(Ice.Current current, Ice.Object servant, java.lang.Object cookie) - { - _deactivateController.lock(); - try - { - if(cookie != null) - { - EvictorElement element = (EvictorElement)cookie; - - boolean enqueue = false; - - if((servant.ice_operationAttributes(current.operation) & 0x1) != 0) - { - synchronized(element) - { - if(element.status == clean) - { - // - // Assume this operation updated the object - // - element.status = modified; - enqueue = true; - } - } - } - - synchronized(this) - { - // - // Only elements with a usageCount == 0 can become stale and we own - // one count! - // - assert !element.stale; - assert element.usageCount >= 1; - - // - // Decrease the usage count of the evictor queue element. - // - element.usageCount--; - - if(enqueue) - { - addToModifiedQueue(element); - } - else if(element.usageCount == 0 && element.keepCount == 0) - { - // - // Evict as many elements as necessary. - // - evict(); - } - } - } - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public void - deactivate(String category) - { - if(_deactivateController.deactivate()) - { - try - { - saveNow(); - - synchronized(this) - { - // - // Set the evictor size to zero, meaning that we will evict - // everything possible. - // - _evictorSize = 0; - evict(); - - _savingThreadDone = true; - notifyAll(); - } - - try - { - _thread.join(); - } - catch(InterruptedException ex) - { - } - - closeDbEnv(); - } - finally - { - _deactivateController.deactivationComplete(); - } - } - } - - @Override - public void - run() - { - try - { - for(;;) - { - java.util.List<EvictorElement> allObjects; - java.util.List<EvictorElement> deadObjects = new java.util.LinkedList<EvictorElement>(); - - int saveNowThreadsSize = 0; - - synchronized(this) - { - while(!_savingThreadDone && - (_saveNowThreads.size() == 0) && - (_saveSizeTrigger < 0 || _modifiedQueue.size() < _saveSizeTrigger)) - { - try - { - if(_savePeriod == 0) - { - wait(); - } - else - { - long preSave = IceInternal.Time.currentMonotonicTimeMillis(); - wait(_savePeriod); - if(IceInternal.Time.currentMonotonicTimeMillis() > preSave + _savePeriod) - { - break; - } - } - } - catch(InterruptedException ex) - { - } - } - - saveNowThreadsSize = _saveNowThreads.size(); - - if(_savingThreadDone) - { - assert(_modifiedQueue.size() == 0); - assert(saveNowThreadsSize == 0); - break; // for(;;) - } - - // - // Check first if there is something to do! - // - if(_modifiedQueue.size() == 0) - { - if(saveNowThreadsSize > 0) - { - _saveNowThreads.clear(); - notifyAll(); - } - continue; // for(;;) - } - - allObjects = _modifiedQueue; - _modifiedQueue = new java.util.ArrayList<EvictorElement>(); - } - - int size = allObjects.size(); - - java.util.List<StreamedObject> streamedObjectQueue = new java.util.ArrayList<StreamedObject>(); - - long streamStart = IceInternal.Time.currentMonotonicTimeMillis(); - - // - // Stream each element - // - for(int i = 0; i < size; i++) - { - EvictorElement element = allObjects.get(i); - - boolean tryAgain; - - do - { - tryAgain = false; - Ice.Object servant = null; - - synchronized(element) - { - byte status = element.status; - - switch(status) - { - case created: - case modified: - { - servant = element.rec.servant; - break; - } - case destroyed: - { - streamedObjectQueue.add(stream(element, streamStart)); - - element.status = dead; - deadObjects.add(element); - break; - } - case dead: - { - deadObjects.add(element); - break; - } - default: - { - // - // Nothing to do (could be a duplicate) - // - break; - } - } - } - - if(servant != null) - { - // - // Lock servant and then facet so that user can safely lock - // servant and call various Evictor operations - // - java.util.concurrent.Future<?> future = null; - if(_timer != null) - { - // - // The timer is used to ensure the streaming of some object does not take more than - // timeout ms. We only measure the time necessary to acquire the lock on the object - // (servant), not the streaming itself. - // - future = _timer.schedule(new Runnable() - { - @Override - public void run() - { - _communicator.getLogger().error(_errorPrefix + - "Fatal error: streaming watch dog timed out"); - - Util.handleFatalError(BackgroundSaveEvictorI.this, _communicator, null); - } - }, _streamTimeout, java.util.concurrent.TimeUnit.MILLISECONDS); - } - synchronized(servant) - { - if(future != null) - { - future.cancel(false); - future = null; - } - - synchronized(element) - { - byte status = element.status; - - switch(status) - { - case created: - case modified: - { - if(servant == element.rec.servant) - { - streamedObjectQueue.add(stream(element, streamStart)); - - element.status = clean; - } - else - { - tryAgain = true; - } - break; - } - case destroyed: - { - streamedObjectQueue.add(stream(element, streamStart)); - - element.status = dead; - deadObjects.add(element); - break; - } - case dead: - { - deadObjects.add(element); - break; - } - default: - { - // - // Nothing to do (could be a duplicate) - // - break; - } - } - } - } - } - } while(tryAgain); - } - - if(_trace >= 1) - { - long now = IceInternal.Time.currentMonotonicTimeMillis(); - _communicator.getLogger().trace("Freeze.Evictor", "streamed " + streamedObjectQueue.size() + - " objects in " + (now - streamStart) + " ms"); - } - - // - // Now let's save all these streamed objects to disk using a transaction - // - - // - // Each time we get a deadlock, we reduce the number of objects to save - // per transaction - // - int txSize = streamedObjectQueue.size(); - if(txSize > _maxTxSize) - { - txSize = _maxTxSize; - } - - boolean tryAgain; - - do - { - tryAgain = false; - - while(streamedObjectQueue.size() > 0) - { - if(txSize > streamedObjectQueue.size()) - { - txSize = streamedObjectQueue.size(); - } - - long saveStart = 0; - if(_trace >= 1) - { - saveStart = IceInternal.Time.currentMonotonicTimeMillis(); - } - String txnId = null; - - try - { - com.sleepycat.db.Transaction tx = _dbEnv.getEnv().beginTransaction(null, null); - - if(_txTrace >= 1) - { - txnId = Long.toHexString((tx.getId() & 0x7FFFFFFF) + 0x80000000L); - - _communicator.getLogger().trace("Freeze.Evictor", _errorPrefix + - "started transaction " + txnId + " in saving thread"); - } - - try - { - for(int i = 0; i < txSize; i++) - { - StreamedObject obj = streamedObjectQueue.get(i); - obj.store.save(obj.key, obj.value, obj.status, tx); - } - - com.sleepycat.db.Transaction toCommit = tx; - tx = null; - toCommit.commit(); - - if(_txTrace >= 1) - { - _communicator.getLogger().trace("Freeze.Evictor", _errorPrefix + - "committed transaction " + txnId); - } - } - finally - { - if(tx != null) - { - tx.abort(); - if(_txTrace >= 1) - { - _communicator.getLogger().trace("Freeze.Evictor", _errorPrefix + - "rolled back transaction " + txnId); - } - } - } - - for(int i = 0; i < txSize; i++) - { - streamedObjectQueue.remove(0); - } - - if(_trace >= 1) - { - long now = IceInternal.Time.currentMonotonicTimeMillis(); - _communicator.getLogger().trace("Freeze.Evictor", "saved " + txSize + " objects in " + - (now - saveStart) + " ms"); - } - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_deadlockWarning) - { - _communicator.getLogger().warning( - "Deadlock in Freeze.BackgroundSaveEvictorI.run while writing " + - "into Db \"" + _filename + "\"; retrying..."); - } - - tryAgain = true; - txSize = (txSize + 1)/2; - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_errorPrefix + "saving: " + dx.getMessage(), dx); - } - } - } while(tryAgain); - - synchronized(this) - { - // - // Release usage count - // - for(int i = 0; i < allObjects.size(); i++) - { - EvictorElement element = allObjects.get(i); - assert element.usageCount > 0; - element.usageCount--; - } - allObjects.clear(); - - for(EvictorElement element : deadObjects) - { - // - // Can be stale when there are duplicates on the deadObjects list - // - if(!element.stale && element.usageCount == 0 && element.keepCount == 0) - { - // - // Get rid of unused dead elements - // - synchronized(element) - { - if(element.status == dead) - { - evict(element); - } - } - } - } - - deadObjects.clear(); - evict(); - - if(saveNowThreadsSize > 0) - { - for(int i = 0; i < saveNowThreadsSize; i++) - { - _saveNowThreads.remove(0); - } - notifyAll(); - } - } - } - } - catch(RuntimeException ex) - { - java.io.StringWriter sw = new java.io.StringWriter(); - java.io.PrintWriter pw = new java.io.PrintWriter(sw); - ex.printStackTrace(pw); - pw.flush(); - _communicator.getLogger().error(_errorPrefix + "Fatal error in saving thread:\n" + sw.toString()); - - Util.handleFatalError(this, _communicator, ex); - } - } - - @Override - protected void - evict() - { - assert Thread.holdsLock(this); - - java.util.Iterator<EvictorElement> p = _evictorList.riterator(); - while(p.hasNext() && _currentEvictorSize > _evictorSize) - { - // - // Get the last unused element from the evictor queue. - // - EvictorElement element = p.next(); - if(element.usageCount == 0) - { - // - // Fine, servant is not in use (and not in the modifiedQueue) - // - - assert !element.stale; - assert element.keepCount == 0; - assert element.evictPosition != null; - - if(_trace >= 2 || (_trace >= 1 && _evictorList.size() % 50 == 0)) - { - String objString = "object \"" + _communicator.identityToString(element.identity) + "\""; - String facet = element.store.facet(); - if(facet.length() > 0) - { - objString += " with facet \"" + facet + "\""; - } - - _communicator.getLogger().trace("Freeze.Evictor", "evicting " + objString + " from the queue; " + - "number of elements in the queue: " + _currentEvictorSize); - } - - // - // Remove last unused element from the evictor queue. - // - element.stale = true; - element.store.cache().unpin(element.identity); - p.remove(); - element.evictPosition = null; - _currentEvictorSize--; - } - } - } - - @Override - protected TransactionI - beforeQuery() - { - saveNow(); - return null; - } - - synchronized private void - saveNow() - { - Thread myself = Thread.currentThread(); - - _saveNowThreads.add(myself); - notifyAll(); - do - { - try - { - wait(); - } - catch(InterruptedException ex) - { - } - } - while(_saveNowThreads.contains(myself)); - } - - private void - fixEvictPosition(EvictorElement element) - { - assert Thread.holdsLock(this); - - assert !element.stale; - - if(element.keepCount == 0) - { - if(element.usageCount < 0) - { - assert element.evictPosition == null; - - // - // New object - // - element.usageCount = 0; - _currentEvictorSize++; - } - else - { - assert element.evictPosition != null; - element.evictPosition.remove(); - } - _evictorList.addFirst(element); - element.evictPosition = _evictorList.iterator(); - // - // Position the iterator "on" the element. - // - element.evictPosition.next(); - } - } - - private void - evict(EvictorElement element) - { - assert Thread.holdsLock(this); - - assert !element.stale; - assert element.keepCount == 0; - - element.evictPosition.remove(); - _currentEvictorSize--; - element.stale = true; - element.store.cache().unpin(element.identity); - } - - private void - addToModifiedQueue(EvictorElement element) - { - assert Thread.holdsLock(this); - - element.usageCount++; - _modifiedQueue.add(element); - - if(_saveSizeTrigger >= 0 && _modifiedQueue.size() >= _saveSizeTrigger) - { - notifyAll(); - } - } - - private StreamedObject - stream(EvictorElement element, long streamStart) - { - assert Thread.holdsLock(element); - - assert element.status != dead; - - StreamedObject obj = new StreamedObject(); - - obj.status = element.status; - obj.store = element.store; - obj.key = ObjectStore.marshalKey(element.identity, _communicator, _encoding); - - if(element.status != destroyed) - { - boolean keepStats = obj.store.keepStats(); - - if(keepStats) - { - updateStats(element.rec.stats, streamStart); - } - obj.value = ObjectStore.marshalValue(element.rec, _communicator, _encoding, keepStats); - } - return obj; - } - - static private class EvictorElement - { - EvictorElement(Ice.Identity identity, ObjectStore store) - { - this.identity = identity; - this.store = store; - } - - final ObjectStore store; - final Ice.Identity identity; - - // - // Protected by SaveAsyncEvictor - // - java.util.Iterator<EvictorElement> evictPosition = null; - int usageCount = -1; - int keepCount = 0; - boolean stale = false; - - // - // Protected by this - // - ObjectRecord rec = null; - byte status = clean; - } - - static private class StreamedObject - { - com.sleepycat.db.DatabaseEntry key = null; - com.sleepycat.db.DatabaseEntry value = null; - byte status = dead; - ObjectStore store = null; - } - - // - // List of EvictorElement with stable iterators - // - private final Freeze.LinkedList<EvictorElement> _evictorList = new Freeze.LinkedList<EvictorElement>(); - private int _currentEvictorSize = 0; - - // - // The _modifiedQueue contains a queue of all modified facets - // Each element in the queue "owns" a usage count, to ensure the - // elements containing them remain in the map. - // - private java.util.List<EvictorElement> _modifiedQueue = new java.util.ArrayList<EvictorElement>(); - - private boolean _savingThreadDone = false; - private java.util.concurrent.ScheduledExecutorService _timer; - private long _streamTimeout; - - // - // Threads that have requested a "saveNow" and are waiting for - // its completion - // - private final java.util.List<Thread> _saveNowThreads = new java.util.ArrayList<Thread>(); - - private int _saveSizeTrigger; - private int _maxTxSize; - private long _savePeriod; - - private Thread _thread; -} diff --git a/java/src/Freeze/src/main/java/Freeze/ConnectionI.java b/java/src/Freeze/src/main/java/Freeze/ConnectionI.java deleted file mode 100644 index 26b2f9f47ab..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/ConnectionI.java +++ /dev/null @@ -1,255 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -public class ConnectionI implements Connection -{ - @Override - public Transaction - beginTransaction() - { - if(_transaction != null) - { - throw new Freeze.TransactionAlreadyInProgressException(); - } - closeAllIterators(); - _transaction = new TransactionI(this); - return _transaction; - } - - @Override - public Transaction - currentTransaction() - { - return _transaction; - } - - @Override - public void - removeMapIndex(String mapName, String indexName) - { - if(_dbEnv == null) - { - throw new DatabaseException("Closed connection"); - } - - try - { - _dbEnv.getEnv().removeDatabase(dbTxn(), mapName + "." + indexName, null); - } - catch(com.sleepycat.db.DeadlockException dx) - { - throw new DeadlockException(errorPrefix() + dx.getMessage(), _transaction, dx); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(errorPrefix() + dx.getMessage(), dx); - } - catch(java.io.FileNotFoundException fne) - { - throw new IndexNotFoundException(mapName, indexName); - } - } - - @Override - public void - close() - { - if(_transaction != null) - { - try - { - _transaction.rollback(); - } - catch(Freeze.DatabaseException dx) - { - // - // Ignored - // - } - } - - java.util.Iterator<Map<?,?> > p = _mapList.iterator(); - while(p.hasNext()) - { - p.next().close(); - } - - if(_dbEnv != null) - { - try - { - _dbEnv.close(); - } - finally - { - _dbEnv = null; - } - } - } - - @Override - public Ice.Communicator - getCommunicator() - { - return _communicator; - } - - @Override - public Ice.EncodingVersion - getEncoding() - { - return _encoding; - } - - @Override - public String - getName() - { - return _envName; - } - - @Override - protected void - finalize() - throws Throwable - { - try - { - if(_dbEnv != null) - { - _logger.warning("leaked Connection for DbEnv \"" + _envName + "\""); - } - } - catch(java.lang.Exception ex) - { - } - finally - { - super.finalize(); - } - } - - ConnectionI(SharedDbEnv dbEnv) - { - _dbEnv = dbEnv; - _communicator = dbEnv.getCommunicator(); - _encoding = dbEnv.getEncoding(); - _logger = _communicator.getLogger(); - _envName = dbEnv.getEnvName(); - _trace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Map"); - _txTrace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Transaction"); - - Ice.Properties properties = _communicator.getProperties(); - _deadlockWarning = properties.getPropertyAsInt("Freeze.Warn.Deadlocks") > 0; - } - - ConnectionI(Ice.Communicator communicator, String envName, com.sleepycat.db.Environment dbEnv) - { - this(SharedDbEnv.get(communicator, envName, dbEnv)); - } - - public void - closeAllIterators() - { - java.util.Iterator<Map<?,?>> p = _mapList.iterator(); - while(p.hasNext()) - { - p.next().closeAllIterators(); - } - } - - public java.util.Iterator<?> - registerMap(Map<?,?> map) - { - _mapList.addFirst(map); - java.util.Iterator<Map<?,?>> p = _mapList.iterator(); - p.next(); - return p; - } - - public void - unregisterMap(java.util.Iterator<?> p) - { - p.remove(); - } - - void - clearTransaction() - { - _transaction = null; - } - - public com.sleepycat.db.Transaction - dbTxn() - { - if(_transaction == null) - { - return null; - } - else - { - return _transaction.dbTxn(); - } - } - - public SharedDbEnv - dbEnv() - { - return _dbEnv; - } - - public String - envName() - { - return _envName; - } - - public Ice.Communicator - communicator() - { - return _communicator; - } - - public final int - trace() - { - return _trace; - } - - public final int - txTrace() - { - return _txTrace; - } - - public final boolean - deadlockWarning() - { - return _deadlockWarning; - } - - private String - errorPrefix() - { - return "DbEnv(\"" + _envName + "\"): "; - } - - private Ice.Communicator _communicator; - private Ice.EncodingVersion _encoding; - private Ice.Logger _logger; - private SharedDbEnv _dbEnv; - private String _envName; - private TransactionI _transaction; - private LinkedList<Map<?,?> > _mapList = new LinkedList<Map<?,?>>(); - private int _trace; - private int _txTrace; - private boolean _deadlockWarning; -} diff --git a/java/src/Freeze/src/main/java/Freeze/EvictorElement.java b/java/src/Freeze/src/main/java/Freeze/EvictorElement.java deleted file mode 100644 index 1cdb633a2fb..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/EvictorElement.java +++ /dev/null @@ -1,63 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -class EvictorElement -{ - // - // Clean object; can become modified or destroyed - // - static final byte clean = 0; - - // - // New object; can become clean, dead or destroyed - // - static final byte created = 1; - - // - // Modified object; can become clean or destroyed - // - static final byte modified = 2; - - // - // Being saved. Can become dead or created - // - static final byte destroyed = 3; - - // - // Exists only in the Evictor; for example the object was created - // and later destroyed (without a save in between), or it was - // destroyed on disk but is still in use. Can become created. - // - static final byte dead = 4; - - EvictorElement(Ice.Identity identity, ObjectStore store) - { - this.identity = identity; - this.store = store; - } - - final ObjectStore store; - final Ice.Identity identity; - - // - // Protected by EvictorI - // - java.util.Iterator<EvictorElement> evictPosition = null; - int usageCount = -1; - int keepCount = 0; - boolean stale = false; - - // - // Protected by this - // - ObjectRecord rec = null; - byte status = clean; -} diff --git a/java/src/Freeze/src/main/java/Freeze/EvictorI.java b/java/src/Freeze/src/main/java/Freeze/EvictorI.java deleted file mode 100644 index e8f1bfb87b8..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/EvictorI.java +++ /dev/null @@ -1,610 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -abstract class EvictorI implements Evictor -{ - // - // The deactivate controller is used by the implementation of all public - // operations to ensure that deactivate() (which closes/clears various - // Berkeley DB objects) is not called during these operations. - // Note that the only threads that may perform such concurrent calls - // are threads other than the dispatch threads of the associated adapter. - // - class DeactivateController - { - synchronized void - activate() - { - assert !_activated; - _activated = true; - } - - synchronized void - lock() - { - assert _activated; - - if(_deactivated || _deactivating) - { - throw new EvictorDeactivatedException(); - } - _guardCount++; - } - - synchronized void - unlock() - { - assert _activated; - - _guardCount--; - if(_deactivating && _guardCount == 0) - { - // - // Notify all the threads -- although we only want to - // reach the thread doing the deactivation. - // - notifyAll(); - } - } - - synchronized boolean - deactivated() - { - return !_activated || _deactivated; - } - - synchronized boolean - deactivate() - { - assert _activated; - - if(_deactivated) - { - return false; - } - - if(_deactivating) - { - // - // Wait for deactivated - // - while(!_deactivated) - { - try - { - wait(); - } - catch(InterruptedException e) - { - // Ignored - } - } - return false; - } - else - { - _deactivating = true; - while(_guardCount > 0) - { - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Evictor", "Waiting for " + _guardCount + - " threads to complete before starting deactivation."); - } - - try - { - wait(); - } - catch(InterruptedException e) - { - // Ignored - } - } - - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Evictor", "Starting deactivation."); - } - return true; - } - } - - synchronized void - deactivationComplete() - { - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Evictor", "Deactivation complete."); - } - - _deactivated = true; - _deactivating = false; - notifyAll(); - } - - private boolean _activated = false; - private boolean _deactivating = false; - private boolean _deactivated = false; - private int _guardCount = 0; - } - - static final String defaultDb = "$default"; - static final String indexPrefix = "$index:"; - - @Override - public Ice.ObjectPrx - add(Ice.Object servant, Ice.Identity ident) - { - return addFacet(servant, ident, ""); - } - - @Override - public Ice.Object - remove(Ice.Identity ident) - { - return removeFacet(ident, ""); - } - - @Override - public boolean - hasObject(Ice.Identity ident) - { - return hasFacet(ident, ""); - } - - @Override - public Ice.Object - locate(Ice.Current current, Ice.LocalObjectHolder cookie) - { - // - // Special ice_ping() handling - // - if(current.operation != null && current.operation.equals("ice_ping")) - { - if(hasFacet(current.id, current.facet)) - { - if(_trace >= 3) - { - _communicator.getLogger().trace( - "Freeze.Evictor", "ice_ping found \"" + _communicator.identityToString(current.id) + - "\" with facet \"" + current.facet + "\""); - } - - cookie.value = null; - return _pingObject; - } - else if(hasAnotherFacet(current.id, current.facet)) - { - if(_trace >= 3) - { - _communicator.getLogger().trace( - "Freeze.Evictor", "ice_ping raises FacetNotExistException for \"" + - _communicator.identityToString(current.id) + "\" with facet \"" + current.facet + "\""); - } - - throw new Ice.FacetNotExistException(); - } - else - { - if(_trace >= 3) - { - _communicator.getLogger().trace( - "Freeze.Evictor", "ice_ping will raise ObjectNotExistException for \"" + - _communicator.identityToString(current.id) + "\" with facet \"" + current.facet + "\""); - } - - return null; - } - } - - Ice.Object result = locateImpl(current, cookie); - - if(result == null) - { - if(hasAnotherFacet(current.id, current.facet)) - { - throw new Ice.FacetNotExistException(current.id, current.facet, current.operation); - } - } - return result; - } - - @Override - synchronized public void - setSize(int evictorSize) - { - _deactivateController.lock(); - try - { - // - // Ignore requests to set the evictor size to values smaller than zero. - // - if(evictorSize < 0) - { - return; - } - - // - // Update the evictor size. - // - _evictorSize = evictorSize; - - // - // Evict as many elements as necessary. - // - evict(); - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - synchronized public int - getSize() - { - return _evictorSize; - } - - @Override - public EvictorIterator - getIterator(String facet, int batchSize) - { - _deactivateController.lock(); - try - { - if(facet == null) - { - facet = ""; - } - TransactionI tx = beforeQuery(); - return new EvictorIteratorI(findStore(facet, false), tx, batchSize); - } - finally - { - _deactivateController.unlock(); - } - } - - abstract protected boolean hasAnotherFacet(Ice.Identity ident, String facet); - - abstract protected Object createEvictorElement(Ice.Identity ident, ObjectRecord rec, ObjectStore store); - - abstract protected Ice.Object locateImpl(Ice.Current current, Ice.LocalObjectHolder cookie); - - abstract protected void evict(); - - protected void - closeDbEnv() - { - assert _dbEnv != null; - for(ObjectStore store : _storeMap.values()) - { - store.close(); - } - _dbEnv.close(); - _dbEnv = null; - } - - protected synchronized ObjectStore - findStore(String facet, boolean createIt) - { - ObjectStore os = _storeMap.get(facet); - - if(os == null && createIt) - { - String facetType = _facetTypes.get(facet); - os = new ObjectStore(facet, facetType, true, this, new java.util.LinkedList<Index>(), false); - _storeMap.put(facet, os); - } - return os; - } - - protected void - initialize(Ice.Identity ident, String facet, Ice.Object servant) - { - if(_initializer != null) - { - _initializer.initialize(_adapter, ident, facet, servant); - } - } - - protected - EvictorI(Ice.ObjectAdapter adapter, String envName, com.sleepycat.db.Environment dbEnv, String filename, - java.util.Map<String, String> facetTypes, ServantInitializer initializer, Index[] indices, - boolean createDb) - { - _adapter = adapter; - _communicator = adapter.getCommunicator(); - _initializer = initializer; - _filename = filename; - _createDb = createDb; - _facetTypes = facetTypes == null ? new java.util.HashMap<String, String>() : - new java.util.HashMap<String, String>(facetTypes); - - _dbEnv = SharedDbEnv.get(_communicator, envName, dbEnv); - _encoding = _dbEnv.getEncoding(); - - _trace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Evictor"); - _txTrace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Transaction"); - _deadlockWarning = _communicator.getProperties().getPropertyAsInt("Freeze.Warn.Deadlocks") > 0; - - _errorPrefix = "Freeze Evictor DbEnv(\"" + envName + "\") Db(\"" + _filename + "\"): "; - - String propertyPrefix = "Freeze.Evictor." + envName + '.' + _filename; - - boolean populateEmptyIndices = - _communicator.getProperties().getPropertyAsIntWithDefault(propertyPrefix + ".PopulateEmptyIndices", 0) > 0; - - // - // Instantiate all Dbs in 2 steps: - // (1) iterate over the indices and create ObjectStore with indices - // (2) open ObjectStores without indices - // - - java.util.List<String> dbs = allDbs(); - // - // Add default db in case it's not there - // - dbs.add(defaultDb); - - if(indices != null) - { - for(int i = 0; i < indices.length; ++i) - { - String facet = indices[i].facet(); - - if(_storeMap.get(facet) == null) - { - java.util.List<Index> storeIndices = new java.util.LinkedList<Index>(); - for(int j = i; j < indices.length; ++j) - { - if(indices[j].facet().equals(facet)) - { - storeIndices.add(indices[j]); - } - } - - String facetType = _facetTypes.get(facet); - ObjectStore store = new ObjectStore(facet, facetType,_createDb, this, storeIndices, - populateEmptyIndices); - _storeMap.put(facet, store); - } - } - } - - for(String facet : dbs) - { - if(facet.equals(defaultDb)) - { - facet = ""; - } - - if(_storeMap.get(facet) == null) - { - String facetType = _facetTypes.get(facet); - - ObjectStore store = new ObjectStore(facet, facetType, _createDb, this, - new java.util.LinkedList<Index>(), populateEmptyIndices); - - _storeMap.put(facet, store); - } - } - _deactivateController.activate(); - } - - protected - EvictorI(Ice.ObjectAdapter adapter, String envName, String filename, java.util.Map<String, String> facetTypes, - ServantInitializer initializer, Index[] indices, boolean createDb) - { - this(adapter, envName, null, filename, facetTypes, initializer, indices, createDb); - } - - abstract TransactionI beforeQuery(); - - static void - updateStats(Statistics stats, long time) - { - long diff = time - (stats.creationTime + stats.lastSaveTime); - if(stats.lastSaveTime == 0) - { - stats.lastSaveTime = diff; - stats.avgSaveTime = diff; - } - else - { - stats.lastSaveTime = time - stats.creationTime; - stats.avgSaveTime = (long)(stats.avgSaveTime * 0.95 + diff * 0.05); - } - } - - final DeactivateController - deactivateController() - { - return _deactivateController; - } - - final Ice.Communicator - communicator() - { - return _communicator; - } - - final Ice.EncodingVersion - encoding() - { - return _encoding; - } - - final SharedDbEnv - dbEnv() - { - return _dbEnv; - } - - final String - filename() - { - return _filename; - } - - final String - errorPrefix() - { - return _errorPrefix; - } - - final boolean - deadlockWarning() - { - return _deadlockWarning; - } - - final int - trace() - { - return _trace; - } - - private java.util.List<String> - allDbs() - { - java.util.List<String> result = new java.util.LinkedList<String>(); - - com.sleepycat.db.Database db = null; - com.sleepycat.db.Cursor dbc = null; - - try - { - com.sleepycat.db.DatabaseConfig config = new com.sleepycat.db.DatabaseConfig(); - config.setType(com.sleepycat.db.DatabaseType.UNKNOWN); - config.setReadOnly(true); - db = _dbEnv.getEnv().openDatabase(null, _filename, null, config); - - dbc = db.openCursor(null, null); - - com.sleepycat.db.DatabaseEntry key = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry value = new com.sleepycat.db.DatabaseEntry(); - - boolean more = true; - while(more) - { - more = (dbc.getNext(key, value, null) == com.sleepycat.db.OperationStatus.SUCCESS); - if(more) - { - // - // Assumes Berkeley-DB encodes the db names in UTF-8! - // - String dbName = new String(key.getData(), 0, key.getSize(), "UTF8"); - - if(!dbName.startsWith(indexPrefix)) - { - result.add(dbName); - } - } - } - - dbc.close(); - dbc = null; - db.close(); - db = null; - } - catch(java.io.UnsupportedEncodingException ix) - { - throw new DatabaseException(_errorPrefix + "cannot decode database names", ix); - } - catch(java.io.FileNotFoundException ix) - { - // - // New file - // - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_errorPrefix + "Db.open: " + dx.getMessage(), dx); - } - finally - { - if(dbc != null) - { - try - { - dbc.close(); - } - catch(com.sleepycat.db.DatabaseException dx) - { - // Ignored - } - } - - if(db != null) - { - try - { - db.close(); - } - catch(com.sleepycat.db.DatabaseException dx) - { - // Ignored - } - } - } - - return result; - } - - static void - checkIdentity(Ice.Identity ident) - { - if(ident.name == null || ident.name.length() == 0) - { - throw new Ice.IllegalIdentityException(ident); - } - } - - static void - checkServant(Ice.Object servant) - { - if(servant == null) - { - throw new Ice.IllegalServantException("cannot add null servant to Freeze Evictor"); - } - } - - protected int _evictorSize = 10; - - protected final java.util.Map<String, ObjectStore> _storeMap = new java.util.HashMap<String, ObjectStore>(); - private final java.util.Map<String, String> _facetTypes; - - protected final Ice.ObjectAdapter _adapter; - protected final Ice.Communicator _communicator; - protected final Ice.EncodingVersion _encoding; - - protected final ServantInitializer _initializer; - - protected SharedDbEnv _dbEnv; - - protected final String _filename; - protected final boolean _createDb; - - protected int _trace = 0; - protected int _txTrace = 0; - - protected String _errorPrefix; - - protected boolean _deadlockWarning; - - protected DeactivateController _deactivateController = new DeactivateController(); - - private Ice.Object _pingObject = new PingObject(); -} diff --git a/java/src/Freeze/src/main/java/Freeze/EvictorIteratorI.java b/java/src/Freeze/src/main/java/Freeze/EvictorIteratorI.java deleted file mode 100644 index ecf3ecb6e7b..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/EvictorIteratorI.java +++ /dev/null @@ -1,230 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -class EvictorIteratorI implements EvictorIterator -{ - @Override - public boolean - hasNext() - { - if(_batchIterator != null && _batchIterator.hasNext()) - { - return true; - } - else - { - _batchIterator = nextBatch(); - return (_batchIterator != null); - } - } - - @Override - public Ice.Identity - next() - { - if(hasNext()) - { - return _batchIterator.next(); - } - else - { - throw new NoSuchElementException(); - } - } - - EvictorIteratorI(ObjectStore store, TransactionI tx, int batchSize) - { - _store = store; - _more = (store != null); - _batchSize = batchSize; - _tx = tx; - - assert batchSize > 0; - - _key.setReuseBuffer(true); - - // - // dlen is 0, so we should not retrieve any value - // - _value.setPartial(true); - } - - private java.util.Iterator<Ice.Identity> - nextBatch() - { - if(!_more) - { - return null; - } - - EvictorI.DeactivateController deactivateController = _store.evictor().deactivateController(); - deactivateController.lock(); - - com.sleepycat.db.Transaction txn = _tx == null ? null : _tx.dbTxn(); - - try - { - Ice.Communicator communicator = _store.communicator(); - Ice.EncodingVersion encoding = _store.encoding(); - - byte[] firstKey = null; - if(_key.getSize() > 0) - { - firstKey = new byte[_key.getSize()]; - System.arraycopy(_key.getData(), 0, firstKey, 0, firstKey.length); - } - - for(;;) - { - com.sleepycat.db.Cursor dbc = null; - - _batch = new java.util.ArrayList<Ice.Identity>(); - - try - { - // - // Move to the first record - // - boolean range = false; - if(firstKey != null) - { - // - // _key represents the next element not yet returned - // if it has been deleted, we want the one after - // - range = true; - } - - dbc = _store.db().openCursor(txn, null); - - boolean done = false; - do - { - com.sleepycat.db.OperationStatus status; - if(range) - { - status = dbc.getSearchKeyRange(_key, _value, null); - } - else - { - status = dbc.getNext(_key, _value, null); - } - _more = (status == com.sleepycat.db.OperationStatus.SUCCESS); - - if(_more) - { - range = false; - - if(_batch.size() < _batchSize) - { - Ice.Identity ident = ObjectStore.unmarshalKey(_key, communicator, encoding); - _batch.add(ident); - } - else - { - // - // Keep the last element in _key - // - done = true; - } - } - } - while(!done && _more); - - break; // for (;;) - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_store.evictor().deadlockWarning()) - { - communicator.getLogger().warning("Deadlock in Freeze.EvictorIteratorI.load while " + - "iterating over Db \"" + _store.evictor().filename() + "/" + - _store.dbName() + "\""); - } - - if(_tx == null) - { - if(firstKey != null) - { - assert(_key.getData().length >= firstKey.length); - System.arraycopy(firstKey, 0, _key.getData(), 0, firstKey.length); - _key.setSize(firstKey.length); - } - else - { - _key.setSize(0); - } - - // - // Retry - // - } - else - { - throw new DeadlockException(_store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), - _tx, dx); - } - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), dx); - } - finally - { - if(dbc != null) - { - try - { - dbc.close(); - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_tx != null) - { - throw new DeadlockException( - _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), _tx, dx); - } - } - catch(com.sleepycat.db.DatabaseException dx) - { - // - // Ignored - // - } - } - } - } - - if(_batch.size() == 0) - { - return null; - } - else - { - return _batch.listIterator(); - } - } - finally - { - deactivateController.unlock(); - } - } - - private final ObjectStore _store; - private final TransactionI _tx; - private final int _batchSize; - private java.util.Iterator<Ice.Identity> _batchIterator; - - private final com.sleepycat.db.DatabaseEntry _key = new com.sleepycat.db.DatabaseEntry(); - private final com.sleepycat.db.DatabaseEntry _value = new com.sleepycat.db.DatabaseEntry(); - private java.util.List<Ice.Identity> _batch = null; - private boolean _more = true; -} diff --git a/java/src/Freeze/src/main/java/Freeze/FatalErrorCallback.java b/java/src/Freeze/src/main/java/Freeze/FatalErrorCallback.java deleted file mode 100644 index bd77d5fa059..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/FatalErrorCallback.java +++ /dev/null @@ -1,25 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -/** - * Handles fatal errors encountered by the background save evictor. - */ -public interface FatalErrorCallback -{ - /** - * Called when the background save evictor encounters a fatal error. - * - * @param evictor The evictor that detected the error. - * @param communicator The communicator associated with the evictor. - * @param ex The exception that caused the error. - */ - void handleError(Evictor evictor, Ice.Communicator communicator, RuntimeException ex); -} diff --git a/java/src/Freeze/src/main/java/Freeze/Index.java b/java/src/Freeze/src/main/java/Freeze/Index.java deleted file mode 100644 index 9a4e242e74e..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/Index.java +++ /dev/null @@ -1,402 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -import java.nio.ByteBuffer; - -public abstract class Index implements com.sleepycat.db.SecondaryKeyCreator -{ - // - // Implementation details - // - - @Override - public boolean - createSecondaryKey(com.sleepycat.db.SecondaryDatabase secondary, - com.sleepycat.db.DatabaseEntry key, - com.sleepycat.db.DatabaseEntry value, - com.sleepycat.db.DatabaseEntry result) - throws com.sleepycat.db.DatabaseException - { - Ice.Communicator communicator = _store.communicator(); - Ice.EncodingVersion encoding = _store.encoding(); - ObjectRecord rec = ObjectStore.unmarshalValue(value, communicator, encoding, _store.keepStats()); - - ByteBuffer secondaryKey = marshalKey(rec.servant); - if(secondaryKey != null) - { - result.setDataNIO(secondaryKey); - return true; - } - else - { - // - // Don't want to index this one - // - return false; - } - } - - public String - name() - { - return _name; - } - - public String - facet() - { - return _facet; - } - - protected - Index(String name, String facet) - { - _name = name; - _facet = facet; - } - - protected abstract ByteBuffer marshalKey(Ice.Object servant); - - protected Ice.Identity[] - untypedFindFirst(ByteBuffer k, int firstN) - { - EvictorI.DeactivateController deactivateController = _store.evictor().deactivateController(); - deactivateController.lock(); - try - { - com.sleepycat.db.DatabaseEntry key = new com.sleepycat.db.DatabaseEntry(k); - - // - // When we have a custom-comparison function, Berkeley DB returns - // the key on-disk (when it finds one). We disable this behavior: - // (ref Oracle SR 5925672.992) - // - // In DB > 5.1.x we can not set DB_DBT_PARTIAL in the key Dbt when calling - // getSearchKey. - // - if(com.sleepycat.db.Environment.getVersionMajor() < 5 || - (com.sleepycat.db.Environment.getVersionMajor() == 5 && - com.sleepycat.db.Environment.getVersionMinor() <= 1)) - { - key.setPartial(true); - } - - com.sleepycat.db.DatabaseEntry pkey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry value = new com.sleepycat.db.DatabaseEntry(); - // - // dlen is 0, so we should not retrieve any value - // - value.setPartial(true); - - Ice.Communicator communicator = _store.communicator(); - Ice.EncodingVersion encoding = _store.encoding(); - - TransactionI transaction = _store.evictor().beforeQuery(); - com.sleepycat.db.Transaction tx = transaction == null ? null : transaction.dbTxn(); - - java.util.List<Ice.Identity> identities; - - for(;;) - { - com.sleepycat.db.SecondaryCursor dbc = null; - identities = new java.util.ArrayList<Ice.Identity>(); - - try - { - // - // Move to the first record - // - dbc = _db.openSecondaryCursor(tx, null); - boolean first = true; - - boolean found; - - do - { - com.sleepycat.db.OperationStatus status; - if(first) - { - status = dbc.getSearchKey(key, pkey, value, null); - } - else - { - status = dbc.getNextDup(key, pkey, value, null); - } - - found = status == com.sleepycat.db.OperationStatus.SUCCESS; - - if(found) - { - Ice.Identity ident = ObjectStore.unmarshalKey(pkey, communicator, encoding); - identities.add(ident); - first = false; - } - } - while((firstN <= 0 || identities.size() < firstN) && found); - - break; // for(;;) - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_store.evictor().deadlockWarning()) - { - communicator.getLogger().warning("Deadlock in Freeze.Index.untypedFindFirst while " + - "iterating over Db \"" + _store.evictor().filename() + - "/" + _dbName + "\""); - } - - if(tx != null) - { - throw new DeadlockException( _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), - transaction, dx); - } - - // - // Otherwise retry - // - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), dx); - } - finally - { - if(dbc != null) - { - try - { - dbc.close(); - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(tx != null) - { - throw new DeadlockException( - _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), transaction, dx); - } - } - catch(com.sleepycat.db.DatabaseException dx) - { - // - // Ignored - // - } - } - } - } - - if(identities.size() != 0) - { - Ice.Identity[] result = new Ice.Identity[identities.size()]; - return identities.toArray(result); - } - else - { - return new Ice.Identity[0]; - } - } - finally - { - deactivateController.unlock(); - } - } - - protected Ice.Identity[] - untypedFind(ByteBuffer key) - { - return untypedFindFirst(key, 0); - } - - protected int - untypedCount(ByteBuffer k) - { - EvictorI.DeactivateController deactivateController = _store.evictor().deactivateController(); - deactivateController.lock(); - try - { - com.sleepycat.db.DatabaseEntry key = new com.sleepycat.db.DatabaseEntry(k); - - // - // When we have a custom-comparison function, Berkeley DB returns - // the key on-disk (when it finds one). We disable this behavior: - // (ref Oracle SR 5925672.992) - // - // In DB > 5.1.x we can not set DB_DBT_PARTIAL in the key Dbt when calling - // getSearchKey. - // - if(com.sleepycat.db.Environment.getVersionMajor() < 5 || - (com.sleepycat.db.Environment.getVersionMajor() == 5 && - com.sleepycat.db.Environment.getVersionMinor() <= 1)) - { - key.setPartial(true); - } - - com.sleepycat.db.DatabaseEntry value = new com.sleepycat.db.DatabaseEntry(); - // - // dlen is 0, so we should not retrieve any value - // - value.setPartial(true); - TransactionI transaction = _store.evictor().beforeQuery(); - com.sleepycat.db.Transaction tx = transaction == null ? null : transaction.dbTxn(); - - for(;;) - { - com.sleepycat.db.Cursor dbc = null; - try - { - dbc = _db.openCursor(tx, null); - if(dbc.getSearchKey(key, value, null) == com.sleepycat.db.OperationStatus.SUCCESS) - { - return dbc.count(); - } - else - { - return 0; - } - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_store.evictor().deadlockWarning()) - { - _store.communicator().getLogger().warning("Deadlock in Freeze.Index.untypedCount while " + - "iterating over Db \"" + - _store.evictor().filename() + "/" + _dbName + "\""); - } - - if(tx != null) - { - throw new DeadlockException( _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), - transaction, dx); - } - // - // Otherwise retry - // - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), dx); - } - finally - { - if(dbc != null) - { - try - { - dbc.close(); - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(tx != null) - { - throw new DeadlockException( - _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), transaction, dx); - } - } - catch(com.sleepycat.db.DatabaseException dx) - { - // - // Ignored - // - } - } - } - } - } - finally - { - deactivateController.unlock(); - } - } - - protected final Ice.Communicator - communicator() - { - return _store.communicator(); - } - - protected final Ice.EncodingVersion - encoding() - { - return _store.encoding(); - } - - void - associate(ObjectStore store, com.sleepycat.db.Transaction txn, boolean createDb, boolean populateIndex) - throws com.sleepycat.db.DatabaseException, java.io.FileNotFoundException - { - assert(txn != null); - _store = store; - - _dbName = EvictorI.indexPrefix + store.dbName() + "." + _name; - - com.sleepycat.db.SecondaryConfig config = new com.sleepycat.db.SecondaryConfig(); - config.setAllowCreate(createDb); - config.setAllowPopulate(populateIndex); - config.setSortedDuplicates(true); - config.setType(com.sleepycat.db.DatabaseType.BTREE); - config.setKeyCreator(this); - - Ice.Properties properties = store.evictor().communicator().getProperties(); - String propPrefix = "Freeze.Evictor." + store.evictor().filename() + "."; - - int btreeMinKey = properties.getPropertyAsInt(propPrefix + _dbName + ".BtreeMinKey"); - if(btreeMinKey > 2) - { - if(store.evictor().trace() >= 1) - { - store.evictor().communicator().getLogger().trace( - "Freeze.Evictor", "Setting \"" + store.evictor().filename() + "." + _dbName + - "\"'s btree minkey to " + btreeMinKey); - } - config.setBtreeMinKey(btreeMinKey); - } - - boolean checksum = properties.getPropertyAsInt(propPrefix + "Checksum") > 0; - if(checksum) - { - // - // No tracing - // - config.setChecksum(true); - } - - // - // Can't change page size - // - - _db = _store.evictor().dbEnv().getEnv().openSecondaryDatabase(txn, _store.evictor().filename(), _dbName, - _store.db(), config); - } - - void - close() - { - if(_db != null) - { - try - { - _db.close(); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_store.evictor().errorPrefix() + "Db.close: " + dx.getMessage(), dx); - } - _db = null; - } - } - - private final String _name; - private final String _facet; - private String _dbName; - - private com.sleepycat.db.SecondaryDatabase _db = null; - private ObjectStore _store = null; -} diff --git a/java/src/Freeze/src/main/java/Freeze/LinkedList.java b/java/src/Freeze/src/main/java/Freeze/LinkedList.java deleted file mode 100644 index a082357161a..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/LinkedList.java +++ /dev/null @@ -1,242 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -// -// Stripped down LinkedList implementation for use in the Evictor. The -// API is similar to java.util.LinkedList. -// -// Major differences: -// * listIterator() is not implemented. -// * Operation riterator() returns a reverse iterator. -// * This implementation also has the property that an Iterator can be -// retained over structural changes to the list itself (similar to an -// STL list). -// -public class LinkedList<T> -{ - public - LinkedList() - { - _header.next = _header.previous = _header; - } - - public T - getFirst() - { - if(_size == 0) - { - throw new java.util.NoSuchElementException(); - } - - return _header.next.element; - } - - public T - getLast() - { - if(_size == 0) - { - throw new java.util.NoSuchElementException(); - } - - return _header.previous.element; - } - - public void - addFirst(T o) - { - addBefore(o, _header.next); - } - - public boolean - isEmpty() - { - return _size == 0; - } - - public int - size() - { - return _size; - } - - public java.util.Iterator<T> - iterator() - { - return new ForwardIterator(); - } - - public java.util.Iterator<T> - riterator() - { - return new ReverseIterator(); - } - - private class ForwardIterator implements java.util.Iterator<T> - { - @Override - public boolean - hasNext() - { - return _next != null; - } - - @Override - public T - next() - { - if(_next == null) - { - throw new java.util.NoSuchElementException(); - } - - _current = _next; - - if(_next.next != _header) - { - _next = _next.next; - } - else - { - _next = null; - } - return _current.element; - } - - @Override - public void - remove() - { - if(_current == null) - { - throw new IllegalStateException(); - } - LinkedList.this.remove(_current); - _current = null; - } - - ForwardIterator() - { - if(_header.next == _header) - { - _next = null; - } - else - { - _next = _header.next; - } - _current = null; - } - - private Entry<T> _current; - private Entry<T> _next; - } - - private class ReverseIterator implements java.util.Iterator<T> - { - @Override - public boolean - hasNext() - { - return _next != null; - } - - @Override - public T - next() - { - if(_next == null) - { - throw new java.util.NoSuchElementException(); - } - - _current = _next; - - if(_next.previous != _header) - { - _next = _next.previous; - } - else - { - _next = null; - } - return _current.element; - } - - @Override - public void - remove() - { - if(_current == null) - { - throw new IllegalStateException(); - } - LinkedList.this.remove(_current); - _current = null; - } - - ReverseIterator() - { - if(_header.next == _header) - { - _next = null; - } - else - { - _next = _header.previous; - } - _current = null; - } - - private Entry<T> _current; - private Entry<T> _next; - } - - private static class Entry<T> - { - T element; - Entry<T> next; - Entry<T> previous; - - Entry(T element, Entry<T> next, Entry<T> previous) - { - this.element = element; - this.next = next; - this.previous = previous; - } - } - - private Entry<T> - addBefore(T o, Entry<T> e) - { - Entry<T> newEntry = new Entry<T>(o, e, e.previous); - newEntry.previous.next = newEntry; - newEntry.next.previous = newEntry; - _size++; - return newEntry; - } - - private void - remove(Entry<T> e) - { - if(e == _header) - { - throw new java.util.NoSuchElementException(); - } - - e.previous.next = e.next; - e.next.previous = e.previous; - _size--; - } - - private Entry<T> _header = new Entry<T>(null, null, null); - private int _size = 0; -} diff --git a/java/src/Freeze/src/main/java/Freeze/Map.java b/java/src/Freeze/src/main/java/Freeze/Map.java deleted file mode 100644 index bb26350041a..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/Map.java +++ /dev/null @@ -1,75 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -/** - * Interface for Freeze maps. - * - * @see Connection - **/ -public interface Map<K, V> extends NavigableMap<K, V> -{ - /** - * Alternative to <code>java.util.SortedMap.put</code>. This version - * is more efficient because it does not decode and return the - * old value. - **/ - void fastPut(K key, V value); - - /** - * Closes the database associated with this map, as well as all open iterators. - * A map must be closed when it is no longer needed, either directly, or by - * closing the {@link Connection} associated with this map. - **/ - void close(); - - /** - * Closes all iterators for this map. - * - * @return The number of iterators that were closed. - **/ - int closeAllIterators(); - - /** - * Closes this map and destroys the underlying Berkeley DB database along with any indexes. - **/ - void destroy(); - - /** - * An <code>EntryIterator</code> allows the application to explicitly - * close an iterator and free resources allocated for the iterator - * in a timely fashion. - **/ - public interface EntryIterator<T> extends java.util.Iterator<T> - { - /** - * Closes this iterator, reclaiming associated resources. - **/ - void close(); - - /** - * Closes this iterator, reclaiming associated resources. This - * method is an alias for {@link #close}. - **/ - void destroy(); - } - - /** - * Returns the connection associated with this map. - * - * @return The connection associated with this map. - **/ - Connection getConnection(); - - /** - * Closes the database for this map. - **/ - void closeDb(); -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapDb.java b/java/src/Freeze/src/main/java/Freeze/MapDb.java deleted file mode 100644 index fdecb81cf35..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapDb.java +++ /dev/null @@ -1,401 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -// -// A MapDb represents the Db object underneath Freeze Maps. Several Freeze Maps often -// share the very same MapDb object; SharedDbEnv manages these shared MapDb objects. -// - -public class MapDb -{ - public - MapDb(ConnectionI connection, String dbName, String key, String value, java.util.Comparator<?> comparator, - MapIndex[] indices, boolean createDb) - { - _communicator = connection.communicator(); - _dbName = dbName; - _errorPrefix = "Freeze DB DbEnv(\"" + connection.dbEnv().getEnvName() + "\") Db(\"" + dbName + "\"): "; - _indices = indices; - _trace = connection.trace(); - - Catalog catalog = new Catalog(connection, Util.catalogName(), true); - CatalogData catalogData = catalog.get(_dbName); - if(catalogData != null) - { - if(catalogData.evictor) - { - throw new DatabaseException(_errorPrefix + "is not an evictor"); - } - _key = catalogData.key; - _value = catalogData.value; - checkTypes(key, value); - } - else - { - _key = key; - _value = value; - } - - com.sleepycat.db.DatabaseConfig config = new com.sleepycat.db.DatabaseConfig(); - - config.setAllowCreate(createDb); - config.setType(com.sleepycat.db.DatabaseType.BTREE); - - if(comparator != null) - { - config.setBtreeComparator(comparator); - } - Ice.Properties properties = _communicator.getProperties(); - String propPrefix = "Freeze.Map." + _dbName + "."; - - int btreeMinKey = properties.getPropertyAsInt(propPrefix + "BtreeMinKey"); - if(btreeMinKey > 2) - { - if(_trace >= 1) - { - _communicator.getLogger().trace( - "Freeze.Map", "Setting \"" + _dbName + "\"'s btree minkey to " + btreeMinKey); - } - config.setBtreeMinKey(btreeMinKey); - } - - boolean checksum = properties.getPropertyAsInt(propPrefix + "Checksum") > 0; - if(checksum) - { - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Map", "Turning checksum on for \"" + _dbName + "\""); - } - - config.setChecksum(true); - } - - int pageSize = properties.getPropertyAsInt(propPrefix + "PageSize"); - if(pageSize > 0) - { - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Map", "Setting \"" + _dbName + "\"'s pagesize to " + pageSize); - } - config.setPageSize(pageSize); - } - - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Map", "opening Db \"" + _dbName + "\""); - } - - Transaction tx = connection.currentTransaction(); - boolean ownTx = (tx == null); - - for(;;) - { - try - { - if(ownTx) - { - tx = null; - tx = connection.beginTransaction(); - } - - com.sleepycat.db.Transaction txn = Util.getTxn(tx); - - _db = connection.dbEnv().getEnv().openDatabase(txn, _dbName, null, config); - - String[] oldIndices = null; - java.util.List<String> newIndices = new java.util.LinkedList<String>(); - - CatalogIndexList catalogIndexList = new CatalogIndexList(connection, Util.catalogIndexListName(), true); - - if(createDb) - { - oldIndices = catalogIndexList.get(_dbName); - } - - if(_indices != null) - { - for(MapIndex i : _indices) - { - String indexName = i.name(); - - i.associate(_dbName, _db, txn, createDb); - - if(createDb) - { - if(oldIndices != null) - { - int j = java.util.Arrays.asList(oldIndices).indexOf(indexName); - if(j != -1) - { - oldIndices[j] = null; - } - } - newIndices.add(indexName); - } - } - } - - if(catalogData == null) - { - catalogData = new CatalogData(); - catalogData.evictor = false; - catalogData.key = key; - catalogData.value = value; - catalog.put(_dbName, catalogData); - } - - if(createDb) - { - boolean indexRemoved = false; - - if(oldIndices != null) - { - // - // Remove old indices and write the new ones - // - for(String index : oldIndices) - { - if(index != null) - { - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Map", "removing old index \"" + index + - "\" on Db \"" + _dbName + "\""); - } - - indexRemoved = true; - - try - { - connection.removeMapIndex(_dbName, index); - } - catch(IndexNotFoundException ife) - { - // Ignored - - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Map", "index \"" + index + - "\" on Db \"" + _dbName + "\" does not exist"); - } - } - } - } - } - - int oldSize = oldIndices == null ? 0 : oldIndices.length; - - if(indexRemoved || newIndices.size() != oldSize) - { - if(newIndices.size() == 0) - { - catalogIndexList.remove(_dbName); - if(_trace >= 1) - { - _communicator.getLogger().trace( - "Freeze.Map", "Removed catalogIndexList entry for Db \"" + _dbName + "\""); - } - } - else - { - catalogIndexList.put(_dbName, newIndices.toArray(new String[0])); - if(_trace >= 1) - { - _communicator.getLogger().trace( - "Freeze.Map", "Updated catalogIndexList entry for Db \"" + _dbName + "\""); - } - } - } - } - - if(ownTx) - { - try - { - tx.commit(); - } - finally - { - tx = null; - } - } - break; // for(;;) - } - catch(java.io.FileNotFoundException dx) - { - clearIndices(); - throw new NotFoundException(_errorPrefix + "Db.open: " + dx.getMessage(), dx); - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(ownTx) - { - if(connection.deadlockWarning()) - { - connection.communicator().getLogger().warning("Deadlock in Freeze.Shared.Shared on Db \"" + - _dbName + "\"; retrying ..."); - } - tx = null; - } - else - { - clearIndices(); - throw new DeadlockException(_errorPrefix + "Db.open: " + dx.getMessage(), tx, dx); - } - } - catch(com.sleepycat.db.DatabaseException dx) - { - clearIndices(); - throw new DatabaseException(_errorPrefix + "Db.open: " + dx.getMessage(), dx); - } - finally - { - if(ownTx && tx != null) - { - try - { - tx.rollback(); - } - catch(DatabaseException de) - { - } - } - } - } - } - - // - // The constructor for catalogs - // - - MapDb(Ice.Communicator communicator, String envName, String dbName, String key, - String value, com.sleepycat.db.Environment dbEnv) - throws com.sleepycat.db.DatabaseException - { - _communicator = communicator; - _dbName = dbName; - _errorPrefix = "Freeze DB DbEnv(\"" + envName + "\") Db(\"" + dbName + "\"): "; - _key = key; - _value = value; - _trace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Map"); - - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Map", "opening Db \"" + _dbName + "\""); - } - - com.sleepycat.db.DatabaseConfig config = new com.sleepycat.db.DatabaseConfig(); - config.setAllowCreate(true); - config.setType(com.sleepycat.db.DatabaseType.BTREE); - config.setTransactional(true); - - try - { - _db = dbEnv.openDatabase(null, _dbName, null, config); - } - catch(java.io.FileNotFoundException dx) - { - // - // This should never happen - // - throw new NotFoundException(_errorPrefix + "Db.open: " + dx.getMessage(), dx); - } - } - - public void - close() - { - if(_trace >= 1) - { - _communicator.getLogger().trace("Freeze.Map", "closing Db \"" + _dbName + "\""); - } - - clearIndices(); - - if(_db != null) - { - try - { - _db.close(); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_errorPrefix + "close: " + dx.getMessage(), dx); - } - finally - { - _db = null; - } - } - } - - void - connectIndices(MapIndex[] indices) - { - if(indices != null) - { - assert(_indices != null && indices.length == _indices.length); - - for(int i = 0; i < indices.length; ++i) - { - indices[i].init(_indices[i]); - } - } - } - - void - clearIndices() - { - if(_indices != null) - { - for(MapIndex i : _indices) - { - i.close(); - } - _indices = null; - } - } - - public com.sleepycat.db.Database - db() - { - return _db; - } - - public String - dbName() - { - return _dbName; - } - - void - checkTypes(String key, String value) - { - if(!key.equals(_key)) - { - throw new DatabaseException(_errorPrefix + _dbName + "'s key type is " + _key + ", not " + key); - } - - if(!value.equals(_value)) - { - throw new DatabaseException(_errorPrefix + _dbName + "'s value type is " + _value + ", not " + value); - } - } - - private com.sleepycat.db.Database _db; - private final Ice.Communicator _communicator; - private final String _dbName; - private final String _errorPrefix; - private String _key; - private String _value; - private final int _trace; - private MapIndex[] _indices; -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapIndex.java b/java/src/Freeze/src/main/java/Freeze/MapIndex.java deleted file mode 100644 index 2c7a0b41b73..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapIndex.java +++ /dev/null @@ -1,22 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -public interface MapIndex -{ - String name(); - - void associate(String dbName, com.sleepycat.db.Database db, com.sleepycat.db.Transaction txn, boolean createDb) - throws com.sleepycat.db.DatabaseException, java.io.FileNotFoundException; - - void init(MapIndex i); - - void close(); -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/EntryI.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/EntryI.java deleted file mode 100644 index 14cded23fee..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/EntryI.java +++ /dev/null @@ -1,137 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -import java.nio.ByteBuffer; - -class EntryI<K, V> implements java.util.Map.Entry<K, V> -{ - public - EntryI(MapI<K, V> map, K key, com.sleepycat.db.DatabaseEntry dbKey, ByteBuffer valueBytes, ByteBuffer indexBytes) - { - _map = map; - _dbKey = dbKey; - _valueBytes = valueBytes; - _indexBytes = indexBytes; - _key = key; - _haveKey = key != null; - } - - @Override - public K - getKey() - { - if(!_haveKey) - { - assert(_dbKey != null); - _key = _map.decodeKey(_dbKey); - _haveKey = true; - } - return _key; - } - - @Override - public V - getValue() - { - if(!_haveValue) - { - assert(_valueBytes != null); - _value = _map.decodeValue(_valueBytes); - _haveValue = true; - // - // Not needed anymore - // - _valueBytes = null; - } - return _value; - } - - public ByteBuffer - getIndexBytes() - { - return _indexBytes; - } - - @Override - public V - setValue(V value) - { - V old = getValue(); - if(_iterator != null) - { - _iterator.setValue(this, value); - } - else - { - _map.putImpl(_dbKey, value); - } - _value = value; - _haveValue = true; - return old; - } - - @Override - public boolean - equals(Object o) - { - if(!(o instanceof EntryI)) - { - return false; - } - @SuppressWarnings("unchecked") - EntryI<K, V> e = (EntryI<K, V>)o; - return eq(getKey(), e.getKey()) && eq(getValue(), e.getValue()); - } - - @Override - public int - hashCode() - { - return ((getKey() == null) ? 0 : getKey().hashCode()) ^ - ((getValue() == null) ? 0 : getValue().hashCode()); - } - - @Override - public String - toString() - { - return getKey() + "=" + getValue(); - } - - void - iterator(IteratorI<K, V> iterator) - { - _iterator = iterator; - } - - com.sleepycat.db.DatabaseEntry - getDbKey() - { - return _dbKey; - } - - private static boolean - eq(Object o1, Object o2) - { - return (o1 == null ? o2 == null : o1.equals(o2)); - } - - private MapI<K, V> _map; - private com.sleepycat.db.DatabaseEntry _dbKey; - private ByteBuffer _valueBytes; - private ByteBuffer _indexBytes; - - private K _key; - private boolean _haveKey = false; - private V _value; - private boolean _haveValue = false; - private IteratorI<K, V> _iterator; -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/Index.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/Index.java deleted file mode 100644 index 191e03098ea..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/Index.java +++ /dev/null @@ -1,752 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -import Freeze.DatabaseException; -import Freeze.DeadlockException; -import Freeze.MapIndex; -import Freeze.NavigableMap; -import java.nio.ByteBuffer; - -public abstract class Index<K, V, I> - implements MapIndex, KeyCodec<I>, com.sleepycat.db.SecondaryKeyCreator, java.util.Comparator<ByteBuffer> -{ - protected - Index(MapI<K, V> map, String name, java.util.Comparator<I> comparator) - { - _map = map; - _name = name; - _comparator = comparator; - } - - // - // Subclasses define this so that we can extract the index key from a value. - // - protected abstract I extractKey(V value); - - // - // MapIndex methods - // - - @Override - public String - name() - { - return _name; - } - - @Override - public void - associate(String dbName, com.sleepycat.db.Database db, com.sleepycat.db.Transaction txn, boolean createDb) - throws com.sleepycat.db.DatabaseException, java.io.FileNotFoundException - { - _dbName = dbName + "." + _name; - _trace = new TraceLevels(_map.connection(), _dbName); - - assert(txn != null); - assert(_db == null); - - com.sleepycat.db.SecondaryConfig config = new com.sleepycat.db.SecondaryConfig(); - config.setAllowCreate(createDb); - config.setAllowPopulate(true); // We always populate empty indices - config.setSortedDuplicates(true); - config.setType(com.sleepycat.db.DatabaseType.BTREE); - if(_comparator != null) - { - java.util.Comparator<byte[]> c = new java.util.Comparator<byte[]>() - { - @Override - public int compare(byte[] a1, byte[] a2) - { - return Index.this.compare(ByteBuffer.wrap(a1), ByteBuffer.wrap(a2)); - } - }; - config.setBtreeComparator(c); - } - config.setKeyCreator(this); - - Ice.Properties properties = _map.connection().getCommunicator().getProperties(); - String propPrefix = "Freeze.Map." + _dbName + "."; - - int btreeMinKey = properties.getPropertyAsInt(propPrefix + "BtreeMinKey"); - if(btreeMinKey > 2) - { - if(_trace.level >= 1) - { - _trace.logger.trace("Freeze.Map", "Setting \"" + _dbName + "\"'s btree minkey to " + btreeMinKey); - } - config.setBtreeMinKey(btreeMinKey); - } - - boolean checksum = properties.getPropertyAsInt(propPrefix + "Checksum") > 0; - if(checksum) - { - if(_trace.level >= 1) - { - _trace.logger.trace("Freeze.Map", "Turning checksum on for \"" + _dbName + "\""); - } - config.setChecksum(true); - } - - int pageSize = properties.getPropertyAsInt(propPrefix + "PageSize"); - if(pageSize > 0) - { - if(_trace.level >= 1) - { - _trace.logger.trace("Freeze.Map", "Setting \"" + _dbName + "\"'s pagesize to " + pageSize); - } - config.setPageSize(pageSize); - } - - _db = _map.connection().dbEnv().getEnv().openSecondaryDatabase(txn, _dbName, null, db, config); - } - - @Override - public void - init(MapIndex f) - { - @SuppressWarnings("unchecked") - Index<K, V, I> from = (Index<K, V, I>)f; - - assert(_name.equals(from._name)); - assert(_db == null); - - _dbName = from._dbName; - _db = from._db; - _comparator = from._comparator; - _trace = _map.traceLevels(); - } - - @Override - public void - close() - { - // - // close() is called by MapDb only on the "main" index - // (the one that was associated) - // - - if(_db != null) - { - try - { - _db.close(); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException( - _trace.errorPrefix + "Db.close for index \"" + _dbName + "\": " + dx.getMessage(), dx); - } - _db = null; - } - } - - // - // SecondaryKeyCreator methods - // - - @Override - public boolean - createSecondaryKey(com.sleepycat.db.SecondaryDatabase secondary, - com.sleepycat.db.DatabaseEntry key, - com.sleepycat.db.DatabaseEntry value, - com.sleepycat.db.DatabaseEntry result) - throws com.sleepycat.db.DatabaseException - { - ByteBuffer secondaryKey = marshalKey(UtilI.getBuffer(value)); - assert(secondaryKey != null); - - result.setDataNIO(secondaryKey); - return true; - } - - // - // java.util.Comparator<ByteBuffer>.compare() - // - @Override - public int compare(ByteBuffer b1, ByteBuffer b2) - { - assert(_comparator != null); - return _comparator.compare(decodeKey(b1), decodeKey(b2)); - } - - private class FindModel implements IteratorModel<K, V> - { - FindModel(I key, boolean onlyDups) - { - _fromKey = key; - _onlyDups = onlyDups; - } - - @Override - public String - dbName() - { - return Index.this.dbName(); - } - - @Override - public TraceLevels - traceLevels() - { - return _trace; - } - - @Override - public com.sleepycat.db.Cursor - openCursor() - throws com.sleepycat.db.DatabaseException - { - return _db.openSecondaryCursor(_map.connection().dbTxn(), null); - } - - @Override - public EntryI<K, V> - firstEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return Index.this.findFirstEntry(cursor, _fromKey); - } - - @Override - public EntryI<K, V> - nextEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return Index.this.findNextEntry(cursor, _onlyDups); - } - - private final I _fromKey; - private final boolean _onlyDups; - } - - public IteratorI<K, V> - find(I key, boolean onlyDups) - { - return new IteratorI<K, V>(_map, new FindModel(key, onlyDups)); - } - - public IteratorI<K, V> - find(I key) - { - return find(key, true); - } - - public int - count(I key) - { - ByteBuffer k = encodeKey(key); - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(k); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - - // - // When we have a custom-comparison function, Berkeley DB returns - // the key on-disk (when it finds one). We disable this behavior: - // (ref Oracle SR 5925672.992) - // - // In DB > 5.1.x we can not set DB_DBT_PARTIAL in the key Dbt when calling - // getSearchKey. - // - if(com.sleepycat.db.Environment.getVersionMajor() < 5 || - (com.sleepycat.db.Environment.getVersionMajor() == 5 && - com.sleepycat.db.Environment.getVersionMinor() <= 1)) - { - dbKey.setPartial(true); - } - - // - // dlen is 0, so we should not retrieve any value - // - dbValue.setPartial(true); - - try - { - for(;;) - { - try - { - com.sleepycat.db.Cursor dbc = null; - try - { - dbc = _db.openCursor(_map.connection().dbTxn(), null); - if(dbc.getSearchKey(dbKey, dbValue, null) == com.sleepycat.db.OperationStatus.SUCCESS) - { - return dbc.count(); - } - else - { - return 0; - } - } - finally - { - if(dbc != null) - { - dbc.close(); - } - } - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_map.connection().dbTxn() != null) - { - throw new DeadlockException(_trace.errorPrefix + "Dbc.count: " + dx.getMessage(), - _map.connection().currentTransaction(), dx); - } - else - { - - if(_trace.deadlockWarning) - { - _trace.logger.warning( - "Deadlock in Freeze.MapInternal.Index.count while iterating over index \"" + _dbName + - "\"; retrying..."); - } - - // - // Retry - // - } - } - } - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException( - _trace.errorPrefix + "Db.cursor for index \"" + _dbName + "\": " + dx.getMessage(), dx); - } - } - - // - // Used by subclasses to implement headMapForXXX. - // - public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - createHeadMap(I toKey, boolean inclusive) - { - if(toKey == null) - { - throw new NullPointerException(); - } - - if(_comparator == null) - { - throw new UnsupportedOperationException("Index '" + _name + "' has no user-defined comparator"); - } - - return new IndexedSubMap<K, V, I>(this, null, false, toKey, inclusive); - } - - // - // Used by subclasses to implement tailMapForXXX. - // - public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - createTailMap(I fromKey, boolean inclusive) - { - if(fromKey == null) - { - throw new NullPointerException(); - } - - if(_comparator == null) - { - throw new UnsupportedOperationException("Index '" + _name + "' has no user-defined comparator"); - } - - return new IndexedSubMap<K, V, I>(this, fromKey, inclusive, null, false); - } - - // - // Used by subclasses to implement subMapForXXX. - // - public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - createSubMap(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - if(fromKey == null || toKey == null ) - { - throw new NullPointerException(); - } - - if(_comparator == null) - { - throw new UnsupportedOperationException("Index '" + _name + "' has no user-defined comparator"); - } - - return new IndexedSubMap<K, V, I>(this, fromKey, fromInclusive, toKey, toInclusive); - } - - // - // Used by subclasses to implement mapForXXX. - // - public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - createMap() - { - if(_comparator == null) - { - throw new UnsupportedOperationException("Index '" + _name + "' has no user-defined comparator"); - } - - return new IndexedSubMap<K, V, I>(this, null, false, null, false); - } - - ByteBuffer encodeKey(I k) - { - IceInternal.BasicStream str = _map.createWriteStream(); - encodeKey(k, str); - return str.prepareWrite().b; - } - - I decodeKey(ByteBuffer buf) - { - return decodeKey(_map.createReadStream(buf)); - } - - I decodeKey(com.sleepycat.db.DatabaseEntry entry) - { - ByteBuffer b = entry.getDataNIO(); - if(b != null) - { - return decodeKey(_map.createReadStream(b)); - } - else - { - byte[] arr = entry.getData(); - assert(arr != null && entry.getOffset() == 0 && entry.getSize() == arr.length); - return decodeKey(_map.createReadStream(arr)); - } - } - - com.sleepycat.db.SecondaryDatabase - db() - { - return _db; - } - - String - dbName() - { - return _dbName; - } - - TraceLevels - traceLevels() - { - return _trace; - } - - java.util.Comparator<I> - comparator() - { - return _comparator; - } - - MapI<K, V> - parent() - { - return _map; - } - - boolean - containsKey(Object o) - { - @SuppressWarnings("unchecked") - I key = (I)o; - - ByteBuffer k = encodeKey(key); - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(k); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - dbValue.setPartial(true); - - if(_trace.level >= 2) - { - _trace.logger.trace("Freeze.MapInternal.Index", "checking key in Db \"" + _dbName + "\""); - } - - for(;;) - { - try - { - return _db.get(_map.connection().dbTxn(), dbKey, dbValue, null) == - com.sleepycat.db.OperationStatus.SUCCESS; - } - catch(com.sleepycat.db.DeadlockException e) - { - if(_map.connection().dbTxn() != null) - { - throw new DeadlockException(_trace.errorPrefix + "Db.get: " + e.getMessage(), - _map.connection().currentTransaction(), e); - } - else - { - if(_trace.deadlockWarning) - { - _trace.logger.warning( - "Deadlock in Freeze.MapInternal.Index.containsKey while " + "reading Db \"" + _dbName + - "\"; retrying..."); - } - // - // Try again - // - } - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Db.get: " + e.getMessage(), e); - } - } - } - - // - // Used by the iterator created by the findByXXX methods. - // - EntryI<K, V> - findFirstEntry(com.sleepycat.db.Cursor cursor, I fromKey) - throws com.sleepycat.db.DatabaseException - { - com.sleepycat.db.SecondaryCursor c = (com.sleepycat.db.SecondaryCursor)cursor; - - assert(fromKey != null); - ByteBuffer k = encodeKey(fromKey); - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbIKey = new com.sleepycat.db.DatabaseEntry(k); - - if(c.getSearchKey(dbIKey, dbKey, dbValue, null) == com.sleepycat.db.OperationStatus.SUCCESS) - { - return new EntryI<K, V>(_map, null, dbKey, UtilI.getBuffer(dbValue), UtilI.getBuffer(dbIKey)); - } - - return null; - } - - // - // Used by the iterator created by the findByXXX methods. - // - EntryI<K, V> - findNextEntry(com.sleepycat.db.Cursor cursor, boolean onlyDups) - throws com.sleepycat.db.DatabaseException - { - com.sleepycat.db.SecondaryCursor c = (com.sleepycat.db.SecondaryCursor)cursor; - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbIKey = new com.sleepycat.db.DatabaseEntry(); - - com.sleepycat.db.OperationStatus status; - - if(onlyDups) - { - status = c.getNextDup(dbIKey, dbKey, dbValue, null); - } - else - { - status = c.getNext(dbIKey, dbKey, dbValue, null); - } - - if(status == com.sleepycat.db.OperationStatus.SUCCESS) - { - return new EntryI<K, V>(_map, null, dbKey, UtilI.getBuffer(dbValue), UtilI.getBuffer(dbIKey)); - } - - return null; - } - - // - // Used by IndexedSubMap for ascending maps. - // - EntryI<K, V> - firstEntry(com.sleepycat.db.Cursor cursor, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - throws com.sleepycat.db.DatabaseException - { - com.sleepycat.db.SecondaryCursor c = (com.sleepycat.db.SecondaryCursor)cursor; - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbIKey = new com.sleepycat.db.DatabaseEntry(); - - com.sleepycat.db.OperationStatus status; - - if(fromKey != null) - { - ByteBuffer k = encodeKey(fromKey); - dbIKey.setDataNIO(k); - dbIKey.setReuseBuffer(false); - - status = c.getSearchKeyRange(dbIKey, dbKey, dbValue, null); - - if(status == com.sleepycat.db.OperationStatus.SUCCESS && !fromInclusive) - { - int cmp = compare(UtilI.getBuffer(dbIKey), k); - assert(cmp >= 0); - if(cmp == 0) - { - status = c.getNextNoDup(dbIKey, dbKey, dbValue, null); - } - } - } - else - { - status = c.getFirst(dbIKey, dbKey, dbValue, null); - } - - if(status == com.sleepycat.db.OperationStatus.SUCCESS) - { - return newEntry(dbIKey, dbKey, dbValue, fromKey, fromInclusive, toKey, toInclusive); - } - - return null; - } - - // - // Used by IndexedSubMap for ascending maps. - // - EntryI<K, V> - nextEntry(com.sleepycat.db.Cursor cursor, I toKey, boolean toInclusive) - throws com.sleepycat.db.DatabaseException - { - com.sleepycat.db.SecondaryCursor c = (com.sleepycat.db.SecondaryCursor)cursor; - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbIKey = new com.sleepycat.db.DatabaseEntry(); - - if(c.getNextNoDup(dbIKey, dbKey, dbValue, null) == com.sleepycat.db.OperationStatus.SUCCESS) - { - return newEntry(dbIKey, dbKey, dbValue, null, false, toKey, toInclusive); - } - - return null; - } - - // - // Used by IndexedSubMap for descending maps. - // - EntryI<K, V> - lastEntry(com.sleepycat.db.Cursor cursor, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - throws com.sleepycat.db.DatabaseException - { - com.sleepycat.db.SecondaryCursor c = (com.sleepycat.db.SecondaryCursor)cursor; - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbIKey = new com.sleepycat.db.DatabaseEntry(); - - com.sleepycat.db.OperationStatus status; - - if(fromKey != null) - { - ByteBuffer k = encodeKey(fromKey); - dbIKey.setDataNIO(k); - dbIKey.setReuseBuffer(false); - - status = c.getSearchKeyRange(dbIKey, dbKey, dbValue, null); - - if(status == com.sleepycat.db.OperationStatus.SUCCESS && !fromInclusive) - { - int cmp = compare(UtilI.getBuffer(dbIKey), k); - assert(cmp >= 0); - if(cmp == 0) - { - status = c.getPrevNoDup(dbIKey, dbKey, dbValue, null); - } - } - } - else - { - status = c.getLast(dbIKey, dbKey, dbValue, null); - } - - if(status == com.sleepycat.db.OperationStatus.SUCCESS) - { - return newEntry(dbIKey, dbKey, dbValue, toKey, toInclusive, fromKey, fromInclusive); - } - - return null; - } - - // - // Used by IndexedSubMap for descending maps. - // - EntryI<K, V> - previousEntry(com.sleepycat.db.Cursor cursor, I toKey, boolean toInclusive) - throws com.sleepycat.db.DatabaseException - { - com.sleepycat.db.SecondaryCursor c = (com.sleepycat.db.SecondaryCursor)cursor; - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbIKey = new com.sleepycat.db.DatabaseEntry(); - - if(c.getPrevNoDup(dbIKey, dbKey, dbValue, null) == com.sleepycat.db.OperationStatus.SUCCESS) - { - return newEntry(dbIKey, dbKey, dbValue, toKey, toInclusive, null, false); - } - - return null; - } - - // - // marshalKey may be overridden by subclasses as an optimization. - // - protected ByteBuffer - marshalKey(ByteBuffer value) - { - V decodedValue = _map.decodeValue(value); - return encodeKey(extractKey(decodedValue)); - } - - private EntryI<K, V> - newEntry(com.sleepycat.db.DatabaseEntry dbIKey, com.sleepycat.db.DatabaseEntry dbKey, - com.sleepycat.db.DatabaseEntry dbValue, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - I key = null; - if(fromKey != null || toKey != null) - { - key = decodeKey(dbIKey); - if(!checkRange(key, fromKey, fromInclusive, toKey, toInclusive)) - { - return null; - } - } - - return new EntryI<K, V>(_map, null, dbKey, UtilI.getBuffer(dbValue), UtilI.getBuffer(dbIKey)); - } - - private boolean - checkRange(I key, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - if(fromKey != null) - { - int cmp = _comparator.compare(key, fromKey); - if((fromInclusive && cmp < 0) || (!fromInclusive && cmp <= 0)) - { - return false; - } - } - if(toKey != null) - { - int cmp = _comparator.compare(key, toKey); - if((toInclusive && cmp > 0) || (!toInclusive && cmp >= 0)) - { - return false; - } - } - return true; - } - - private MapI<K, V> _map; - private String _name; - private java.util.Comparator<I> _comparator; - private TraceLevels _trace; - private String _dbName; - private com.sleepycat.db.SecondaryDatabase _db; -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/IndexedSubMap.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/IndexedSubMap.java deleted file mode 100644 index a77e0c24892..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/IndexedSubMap.java +++ /dev/null @@ -1,929 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -import Freeze.DatabaseException; -import Freeze.Map; -import Freeze.NavigableMap; -import java.nio.ByteBuffer; - -// -// Indexed submap of a Freeze Map or of another submap -// -class IndexedSubMap<K, V, I> - extends java.util.AbstractMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - implements NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> -{ - private class Value extends java.util.AbstractSet<java.util.Map.Entry<K, V>> - { - @Override - public java.util.Iterator<java.util.Map.Entry<K, V>> - iterator() - { - return _index.find(_myKey, true); - } - - @Override - public int - size() - { - return _index.count(_myKey); - } - - @Override - public boolean - equals(Object o) - { - if(o instanceof IndexedSubMap.Value) - { - IndexedSubMap<?,?,?>.Value v = (IndexedSubMap<?,?,?>.Value)o; - return v._myKey.equals(_myKey); - } - else - { - return false; - } - } - - @Override - public int - hashCode() - { - return _myKey.hashCode(); - } - - private - Value(I key) - { - _myKey = key; - } - - private I - getKey() - { - return _myKey; - } - - private I _myKey; - } - - private class Entry implements java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - { - @Override - public I - getKey() - { - return _value.getKey(); - } - - @Override - public java.util.Set<java.util.Map.Entry<K, V>> - getValue() - { - return _value; - } - - @Override - public java.util.Set<java.util.Map.Entry<K, V>> - setValue(java.util.Set<java.util.Map.Entry<K, V>> value) - { - throw new UnsupportedOperationException(); - } - - @Override - public boolean - equals(Object o) - { - if(o instanceof IndexedSubMap.Entry) - { - IndexedSubMap<?,?,?>.Entry e = (IndexedSubMap<?,?,?>.Entry)o; - return e._value.equals(_value); - } - else - { - return false; - } - } - - @Override - public int - hashCode() - { - return _value.hashCode(); - } - - IndexedSubMap<K, V, I> - parent() - { - return IndexedSubMap.this; - } - - private - Entry(I key) - { - _value = new Value(key); - } - - private Value _value; - } - - private class Iterator - implements Map.EntryIterator<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>> - { - @Override - public boolean - hasNext() - { - return _iterator.hasNext(); - } - - @Override - public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - next() - { - EntryI<K, V> entry = (EntryI<K, V>)_iterator.next(); - return new Entry(_index.decodeKey(entry.getIndexBytes())); - } - - @Override - public void - remove() - { - _iterator.remove(); - } - - @Override - public void - close() - { - _iterator.close(); - } - - @Override - public void - destroy() - { - close(); - } - - private - Iterator() - { - assert(_index != null); - _iterator = new IteratorI<K, V>(_map, _view); - } - - Map.EntryIterator<java.util.Map.Entry<K, V>> _iterator; - } - - IndexedSubMap(Index<K, V, I> index, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - _map = index.parent(); - _index = index; - _view = new AscendingView(fromKey, fromInclusive, toKey, toInclusive); - } - - private - IndexedSubMap(Index<K, V, I> index, View v) - { - _map = index.parent(); - _index = index; - _view = v; - } - - // - // NavigableMap methods - // - - @Override - public boolean - fastRemove(I key) - { - if(!_view.inRange(key, true)) - { - return false; - } - - // - // Not yet implemented - // - throw new UnsupportedOperationException(); - } - - @Override - public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - firstEntry() - { - return _view.first(); - } - - @Override - public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - lastEntry() - { - return _view.last(); - } - - @Override - public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - ceilingEntry(I key) - { - return _view.ceiling(key); - } - - @Override - public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - floorEntry(I key) - { - return _view.floor(key); - } - - @Override - public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - higherEntry(I key) - { - return _view.higher(key); - } - - @Override - public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - lowerEntry(I key) - { - return _view.lower(key); - } - - @Override - public I - ceilingKey(I key) - { - Entry e = _view.ceiling(key); - return e != null ? e.getKey() : null; - } - - @Override - public I - floorKey(I key) - { - Entry e = _view.floor(key); - return e != null ? e.getKey() : null; - } - - @Override - public I - higherKey(I key) - { - Entry e = _view.higher(key); - return e != null ? e.getKey() : null; - } - - @Override - public I - lowerKey(I key) - { - Entry e = _view.lower(key); - return e != null ? e.getKey() : null; - } - - @Override - public java.util.Set<I> - descendingKeySet() - { - return descendingMap().keySet(); - } - - @Override - public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - descendingMap() - { - if(_descendingMap == null) - { - View v = _view.descendingView(); - _descendingMap = new IndexedSubMap<K, V, I>(_index, v); - } - return _descendingMap; - } - - @Override - public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - headMap(I toKey, boolean inclusive) - { - if(toKey == null) - { - throw new NullPointerException(); - } - View v = _view.subView(null, false, toKey, inclusive); - return new IndexedSubMap<K, V, I>(_index, v); - } - - @Override - public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - subMap(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - if(fromKey == null || toKey == null) - { - throw new NullPointerException(); - } - View v = _view.subView(fromKey, fromInclusive, toKey, toInclusive); - return new IndexedSubMap<K, V, I>(_index, v); - } - - @Override - public NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - tailMap(I fromKey, boolean inclusive) - { - if(fromKey == null) - { - throw new NullPointerException(); - } - View v = _view.subView(fromKey, inclusive, null, false); - return new IndexedSubMap<K, V, I>(_index, v); - } - - @Override - public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - pollFirstEntry() - { - // - // Not yet implemented - // - throw new UnsupportedOperationException(); - } - - @Override - public java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>> - pollLastEntry() - { - // - // Not yet implemented - // - throw new UnsupportedOperationException(); - } - - // - // SortedMap methods - // - - @Override - public java.util.Comparator<? super I> - comparator() - { - return _view.comparator(); - } - - @Override - public I - firstKey() - { - Entry e = _view.first(); - if(e == null) - { - throw new java.util.NoSuchElementException(); - } - return e.getKey(); - } - - @Override - public I - lastKey() - { - Entry e = _view.last(); - if(e == null) - { - throw new java.util.NoSuchElementException(); - } - return e.getKey(); - } - - @Override - public java.util.SortedMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - headMap(I toKey) - { - return headMap(toKey, false); - } - - @Override - public java.util.SortedMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - tailMap(I fromKey) - { - return tailMap(fromKey, true); - } - - @Override - public java.util.SortedMap<I, java.util.Set<java.util.Map.Entry<K, V>>> - subMap(I fromKey, I toKey) - { - return subMap(fromKey, true, toKey, false); - } - - // - // Map methods - // - - @Override - public java.util.Set<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>> - entrySet() - { - if(_entrySet == null) - { - _entrySet = new java.util.AbstractSet<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>>() - { - @Override - public java.util.Iterator<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>> - iterator() - { - return new Iterator(); - } - - @Override - public boolean - contains(Object o) - { - if(o instanceof IndexedSubMap.Entry) - { - IndexedSubMap<?,?,?>.Entry e = (IndexedSubMap<?,?,?>.Entry)o; - return e.parent() == IndexedSubMap.this && _index.containsKey(e.getKey()); - } - else - { - return false; - } - } - - @Override - public boolean - remove(Object o) - { - // - // Not yet implemented, should remove all objects that - // match this index-key - // - throw new UnsupportedOperationException(); - } - - @Override - public int - size() - { - throw new UnsupportedOperationException(); - } - - @Override - public boolean - isEmpty() - { - try - { - firstKey(); - return false; - } - catch(java.util.NoSuchElementException e) - { - return true; - } - } - }; - } - return _entrySet; - } - - // - // Put is not implemented (you have to put in the main map view) - // - - public boolean - constainsKey(Object key) - { - @SuppressWarnings("unchecked") - I k = (I)key; - if(!_view.inRange(k, true)) - { - return false; - } - - return _index.containsKey(key); - } - - @Override - public java.util.Set<java.util.Map.Entry<K, V>> - get(Object key) - { - @SuppressWarnings("unchecked") - I k = (I)key; - if(!_view.inRange(k, true)) - { - return null; - } - - if(_index.containsKey(k)) - { - return new Value(k); - } - else - { - return null; - } - } - - @Override - public java.util.Set<java.util.Map.Entry<K, V>> - remove(Object key) - { - @SuppressWarnings("unchecked") - I k = (I)key; - if(!_view.inRange(k, true)) - { - return null; - } - - // - // Not yet implemented - // - throw new UnsupportedOperationException(); - } - - private Entry - entrySearch(Search.Type type, ByteBuffer key) - { - if(type != Search.Type.FIRST && type != Search.Type.LAST && key == null) - { - throw new NullPointerException(); - } - - if(_index.db() == null) - { - DatabaseException ex = new DatabaseException(); - ex.message = _index.traceLevels().errorPrefix + "\"" + _index.dbName() + "\" has been closed"; - throw ex; - } - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(key); - - if(Search.search(type, _map.connection(), _index.dbName(), _index.db(), dbKey, null, _index, _view, - _index.traceLevels())) - { - I k = _index.decodeKey(dbKey); - return new Entry(k); - } - - return null; - } - - private abstract class View implements IteratorModel<K, V>, Search.KeyValidator - { - protected - View(java.util.Comparator<? super I> comparator, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - _comparator = comparator; - _fromKey = fromKey; - _fromInclusive = fromInclusive; - _toKey = toKey; - _toInclusive = toInclusive; - - // - // Validate the key range. - // - if(_fromKey != null && _toKey != null) - { - int cmp = comparator.compare(_fromKey, _toKey); - if(cmp > 0 || (cmp == 0 && !(_fromInclusive && _toInclusive))) - { - throw new IllegalArgumentException(); - } - } - } - - protected - View(View v, java.util.Comparator<? super I> comparator, I fromKey, boolean fromInclusive, I toKey, - boolean toInclusive) - { - this(comparator, fromKey, fromInclusive, toKey, toInclusive); - - // - // Verify that the key range is correct with respect to the original view. - // - if(!v.inRange(_fromKey, _fromInclusive) || !v.inRange(_toKey, _toInclusive)) - { - throw new IllegalArgumentException(); - } - } - - abstract Search.Type mapSearchType(Search.Type type); - abstract View copy(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive); - abstract View descendingView(); - - final Entry - first() - { - Search.Type type; - ByteBuffer key = null; - if(_fromKey != null) - { - type = _fromInclusive ? mapSearchType(Search.Type.CEILING) : mapSearchType(Search.Type.HIGHER); - key = fromKeyBytes(); - } - else - { - type = mapSearchType(Search.Type.FIRST); - } - return entrySearch(type, key); - } - - final Entry - last() - { - Search.Type type; - ByteBuffer key = null; - if(_toKey != null) - { - type = _toInclusive ? mapSearchType(Search.Type.FLOOR) : mapSearchType(Search.Type.LOWER); - key = toKeyBytes(); - } - else - { - type = mapSearchType(Search.Type.LAST); - } - return entrySearch(type, key); - } - - final Entry - ceiling(I key) - { - ByteBuffer k = _index.encodeKey(key); - return entrySearch(mapSearchType(Search.Type.CEILING), k); - } - - final Entry - floor(I key) - { - ByteBuffer k = _index.encodeKey(key); - return entrySearch(mapSearchType(Search.Type.FLOOR), k); - } - - final Entry - higher(I key) - { - ByteBuffer k = _index.encodeKey(key); - return entrySearch(mapSearchType(Search.Type.HIGHER), k); - } - - final Entry - lower(I key) - { - ByteBuffer k = _index.encodeKey(key); - return entrySearch(mapSearchType(Search.Type.LOWER), k); - } - - final View - subView(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - if(fromKey == null) - { - fromKey = _fromKey; - fromInclusive = _fromInclusive; - } - if(toKey == null) - { - toKey = _toKey; - toInclusive = _toInclusive; - } - return copy(fromKey, fromInclusive, toKey, toInclusive); - } - - // - // IteratorModel methods (partial) - // - - @Override - final public String - dbName() - { - return _index.dbName(); - } - - @Override - final public TraceLevels - traceLevels() - { - return _index.traceLevels(); - } - - @Override - final public com.sleepycat.db.Cursor - openCursor() - throws com.sleepycat.db.DatabaseException - { - return _index.db().openSecondaryCursor(_map.connection().dbTxn(), null); - } - - // - // Search.KeyValidator methods - // - - @Override - final public boolean - keyInRange(ByteBuffer key) - { - I k = _index.decodeKey(key); - return inRange(k, true); - } - - final boolean - inRange(I key, boolean inclusive) - { - return !tooLow(key, inclusive, _fromKey, _fromInclusive) && - !tooHigh(key, inclusive, _toKey, _toInclusive); - } - - final java.util.Comparator<? super I> - comparator() - { - return _comparator; - } - - final protected ByteBuffer - fromKeyBytes() - { - if(_fromKey != null && _fromKeyBytes == null) - { - _fromKeyBytes = _index.encodeKey(_fromKey); - } - return _fromKeyBytes; - } - - final protected ByteBuffer - toKeyBytes() - { - if(_toKey != null && _toKeyBytes == null) - { - _toKeyBytes = _index.encodeKey(_toKey); - } - return _toKeyBytes; - } - - final protected boolean - tooLow(I key, boolean inclusive, I targetKey, boolean targetInclusive) - { - if(key != null && targetKey != null) - { - int cmp = comparator().compare(key, targetKey); - if(cmp < 0 || (cmp == 0 && inclusive && !targetInclusive)) - { - return true; - } - } - return false; - } - - final protected boolean - tooHigh(I key, boolean inclusive, I targetKey, boolean targetInclusive) - { - if(key != null && targetKey != null) - { - int cmp = comparator().compare(key, targetKey); - if(cmp > 0 || (cmp == 0 && inclusive && !targetInclusive)) - { - return true; - } - } - return false; - } - - final java.util.Comparator<? super I> _comparator; - final I _fromKey; - final boolean _fromInclusive; - final I _toKey; - final boolean _toInclusive; - private ByteBuffer _fromKeyBytes; - private ByteBuffer _toKeyBytes; - } - - private class AscendingView extends View - { - AscendingView(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - super(_index.comparator(), fromKey, fromInclusive, toKey, toInclusive); - } - - AscendingView(View v, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - super(v, _index.comparator(), fromKey, fromInclusive, toKey, toInclusive); - } - - // - // View methods - // - - @Override - Search.Type - mapSearchType(Search.Type type) - { - return type; - } - - @Override - View - copy(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - return new AscendingView(this, fromKey, fromInclusive, toKey, toInclusive); - } - - @Override - View - descendingView() - { - return new DescendingView(this, _toKey, _toInclusive, _fromKey, _fromInclusive); - } - - // - // IteratorModel methods - // - - @Override - public EntryI<K, V> - firstEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return _index.firstEntry(cursor, _fromKey, _fromInclusive, _toKey, _toInclusive); - } - - @Override - public EntryI<K, V> - nextEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return _index.nextEntry(cursor, _toKey, _toInclusive); - } - } - - private class DescendingView extends View - { - DescendingView(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - super(java.util.Collections.reverseOrder(_index.comparator()), fromKey, fromInclusive, toKey, toInclusive); - } - - DescendingView(View v, I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - super(v, java.util.Collections.reverseOrder(_index.comparator()), fromKey, fromInclusive, toKey, - toInclusive); - } - - // - // View methods - // - - @Override - Search.Type - mapSearchType(Search.Type type) - { - return type.descending(); - } - - @Override - View - copy(I fromKey, boolean fromInclusive, I toKey, boolean toInclusive) - { - return new DescendingView(this, fromKey, fromInclusive, toKey, toInclusive); - } - - @Override - View - descendingView() - { - return new AscendingView(this, _toKey, _toInclusive, _fromKey, _fromInclusive); - } - - // - // IteratorModel methods - // - - @Override - public EntryI<K, V> - firstEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return _index.lastEntry(cursor, _fromKey, _fromInclusive, _toKey, _toInclusive); - } - - @Override - public EntryI<K, V> - nextEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return _index.previousEntry(cursor, _toKey, _toInclusive); - } - } - - private final MapI<K, V> _map; - private final Index<K, V, I> _index; - private final View _view; - private java.util.Set<java.util.Map.Entry<I, java.util.Set<java.util.Map.Entry<K, V>>>> _entrySet; - private NavigableMap<I, java.util.Set<java.util.Map.Entry<K, V>>> _descendingMap; -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/IteratorI.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/IteratorI.java deleted file mode 100644 index 7cc99d7bb76..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/IteratorI.java +++ /dev/null @@ -1,397 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -import Freeze.DatabaseException; -import Freeze.DeadlockException; -import Freeze.NotFoundException; -import java.nio.ByteBuffer; - -class IteratorI<K, V> implements Freeze.Map.EntryIterator<java.util.Map.Entry<K, V>> -{ - IteratorI(MapI<K, V> map, IteratorModel<K, V> model) - { - _map = map; - _model = model; - _trace = model.traceLevels(); - _dbName = model.dbName(); - - try - { - _txn = _map.connection().dbTxn(); - _cursor = _model.openCursor(); - } - catch(com.sleepycat.db.DeadlockException dx) - { - dead(); - throw new DeadlockException(_trace.errorPrefix + "EntryIterator constructor: " + dx.getMessage(), - _map.connection().currentTransaction(), dx); - } - catch(com.sleepycat.db.DatabaseException dx) - { - dead(); - throw new DatabaseException(_trace.errorPrefix + "EntryIterator constructor: " + dx.getMessage(), dx); - } - - _iteratorListToken = _map.addIterator(this); - } - - @Override - public boolean - hasNext() - { - if(_current == null || _current == _lastReturned) - { - try - { - if(_current == null) - { - _current = _model.firstEntry(_cursor); - } - else - { - _current = _model.nextEntry(_cursor); - } - } - catch(com.sleepycat.db.DeadlockException dx) - { - dead(); - throw new DeadlockException(_trace.errorPrefix + "Dbc.get: " + dx.getMessage(), - _map.connection().currentTransaction(), dx); - } - catch(com.sleepycat.db.DatabaseException dx) - { - dead(); - throw new DatabaseException(_trace.errorPrefix + "Dbc.get: " + dx.getMessage(), dx); - } - - // - // For a read-only iterator, we can close the cursor automatically when there - // are no more entries. - // - if(_current == null && _txn == null) - { - close(); - } - - if(_current != null) - { - _current.iterator(this); - } - - return _current != null; - } - else - { - return true; - } - } - - @Override - public java.util.Map.Entry<K, V> - next() - { - if(hasNext()) - { - _lastReturned = _current; - return _lastReturned; - } - else - { - throw new java.util.NoSuchElementException(); - } - } - - @Override - public void - remove() - { - if(_txn == null) - { - throw new UnsupportedOperationException( - _trace.errorPrefix + "Cannot remove using an iterator without a transaction"); - } - - // - // Remove the last object returned by next() - // - if(_lastReturned == null) - { - throw new IllegalStateException(); - } - - if(_lastReturned == _current) - { - try - { - if(_cursor.delete() == com.sleepycat.db.OperationStatus.KEYEMPTY) - { - throw new IllegalStateException(); - } - } - catch(com.sleepycat.db.DeadlockException e) - { - dead(); - throw new DeadlockException(_trace.errorPrefix + "Dbc.del: " + e.getMessage(), - _map.connection().currentTransaction(), e); - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Dbc.del: " + e.getMessage(), e); - } - } - else - { - // - // Duplicate the cursor and move the _lastReturned element to delete it (using the duplicate). - // - - // - // This works only for non-index iterators. - // - if(_cursor instanceof com.sleepycat.db.SecondaryCursor) - { - throw new UnsupportedOperationException( - _trace.errorPrefix + "Cannot remove using an iterator retrieved through an index"); - } - - com.sleepycat.db.Cursor clone = null; - - try - { - clone = _cursor.dup(true); - - // - // Not interested in data. - // - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - dbValue.setPartial(true); - - com.sleepycat.db.OperationStatus rc = clone.getSearchKey(_lastReturned.getDbKey(), dbValue, null); - - if(rc == com.sleepycat.db.OperationStatus.NOTFOUND) - { - throw new IllegalStateException(); - } - if(clone.delete() == com.sleepycat.db.OperationStatus.KEYEMPTY) - { - throw new IllegalStateException(); - } - } - catch(com.sleepycat.db.DeadlockException e) - { - dead(); - throw new DeadlockException(_trace.errorPrefix + "EntryIterator.remove: " + e.getMessage(), - _map.connection().currentTransaction(), e); - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "EntryIterator.remove: " + e.getMessage(), e); - } - finally - { - if(clone != null) - { - closeCursor(clone); - } - } - } - } - - // - // Extra operations. - // - @Override - public void - close() - { - if(_iteratorListToken != null) - { - _map.removeIterator(_iteratorListToken); - _iteratorListToken = null; - } - - if(_cursor != null) - { - com.sleepycat.db.Cursor cursor = _cursor; - _cursor = null; - closeCursor(cursor); - } - } - - // - // An alias for close() - // - @Override - public void - destroy() - { - close(); - } - - @Override - protected void - finalize() - throws Throwable - { - try - { - if(_cursor != null) - { - _trace.logger.warning( - "iterator leaked for Map \"" + _dbName + "\"; the application " + - "should have closed it earlier by calling Map.EntryIterator.close(), " + - "Map.closeAllIterators(), Map.close(), Connection.close(), or (if also " + - "leaking a transaction) Transaction.commit() or Transaction.rollback()"); - } - } - catch(java.lang.Exception ex) - { - } - finally - { - super.finalize(); - } - } - - void - setValue(EntryI<K, V> entry, V value) - { - if(_cursor instanceof com.sleepycat.db.SecondaryCursor) - { - throw new UnsupportedOperationException( - _trace.errorPrefix + "Cannot set an iterator retrieved through an index"); - } - - if(_txn == null) - { - throw new UnsupportedOperationException( - _trace.errorPrefix + "Cannot set a value without a transaction"); - } - - // - // Are we trying to update the current value? - // - if(_current == entry) - { - // - // Yes, update it directly - // - ByteBuffer v = _map.encodeValue(value); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(v); - - try - { - _cursor.putCurrent(dbValue); - } - catch(com.sleepycat.db.DeadlockException e) - { - dead(); - throw new DeadlockException(_trace.errorPrefix + "Dbc.put: " + e.getMessage(), - _map.connection().currentTransaction(), e); - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Dbc.put: " + e.getMessage(), e); - } - } - else - { - // - // Duplicate the cursor and move the entry - // element to update it (using the duplicate cursor) - // - - com.sleepycat.db.Cursor clone = null; - - try - { - clone = _cursor.dup(true); - - // - // Not interested in data - // - com.sleepycat.db.DatabaseEntry dummy = new com.sleepycat.db.DatabaseEntry(); - dummy.setPartial(true); - - com.sleepycat.db.OperationStatus rc = clone.getSearchKey(entry.getDbKey(), dummy, null); - - if(rc == com.sleepycat.db.OperationStatus.NOTFOUND) - { - NotFoundException ex = new NotFoundException(); - ex.message = _trace.errorPrefix + "Dbc.get: DB_NOTFOUND"; - throw ex; - } - - ByteBuffer v = _map.encodeValue(value); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(v); - clone.putCurrent(dbValue); - } - catch(com.sleepycat.db.DeadlockException e) - { - dead(); - throw new DeadlockException(_trace.errorPrefix + "EntryIterator.setValue: " + e.getMessage(), - _map.connection().currentTransaction(), e); - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "EntryIterator.setValue: " + e.getMessage(), e); - } - finally - { - if(clone != null) - { - closeCursor(clone); - } - } - } - } - - private void - closeCursor(com.sleepycat.db.Cursor cursor) - { - try - { - cursor.close(); - } - catch(com.sleepycat.db.DeadlockException e) - { - dead(); - throw new DeadlockException(_trace.errorPrefix + "Dbc.close: " + e.getMessage(), - _map.connection().currentTransaction(), e); - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Dbc.close: " + e.getMessage(), e); - } - } - - private void - dead() - { - if(_cursor != null) - { - com.sleepycat.db.Cursor cursor = _cursor; - _cursor = null; - closeCursor(cursor); - } - } - - private final MapI<K, V> _map; - private final IteratorModel<K, V> _model; - - private final TraceLevels _trace; - private final String _dbName; // For use in finalizer. - private final com.sleepycat.db.Transaction _txn; - private com.sleepycat.db.Cursor _cursor; - private EntryI<K, V> _current; - private EntryI<K, V> _lastReturned; - private Object _iteratorListToken; -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/IteratorModel.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/IteratorModel.java deleted file mode 100644 index 5f62425e441..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/IteratorModel.java +++ /dev/null @@ -1,25 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -interface IteratorModel<K, V> -{ - String dbName(); - TraceLevels traceLevels(); - - com.sleepycat.db.Cursor openCursor() - throws com.sleepycat.db.DatabaseException; - - EntryI<K, V> firstEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException; - - EntryI<K, V> nextEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException; -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/KeyCodec.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/KeyCodec.java deleted file mode 100644 index 9532113ab02..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/KeyCodec.java +++ /dev/null @@ -1,16 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -interface KeyCodec<K> -{ - public abstract void encodeKey(K k, IceInternal.BasicStream str); - public abstract K decodeKey(IceInternal.BasicStream str); -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/MapI.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/MapI.java deleted file mode 100644 index 7e46686635b..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/MapI.java +++ /dev/null @@ -1,1703 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -import Freeze.Catalog; -import Freeze.CatalogIndexList; -import Freeze.Connection; -import Freeze.ConnectionI; -import Freeze.DatabaseException; -import Freeze.DeadlockException; -import Freeze.IndexNotFoundException; -import Freeze.LinkedList; -import Freeze.Map; -import Freeze.MapDb; -import Freeze.MapIndex; -import Freeze.NavigableMap; -import Freeze.Transaction; -import Freeze.Util; -import java.nio.ByteBuffer; - -public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> - implements Map<K, V>, KeyCodec<K>, IteratorModel<K, V> -{ - public abstract void encodeValue(V v, IceInternal.BasicStream str); - public abstract V decodeValue(IceInternal.BasicStream str); - - protected - MapI(Connection connection, String dbName, String key, String value, boolean createDb, - java.util.Comparator<K> comparator) - { - _connection = (ConnectionI)connection; - _dbName = dbName; - _comparator = (comparator == null) ? null : new Comparator(comparator); - _dbComparator = (comparator == null) ? null : new DbComparator(comparator); - - _communicator = _connection.getCommunicator(); - _encoding = _connection.getEncoding(); - _trace = new TraceLevels(_connection, dbName); - - init(null, dbName, key, value, createDb); - } - - protected - MapI(Connection connection, String dbName, java.util.Comparator<K> comparator) - { - _connection = (ConnectionI)connection; - _dbName = dbName; - _comparator = (comparator == null) ? null : new Comparator(comparator); - _dbComparator = (comparator == null) ? null : new DbComparator(comparator); - - _communicator = _connection.getCommunicator(); - _encoding = _connection.getEncoding(); - _trace = new TraceLevels(_connection, dbName); - } - - protected static <K, V> void - recreate(MapI<K, V> map, String dbName, String key, String value, MapIndex[] indices) - { - ConnectionI connection = map._connection; - TraceLevels trace = map._trace; - - if(dbName.equals(Util.catalogName()) || dbName.equals(Util.catalogIndexListName())) - { - throw new DatabaseException(trace.errorPrefix + "You cannot recreate the \"" + dbName + "\" database"); - } - - if(trace.level >= 1) - { - trace.logger.trace("Freeze.MapInternal.MapI", "Recreating \"" + dbName + "\""); - } - - Transaction tx = connection.currentTransaction(); - boolean ownTx = (tx == null); - - com.sleepycat.db.DatabaseEntry keyEntry = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry valueEntry = new com.sleepycat.db.DatabaseEntry(); - - com.sleepycat.db.Database oldDb = null; - MapDb newDb = null; - - for(;;) - { - try - { - if(ownTx) - { - tx = null; - tx = connection.beginTransaction(); - } - - com.sleepycat.db.Transaction txn = connection.dbTxn(); - - if(trace.level >= 2) - { - trace.logger.trace("Freeze.MapInternal.MapI", "Removing all existing indices for \"" + dbName + - "\""); - } - - CatalogIndexList catalogIndexList = new CatalogIndexList(connection, Util.catalogIndexListName(), true); - String[] oldIndices = catalogIndexList.remove(dbName); - - if(oldIndices != null) - { - for(String oldIndex : oldIndices) - { - try - { - connection.removeMapIndex(dbName, oldIndex); - } - catch(IndexNotFoundException e) - { - // - // Ignored - // - } - } - } - - // - // Rename existing database - // - String oldDbName = dbName + ".old-" + java.util.UUID.randomUUID().toString(); - - if(trace.level >= 2) - { - trace.logger.trace("Freeze.MapInternal.MapI", "Renaming \"" + dbName + "\" to \"" + oldDbName + - "\""); - } - - connection.dbEnv().getEnv().renameDatabase(txn, dbName, null, oldDbName); - - com.sleepycat.db.DatabaseConfig oldDbConfig = new com.sleepycat.db.DatabaseConfig(); - oldDbConfig.setType(com.sleepycat.db.DatabaseType.BTREE); - - oldDb = connection.dbEnv().getEnv().openDatabase(txn, oldDbName, null, oldDbConfig); - - newDb = new MapDb(connection, dbName, key, value, map._dbComparator, indices, true); - map.init(newDb, indices); - - if(trace.level >= 2) - { - trace.logger.trace("Freeze.MapInternal.MapI", "Writing contents of \"" + oldDbName + - "\" to fresh \"" + dbName + "\""); - } - - // - // Now simply write all of oldDb into newDb - // - com.sleepycat.db.Cursor dbc = null; - try - { - dbc = oldDb.openCursor(txn, null); - - while(dbc.getNext(keyEntry, valueEntry, null) == com.sleepycat.db.OperationStatus.SUCCESS) - { - newDb.db().put(txn, keyEntry, valueEntry); - } - } - finally - { - if(dbc != null) - { - dbc.close(); - } - } - - if(trace.level >= 2) - { - trace.logger.trace("Freeze.MapInternal.MapI", "Transfer complete; removing \"" + oldDbName + "\""); - } - connection.dbEnv().getEnv().removeDatabase(txn, oldDbName, null); - - if(ownTx) - { - try - { - tx.commit(); - } - finally - { - tx = null; - } - } - - break; // for (;;) - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(ownTx) - { - if(trace.deadlockWarning) - { - trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.recreate on Db \"" + dbName + - "\"; retrying ..."); - } - - // - // Ignored, try again - // - } - else - { - throw new DeadlockException(trace.errorPrefix + "Map.recreate: " + dx.getMessage(), tx, dx); - } - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(trace.errorPrefix + "Map.recreate: " + dx.getMessage(), dx); - } - catch(java.io.FileNotFoundException fne) - { - throw new DatabaseException(trace.errorPrefix + "Map.recreate: " + fne.getMessage(), fne); - } - finally - { - if(ownTx && tx != null) - { - try - { - tx.rollback(); - } - catch(DatabaseException de) - { - } - } - - try - { - if(newDb != null) - { - newDb.close(); - } - - if(oldDb != null) - { - try - { - oldDb.close(); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(trace.errorPrefix + "Map.recreate: " + dx.getMessage(), dx); - } - } - } - finally - { - newDb = null; - oldDb = null; - } - } - } - } - - protected void - init(MapIndex[] indices, String dbName, String key, String value, boolean createDb) - { - init(_connection.dbEnv().getSharedMapDb(dbName, key, value, _dbComparator, indices, createDb), indices); - } - - protected void - init(MapDb db, MapIndex[] indices) - { - _db = db; - _token = _connection.registerMap(this); - - if(indices != null) - { - for(MapIndex index : indices) - { - _indexMap.put(index.name(), index); - } - } - } - - // - // Freeze.Map methods - // - - @Override - public void - fastPut(K key, V value) - { - putImpl(new com.sleepycat.db.DatabaseEntry(encodeKey(key)), value); - } - - @Override - public boolean - fastRemove(K key) - { - return removeImpl(new com.sleepycat.db.DatabaseEntry(encodeKey(key))); - } - - @Override - public void - close() - { - if(_db != null) - { - try - { - closeAllIterators(); - } - finally - { - _db = null; - _connection.unregisterMap(_token); - _token = null; - } - } - } - - @Override - public int - closeAllIterators() - { - return closeAllIteratorsExcept(null); - } - - // - // Close this map and destroy the underlying Berkeley DB database - // - @Override - public void - destroy() - { - if(_db == null) - { - throw new DatabaseException(_trace.errorPrefix + "This map is closed"); - } - - if(_dbName.equals(Util.catalogName()) || _dbName.equals(Util.catalogIndexListName())) - { - throw new DatabaseException(_trace.errorPrefix + "You cannot destroy the \"" + _dbName + "\" database"); - } - - if(_connection.currentTransaction() != null) - { - throw new DatabaseException( - _trace.errorPrefix + "You cannot destroy a database within an active transaction"); - } - - if(_trace.level >= 1) - { - _trace.logger.trace("Freeze.MapInternal.MapI", "destroying \"" + _dbName + "\""); - } - - closeDb(); - - for(;;) - { - Transaction tx = null; - try - { - tx = _connection.beginTransaction(); - com.sleepycat.db.Transaction txn = _connection.dbTxn(); - - Catalog catalog = new Catalog(_connection, Util.catalogName(), true); - catalog.remove(_dbName); - - CatalogIndexList catalogIndexList = - new CatalogIndexList(_connection, Util.catalogIndexListName(), true); - catalogIndexList.remove(_dbName); - - _connection.dbEnv().getEnv().removeDatabase(txn, _dbName, null); - - // - // Remove all indices - // - for(String index : _indexMap.keySet()) - { - _connection.removeMapIndex(_dbName, index); - } - - tx.commit(); - - break; // for(;;) - } - catch(java.io.FileNotFoundException dx) - { - try - { - tx.rollback(); - } - catch(DatabaseException e) - { - } - - throw new DatabaseException(_trace.errorPrefix + "file not found", dx); - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_trace.deadlockWarning) - { - _trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.destroy on Db \"" + _dbName + - "\"; retrying..."); - } - - // - // Ignored, try again - // - } - catch(com.sleepycat.db.DatabaseException dx) - { - try - { - tx.rollback(); - } - catch(DatabaseException e) - { - } - - throw new DatabaseException(_trace.errorPrefix + dx.getMessage(), dx); - } - catch(RuntimeException rx) - { - try - { - tx.rollback(); - } - catch(DatabaseException e) - { - } - - throw rx; - } - } - } - - @Override - public Connection - getConnection() - { - return _connection; - } - - @Override - public void - closeDb() - { - close(); - _connection.dbEnv().removeSharedMapDb(_dbName); - } - - // - // NavigableMap methods - // - - @Override - public java.util.Map.Entry<K, V> - firstEntry() - { - return entrySearch(Search.Type.FIRST, null, true); - } - - @Override - public java.util.Map.Entry<K, V> - lastEntry() - { - return entrySearch(Search.Type.LAST, null, true); - } - - @Override - public java.util.Map.Entry<K, V> - ceilingEntry(K key) - { - return entrySearch(Search.Type.CEILING, encodeKey(key), true); - } - - @Override - public java.util.Map.Entry<K, V> - floorEntry(K key) - { - return entrySearch(Search.Type.FLOOR, encodeKey(key), true); - } - - @Override - public java.util.Map.Entry<K, V> - higherEntry(K key) - { - return entrySearch(Search.Type.HIGHER, encodeKey(key), true); - } - - @Override - public java.util.Map.Entry<K, V> - lowerEntry(K key) - { - return entrySearch(Search.Type.LOWER, encodeKey(key), true); - } - - @Override - public K - ceilingKey(K key) - { - java.util.Map.Entry<K, V> e = entrySearch(Search.Type.CEILING, encodeKey(key), false); - return e != null ? e.getKey() : null; - } - - @Override - public K - floorKey(K key) - { - java.util.Map.Entry<K, V> e = entrySearch(Search.Type.FLOOR, encodeKey(key), false); - return e != null ? e.getKey() : null; - } - - @Override - public K - higherKey(K key) - { - java.util.Map.Entry<K, V> e = entrySearch(Search.Type.HIGHER, encodeKey(key), false); - return e != null ? e.getKey() : null; - } - - @Override - public K - lowerKey(K key) - { - java.util.Map.Entry<K, V> e = entrySearch(Search.Type.LOWER, encodeKey(key), false); - return e != null ? e.getKey() : null; - } - - @Override - public java.util.Set<K> - descendingKeySet() - { - return descendingMap().keySet(); - } - - @Override - public NavigableMap<K, V> - descendingMap() - { - if(_comparator == null) - { - throw new UnsupportedOperationException("A comparator is required"); - } - - if(_descendingMap == null) - { - _descendingMap = new SubMap<K, V>(this, null, false, null, false, false); - } - - return _descendingMap; - } - - @Override - public NavigableMap<K, V> - headMap(K toKey, boolean inclusive) - { - if(toKey == null) - { - throw new NullPointerException(); - } - - if(_comparator == null) - { - throw new UnsupportedOperationException("A comparator is required"); - } - - return new SubMap<K, V>(this, null, false, toKey, inclusive, true); - } - - @Override - public NavigableMap<K, V> - subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - if(fromKey == null || toKey == null) - { - throw new NullPointerException(); - } - - if(_comparator == null) - { - throw new UnsupportedOperationException("A comparator is required"); - } - - return new SubMap<K, V>(this, fromKey, fromInclusive, toKey, toInclusive, true); - } - - @Override - public NavigableMap<K, V> - tailMap(K fromKey, boolean inclusive) - { - if(fromKey == null) - { - throw new NullPointerException(); - } - - if(_comparator == null) - { - throw new UnsupportedOperationException("A comparator is required"); - } - - return new SubMap<K, V>(this, fromKey, inclusive, null, false, true); - } - - @Override - public java.util.Map.Entry<K, V> - pollFirstEntry() - { - EntryI<K, V> e = entrySearch(Search.Type.FIRST, null, true); - if(e != null) - { - removeImpl(e.getDbKey()); - } - return e; - } - - @Override - public java.util.Map.Entry<K, V> - pollLastEntry() - { - EntryI<K, V> e = entrySearch(Search.Type.LAST, null, true); - if(e != null) - { - removeImpl(e.getDbKey()); - } - return e; - } - - // - // SortedMap methods - // - - @Override - public java.util.Comparator<? super K> - comparator() - { - if(_comparator == null) - { - return null; - } - else - { - // - // Return's the user's comparator, not the DB comparator. - // - return _comparator.comparator(); - } - } - - @Override - public K - firstKey() - { - EntryI<K, V> e = entrySearch(Search.Type.FIRST, null, false); - if(e == null) - { - throw new java.util.NoSuchElementException(); - } - return e.getKey(); - } - - @Override - public K - lastKey() - { - EntryI<K, V> e = entrySearch(Search.Type.LAST, null, false); - if(e == null) - { - throw new java.util.NoSuchElementException(); - } - return e.getKey(); - } - - @Override - public java.util.SortedMap<K, V> - headMap(K toKey) - { - return headMap(toKey, false); - } - - @Override - public java.util.SortedMap<K, V> - tailMap(K fromKey) - { - return tailMap(fromKey, true); - } - - @Override - public java.util.SortedMap<K, V> - subMap(K fromKey, K toKey) - { - return subMap(fromKey, true, toKey, false); - } - - // - // Map methods - // - - @Override - public int - size() - { - if(_db == null) - { - DatabaseException ex = new DatabaseException(); - ex.message = _trace.errorPrefix + "\"" + _dbName + "\" is closed"; - throw ex; - } - - for(;;) - { - - try - { - com.sleepycat.db.BtreeStats s = - (com.sleepycat.db.BtreeStats)_db.db().getStats(_connection.dbTxn(), null); - return s.getNumKeys(); - } - catch(com.sleepycat.db.DeadlockException e) - { - if(_connection.dbTxn() != null) - { - throw new DeadlockException(_trace.errorPrefix + "Db.getStats: " + e.getMessage(), - _connection.currentTransaction(), e); - } - else - { - if(_trace.deadlockWarning) - { - _trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.size while " + "reading Db \"" + - _dbName + "\"; retrying..."); - } - // - // Try again - // - } - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Db.getStats: " + e.getMessage(), e); - } - } - } - - @Override - public boolean - containsValue(Object value) - { - for(;;) - { - Map.EntryIterator<java.util.Map.Entry<K, V>> p = null; - try - { - p = (Map.EntryIterator<java.util.Map.Entry<K, V>>)entrySet().iterator(); - - if(value == null) - { - while(p.hasNext()) - { - Entry<K,V> e = p.next(); - if(e.getValue() == null) - { - return true; - } - } - } - else - { - while(p.hasNext()) - { - Entry<K,V> e = p.next(); - if(value.equals(e.getValue())) - { - return true; - } - } - } - return false; - } - catch(DeadlockException e) - { - if(_connection.dbTxn() != null) - { - throw e; - } - else - { - if(_trace.deadlockWarning) - { - _trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.containsValue while " + - "iterating over Db \"" + _dbName + "\"; retrying..."); - } - - // - // Try again - // - } - } - finally - { - if(p != null) - { - p.close(); - } - } - } - } - - @Override - public boolean - containsKey(Object o) - { - @SuppressWarnings("unchecked") - K key = (K)o; - - if(_db == null) - { - DatabaseException ex = new DatabaseException(); - ex.message = _trace.errorPrefix + "\"" + _dbName + "\" is closed"; - throw ex; - } - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(encodeKey(key)); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - dbValue.setPartial(true); - - if(_trace.level >= 2) - { - _trace.logger.trace("Freeze.MapInternal.MapI", "checking key in Db \"" + _dbName + "\""); - } - - for(;;) - { - try - { - return _db.db().get(_connection.dbTxn(), dbKey, dbValue, null) == - com.sleepycat.db.OperationStatus.SUCCESS; - } - catch(com.sleepycat.db.DeadlockException e) - { - if(_connection.dbTxn() != null) - { - throw new DeadlockException(_trace.errorPrefix + "Db.get: " + e.getMessage(), - _connection.currentTransaction(), e); - } - else - { - if(_trace.deadlockWarning) - { - _trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.containsKey while " + - "reading Db \"" + _dbName + "\"; retrying..."); - } - // - // Try again - // - } - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Db.get: " + e.getMessage(), e); - } - } - } - - @Override - public V - get(Object o) - { - @SuppressWarnings("unchecked") - K key = (K)o; - - ByteBuffer v = getImpl(new com.sleepycat.db.DatabaseEntry(encodeKey(key))); - if(v == null) - { - return null; - } - else - { - return decodeValue(v); - } - } - - @Override - public V - put(K key, V value) - { - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(encodeKey(key)); - ByteBuffer v = getImpl(dbKey); - V old = null; - if(v != null) - { - old = decodeValue(v); - } - putImpl(dbKey, value); - return old; - } - - @Override - public V - remove(Object o) - { - @SuppressWarnings("unchecked") - K key = (K)o; - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(encodeKey(key)); - ByteBuffer v = getImpl(dbKey); - - if(v != null && removeImpl(dbKey)) - { - return decodeValue(v); - } - else - { - return null; - } - } - - @Override - public void - clear() - { - if(_db == null) - { - DatabaseException ex = new DatabaseException(); - ex.message = _trace.errorPrefix + "\"" + _dbName + "\" is closed"; - throw ex; - } - - com.sleepycat.db.Transaction txn = _connection.dbTxn(); - if(txn == null) - { - closeAllIterators(); - } - - com.sleepycat.db.DatabaseEntry keyEntry = new com.sleepycat.db.DatabaseEntry(); - keyEntry.setPartial(true); - - com.sleepycat.db.DatabaseEntry valueEntry = new com.sleepycat.db.DatabaseEntry(); - valueEntry.setPartial(true); - - for(;;) - { - try - { - com.sleepycat.db.Cursor dbc = null; - Transaction tx = null; - try - { - if(txn == null) - { - tx = _connection.beginTransaction(); - txn = _connection.dbTxn(); - } - - dbc = _db.db().openCursor(txn, null); - while(dbc.getNext(keyEntry, valueEntry, null) == com.sleepycat.db.OperationStatus.SUCCESS) - { - dbc.delete(); - } - break; - } - finally - { - try - { - if(dbc != null) - { - dbc.close(); - } - } - finally - { - if(tx != null) - { - tx.commit(); - } - } - } - } - catch(com.sleepycat.db.DeadlockException e) - { - if(txn != null) - { - throw new DeadlockException(_trace.errorPrefix + "Db.truncate: " + e.getMessage(), - _connection.currentTransaction(), e); - } - else - { - if(_trace.deadlockWarning) - { - _trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.clear on Db \"" + _dbName + - "\"; retrying..."); - } - - // - // Try again - // - } - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Db.truncate: " + e.getMessage(), e); - } - } - } - - @Override - public java.util.Set<java.util.Map.Entry<K, V>> - entrySet() - { - if(_entrySet == null) - { - _entrySet = new java.util.AbstractSet<java.util.Map.Entry<K, V>>() - { - @Override - public java.util.Iterator<java.util.Map.Entry<K, V>> - iterator() - { - return new IteratorI<K, V>(MapI.this, MapI.this); - } - - @Override - public boolean - contains(Object o) - { - if(!(o instanceof Entry)) - { - return false; - } - @SuppressWarnings("unchecked") - EntryI<K, V> entry = (EntryI<K, V>)o; - V value = entry.getValue(); - - ByteBuffer v = getImpl(entry.getDbKey()); - return v != null && valEquals(decodeValue(v), value); - } - - @Override - public boolean - remove(Object o) - { - if(!(o instanceof Entry)) - { - return false; - } - @SuppressWarnings("unchecked") - EntryI<K, V> entry = (EntryI<K, V>)o; - V value = entry.getValue(); - - ByteBuffer v = getImpl(entry.getDbKey()); - if(v != null && valEquals(decodeValue(v), value)) - { - return removeImpl(entry.getDbKey()); - } - return false; - } - - @Override - public int - size() - { - return MapI.this.size(); - } - - @Override - public void - clear() - { - MapI.this.clear(); - } - }; - } - - return _entrySet; - } - - // - // IteratorModel methods - // - - @Override - public String - dbName() - { - return _dbName; - } - - @Override - public TraceLevels - traceLevels() - { - return _trace; - } - - @Override - public com.sleepycat.db.Cursor - openCursor() - throws com.sleepycat.db.DatabaseException - { - return _db.db().openCursor(_connection.dbTxn(), null); - } - - @Override - public EntryI<K, V> - firstEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return firstEntry(cursor, null, false, null, false); - } - - @Override - public EntryI<K, V> - nextEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return nextEntry(cursor, null, false); - } - - ByteBuffer encodeKey(K k) - { - IceInternal.BasicStream str = createWriteStream(); - encodeKey(k, str); - return str.prepareWrite().b; - } - - K decodeKey(ByteBuffer buf) - { - return decodeKey(createReadStream(buf)); - } - - K decodeKey(com.sleepycat.db.DatabaseEntry entry) - { - ByteBuffer b = entry.getDataNIO(); - if(b != null) - { - return decodeKey(createReadStream(b)); - } - else - { - byte[] arr = entry.getData(); - assert(arr != null && entry.getOffset() == 0 && entry.getSize() == arr.length); - return decodeKey(createReadStream(arr)); - } - } - - ByteBuffer encodeValue(V v) - { - IceInternal.BasicStream str = createWriteStream(); - encodeValue(v, str); - return str.prepareWrite().b; - } - - V decodeValue(ByteBuffer buf) - { - return decodeValue(createReadStream(buf)); - } - - V decodeValue(com.sleepycat.db.DatabaseEntry entry) - { - ByteBuffer b = entry.getDataNIO(); - if(b != null) - { - return decodeValue(createReadStream(b)); - } - else - { - byte[] arr = entry.getData(); - assert(arr != null && entry.getOffset() == 0 && entry.getSize() == arr.length); - return decodeValue(createReadStream(arr)); - } - } - - IceInternal.BasicStream createWriteStream() - { - return new IceInternal.BasicStream(IceInternal.Util.getInstance(_communicator), _encoding, false); - } - - IceInternal.BasicStream createReadStream(byte[] arr) - { - return new IceInternal.BasicStream(IceInternal.Util.getInstance(_communicator), _encoding, arr); - } - - IceInternal.BasicStream createReadStream(ByteBuffer buf) - { - buf.rewind(); - return new IceInternal.BasicStream(IceInternal.Util.getInstance(_communicator), _encoding, buf); - } - - com.sleepycat.db.Database - db() - { - return _db.db(); - } - - ConnectionI - connection() - { - return _connection; - } - - int - closeAllIteratorsExcept(Map.EntryIterator<java.util.Map.Entry<K, V>> except) - { - int count = 0; - - java.util.Iterator<IteratorI<K, V>> p = _iteratorList.iterator(); - - while(p.hasNext()) - { - IteratorI<K, V> i = p.next(); - if(i != except) - { - i.close(); - ++count; - } - } - - return count; - } - - Object - addIterator(IteratorI<K, V> i) - { - _iteratorList.addFirst(i); - java.util.Iterator<IteratorI<K, V>> p = _iteratorList.iterator(); - p.next(); - return p; - } - - void - removeIterator(Object token) - { - @SuppressWarnings("unchecked") - java.util.Iterator<IteratorI<K, V>> i = (java.util.Iterator<IteratorI<K, V>>)token; - i.remove(); - } - - // - // Convenience method for use in this class. - // - private EntryI<K, V> - entrySearch(Search.Type type, ByteBuffer key, boolean includeValue) - { - return entrySearch(type, key, includeValue, null); - } - - // - // Also used by SubMap. - // - EntryI<K, V> - entrySearch(Search.Type type, ByteBuffer key, boolean includeValue, Search.KeyValidator validator) - { - if(type != Search.Type.FIRST && type != Search.Type.LAST && key == null) - { - throw new NullPointerException(); - } - - if(_db.db() == null) - { - DatabaseException ex = new DatabaseException(); - ex.message = _trace.errorPrefix + "\"" + dbName() + "\" is closed"; - throw ex; - } - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(key); - com.sleepycat.db.DatabaseEntry dbValue = includeValue ? new com.sleepycat.db.DatabaseEntry() : null; - - if(Search.search(type, _connection, _dbName, _db.db(), dbKey, dbValue, _comparator, validator, _trace)) - { - return new EntryI<K, V>(this, null, dbKey, dbValue != null ? UtilI.getBuffer(dbValue) : null, null); - } - - return null; - } - - // - // For ascending maps. - // - EntryI<K, V> - firstEntry(com.sleepycat.db.Cursor cursor, K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - throws com.sleepycat.db.DatabaseException - { - if(fromKey != null) - { - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - - ByteBuffer k = encodeKey(fromKey); - dbKey.setDataNIO(k); - dbKey.setReuseBuffer(false); - - com.sleepycat.db.OperationStatus status = cursor.getSearchKeyRange(dbKey, dbValue, null); - - if(status == com.sleepycat.db.OperationStatus.SUCCESS && !fromInclusive) - { - int cmp = _comparator.compare(UtilI.getBuffer(dbKey), k); - assert(cmp >= 0); - if(cmp == 0) - { - status = cursor.getNextNoDup(dbKey, dbValue, null); - } - } - - if(status == com.sleepycat.db.OperationStatus.SUCCESS) - { - return newEntry(dbKey, dbValue, fromKey, fromInclusive, toKey, toInclusive); - } - - return null; - } - else - { - return nextEntry(cursor, toKey, toInclusive); - } - } - - // - // For ascending maps. - // - EntryI<K, V> - nextEntry(com.sleepycat.db.Cursor cursor, K toKey, boolean toInclusive) - throws com.sleepycat.db.DatabaseException - { - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - - if(cursor.getNext(dbKey, dbValue, null) == com.sleepycat.db.OperationStatus.SUCCESS) - { - return newEntry(dbKey, dbValue, null, false, toKey, toInclusive); - } - - return null; - } - - // - // For descending maps. - // - EntryI<K, V> - lastEntry(com.sleepycat.db.Cursor cursor, K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - throws com.sleepycat.db.DatabaseException - { - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.OperationStatus status = null; - - if(fromKey != null) - { - ByteBuffer k = encodeKey(fromKey); - dbKey.setDataNIO(k); - dbKey.setReuseBuffer(false); - - status = cursor.getSearchKeyRange(dbKey, dbValue, null); - - if(status == com.sleepycat.db.OperationStatus.SUCCESS && !fromInclusive) - { - int cmp = _comparator.compare(UtilI.getBuffer(dbKey), k); - assert(cmp >= 0); - if(cmp == 0) - { - status = cursor.getPrevNoDup(dbKey, dbValue, null); - } - } - } - else - { - status = cursor.getLast(dbKey, dbValue, null); - } - - if(status == com.sleepycat.db.OperationStatus.SUCCESS) - { - return newEntry(dbKey, dbValue, toKey, toInclusive, fromKey, fromInclusive); - } - - return null; - } - - // - // For descending maps. - // - EntryI<K, V> - previousEntry(com.sleepycat.db.Cursor cursor, K toKey, boolean toInclusive) - throws com.sleepycat.db.DatabaseException - { - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - - if(cursor.getPrev(dbKey, dbValue, null) == com.sleepycat.db.OperationStatus.SUCCESS) - { - return newEntry(dbKey, dbValue, toKey, toInclusive, null, false); - } - - return null; - } - - void - putImpl(com.sleepycat.db.DatabaseEntry dbKey, V value) - { - if(_db == null) - { - DatabaseException ex = new DatabaseException(); - ex.message = _trace.errorPrefix + "\"" + _dbName + "\" is closed"; - throw ex; - } - - ByteBuffer v = encodeValue(value); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(v); - - if(_trace.level >= 2) - { - _trace.logger.trace("Freeze.MapInternal.MapI", "writing value in Db \"" + _dbName + "\""); - } - - com.sleepycat.db.Transaction txn = _connection.dbTxn(); - if(txn == null) - { - closeAllIterators(); - } - - for(;;) - { - try - { - _db.db().put(txn, dbKey, dbValue); - break; - } - catch(com.sleepycat.db.DeadlockException e) - { - if(txn != null) - { - throw new DeadlockException(_trace.errorPrefix + "Db.put: " + e.getMessage(), - _connection.currentTransaction(), e); - } - else - { - if(_trace.deadlockWarning) - { - _trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.putImpl while " + - "writing into Db \"" + _dbName + "\"; retrying..."); - } - - // - // Try again - // - } - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Db.put: " + e.getMessage(), e); - } - } - } - - boolean - removeImpl(com.sleepycat.db.DatabaseEntry dbKey) - { - if(_db == null) - { - DatabaseException ex = new DatabaseException(); - ex.message = _trace.errorPrefix + "\"" + _dbName + "\" is closed"; - throw ex; - } - - if(_trace.level >= 2) - { - _trace.logger.trace("Freeze.MapInternal.MapI", "deleting value from Db \"" + _dbName + "\""); - } - - com.sleepycat.db.Transaction txn = _connection.dbTxn(); - if(txn == null) - { - closeAllIterators(); - } - - for(;;) - { - try - { - com.sleepycat.db.OperationStatus rc = _db.db().delete(txn, dbKey); - return (rc == com.sleepycat.db.OperationStatus.SUCCESS); - } - catch(com.sleepycat.db.DeadlockException e) - { - if(txn != null) - { - throw new DeadlockException(_trace.errorPrefix + "Db.del: " + e.getMessage(), - _connection.currentTransaction(), e); - } - else - { - if(_trace.deadlockWarning) - { - _trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.removeImpl while " + - "writing into Db \"" + _dbName + "\"; retrying..."); - } - - // - // Try again - // - } - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Db.del: " + e.getMessage(), e); - } - } - } - - private ByteBuffer - getImpl(com.sleepycat.db.DatabaseEntry dbKey) - { - if(_db == null) - { - DatabaseException ex = new DatabaseException(); - ex.message = _trace.errorPrefix + "\"" + _dbName + "\" is closed"; - throw ex; - } - - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - - if(_trace.level >= 2) - { - _trace.logger.trace("Freeze.MapInternal.MapI", "reading value from Db \"" + _dbName + "\""); - } - - for(;;) - { - try - { - com.sleepycat.db.OperationStatus rc = _db.db().get(_connection.dbTxn(), dbKey, dbValue, null); - if(rc == com.sleepycat.db.OperationStatus.SUCCESS) - { - return UtilI.getBuffer(dbValue); - } - else - { - return null; - } - } - catch(com.sleepycat.db.DeadlockException e) - { - if(_connection.dbTxn() != null) - { - throw new DeadlockException(_trace.errorPrefix + "Db.get: " + e.getMessage(), - _connection.currentTransaction(), e); - } - else - { - if(_trace.deadlockWarning) - { - _trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.getImpl while " + - "reading Db \"" + _dbName + "\"; retrying..."); - } - - // - // Try again - // - } - } - catch(com.sleepycat.db.DatabaseException e) - { - throw new DatabaseException(_trace.errorPrefix + "Db.get: " + e.getMessage(), e); - } - } - } - - private EntryI<K, V> - newEntry(com.sleepycat.db.DatabaseEntry dbKey, com.sleepycat.db.DatabaseEntry dbValue, K fromKey, - boolean fromInclusive, K toKey, boolean toInclusive) - { - K key = null; - if(fromKey != null || toKey != null) - { - key = decodeKey(dbKey); - if(!checkRange(key, fromKey, fromInclusive, toKey, toInclusive)) - { - return null; - } - } - - return new EntryI<K, V>(this, key, dbKey, UtilI.getBuffer(dbValue), null); - } - - private boolean - checkRange(K key, K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - if(fromKey != null) - { - int cmp = _comparator.comparator().compare(key, fromKey); - if((fromInclusive && cmp < 0) || (!fromInclusive && cmp <= 0)) - { - return false; - } - } - if(toKey != null) - { - int cmp = _comparator.comparator().compare(key, toKey); - if((toInclusive && cmp > 0) || (!toInclusive && cmp >= 0)) - { - return false; - } - } - return true; - } - - private static boolean - valEquals(Object v1, Object v2) - { - return (v1 == null ? v2 == null : v1.equals(v2)); - } - - private class DbComparator implements java.util.Comparator<byte[]> - { - DbComparator(java.util.Comparator<K> comparator) - { - _comparator = comparator; - } - - @Override - public int compare(byte[] d1, byte[] d2) - { - final ByteBuffer b1 = ByteBuffer.wrap(d1); - final ByteBuffer b2 = ByteBuffer.wrap(d2); - return _comparator.compare(decodeKey(b1), decodeKey(b2)); - } - - // - // The user-supplied comparator - // - private final java.util.Comparator<K> _comparator; - } - - private class Comparator implements java.util.Comparator<ByteBuffer> - { - Comparator(java.util.Comparator<K> comparator) - { - _comparator = comparator; - } - - public java.util.Comparator<K> comparator() - { - return _comparator; - } - - @Override - public int compare(ByteBuffer b1, ByteBuffer b2) - { - return _comparator.compare(decodeKey(b1), decodeKey(b2)); - } - - // - // The user-supplied comparator - // - private final java.util.Comparator<K> _comparator; - } - - public static class Patcher implements IceInternal.Patcher - { - public - Patcher(String type) - { - this.type = type; - } - - @Override - public void - patch(Ice.Object v) - { - value = v; - } - - @Override - public String - type() - { - return this.type; - } - - public Ice.Object - value() - { - return this.value; - } - - public String type; - public Ice.Object value; - } - - private final ConnectionI _connection; - private final Comparator _comparator; - private final DbComparator _dbComparator; - private final String _dbName; - - private final Ice.Communicator _communicator; - private final Ice.EncodingVersion _encoding; - private final TraceLevels _trace; - private java.util.Iterator<?> _token; - private MapDb _db; - - private java.util.Set<java.util.Map.Entry<K, V>> _entrySet; - private NavigableMap<K, V> _descendingMap; - private LinkedList<IteratorI<K, V>> _iteratorList = new LinkedList<IteratorI<K, V>>(); - private java.util.Map<String, MapIndex> _indexMap = new java.util.HashMap<String, MapIndex>(); -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/Search.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/Search.java deleted file mode 100644 index 2492fe8aaa9..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/Search.java +++ /dev/null @@ -1,240 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -import Freeze.ConnectionI; -import Freeze.DatabaseException; -import Freeze.DeadlockException; -import java.nio.ByteBuffer; - -class Search -{ - enum Type - { - FIRST // The first entry - { - @Override - Type descending() { return LAST; }; - }, - LAST // The last entry - { - @Override - Type descending() { return FIRST; }; - }, - CEILING // The entry with the smallest key greater than or equal to the target key - { - @Override - Type descending() { return FLOOR; }; - }, - FLOOR // The entry with the greatest key less than or equal to the target key - { - @Override - Type descending() { return CEILING; }; - }, - HIGHER // The entry with the smallest key greater than the target key - { - @Override - Type descending() { return LOWER; }; - }, - LOWER // The entry with the greatest key less than the target key - { - @Override - Type descending() { return HIGHER; }; - }; - - abstract Type descending(); // Returns the descending (opposite) value. - } - - interface KeyValidator - { - boolean keyInRange(ByteBuffer key); - } - - static boolean - search(Type type, ConnectionI connection, String dbName, com.sleepycat.db.Database db, - com.sleepycat.db.DatabaseEntry key, com.sleepycat.db.DatabaseEntry value, - java.util.Comparator<ByteBuffer> comparator, KeyValidator validator, TraceLevels trace) - { - if(type != Type.FIRST && type != Type.LAST && comparator == null) - { - throw new UnsupportedOperationException("A comparator is required"); - } - - if(trace.level >= 2) - { - trace.logger.trace("Freeze.Map", "searching Db \"" + dbName + "\""); - } - - if(value == null) - { - value = new com.sleepycat.db.DatabaseEntry(); - value.setPartial(true); // Not interested in the value. - } - - try - { - for(;;) - { - com.sleepycat.db.Cursor dbc = null; - try - { - com.sleepycat.db.DatabaseEntry dbcKey = - new com.sleepycat.db.DatabaseEntry(key != null ? UtilI.getBuffer(key) : null); - dbcKey.setReuseBuffer(false); - - dbc = db.openCursor(connection.dbTxn(), null); - - com.sleepycat.db.OperationStatus status = null; - - switch(type) - { - case FIRST: - { - status = dbc.getFirst(dbcKey, value, null); - break; - } - case LAST: - { - status = dbc.getLast(dbcKey, value, null); - break; - } - case CEILING: - { - // - // The semantics of getSearchKeyRange match CEILING. - // - status = dbc.getSearchKeyRange(dbcKey, value, null); - break; - } - case FLOOR: - { - status = dbc.getSearchKeyRange(dbcKey, value, null); - if(status == com.sleepycat.db.OperationStatus.SUCCESS) - { - // - // getSearchKeyRange returns the smallest key greater than or equal to - // the target key. If the matching key is greater than the target key - // then we need to get the previous entry. - // - int cmp = comparator.compare(UtilI.getBuffer(dbcKey), UtilI.getBuffer(key)); - assert(cmp >= 0); - if(cmp > 0) - { - status = dbc.getPrevNoDup(dbcKey, value, null); - } - } - else if(status == com.sleepycat.db.OperationStatus.NOTFOUND) - { - // - // All keys must be less than the target key so we pick the largest of - // all (the last one). - // - status = dbc.getLast(dbcKey, value, null); - } - break; - } - case HIGHER: - { - status = dbc.getSearchKeyRange(dbcKey, value, null); - if(status == com.sleepycat.db.OperationStatus.SUCCESS) - { - // - // getSearchKeyRange returns the smallest key greater than or equal to - // the target key. If the matching key is equal to the target key - // then we need to get the next entry. - // - int cmp = comparator.compare(UtilI.getBuffer(dbcKey), UtilI.getBuffer(key)); - assert(cmp >= 0); - if(cmp == 0) - { - status = dbc.getNextNoDup(dbcKey, value, null); - } - } - break; - } - case LOWER: - { - status = dbc.getSearchKeyRange(dbcKey, value, null); - if(status == com.sleepycat.db.OperationStatus.SUCCESS) - { - // - // getSearchKeyRange returns the smallest key greater than or equal to - // the target key. We move to the previous entry, whose key must be less - // than the target key. - // - status = dbc.getPrevNoDup(dbcKey, value, null); - } - else if(status == com.sleepycat.db.OperationStatus.NOTFOUND) - { - // - // All keys must be less than the target key so we pick the largest of - // all (the last one). - // - status = dbc.getLast(dbcKey, value, null); - } - break; - } - } - - if(status == com.sleepycat.db.OperationStatus.SUCCESS) - { - if(validator == null || validator.keyInRange(UtilI.getBuffer(dbcKey))) - { - key.setDataNIO(UtilI.getBuffer(dbcKey)); - return true; - } - } - - return false; - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(connection.dbTxn() != null) - { - throw new DeadlockException(trace.errorPrefix + dx.getMessage(), - connection.currentTransaction(), dx); - } - else - { - if(trace.deadlockWarning) - { - trace.logger.warning("Deadlock in Freeze.Map while searching \"" + dbName + - "\"; retrying..."); - } - - // - // Retry - // - } - } - finally - { - if(dbc != null) - { - try - { - dbc.close(); - } - catch(com.sleepycat.db.DeadlockException dx) - { - // - // Ignored - // - } - } - } - } - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(trace.errorPrefix + dx.getMessage(), dx); - } - } -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/SubMap.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/SubMap.java deleted file mode 100644 index fd270271c57..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/SubMap.java +++ /dev/null @@ -1,740 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -import Freeze.NavigableMap; -import java.nio.ByteBuffer; - -// -// Submap of a Freeze Map or of another submap -// - -class SubMap<K, V> extends java.util.AbstractMap<K, V> implements NavigableMap<K, V> -{ - SubMap(MapI<K, V> map, K fromKey, boolean fromInclusive, K toKey, boolean toInclusive, boolean ascending) - { - _map = map; - if(ascending) - { - _view = new AscendingView(fromKey, fromInclusive, toKey, toInclusive); - } - else - { - _view = new DescendingView(fromKey, fromInclusive, toKey, toInclusive); - } - } - - private - SubMap(MapI<K, V> map, View v) - { - _map = map; - _view = v; - } - - // - // NavigableMap methods - // - - @Override - public boolean - fastRemove(K key) - { - if(!_view.inRange(key, true)) - { - return false; - } - - return _map.fastRemove(key); - } - - @Override - public java.util.Map.Entry<K, V> - firstEntry() - { - return _view.first(); - } - - @Override - public java.util.Map.Entry<K, V> - lastEntry() - { - return _view.last(); - } - - @Override - public java.util.Map.Entry<K, V> - ceilingEntry(K key) - { - return _view.ceiling(key); - } - - @Override - public java.util.Map.Entry<K, V> - floorEntry(K key) - { - return _view.floor(key); - } - - @Override - public java.util.Map.Entry<K, V> - higherEntry(K key) - { - return _view.higher(key); - } - - @Override - public java.util.Map.Entry<K, V> - lowerEntry(K key) - { - return _view.lower(key); - } - - @Override - public K - ceilingKey(K key) - { - EntryI<K, V> e = _view.ceiling(key); - return e != null ? e.getKey() : null; - } - - @Override - public K - floorKey(K key) - { - EntryI<K, V> e = _view.floor(key); - return e != null ? e.getKey() : null; - } - - @Override - public K - higherKey(K key) - { - EntryI<K, V> e = _view.higher(key); - return e != null ? e.getKey() : null; - } - - @Override - public K - lowerKey(K key) - { - EntryI<K, V> e = _view.lower(key); - return e != null ? e.getKey() : null; - } - - @Override - public java.util.Set<K> - descendingKeySet() - { - return descendingMap().keySet(); - } - - @Override - public NavigableMap<K, V> - descendingMap() - { - if(_descendingMap == null) - { - View v = _view.descendingView(); - _descendingMap = new SubMap<K, V>(_map, v); - } - return _descendingMap; - } - - @Override - public NavigableMap<K, V> - headMap(K toKey, boolean inclusive) - { - if(toKey == null) - { - throw new NullPointerException(); - } - View v = _view.subView(null, false, toKey, inclusive); - return new SubMap<K, V>(_map, v); - } - - @Override - public NavigableMap<K, V> - subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - if(fromKey == null || toKey == null) - { - throw new NullPointerException(); - } - View v = _view.subView(fromKey, fromInclusive, toKey, toInclusive); - return new SubMap<K, V>(_map, v); - } - - @Override - public NavigableMap<K, V> - tailMap(K fromKey, boolean inclusive) - { - if(fromKey == null) - { - throw new NullPointerException(); - } - View v = _view.subView(fromKey, inclusive, null, false); - return new SubMap<K, V>(_map, v); - } - - @Override - public java.util.Map.Entry<K, V> - pollFirstEntry() - { - EntryI<K, V> e = _view.first(); - if(e != null) - { - _map.removeImpl(e.getDbKey()); - } - return e; - } - - @Override - public java.util.Map.Entry<K, V> - pollLastEntry() - { - EntryI<K, V> e = _view.last(); - if(e != null) - { - _map.removeImpl(e.getDbKey()); - } - return e; - } - - // - // SortedMap methods - // - - @Override - public java.util.Comparator<? super K> - comparator() - { - return _view.comparator(); - } - - @Override - public K - firstKey() - { - EntryI<K, V> e = _view.first(); - if(e == null) - { - throw new java.util.NoSuchElementException(); - } - return e.getKey(); - } - - @Override - public K - lastKey() - { - EntryI<K, V> e = _view.last(); - if(e == null) - { - throw new java.util.NoSuchElementException(); - } - return e.getKey(); - } - - @Override - public java.util.SortedMap<K, V> - headMap(K toKey) - { - return headMap(toKey, false); - } - - @Override - public java.util.SortedMap<K, V> - tailMap(K fromKey) - { - return tailMap(fromKey, true); - } - - @Override - public java.util.SortedMap<K, V> - subMap(K fromKey, K toKey) - { - return subMap(fromKey, true, toKey, false); - } - - // - // Map methods - // - - @Override - public java.util.Set<java.util.Map.Entry<K, V>> - entrySet() - { - if(_entrySet == null) - { - _entrySet = new java.util.AbstractSet<java.util.Map.Entry<K, V>>() - { - @Override - public java.util.Iterator<java.util.Map.Entry<K, V>> - iterator() - { - return new IteratorI<K, V>(_map, _view); - } - - @Override - public boolean - contains(Object o) - { - // - // If the main map contains this object, verify it's within the range of this submap. - // - if(_map.entrySet().contains(o)) - { - @SuppressWarnings("unchecked") - EntryI<K, V> entry = (EntryI<K, V>)o; - return _view.inRange(entry.getKey(), true); - } - else - { - return false; - } - } - - @Override - public boolean - remove(Object o) - { - if(o instanceof EntryI) - { - @SuppressWarnings("unchecked") - EntryI<K, V> entry = (EntryI<K, V>)o; - return _view.inRange(entry.getKey(), true) && _map.entrySet().remove(o); - } - else - { - return false; - } - } - - @Override - public int - size() - { - throw new UnsupportedOperationException(); - } - - @Override - public boolean - isEmpty() - { - try - { - firstKey(); - return false; - } - catch(java.util.NoSuchElementException e) - { - return true; - } - } - }; - } - return _entrySet; - } - - // - // Put is not implemented (you have to put in the main map view) - // - - public boolean - constainsKey(Object key) - { - @SuppressWarnings("unchecked") - K k = (K)key; - if(!_view.inRange(k, true)) - { - return false; - } - - return _map.containsKey(k); - } - - @Override - public V - get(Object key) - { - @SuppressWarnings("unchecked") - K k = (K)key; - if(!_view.inRange(k, true)) - { - return null; - } - - return _map.get(k); - } - - @Override - public V - remove(Object key) - { - @SuppressWarnings("unchecked") - K k = (K)key; - if(!_view.inRange(k, true)) - { - return null; - } - - return _map.remove(k); - } - - private abstract class View implements IteratorModel<K, V>, Search.KeyValidator - { - protected - View(java.util.Comparator<? super K> comparator, K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - _comparator = comparator; - _fromKey = fromKey; - _fromInclusive = fromInclusive; - _toKey = toKey; - _toInclusive = toInclusive; - - // - // Validate the key range. - // - if(_fromKey != null && _toKey != null) - { - int cmp = comparator.compare(_fromKey, _toKey); - if(cmp > 0 || (cmp == 0 && !(_fromInclusive && _toInclusive))) - { - throw new IllegalArgumentException(); - } - } - } - - protected - View(View v, java.util.Comparator<? super K> comparator, K fromKey, boolean fromInclusive, K toKey, - boolean toInclusive) - { - this(comparator, fromKey, fromInclusive, toKey, toInclusive); - - // - // Verify that the key range is correct with respect to the original view. - // - if(!v.inRange(_fromKey, _fromInclusive) || !v.inRange(_toKey, _toInclusive)) - { - throw new IllegalArgumentException(); - } - } - - abstract Search.Type mapSearchType(Search.Type type); - abstract View copy(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive); - abstract View descendingView(); - - final EntryI<K, V> - first() - { - Search.Type type; - ByteBuffer key = null; - if(_fromKey != null) - { - type = _fromInclusive ? mapSearchType(Search.Type.CEILING) : mapSearchType(Search.Type.HIGHER); - key = fromKeyBytes(); - } - else - { - type = mapSearchType(Search.Type.FIRST); - } - return _map.entrySearch(type, key, true, this); - } - - final EntryI<K, V> - last() - { - Search.Type type; - ByteBuffer key = null; - if(_toKey != null) - { - type = _toInclusive ? mapSearchType(Search.Type.FLOOR) : mapSearchType(Search.Type.LOWER); - key = toKeyBytes(); - } - else - { - type = mapSearchType(Search.Type.LAST); - } - return _map.entrySearch(type, key, true, this); - } - - final EntryI<K, V> - ceiling(K key) - { - ByteBuffer k = _map.encodeKey(key); - return _map.entrySearch(mapSearchType(Search.Type.CEILING), k, true, this); - } - - final EntryI<K, V> - floor(K key) - { - ByteBuffer k = _map.encodeKey(key); - return _map.entrySearch(mapSearchType(Search.Type.FLOOR), k, true, this); - } - - final EntryI<K, V> - higher(K key) - { - ByteBuffer k = _map.encodeKey(key); - return _map.entrySearch(mapSearchType(Search.Type.HIGHER), k, true, this); - } - - final EntryI<K, V> - lower(K key) - { - ByteBuffer k = _map.encodeKey(key); - return _map.entrySearch(mapSearchType(Search.Type.LOWER), k, true, this); - } - - final View - subView(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - if(fromKey == null) - { - fromKey = _fromKey; - fromInclusive = _fromInclusive; - } - if(toKey == null) - { - toKey = _toKey; - toInclusive = _toInclusive; - } - return copy(fromKey, fromInclusive, toKey, toInclusive); - } - - // - // IteratorModel methods (partial) - // - - @Override - final public String - dbName() - { - return _map.dbName(); - } - - @Override - final public TraceLevels - traceLevels() - { - return _map.traceLevels(); - } - - @Override - final public com.sleepycat.db.Cursor - openCursor() - throws com.sleepycat.db.DatabaseException - { - return _map.openCursor(); - } - - // - // Search.KeyValidator methods - // - - @Override - final public boolean - keyInRange(ByteBuffer key) - { - K k = _map.decodeKey(key); - return inRange(k, true); - } - - final boolean - inRange(K key, boolean inclusive) - { - return !tooLow(key, inclusive, _fromKey, _fromInclusive) && - !tooHigh(key, inclusive, _toKey, _toInclusive); - } - - final java.util.Comparator<? super K> - comparator() - { - return _comparator; - } - - final protected ByteBuffer - fromKeyBytes() - { - if(_fromKey != null && _fromKeyBytes == null) - { - _fromKeyBytes = _map.encodeKey(_fromKey); - } - return _fromKeyBytes; - } - - final protected ByteBuffer - toKeyBytes() - { - if(_toKey != null && _toKeyBytes == null) - { - _toKeyBytes = _map.encodeKey(_toKey); - } - return _toKeyBytes; - } - - final protected boolean - tooLow(K key, boolean inclusive, K targetKey, boolean targetInclusive) - { - if(key != null && targetKey != null) - { - int cmp = comparator().compare(key, targetKey); - if(cmp < 0 || (cmp == 0 && inclusive && !targetInclusive)) - { - return true; - } - } - return false; - } - - final protected boolean - tooHigh(K key, boolean inclusive, K targetKey, boolean targetInclusive) - { - if(key != null && targetKey != null) - { - int cmp = comparator().compare(key, targetKey); - if(cmp > 0 || (cmp == 0 && inclusive && !targetInclusive)) - { - return true; - } - } - return false; - } - - final java.util.Comparator<? super K> _comparator; - final K _fromKey; - final boolean _fromInclusive; - final K _toKey; - final boolean _toInclusive; - private ByteBuffer _fromKeyBytes; - private ByteBuffer _toKeyBytes; - } - - private class AscendingView extends View - { - AscendingView(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - super(_map.comparator(), fromKey, fromInclusive, toKey, toInclusive); - } - - AscendingView(View v, K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - super(v, _map.comparator(), fromKey, fromInclusive, toKey, toInclusive); - } - - // - // View methods - // - - @Override - Search.Type - mapSearchType(Search.Type type) - { - return type; - } - - @Override - View - copy(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - return new AscendingView(this, fromKey, fromInclusive, toKey, toInclusive); - } - - @Override - View - descendingView() - { - return new DescendingView(this, _toKey, _toInclusive, _fromKey, _fromInclusive); - } - - // - // IteratorModel methods - // - - @Override - public EntryI<K, V> - firstEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return _map.firstEntry(cursor, _fromKey, _fromInclusive, _toKey, _toInclusive); - } - - @Override - public EntryI<K, V> - nextEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return _map.nextEntry(cursor, _toKey, _toInclusive); - } - } - - private class DescendingView extends View - { - DescendingView(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - super(java.util.Collections.reverseOrder(_map.comparator()), fromKey, fromInclusive, toKey, toInclusive); - } - - DescendingView(View v, K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - super(v, java.util.Collections.reverseOrder(_map.comparator()), fromKey, fromInclusive, toKey, toInclusive); - } - - // - // View methods - // - - @Override - Search.Type - mapSearchType(Search.Type type) - { - return type.descending(); - } - - @Override - View - copy(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) - { - return new DescendingView(this, fromKey, fromInclusive, toKey, toInclusive); - } - - @Override - View - descendingView() - { - return new AscendingView(this, _toKey, _toInclusive, _fromKey, _fromInclusive); - } - - // - // IteratorModel methods - // - - @Override - public EntryI<K, V> - firstEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return _map.lastEntry(cursor, _fromKey, _fromInclusive, _toKey, _toInclusive); - } - - @Override - public EntryI<K, V> - nextEntry(com.sleepycat.db.Cursor cursor) - throws com.sleepycat.db.DatabaseException - { - return _map.previousEntry(cursor, _toKey, _toInclusive); - } - } - - private final MapI<K, V> _map; - private final View _view; - private java.util.Set<java.util.Map.Entry<K, V>> _entrySet; - private NavigableMap<K, V> _descendingMap; -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/TraceLevels.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/TraceLevels.java deleted file mode 100644 index fdec33615ff..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/TraceLevels.java +++ /dev/null @@ -1,28 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -import Freeze.ConnectionI; - -class TraceLevels -{ - TraceLevels(ConnectionI connection, String dbName) - { - errorPrefix = "Freeze DB DbEnv(\"" + connection.envName() + "\") Db(\"" + dbName + "\"): "; - level = connection.trace(); - deadlockWarning = connection.deadlockWarning(); - logger = connection.getCommunicator().getLogger(); - } - - final String errorPrefix; - final int level; - final boolean deadlockWarning; - final Ice.Logger logger; -} diff --git a/java/src/Freeze/src/main/java/Freeze/MapInternal/UtilI.java b/java/src/Freeze/src/main/java/Freeze/MapInternal/UtilI.java deleted file mode 100644 index 735e3473b8c..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/MapInternal/UtilI.java +++ /dev/null @@ -1,35 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze.MapInternal; - -import java.nio.ByteBuffer; - -class UtilI -{ - // - // A DatabaseEntry object won't always have a ByteBuffer. In fact, the entry only - // keeps a ByteBuffer if it's a direct buffer, otherwise the entry keeps a reference - // to the buffer's backing array and discards the buffer. - // - public static ByteBuffer getBuffer(com.sleepycat.db.DatabaseEntry entry) - { - ByteBuffer b = entry.getDataNIO(); - if(b == null) - { - byte[] arr = entry.getData(); - if(arr != null) - { - b = ByteBuffer.wrap(arr, entry.getOffset(), entry.getSize()); - } - } - - return b; - } -} diff --git a/java/src/Freeze/src/main/java/Freeze/NavigableMap.java b/java/src/Freeze/src/main/java/Freeze/NavigableMap.java deleted file mode 100644 index d37e036e02b..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/NavigableMap.java +++ /dev/null @@ -1,233 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -/** - * Parent interface for Freeze maps. - **/ -public interface NavigableMap<K, V> extends java.util.SortedMap<K, V> -{ - /** - * Alternative to <code>java.util.SortedMap.remove</code>. This version - * is more efficient because it does not decode and return the - * old value. - * - * @param key The key of the entry to remove. - * @return <code>true</code> if an entry for <code>key</code> was removed; - * <code>false</code>, otherwise. - **/ - boolean fastRemove(K key); - - /** - * Returns The entry with the least key. - * - * @return The entry with the least key, or <code>null</code> if the map is empty. - **/ - java.util.Map.Entry<K, V> firstEntry(); - - /** - * Returns The entry with the greatest key. - * - * @return The entry with the greatest key, or <code>null</code> if the map is empty. - **/ - java.util.Map.Entry<K, V> lastEntry(); - - /** - * Returns the entry with the least key greater than or equal to the given key. - * - * @param key The key. - * @return The entry with the least key greater than or equal to the given key, - * or <code>null</code> if there is no such entry. - **/ - java.util.Map.Entry<K, V> ceilingEntry(K key); - - /** - * Returns the entry with the greatest key less than or equal to the given key. - * - * @param key The key. - * @return The entry with the greatest key less than or equal to the given key, - * or <code>null</code> if there is no such entry. - **/ - java.util.Map.Entry<K, V> floorEntry(K key); - - /** - * Returns the entry with the least key strictly greater than the given key. - * - * @param key The key. - * @return The entry with the least key strictly greater than the given key, - * or <code>null</code> if there is no such entry. - **/ - java.util.Map.Entry<K, V> higherEntry(K key); - - /** - * Returns the entry with the greatest key strictly less than the given key. - * - * @param key The key. - * @return The entry with the greatest key strictly less than the given key, - * or <code>null</code> if there is no such entry. - **/ - java.util.Map.Entry<K, V> lowerEntry(K key); - - /** - * Returns the least key greater than or equal to the given key. - * - * @param key The key. - * @return The least key greater than or equal to the given key, - * or <code>null</code> if there is no such key. - **/ - K ceilingKey(K key); - - /** - * Returns the greatest key less than or equal to the given key. - * - * @param key The key. - * @return The greatest key less than or equal to the given key, - * or <code>null</code> if there is no such key. - **/ - K floorKey(K key); - - /** - * Returns the least key strictly greater than the given key. - * - * @return The least key strictly greater than the given key, - * or <code>null</code> if there is no such key. - **/ - K higherKey(K key); - - /** - * Returns the greatest key strictly less than the given key. - * - * @return The greatest key strictly less than the given key, - * or <code>null</code> if there is no such key. - **/ - K lowerKey(K key); - - /** - * Returns a reverse order <code>Set</code> view of the keys contained in this map. The set's iterator - * returns the keys in descending order. The set is backed by the map, so changes to the map are reflected - * in the set and vice versa. - * <p> - * Insertions cannot be made via the returned set and must be made on the main map. - * <p> - * <code>remove</code> and <code>fastRemove</code> on the returned set or on an iterator for the returned - * set are supported, but only if this map does not have an index; otherwise, attempts to remove an element - * raise <code>UnsupportedOperationException</code>. - * <p> - * If the map is modified while an iteration over the set is in progress - * (except through the iterator's own <code>remove</code> or <code>fastRemove</code> operation), the results - * of the iteration are undefined. - * - * @return A reverse order set view of the keys in this map. - **/ - java.util.Set<K> descendingKeySet(); - - /** - * Returns a reverse order view of the mappings contained in this map. - * The descending map is backed by this map, so changes to the map are reflected in the descending map - * and vice versa. - *<p> - * Insertions cannot be made via the descending map and must be made on the main map. - * <p> - * <code>remove</code> and <code>fastRemove</code> on the descending map or on an iterator for the descending - * map are supported, but only if this map does not have an index; otherwise, attempts to remove an entry - * raise <code>UnsupportedOperationException</code>. - * <p> - * If either map is modified while an iteration over either map is - * in progress (except through the iterator's own <code>remove</code> or <code>fastRemove</code>operation), - * the results of the iteration are undefined. - * - * @return a Reverse order view of this map. - **/ - NavigableMap<K, V> descendingMap(); - - /** - * Returns a view of the portion of this map whose keys are strictly less than <code>toKey</code>, or less than - * or equal to <code>toKey</code> if <code>inclusive</code> is true. - * The returned map is backed by this map, so changes to this map are reflected in the returned map - * and vice versa. - *<p> - * Insertions cannot be made via the returned map and must be made on the main map. - * <p> - * <code>remove</code> and <code>fastRemove</code> on the returned map or on an iterator for the returned - * map are supported, but only if this map does not have an index; otherwise, attempts to remove an entry - * raise <code>UnsupportedOperationException</code>. - * <p> - * The returned map throws an <code>IllegalArgumentException</code> on an attempt to insert a key - * outside its range. - * - * @param toKey High endpoint of the keys in the returned map. - * @param inclusive If <code>true</code>, the endpoint is included in the returned map; otherwise, the endpoint - * is excluded. - * @return A view of the portion of this map whose keys are strictly less than <code>toKey</code>, or - * less than or equal to <code>toKey</code> if <code>inclusive</code> is true. - **/ - NavigableMap<K, V> headMap(K toKey, boolean inclusive); - - /** - * Returns a view of the portion of this map whose keys are strictly greater than <code>fromKey</code>, or greater - * than or equal to <code>fromKey</code> if <code>inclusive</code> is true. - *<p> - * Insertions cannot be made via the returned map and must be made on the main map. - * <p> - * <code>remove</code> and <code>fastRemove</code> on the returned map or on an iterator for the returned - * map are supported, but only if this map does not have an index; otherwise, attempts to remove an entry - * raise <code>UnsupportedOperationException</code>. - * <p> - * The returned map throws an <code>IllegalArgumentException</code> on an attempt to insert a key - * outside its range. - * - * @param fromKey Low endpoint of the keys in the returned map. - * @param inclusive If <code>true</code>, the endpoint is included in the returned map; otherwise, the endpoint - * is excluded. - * @return A view of the portion of this map whose keys are strictly greater than <code>fromKey</code>, or - * greater than or equal to <code>fromKey</code> if <code>inclusive</code> is true. - **/ - NavigableMap<K, V> tailMap(K fromKey, boolean inclusive); - - /** - * Returns a view of the portion of this map whose keys range from <code>fromKey</code> to <code>toKey</code>. - * If <code>fromKey</code> and <code>toKey</code> are equal, the returned map is empty unless - * <code>fromInclusive</code> and <code>toInclusive</code> are both <code>true</code>. - *<p> - * Insertions cannot be made via the returned map and must be made on the main map. - * <p> - * <code>remove</code> and <code>fastRemove</code> on the returned map or on an iterator for the returned - * map are supported, but only if this map does not have an index; otherwise, attempts to remove an entry - * raise <code>UnsupportedOperationException</code>. - * <p> - * The returned map throws an <code>IllegalArgumentException</code> on an attempt to insert a key - * outside its range. - * - * @param fromKey Low endpoint of the keys in the returned map. - * @param fromInclusive <code>true</code> if the low endpoint is to be included in the returned view; - * <code>false</code>, otherwise. - * @param toKey High endpoint of the keys in the returned map. - * @param toInclusive <code>true</code> if the high endpoint is to be included in the returned view; - * <code>false</code>, otherwise. - * @return A view of the portion of this map whose keys range from <code>fromKey</code> to <code>toKey</code>. - **/ - NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive); - - /** - * Removes and returns a key-value mapping associated with the least key in this map, - * or <code>null</code> if the map is empty. - * - * @return The removed first entry, or <code>null</code> if this map is empty. - **/ - java.util.Map.Entry<K, V> pollFirstEntry(); - - /** - * Removes and returns a key-value mapping associated with the greatest key in this map, - * or <code>null</code> if the map is empty. - * - * @return The removed last entry, or <code>null</code> if this map is empty. - **/ - java.util.Map.Entry<K, V> pollLastEntry(); -} diff --git a/java/src/Freeze/src/main/java/Freeze/ObjectStore.java b/java/src/Freeze/src/main/java/Freeze/ObjectStore.java deleted file mode 100644 index 1e846540792..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/ObjectStore.java +++ /dev/null @@ -1,698 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -class ObjectStore implements IceUtil.Store -{ - ObjectStore(String facet, String facetType, boolean createDb, EvictorI evictor, java.util.List<Index> indices, - boolean populateEmptyIndices) - { - _cache = new IceUtil.Cache(this); - - _facet = facet; - - _evictor = evictor; - _indices = indices; - _communicator = evictor.communicator(); - _encoding = evictor.encoding(); - _keepStats = false; - - if(facet.equals("")) - { - _dbName = EvictorI.defaultDb; - } - else - { - _dbName = facet; - } - - if(facetType != null) - { - // - // Create a sample servant with this type - // - Ice.ObjectFactory factory = _communicator.findObjectFactory(facetType); - if(factory == null) - { - throw new DatabaseException(_evictor.errorPrefix() + "No object factory registered for type-id '" + - facetType + "'"); - } - - _sampleServant = factory.create(facetType); - } - - Connection connection = Util.createConnection(_communicator, evictor.dbEnv().getEnvName()); - - try - { - Catalog catalog = new Catalog(connection, Util.catalogName(), true); - CatalogData catalogData = catalog.get(evictor.filename()); - - if(catalogData != null) - { - if(catalogData.evictor) - { - _keepStats = catalogData.value.isEmpty(); - } - else - { - DatabaseException ex = new DatabaseException(); - ex.message = _evictor.errorPrefix() + evictor.filename() + " is not an evictor database"; - throw ex; - } - } - - com.sleepycat.db.Environment dbEnv = evictor.dbEnv().getEnv(); - - // - // TODO: FREEZE_DB_MODE - // - com.sleepycat.db.DatabaseConfig config = new com.sleepycat.db.DatabaseConfig(); - config.setType(com.sleepycat.db.DatabaseType.BTREE); - config.setAllowCreate(createDb); - - Ice.Properties properties = _evictor.communicator().getProperties(); - String propPrefix = "Freeze.Evictor." + _evictor.filename() + "."; - - int btreeMinKey = properties.getPropertyAsInt(propPrefix + _dbName + ".BtreeMinKey"); - if(btreeMinKey > 2) - { - if(_evictor.trace() >= 1) - { - _evictor.communicator().getLogger().trace( - "Freeze.Evictor", "Setting \"" + _evictor.filename() + "." + _dbName + - "\"'s btree minkey to " + btreeMinKey); - } - config.setBtreeMinKey(btreeMinKey); - } - - boolean checksum = properties.getPropertyAsInt(propPrefix + "Checksum") > 0; - if(checksum) - { - if(_evictor.trace() >= 1) - { - _evictor.communicator().getLogger().trace( - "Freeze.Evictor", "Turning checksum on for \"" + _evictor.filename() + "\""); - } - - config.setChecksum(true); - } - - int pageSize = properties.getPropertyAsInt(propPrefix + "PageSize"); - if(pageSize > 0) - { - if(_evictor.trace() >= 1) - { - _evictor.communicator().getLogger().trace( - "Freeze.Evictor", "Setting \"" + _evictor.filename() + "\"'s pagesize to " + pageSize); - } - config.setPageSize(pageSize); - } - - try - { - Transaction tx = connection.beginTransaction(); - com.sleepycat.db.Transaction txn = Util.getTxn(tx); - - _db = dbEnv.openDatabase(txn, evictor.filename(), _dbName, config); - - for(Index index : _indices) - { - index.associate(this, txn, createDb, populateEmptyIndices); - } - - if(catalogData == null) - { - catalogData = new CatalogData(true, "::Ice::Identity", "Object"); - catalog.put(evictor.filename(), catalogData); - } - - tx.commit(); - } - catch(java.io.FileNotFoundException dx) - { - throw new NotFoundException(_evictor.errorPrefix() + "Db.open: " + dx.getMessage(), dx); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_evictor.errorPrefix() + "Db.open: " + dx.getMessage(), dx); - } - finally - { - Transaction tx = connection.currentTransaction(); - if(tx != null) - { - try - { - tx.rollback(); - } - catch(DatabaseException de) - { - } - } - } - } - finally - { - connection.close(); - } - } - - void - close() - { - try - { - _db.close(); - - for(Index index : _indices) - { - index.close(); - } - _indices.clear(); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_evictor.errorPrefix() + "Db.close: " + dx.getMessage(), dx); - } - _db = null; - } - - boolean - dbHasObject(Ice.Identity ident, TransactionI transaction) - { - com.sleepycat.db.Transaction tx = null; - - if(transaction != null) - { - tx = transaction.dbTxn(); - if(tx == null) - { - throw new DatabaseException(_evictor.errorPrefix() + "inactive transaction"); - } - } - - com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); - - // - // Keep 0 length since we're not interested in the data - // - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - dbValue.setPartial(true); - - for(;;) - { - try - { - com.sleepycat.db.OperationStatus err = _db.get(tx, dbKey, dbValue, null); - - if(err == com.sleepycat.db.OperationStatus.SUCCESS) - { - return true; - } - else if(err == com.sleepycat.db.OperationStatus.NOTFOUND) - { - return false; - } - else - { - throw new DatabaseException(); - } - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_evictor.deadlockWarning()) - { - _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.dhHasObject while reading " + - "Db \"" + _evictor.filename() + "/" + _dbName + "\""); - } - - if(tx != null) - { - throw new DeadlockException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(), transaction, dx); - } - // - // Otherwise try again - // - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(), dx); - } - } - } - - void - save(com.sleepycat.db.DatabaseEntry key, com.sleepycat.db.DatabaseEntry value, byte status, - com.sleepycat.db.Transaction tx) - throws com.sleepycat.db.DatabaseException - { - assert tx != null; - - switch(status) - { - case BackgroundSaveEvictorI.created: - case BackgroundSaveEvictorI.modified: - { - com.sleepycat.db.OperationStatus err; - if(status == BackgroundSaveEvictorI.created) - { - err = _db.putNoOverwrite(tx, key, value); - } - else - { - err = _db.put(tx, key, value); - } - if(err != com.sleepycat.db.OperationStatus.SUCCESS) - { - throw new DatabaseException(); - } - break; - } - case BackgroundSaveEvictorI.destroyed: - { - com.sleepycat.db.OperationStatus err = _db.delete(tx, key); - if(err != com.sleepycat.db.OperationStatus.SUCCESS) - { - throw new DatabaseException(); - } - break; - } - default: - { - assert false; - } - } - } - - static com.sleepycat.db.DatabaseEntry - marshalKey(Ice.Identity v, Ice.Communicator communicator, Ice.EncodingVersion encoding) - { - IceInternal.BasicStream os = - new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, false); - v.__write(os); - return new com.sleepycat.db.DatabaseEntry(os.prepareWrite().b); - } - - static Ice.Identity - unmarshalKey(com.sleepycat.db.DatabaseEntry e, Ice.Communicator communicator, Ice.EncodingVersion encoding) - { - IceInternal.BasicStream is; - if(e.getDataNIO() != null) - { - is = new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, e.getDataNIO()); - } - else - { - is = new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, e.getData()); - } - Ice.Identity key = new Ice.Identity(); - key.__read(is); - return key; - } - - static com.sleepycat.db.DatabaseEntry - marshalValue(ObjectRecord v, Ice.Communicator communicator, Ice.EncodingVersion encoding, boolean keepStats) - { - IceInternal.BasicStream os = - new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, false); - os.startWriteEncaps(); - - if(keepStats) - { - v.__write(os); - } - else - { - os.writeObject(v.servant); - } - os.writePendingObjects(); - os.endWriteEncaps(); - return new com.sleepycat.db.DatabaseEntry(os.prepareWrite().b); - } - - static ObjectRecord - unmarshalValue(com.sleepycat.db.DatabaseEntry e, Ice.Communicator communicator, Ice.EncodingVersion encoding, - boolean keepStats) - { - IceInternal.BasicStream is; - if(e.getDataNIO() != null) - { - is = new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, e.getDataNIO()); - } - else - { - is = new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, e.getData()); - } - is.sliceObjects(false); - ObjectRecord rec = new ObjectRecord(); - is.startReadEncaps(); - if(keepStats) - { - rec.__read(is); - is.readPendingObjects(); - } - else - { - Ice.ObjectHolder holder = new Ice.ObjectHolder(); - is.readObject(holder); - is.readPendingObjects(); - rec.servant = holder.value; - } - is.endReadEncaps(); - return rec; - } - - final IceUtil.Cache - cache() - { - return _cache; - } - - final com.sleepycat.db.Database - db() - { - return _db; - } - - final Ice.Communicator - communicator() - { - return _communicator; - } - - final Ice.EncodingVersion - encoding() - { - return _encoding; - } - - final boolean - keepStats() - { - return _keepStats; - } - - final EvictorI - evictor() - { - return _evictor; - } - - final String - facet() - { - return _facet; - } - - final String - dbName() - { - return _dbName; - } - - final Ice.Object - sampleServant() - { - return _sampleServant; - } - - // - // Load a servant from the database; will end up in the cache associated with - // this ObjectStore. This load is not transactional. - // - @Override - public Object - load(Object identObj) - { - Ice.Identity ident = (Ice.Identity)identObj; - - com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - - for(;;) - { - try - { - com.sleepycat.db.OperationStatus rs = _db.get(null, dbKey, dbValue, null); - - if(rs == com.sleepycat.db.OperationStatus.NOTFOUND) - { - return null; - } - else if (rs != com.sleepycat.db.OperationStatus.SUCCESS) - { - assert false; - throw new DatabaseException(); - } - break; - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_evictor.deadlockWarning()) - { - _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.load while reading Db \"" + - _evictor.filename() + "/" + _dbName + "\"; retrying..."); - } - - // - // Ignored, try again - // - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(), dx); - } - } - - ObjectRecord rec = unmarshalValue(dbValue, _communicator, _encoding, _keepStats); - _evictor.initialize(ident, _facet, rec.servant); - - Object result = _evictor.createEvictorElement(ident, rec, this); - return result; - } - - // - // Load a servant from the database using the given transaction; this servant - // is NOT cached in the ObjectStore associated cache - // - ObjectRecord - load(Ice.Identity ident, TransactionI transaction) - { - if(transaction == null) - { - throw new DatabaseException(_evictor.errorPrefix() + "no active transaction"); - } - com.sleepycat.db.Transaction tx = transaction.dbTxn(); - if(tx == null) - { - throw new DatabaseException(_evictor.errorPrefix() + "inactive transaction"); - } - - com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); - com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - - try - { - com.sleepycat.db.OperationStatus rs = _db.get(tx, dbKey, dbValue, null); - - if(rs == com.sleepycat.db.OperationStatus.NOTFOUND) - { - return null; - } - else if(rs != com.sleepycat.db.OperationStatus.SUCCESS) - { - assert false; - throw new DatabaseException(); - } - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_evictor.deadlockWarning()) - { - _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.load while reading Db \"" + - _evictor.filename() + "/" + _dbName + "\""); - } - - throw new DeadlockException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(), transaction, dx); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(), dx); - } - - ObjectRecord rec = unmarshalValue(dbValue, _communicator, _encoding, _keepStats); - _evictor.initialize(ident, _facet, rec.servant); - return rec; - } - - void - update(Ice.Identity ident, ObjectRecord objectRecord, TransactionI transaction) - { - if(transaction == null) - { - throw new DatabaseException(_evictor.errorPrefix() + "no active transaction"); - } - com.sleepycat.db.Transaction tx = transaction.dbTxn(); - if(tx == null) - { - throw new DatabaseException(_evictor.errorPrefix() + "inactive transaction"); - } - - if(_sampleServant != null && !objectRecord.servant.ice_id().equals(_sampleServant.ice_id())) - { - String msg = _evictor.errorPrefix() + "Attempting to save a '" + objectRecord.servant.ice_id() - + "' servant in a database of '" + _sampleServant.ice_id() + "' servants"; - - throw new DatabaseException(msg); - } - - com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); - com.sleepycat.db.DatabaseEntry dbValue = marshalValue(objectRecord, _communicator, _encoding, _keepStats); - - try - { - com.sleepycat.db.OperationStatus err = _db.put(tx, dbKey, dbValue); - if(err != com.sleepycat.db.OperationStatus.SUCCESS) - { - throw new DatabaseException(); - } - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_evictor.deadlockWarning()) - { - _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.update while updating Db \"" + - _evictor.filename() + "/" + _dbName + "\""); - } - - throw new DeadlockException(_evictor.errorPrefix() + "Db.put: " + dx.getMessage(), transaction, dx); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_evictor.errorPrefix() + "Db.put: " + dx.getMessage(), dx); - } - } - - boolean - insert(Ice.Identity ident, ObjectRecord objectRecord, TransactionI transaction) - { - com.sleepycat.db.Transaction tx = null; - - if(transaction != null) - { - tx = transaction.dbTxn(); - if(tx == null) - { - throw new DatabaseException(_evictor.errorPrefix() + "invalid transaction"); - } - } - - com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); - com.sleepycat.db.DatabaseEntry dbValue = marshalValue(objectRecord, _communicator, _encoding, _keepStats); - - if(_sampleServant != null && !objectRecord.servant.ice_id().equals(_sampleServant.ice_id())) - { - String msg = _evictor.errorPrefix() + "Attempting to save a '" + objectRecord.servant.ice_id() + - "' servant in a database of '" + _sampleServant.ice_id() + "' servants"; - - throw new DatabaseException(msg); - } - - for(;;) - { - try - { - return _db.putNoOverwrite(tx, dbKey, dbValue) == com.sleepycat.db.OperationStatus.SUCCESS; - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_evictor.deadlockWarning()) - { - _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.update while updating Db \"" + - _evictor.filename() + "/" + _dbName + "\""); - } - - if(tx != null) - { - throw new DeadlockException(_evictor.errorPrefix() + "Db.putNoOverwrite: " + dx.getMessage(), - transaction, dx); - } - // - // Otherwise retry - // - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_evictor.errorPrefix() + "Db.putNoOverwrite: " + dx.getMessage(), dx); - } - } - } - - boolean - remove(Ice.Identity ident, TransactionI transaction) - { - com.sleepycat.db.Transaction tx = null; - - if(transaction != null) - { - tx = transaction.dbTxn(); - if(tx == null) - { - throw new DatabaseException(_evictor.errorPrefix() + "invalid transaction"); - } - } - - com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); - - for(;;) - { - try - { - return _db.delete(tx, dbKey) == com.sleepycat.db.OperationStatus.SUCCESS; - } - catch(com.sleepycat.db.DeadlockException dx) - { - if(_evictor.deadlockWarning()) - { - _communicator.getLogger().warning("Deadlock in Freeze.ObjectStore.remove while updating Db \"" + - _evictor.filename() + "/" + _dbName + "\""); - } - - if(tx != null) - { - throw new DeadlockException(_evictor.errorPrefix() + "Db.delete: " + dx.getMessage(), transaction, - dx); - } - - // - // Otherwise retry - // - - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(_evictor.errorPrefix() + "Db.delete: " + dx.getMessage(), dx); - } - } - } - - private final IceUtil.Cache _cache; - private final String _facet; - private final String _dbName; - private final EvictorI _evictor; - private final java.util.List<Index> _indices; - private final Ice.Communicator _communicator; - private final Ice.EncodingVersion _encoding; - private boolean _keepStats; - - private com.sleepycat.db.Database _db; - private Ice.Object _sampleServant; -} diff --git a/java/src/Freeze/src/main/java/Freeze/PingObject.ice b/java/src/Freeze/src/main/java/Freeze/PingObject.ice deleted file mode 100644 index c8fa1744472..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/PingObject.ice +++ /dev/null @@ -1,19 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -#pragma once - -module Freeze -{ - -class PingObject -{ -}; - -}; diff --git a/java/src/Freeze/src/main/java/Freeze/PostCompletionCallback.java b/java/src/Freeze/src/main/java/Freeze/PostCompletionCallback.java deleted file mode 100644 index ed35281552d..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/PostCompletionCallback.java +++ /dev/null @@ -1,15 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -interface PostCompletionCallback -{ - void postCompletion(boolean committed, boolean deadlock, SharedDbEnv dbEnv); -} diff --git a/java/src/Freeze/src/main/java/Freeze/SharedDbEnv.java b/java/src/Freeze/src/main/java/Freeze/SharedDbEnv.java deleted file mode 100644 index 052207fa01c..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/SharedDbEnv.java +++ /dev/null @@ -1,579 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -public class SharedDbEnv implements com.sleepycat.db.ErrorHandler, Runnable -{ - public static SharedDbEnv - get(Ice.Communicator communicator, String envName, com.sleepycat.db.Environment dbEnv) - { - MapKey key = new MapKey(envName, communicator); - - SharedDbEnv result; - - synchronized(_map) - { - result = _map.get(key); - if(result == null) - { - result = new SharedDbEnv(key, dbEnv); - - SharedDbEnv previousValue = _map.put(key, result); - assert(previousValue == null); - } - else - { - result._refCount++; - } - } - - return result; - } - - // - // Returns a shared map Db; the caller should NOT close this Db. - // - public MapDb - getSharedMapDb(String dbName, String key, String value, java.util.Comparator<?> comparator, MapIndex[] indices, - boolean createDb) - { - if(dbName.equals(_catalog.dbName())) - { - _catalog.checkTypes(key, value); - return _catalog; - } - else if(dbName.equals(_catalogIndexList.dbName())) - { - _catalogIndexList.checkTypes(key, value); - return _catalogIndexList; - } - - synchronized(_sharedDbMap) - { - MapDb db = _sharedDbMap.get(dbName); - if(db == null) - { - ConnectionI insertConnection = (ConnectionI)Util.createConnection(_key.communicator, _key.envName); - - try - { - db = new MapDb(insertConnection, dbName, key, value, comparator, indices, createDb); - } - finally - { - insertConnection.close(); - } - - MapDb previousValue = _sharedDbMap.put(dbName, db); - assert(previousValue == null); - } - else - { - db.checkTypes(key, value); - db.connectIndices(indices); - } - return db; - } - } - - // - // Tell SharedDbEnv to close and remove this Shared Db from the map - // - public void - removeSharedMapDb(String dbName) - { - synchronized(_sharedDbMap) - { - MapDb db = _sharedDbMap.remove(dbName); - if(db != null) - { - db.close(); - } - } - } - - public String - getEnvName() - { - return _key.envName; - } - - public Ice.Communicator - getCommunicator() - { - return _key.communicator; - } - - public Ice.EncodingVersion - getEncoding() - { - return _encoding; - } - - public com.sleepycat.db.Environment - getEnv() - { - return _dbEnv; - } - - public void - close() - { - synchronized(_map) - { - if(--_refCount == 0) - { - // - // Remove from map - // - SharedDbEnv value = _map.remove(_key); - assert(value == this); - - // - // Cleanup with _map locked to prevent concurrent cleanup of the same dbEnv - // - cleanup(); - } - } - } - - @Override - public void - run() - { - for(;;) - { - synchronized(this) - { - while(!_done) - { - try - { - wait(_checkpointPeriod); - } - catch(InterruptedException ex) - { - continue; - } - break; - } - if(_done) - { - return; - } - } - - if(_trace >= 2) - { - _key.communicator.getLogger().trace("Freeze.DbEnv", "checkpointing environment \"" + _key.envName + - "\""); - } - - try - { - com.sleepycat.db.CheckpointConfig config = new com.sleepycat.db.CheckpointConfig(); - config.setKBytes(_kbyte); - _dbEnv.checkpoint(config); - } - catch(com.sleepycat.db.DatabaseException dx) - { - _key.communicator.getLogger().warning("checkpoint on DbEnv \"" + _key.envName + - "\" raised DbException: " + dx.getMessage()); - } - } - } - - @Override - public void - error(com.sleepycat.db.Environment env, String errorPrefix, String message) - { - _key.communicator.getLogger().error("Freeze database error in DbEnv \"" + _key.envName + "\": " + message); - } - - // - // EvictorContext factory/manager - // - - // - // Create an evictor context associated with the calling thread - // - synchronized TransactionalEvictorContext - createCurrent() - { - Thread t = Thread.currentThread(); - - TransactionalEvictorContext ctx = _ctxMap.get(t); - assert ctx == null; - - ctx = new TransactionalEvictorContext(this); - synchronized(_map) - { - _refCount++; // owned by the underlying ConnectionI - } - _ctxMap.put(t, ctx); - - return ctx; - } - - synchronized TransactionalEvictorContext - getCurrent() - { - Thread t = Thread.currentThread(); - return _ctxMap.get(t); - } - - synchronized void - setCurrentTransaction(Transaction tx) - { - TransactionI txi = (TransactionI)tx; - - if(txi != null) - { - if(txi.getConnectionI() == null || txi.getConnectionI().dbEnv() == null) - { - throw new DatabaseException(errorPrefix(_key.envName) + "invalid transaction"); - } - - // - // Check this tx refers to this DbEnv - // - if(txi.getConnectionI().dbEnv() != this) - { - throw new DatabaseException(errorPrefix(_key.envName) + - "the given transaction is bound to environment '" + - txi.getConnectionI().dbEnv()._key.envName + "'"); - } - } - - Thread t = Thread.currentThread(); - - if(txi != null) - { - TransactionalEvictorContext ctx = _ctxMap.get(t); - if(ctx == null || !tx.equals(ctx.transaction())) - { - ctx = new TransactionalEvictorContext(txi, getCommunicator()); - _ctxMap.put(t, ctx); - } - } - else - { - _ctxMap.put(t, null); - } - } - - private - SharedDbEnv(MapKey key, com.sleepycat.db.Environment dbEnv) - { - _key = key; - _dbEnv = dbEnv; - _ownDbEnv = (dbEnv == null); - - Ice.Properties properties = key.communicator.getProperties(); - _trace = properties.getPropertyAsInt("Freeze.Trace.DbEnv"); - - String propertyPrefix = "Freeze.DbEnv." + _key.envName; - String dbHome = properties.getPropertyWithDefault(propertyPrefix + ".DbHome", _key.envName); - - String encoding = properties.getPropertyWithDefault( - propertyPrefix + ".EncodingVersion", Ice.Util.encodingVersionToString(Ice.Util.currentEncoding())); - _encoding = Ice.Util.stringToEncodingVersion(encoding); - IceInternal.Protocol.checkSupportedEncoding(_encoding); - - java.io.File dir = new java.io.File(dbHome); - if(!dir.exists()) - { - throw new DatabaseException("DbHome directory `" + dbHome + "' does not exists"); - } - - // - // Normally the file lock is necessary, but for read-only situations (such as when - // using the FreezeScript utilities) this property allows the file lock to be - // disabled. - // - if(properties.getPropertyAsIntWithDefault(propertyPrefix + ".LockFile", 1) > 0) - { - // - // Use a file lock to prevent multiple processes from opening the same db env. We - // create the lock file in a sub-directory to ensure db_hotbackup won't try to copy - // the file when backing up the environment (this would fail on Windows where copying - // a locked file isn't possible). - // - dir = new java.io.File(dbHome + "/__Freeze"); - if(!dir.exists()) - { - if(!dir.mkdir()) - { - throw new DatabaseException("Failed to create directory `" + dbHome + "/__Freeze'"); - } - } - _fileLock = new IceUtilInternal.FileLock(dbHome + "/__Freeze/lock"); - } - - try - { - if(_ownDbEnv) - { - com.sleepycat.db.EnvironmentConfig config = new com.sleepycat.db.EnvironmentConfig(); - - config.setErrorHandler(this); - config.setInitializeLocking(true); - config.setInitializeLogging(true); - config.setInitializeCache(true); - config.setAllowCreate(true); - config.setTransactional(true); - - // - // Deadlock detection - // - config.setLockDetectMode(com.sleepycat.db.LockDetectMode.YOUNGEST); - - if(properties.getPropertyAsInt(propertyPrefix + ".DbRecoverFatal") > 0) - { - config.setRunFatalRecovery(true); - } - else - { - config.setRunRecovery(true); - } - - if(properties.getPropertyAsIntWithDefault(propertyPrefix + ".DbPrivate", 1) > 0) - { - config.setPrivate(true); - } - - if(properties.getPropertyAsIntWithDefault(propertyPrefix + ".OldLogsAutoDelete", 1) > 0) - { - config.setLogAutoRemove(true); - } - - if(_trace >= 1) - { - _key.communicator.getLogger().trace("Freeze.DbEnv", "opening database environment \"" + - _key.envName + "\""); - } - - try - { - java.io.File home = new java.io.File(dbHome); - _dbEnv = new com.sleepycat.db.Environment(home, config); - } - catch(java.io.FileNotFoundException dx) - { - throw new NotFoundException(errorPrefix(_key.envName) + "open: " + dx.getMessage(), dx); - } - - // - // Default checkpoint period is every 120 seconds - // - _checkpointPeriod = - properties.getPropertyAsIntWithDefault(propertyPrefix + ".CheckpointPeriod", 120) * 1000; - - _kbyte = properties.getPropertyAsIntWithDefault(propertyPrefix + ".PeriodicCheckpointMinSize", 0); - - String threadName; - String programName = properties.getProperty("Ice.ProgramName"); - if(programName.length() > 0) - { - threadName = programName + "-"; - } - else - { - threadName = ""; - } - threadName += "FreezeCheckpointThread(" + _key.envName + ")"; - - if(_checkpointPeriod > 0) - { - _thread = new Thread(this, threadName); - _thread.start(); - } - } - - _catalog = new MapDb(_key.communicator, _key.envName, Util.catalogName(), "string", - "::Freeze::CatalogData", _dbEnv); - _catalogIndexList = new MapDb(_key.communicator, _key.envName, Util.catalogIndexListName(), - "string", "::Ice::StringSeq", _dbEnv); - } - catch(com.sleepycat.db.DatabaseException dx) - { - cleanup(); - throw new DatabaseException(errorPrefix(_key.envName) + "creation: " + dx.getMessage(), dx); - } - catch(java.lang.RuntimeException ex) - { - cleanup(); - throw ex; - } - catch(java.lang.Error ex) - { - cleanup(); - throw ex; - } - - _refCount = 1; - } - - private void - cleanup() - { - // - // Join thread - // - synchronized(this) - { - _done = true; - notify(); - } - - while(_thread != null) - { - try - { - _thread.join(); - _thread = null; - break; - } - catch(InterruptedException ex) - { - } - } - - // - // Release catalogs - // - if(_catalog != null) - { - try - { - _catalog.close(); - } - finally - { - _catalog = null; - } - } - - if(_catalogIndexList != null) - { - try - { - _catalogIndexList.close(); - } - finally - { - _catalogIndexList = null; - } - } - - // - // Close Dbs - // - for(MapDb db: _sharedDbMap.values()) - { - db.close(); - } - - if(_trace >= 1) - { - _key.communicator.getLogger().trace("Freeze.DbEnv", "closing database environment \"" + - _key.envName + "\""); - } - - if(_ownDbEnv && _dbEnv != null) - { - try - { - _dbEnv.close(); - } - catch(com.sleepycat.db.DatabaseException dx) - { - throw new DatabaseException(errorPrefix(_key.envName) + "close: " + dx.getMessage(), dx); - } - finally - { - _dbEnv = null; - } - } - - // - // Release the file lock - // - if(_fileLock != null) - { - _fileLock.release(); - } - } - - private static String - errorPrefix(String envName) - { - return "DbEnv(\"" + envName + "\"): "; - } - - private static class MapKey - { - final String envName; - final Ice.Communicator communicator; - - MapKey(String envName, Ice.Communicator communicator) - { - this.envName = envName; - this.communicator = communicator; - } - - @Override - public boolean - equals(Object o) - { - try - { - MapKey k = (MapKey)o; - return (communicator == k.communicator) && envName.equals(k.envName); - } - catch(ClassCastException ex) - { - communicator.getLogger().trace("Freeze.DbEnv", "equals cast failed"); - return false; - } - } - - @Override - public int - hashCode() - { - int h = 5381; - h = IceInternal.HashUtil.hashAdd(h, envName); - return IceInternal.HashUtil.hashAdd(h, communicator); - } - } - - private MapKey _key; - private com.sleepycat.db.Environment _dbEnv; - private boolean _ownDbEnv; - private MapDb _catalog; - private MapDb _catalogIndexList; - private int _refCount = 0; // protected by _map! - private boolean _done = false; - private int _trace = 0; - private long _checkpointPeriod = 0; - private int _kbyte = 0; - private Thread _thread; - private Ice.EncodingVersion _encoding; - - private java.util.Map<Thread, TransactionalEvictorContext> _ctxMap = - new java.util.HashMap<Thread, TransactionalEvictorContext>(); - - private java.util.Map<String, MapDb> _sharedDbMap = new java.util.HashMap<String, MapDb>(); - - private static java.util.Map<MapKey, SharedDbEnv> _map = new java.util.HashMap<MapKey, SharedDbEnv>(); - - private IceUtilInternal.FileLock _fileLock; -} diff --git a/java/src/Freeze/src/main/java/Freeze/TransactionI.java b/java/src/Freeze/src/main/java/Freeze/TransactionI.java deleted file mode 100644 index 3436e4892c1..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/TransactionI.java +++ /dev/null @@ -1,235 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -class TransactionI implements Transaction -{ - @Override - public void - commit() - { - if(_txn == null) - { - throw new DatabaseException(_errorPrefix + "inactive transaction"); - } - - String txnId = null; - boolean committed = false; - boolean deadlock = false; - - try - { - _connection.closeAllIterators(); - - if(_txTrace >= 1) - { - txnId = Long.toHexString((_txn.getId() & 0x7FFFFFFF) + 0x80000000L); - } - - _txn.commit(); - committed = true; - - if(_txTrace >= 1) - { - _connection.communicator().getLogger().trace("Freeze.Transaction", _errorPrefix + - "committed transaction " + txnId); - } - } - catch(com.sleepycat.db.DeadlockException e) - { - deadlock = true; - - if(_txTrace >= 1) - { - _connection.communicator().getLogger().trace("Freeze.Transaction", _errorPrefix + - "failed to commit transaction " + txnId + ": " + - e.getMessage()); - } - - throw new DeadlockException(_errorPrefix + "DbTxn.commit: " + e.getMessage(), this, e); - } - catch(com.sleepycat.db.DatabaseException e) - { - if(_txTrace >= 1) - { - _connection.communicator().getLogger().trace("Freeze.Transaction", _errorPrefix + - "failed to commit transaction " + txnId + ": " + - e.getMessage()); - } - - throw new DatabaseException(_errorPrefix + "DbTxn.commit: " + e.getMessage(), e); - } - finally - { - _connection.clearTransaction(); - SharedDbEnv dbEnv = _connection.dbEnv(); - if(_ownConnection) - { - _connection.close(); - } - _connection = null; - _txn = null; - postCompletion(committed, deadlock, dbEnv); - } - } - - @Override - public void - rollback() - { - if(_txn == null) - { - throw new DatabaseException(_errorPrefix + "inactive transaction"); - } - - String txnId = null; - boolean deadlock = false; - - try - { - _connection.closeAllIterators(); - - if(_txTrace >= 1) - { - txnId = Long.toHexString((_txn.getId() & 0x7FFFFFFF) + 0x80000000L); - } - - _txn.abort(); - - if(_txTrace >= 1) - { - _connection.communicator().getLogger().trace("Freeze.Transaction", _errorPrefix + - "rolled back transaction " + txnId); - } - } - catch(com.sleepycat.db.DeadlockException e) - { - if(_txTrace >= 1) - { - _connection.communicator().getLogger().trace("Freeze.Transaction", _errorPrefix + - "failed to rollback transaction " + txnId + ": " + - e.getMessage()); - } - - deadlock = true; - - throw new DeadlockException(_errorPrefix + "DbTxn.abort: " + e.getMessage(), this, e); - } - catch(com.sleepycat.db.DatabaseException e) - { - if(_txTrace >= 1) - { - _connection.communicator().getLogger().trace("Freeze.Transaction", _errorPrefix + - "failed to rollback transaction " + txnId + ": " + - e.getMessage()); - } - - throw new DatabaseException(_errorPrefix + "DbTxn.abort: " + e.getMessage(), e); - } - finally - { - _connection.clearTransaction(); - SharedDbEnv dbEnv = _connection.dbEnv(); - if(_ownConnection) - { - _connection.close(); - } - _connection = null; - _txn = null; - postCompletion(false, deadlock, dbEnv); - } - } - - @Override - public Connection - getConnection() - { - return _connection; - } - - TransactionI(ConnectionI connection) - { - _connection = connection; - _txTrace = connection.txTrace(); - _errorPrefix = "DbEnv(\"" + _connection.envName() + "\"): "; - - try - { - _txn = _connection.dbEnv().getEnv().beginTransaction(null, null); - - if(_txTrace >= 1) - { - String txnId = Long.toHexString((_txn.getId() & 0x7FFFFFFF) + 0x80000000L); - - _connection.communicator().getLogger().trace("Freeze.Transaction", _errorPrefix + - "started transaction " + txnId); - } - } - catch(com.sleepycat.db.DatabaseException e) - { - if(_txTrace >= 1) - { - _connection.communicator().getLogger().trace("Freeze.Transaction", _errorPrefix + - "failed to start transaction: " + e.getMessage()); - } - - throw new DatabaseException(_errorPrefix + "txn_begin: " + e.getMessage(), e); - } - } - - void - setPostCompletionCallback(PostCompletionCallback cb) - { - _postCompletionCallback = cb; - } - - void - adoptConnection() - { - _ownConnection = true; - } - - ConnectionI - getConnectionI() - { - return _connection; - } - - com.sleepycat.db.Transaction - dbTxn() - { - return _txn; - } - - private void - postCompletion(boolean committed, boolean deadlock, SharedDbEnv dbEnv) - { - if(_postCompletionCallback != null) - { - try - { - _postCompletionCallback.postCompletion(committed, deadlock, dbEnv); - } - finally - { - _postCompletionCallback = null; - } - } - } - - private ConnectionI _connection; - private int _txTrace; - private com.sleepycat.db.Transaction _txn; - - private PostCompletionCallback _postCompletionCallback; - - private String _errorPrefix; - private boolean _ownConnection = false; -} diff --git a/java/src/Freeze/src/main/java/Freeze/TransactionalEvictorContext.java b/java/src/Freeze/src/main/java/Freeze/TransactionalEvictorContext.java deleted file mode 100644 index f3e6282a4a6..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/TransactionalEvictorContext.java +++ /dev/null @@ -1,418 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -// -// A Freeze transactional evictor context holds a Berkeley DB transaction -// and a number of servants loaded using this transaction. -// - -class TransactionalEvictorContext implements Ice.DispatchInterceptorAsyncCallback, PostCompletionCallback -{ - @Override - public void - postCompletion(boolean committed, boolean deadlock, SharedDbEnv dbEnv) - { - // - // Clear the current context - // - dbEnv.setCurrentTransaction(null); - - try - { - if(committed) - { - // - // Remove updated & removed objects from cache - // - for(ToInvalidate ti : _invalidateList) - { - ti.invalidate(); - } - _invalidateList.clear(); - } - } - finally - { - synchronized(this) - { - if(_tx != null) - { - if(deadlock) - { - _deadlockExceptionDetected = true; - } - _tx = null; - notifyAll(); - } - } - } - } - - @Override - public boolean - response(boolean ok) - { - if(Thread.currentThread().equals(_owner)) - { - if(!ok) - { - _userExceptionDetected = true; - } - - return true; - } - else - { - synchronized(this) - { - if(_deadlockExceptionDetected) - { - return false; - } - if(_tx == null) - { - return true; - } - try - { - wait(); - } - catch(InterruptedException ex) - { - } - return !_deadlockExceptionDetected; - } - } - } - - @Override - public boolean - exception(java.lang.Exception ex) - { - if(ex instanceof DeadlockException && Thread.currentThread().equals(_owner)) - { - _deadlockException = (DeadlockException)ex; - return false; - } - - if(ex instanceof TransactionalEvictorDeadlockException && Thread.currentThread().equals(_owner)) - { - _nestedCallDeadlockException = (TransactionalEvictorDeadlockException)ex; - return false; - } - - return true; - } - - class ServantHolder - { - ServantHolder(Ice.Current current, ObjectStore store) - { - _current = current; - _store = store; - - ServantHolder sh = findServantHolder(_current.id, _store); - if(sh != null) - { - if(!sh._removed) - { - _rec = sh._rec; - _readOnly = sh._readOnly; - - if(_trace >= 3) - { - _communicator.getLogger().trace("Freeze.Evictor", "found \"" + - _communicator.identityToString(_current.id) + - "\" with facet \"" + _store.facet() + "\" in current context"); - } - } - } - else - { - // - // Let's load this servant - // - _rec = store.load(current.id, _tx); - if(_rec != null) - { - if(_trace >= 3) - { - _communicator.getLogger().trace("Freeze.Evictor", "loaded \"" + - _communicator.identityToString(_current.id) + - "\" with facet \"" + _store.facet() + - "\" into current context"); - } - - _stack.push(this); - _ownServant = true; - } - } - } - - void - markReadWrite() - { - if(_ownServant) - { - _readOnly = false; - } - else - { - if(_readOnly) - { - throw new DatabaseException("freeze:write operation called from freeze:read operation"); - } - } - } - - void - release() - { - if(_ownServant) - { - if(_tx != null) - { - if(!_readOnly && !_removed) - { - if(_store.keepStats()) - { - EvictorI.updateStats(_rec.stats, IceInternal.Time.currentMonotonicTimeMillis()); - } - _store.update(_current.id, _rec, _tx); - - if(_trace >= 3) - { - _communicator.getLogger().trace("Freeze.Evictor", "updated \"" + - _communicator.identityToString(_current.id) + - "\" with facet \"" + _store.facet() + - "\" within transaction"); - } - } - - if(!_readOnly || _removed) - { - _invalidateList.add(new ToInvalidate(_current.id, _store)); - } - } - _stack.pop(); - } - } - - boolean - matches(Ice.Identity ident, ObjectStore store) - { - return ident.equals(_current.id) && store == _store; - } - - Ice.Object - servant() - { - if(_rec == null) - { - return null; - } - else - { - return _rec.servant; - } - } - - void - removed() - { - _removed = true; - } - - private boolean _ownServant = false; - private boolean _removed = false; - private boolean _readOnly = true; - - private final Ice.Current _current; - private final ObjectStore _store; - private ObjectRecord _rec; - } - - TransactionalEvictorContext(SharedDbEnv dbEnv) - { - _communicator = dbEnv.getCommunicator(); - _trace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Evictor"); - _tx = (TransactionI)(new ConnectionI(dbEnv).beginTransaction()); - _owner = Thread.currentThread(); - _tx.adoptConnection(); - - _tx.setPostCompletionCallback(this); - } - - TransactionalEvictorContext(TransactionI tx, Ice.Communicator communicator) - { - _communicator = communicator; - _trace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Evictor"); - _tx = tx; - _owner = Thread.currentThread(); - - _tx.setPostCompletionCallback(this); - } - - Ice.Object - findServant(Ice.Identity ident, ObjectStore store) - { - ServantHolder sh = findServantHolder(ident, store); - return sh != null ? sh.servant() : null; - } - - void - rollback() - { - if(_tx != null) - { - _tx.rollback(); - } - } - - void - commit() - { - if(_tx != null) - { - _tx.commit(); - } - } - - void - checkDeadlockException() - { - if(_deadlockException != null) - { - throw _deadlockException; - } - - if(_nestedCallDeadlockException != null) - { - throw _nestedCallDeadlockException; - } - } - - boolean - clearUserException() - { - // - // No need to synchronize; _userExceptionDetected is only read/written by the - // dispatch thread - // - boolean result = _userExceptionDetected; - _userExceptionDetected = false; - return result; - } - - TransactionI - transaction() - { - return _tx; - } - - ServantHolder - createServantHolder(Ice.Current current, ObjectStore store) - { - return new ServantHolder(current, store); - } - - void - deadlockException() - { - synchronized(this) - { - _deadlockExceptionDetected = true; - notifyAll(); - } - - rollback(); - } - - Ice.Object - servantRemoved(Ice.Identity ident, ObjectStore store) - { - if(_tx != null) - { - // - // Lookup servant holder on stack - // - ServantHolder sh = findServantHolder(ident, store); - if(sh != null) - { - sh.removed(); - return sh.servant(); - } - else - { - _invalidateList.add(new ToInvalidate(ident, store)); - return null; - } - } - return null; - } - - private ServantHolder - findServantHolder(Ice.Identity ident, ObjectStore store) - { - for(ServantHolder sh : _stack) - { - if(sh.matches(ident, store)) - { - return sh; - } - } - return null; - } - - private static class ToInvalidate - { - ToInvalidate(Ice.Identity ident, ObjectStore store) - { - _ident = ident; - _store = store; - } - - void - invalidate() - { - ((TransactionalEvictorI)_store.evictor()).evict(_ident, _store); - } - - private final Ice.Identity _ident; - private final ObjectStore _store; - } - - private final java.util.Stack<ServantHolder> _stack = new java.util.Stack<ServantHolder>(); - - // - // List of objects to invalidate from the caches upon commit - // - private final java.util.List<ToInvalidate> _invalidateList = new java.util.LinkedList<ToInvalidate>(); - - private TransactionI _tx; - private final Thread _owner; - - private DeadlockException _deadlockException; - private TransactionalEvictorDeadlockException _nestedCallDeadlockException; - - // - // Protected by this - // - private boolean _deadlockExceptionDetected = false; - - // - // Only updated by the dispatch thread - // - private boolean _userExceptionDetected = false; - - private final int _trace; - private final Ice.Communicator _communicator; -} diff --git a/java/src/Freeze/src/main/java/Freeze/TransactionalEvictorDeadlockException.java b/java/src/Freeze/src/main/java/Freeze/TransactionalEvictorDeadlockException.java deleted file mode 100644 index e22d2735c28..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/TransactionalEvictorDeadlockException.java +++ /dev/null @@ -1,27 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -public class TransactionalEvictorDeadlockException extends Ice.SystemException -{ - public TransactionalEvictorDeadlockException(Transaction transaction) - { - tx = transaction; - } - - @Override - public String - ice_name() - { - return "Freeze::TransactionalEvictorDeadlockException"; - } - - public Transaction tx; -} diff --git a/java/src/Freeze/src/main/java/Freeze/TransactionalEvictorI.java b/java/src/Freeze/src/main/java/Freeze/TransactionalEvictorI.java deleted file mode 100644 index c9a5729068f..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/TransactionalEvictorI.java +++ /dev/null @@ -1,854 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -class TransactionalEvictorI extends EvictorI implements TransactionalEvictor -{ - // - // Must be in sync with Parser.cpp - // - static final int supports = 0; - static final int mandatory = 1; - static final int required = 2; - static final int never = 3; - - @Override - public Transaction - getCurrentTransaction() - { - _deactivateController.lock(); - try - { - TransactionalEvictorContext ctx = _dbEnv.getCurrent(); - return ctx == null ? null : ctx.transaction(); - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public void - setCurrentTransaction(Transaction tx) - { - _deactivateController.lock(); - try - { - _dbEnv.setCurrentTransaction(tx); - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public Ice.ObjectPrx - addFacet(Ice.Object servant, Ice.Identity ident, String facet) - { - checkIdentity(ident); - checkServant(servant); - - if(facet == null) - { - facet = ""; - } - - _deactivateController.lock(); - try - { - ObjectStore store = findStore(facet, _createDb); - if(store == null) - { - NotFoundException ex = new NotFoundException(); - ex.message = _errorPrefix + "addFacet: could not open database for facet '" + facet + "'"; - throw ex; - } - - long currentTime = 0; - ObjectRecord rec; - - if(store.keepStats()) - { - currentTime = IceInternal.Time.currentMonotonicTimeMillis(); - rec = new ObjectRecord(servant, new Statistics(currentTime, 0, 0)); - } - else - { - rec = new ObjectRecord(servant, null); - } - - TransactionI tx = beforeQuery(); - - if(store.keepStats()) - { - updateStats(rec.stats, currentTime); - } - - if(!store.insert(ident, rec, tx)) - { - Ice.AlreadyRegisteredException ex = new Ice.AlreadyRegisteredException(); - ex.kindOfObject = "servant"; - ex.id = _communicator.identityToString(ident); - if(facet.length() > 0) - { - ex.id += " -f " + IceUtilInternal.StringUtil.escapeString(facet, ""); - } - throw ex; - } - - if(_trace >= 1) - { - String objString = "object \"" + _communicator.identityToString(ident) + "\""; - if(!facet.equals("")) - { - objString += " with facet \"" + facet + "\""; - } - - _communicator.getLogger().trace("Freeze.Evictor", "added " + objString + " to Db \"" + _filename + - "\""); - } - - Ice.ObjectPrx obj = _adapter.createProxy(ident); - if(facet.length() > 0) - { - obj = obj.ice_facet(facet); - } - return obj; - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public Ice.Object - removeFacet(Ice.Identity ident, String facet) - { - checkIdentity(ident); - if(facet == null) - { - facet = ""; - } - - _deactivateController.lock(); - try - { - Ice.Object servant = null; - boolean removed = false; - - ObjectStore store = findStore(facet, false); - if(store != null) - { - TransactionalEvictorContext ctx = _dbEnv.getCurrent(); - TransactionI tx = null; - if(ctx != null) - { - tx = ctx.transaction(); - if(tx == null) - { - throw new DatabaseException(_errorPrefix + "inactive transaction"); - } - } - - removed = store.remove(ident, tx); - - if(removed) - { - if(ctx != null) - { - // - // Remove from cache when transaction commits - // - servant = ctx.servantRemoved(ident, store); - } - else - { - // - // Remove from cache immediately - // - servant = evict(ident, store); - } - } - } - - if(!removed) - { - Ice.NotRegisteredException ex = new Ice.NotRegisteredException(); - ex.kindOfObject = "servant"; - ex.id = _communicator.identityToString(ident); - if(facet.length() > 0) - { - ex.id += " -f " + IceUtilInternal.StringUtil.escapeString(facet, ""); - } - throw ex; - } - - if(_trace >= 1) - { - String objString = "object \"" + _communicator.identityToString(ident) + "\""; - if(!facet.equals("")) - { - objString += " with facet \"" + facet + "\""; - } - - _communicator.getLogger().trace("Freeze.Evictor", "removed " + objString + " from Db \"" + _filename + - "\""); - } - return servant; - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public boolean - hasFacet(Ice.Identity ident, String facet) - { - checkIdentity(ident); - if(facet == null) - { - facet = ""; - } - - _deactivateController.lock(); - try - { - ObjectStore store = findStore(facet, false); - - if(store == null) - { - return false; - } - - TransactionI tx = beforeQuery(); - - if(tx == null) - { - EvictorElement element = (EvictorElement)store.cache().getIfPinned(ident); - if(element != null) - { - return true; - } - - return store.dbHasObject(ident, null); - } - else - { - return store.dbHasObject(ident, tx); - } - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - public void - finished(Ice.Current current, Ice.Object servant, java.lang.Object cookieObj) - { - // - // Nothing to do - // - } - - @Override - public void - deactivate(String category) - { - if(_deactivateController.deactivate()) - { - synchronized(this) - { - // - // Set the evictor size to zero, meaning that we will evict - // everything possible. - // - _evictorSize = 0; - evict(); - } - - try - { - closeDbEnv(); - } - finally - { - _deactivateController.deactivationComplete(); - } - } - } - - TransactionalEvictorI(Ice.ObjectAdapter adapter, String envName, com.sleepycat.db.Environment dbEnv, - String filename, java.util.Map<String, String> facetTypes, ServantInitializer initializer, - Index[] indices, boolean createDb) - { - super(adapter, envName, dbEnv, filename, facetTypes, initializer, indices, createDb); - - String propertyPrefix = "Freeze.Evictor." + envName + '.' + filename; - _rollbackOnUserException = _communicator.getProperties(). - getPropertyAsIntWithDefault(propertyPrefix + ".RollbackOnUserException", 0) > 0; - } - - TransactionalEvictorI(Ice.ObjectAdapter adapter, String envName, String filename, - java.util.Map<String, String> facetTypes, ServantInitializer initializer, Index[] indices, - boolean createDb) - { - this(adapter, envName, null, filename, facetTypes, initializer, indices, createDb); - } - - // - // The interceptor dispatch call - // - Ice.DispatchStatus - dispatch(Ice.Request request) - { - _deactivateController.lock(); - try - { - Ice.Current current = request.getCurrent(); - - ObjectStore store = findStore(current.facet, false); - if(store == null) - { - servantNotFound(current); - } - - TransactionalEvictorContext ctx = _dbEnv.getCurrent(); - - Ice.Object sample = store.sampleServant(); - Ice.Object cachedServant = null; - - TransactionalEvictorContext.ServantHolder servantHolder = null; - try - { - if(sample == null) - { - if(ctx != null) - { - try - { - servantHolder = ctx.createServantHolder(current, store); - } - catch(DeadlockException dx) - { - assert dx.tx == ctx.transaction(); - ctx.deadlockException(); - throw new TransactionalEvictorDeadlockException(dx.tx); - } - sample = servantHolder.servant(); - } - else - { - // - // find / load read-only servant - // - - cachedServant = loadCachedServant(current.id, store); - - if(cachedServant == null) - { - servantNotFound(current); - } - sample = cachedServant; - } - } - - assert sample != null; - - int operationAttributes = sample.ice_operationAttributes(current.operation); - - if(operationAttributes < 0) - { - throw new Ice.OperationNotExistException(); - } - - boolean readOnly = (operationAttributes & 0x1) == 0; - - int txMode = (operationAttributes & 0x6) >> 1; - - boolean ownCtx = false; - - // - // Establish the proper context - // - switch(txMode) - { - case never: - { - assert readOnly; - if(ctx != null) - { - throw new DatabaseException(_errorPrefix + "transaction rejected by 'never' metadata"); - } - break; - } - case supports: - { - assert readOnly; - break; - } - case mandatory: - { - if(ctx == null) - { - throw new DatabaseException(_errorPrefix + "operation with a mandatory transaction"); - } - break; - } - case required: - { - if(ctx == null) - { - ownCtx = true; - } - break; - } - default: - { - assert false; - throw new Ice.OperationNotExistException(); - } - } - - if(ctx == null && !ownCtx) - { - // - // Read-only dispatch - // - assert readOnly; - if(cachedServant == null) - { - cachedServant = loadCachedServant(current.id, store); - - if(cachedServant == null) - { - servantNotFound(current); - } - } - return cachedServant.ice_dispatch(request, null); - } - else - { - boolean tryAgain = false; - do - { - Transaction tx = null; - - if(ownCtx) - { - ctx = _dbEnv.createCurrent(); - } - - tx = ctx.transaction(); - - try - { - try - { - TransactionalEvictorContext.ServantHolder sh; - if(servantHolder != null) - { - assert !ownCtx; - // - // Adopt it - // - sh = servantHolder; - servantHolder = null; - } - else - { - sh = ctx.createServantHolder(current, store); - } - - if(sh.servant() == null) - { - servantNotFound(current); - } - - if(!readOnly) - { - sh.markReadWrite(); - } - - try - { - Ice.DispatchStatus dispatchStatus = sh.servant().ice_dispatch(request, ctx); - if(dispatchStatus == Ice.DispatchStatus.DispatchUserException && - _rollbackOnUserException) - { - ctx.rollback(); - } - if(dispatchStatus == Ice.DispatchStatus.DispatchAsync) - { - // - // Can throw DeadlockException or TransactionalEvictorDeadlockException - // - ctx.checkDeadlockException(); - - if(ctx.clearUserException() && _rollbackOnUserException) - { - ctx.rollback(); - } - } - return dispatchStatus; - } - catch(RuntimeException ex) - { - ctx.rollback(); - throw ex; - } - finally - { - sh.release(); - if(ownCtx) - { - ctx.commit(); - } - } - } - catch(DeadlockException dx) - { - if(dx.tx == tx) - { - ctx.deadlockException(); - } - throw dx; - } - finally - { - if(ownCtx) - { - ctx.rollback(); - } - } - } - catch(DeadlockException dx) - { - if(ownCtx && dx.tx == tx) - { - tryAgain = true; - } - else - { - throw new TransactionalEvictorDeadlockException(dx.tx); - } - } - catch(TransactionalEvictorDeadlockException dx) - { - if(ownCtx && dx.tx == tx) - { - tryAgain = true; - } - else - { - throw dx; - } - } - finally - { - if(ownCtx) - { - // - // The commit or rollback above must have cleared it - // - assert(_dbEnv.getCurrent() == null); - } - } - } while(tryAgain); - } - } - finally - { - if(servantHolder != null) - { - // - // Won't raise anything since it's read-only - // - servantHolder.release(); - } - } - - // - // Javac does not detect this can't be reached - // - assert(false); - throw new Ice.OperationNotExistException(); - } - finally - { - _deactivateController.unlock(); - } - } - - synchronized Ice.Object - evict(Ice.Identity ident, ObjectStore store) - { - EvictorElement element = (EvictorElement)store.cache().unpin(ident); - - if(element != null) - { - element.evict(false); - return element.servant; - } - return null; - } - - @Override - protected Object - createEvictorElement(Ice.Identity ident, ObjectRecord rec, ObjectStore store) - { - return new EvictorElement(rec.servant, ident, store); - } - - @Override - protected Ice.Object - locateImpl(Ice.Current current, Ice.LocalObjectHolder cookie) - { - return _interceptor; - } - - @Override - protected boolean - hasAnotherFacet(Ice.Identity ident, String facet) - { - _deactivateController.lock(); - try - { - java.util.Map<String, ObjectStore> storeMapCopy; - synchronized(this) - { - storeMapCopy = new java.util.HashMap<String, ObjectStore>(_storeMap); - } - - TransactionI tx = beforeQuery(); - - for(java.util.Map.Entry<String, ObjectStore> entry : storeMapCopy.entrySet()) - { - // - // Do not check this facet again - // - if(!facet.equals(entry.getKey())) - { - ObjectStore store = entry.getValue(); - - if(tx == null && store.cache().getIfPinned(ident) != null) - { - return true; - } - - if(store.dbHasObject(ident, tx)) - { - return true; - } - } - } - - return false; - } - finally - { - _deactivateController.unlock(); - } - } - - @Override - protected void - evict() - { - assert Thread.holdsLock(this); - - while(_currentEvictorSize > _evictorSize) - { - // - // Evict, no matter what! - // - EvictorElement element = _evictorList.getLast(); - element.evict(true); - } - } - - @Override - protected TransactionI - beforeQuery() - { - TransactionalEvictorContext ctx = _dbEnv.getCurrent(); - TransactionI tx = null; - if(ctx != null) - { - tx = ctx.transaction(); - if(tx == null) - { - throw new DatabaseException(_errorPrefix + "inactive transaction"); - } - } - - return tx; - } - - private void - servantNotFound(Ice.Current current) - { - if(_trace >= 2) - { - _communicator.getLogger().trace("Freeze.Evictor", "could not find \"" + - _communicator.identityToString(current.id) + "\" with facet \"" + - current.facet + "\""); - } - - if(hasAnotherFacet(current.id, current.facet)) - { - throw new Ice.FacetNotExistException(current.id, current.facet, current.operation); - } - else - { - throw new Ice.ObjectNotExistException(current.id, current.facet, current.operation); - } - } - - private Ice.Object - loadCachedServant(Ice.Identity ident, ObjectStore store) - { - for(;;) - { - EvictorElement element = (EvictorElement)store.cache().pin(ident); - - if(element == null) - { - return null; - } - - synchronized(this) - { - if(element.stale) - { - // - // try again - // - continue; - } - - element.fixEvictPosition(); - - // - // if _evictorSize is 0, I may evict myself ... no big deal - // - evict(); - - if(_trace >= 3) - { - _communicator.getLogger().trace("Freeze.Evictor", "loaded \"" - + _communicator.identityToString(ident) + "\" with facet \"" + - store.facet() + "\" into the cache"); - } - return element.servant; - } - } - } - - private class EvictorElement - { - EvictorElement(Ice.Object servant, Ice.Identity identity, ObjectStore store) - { - this.servant = servant; - _identity = identity; - _store = store; - } - - void - evict(boolean unpin) - { - assert Thread.holdsLock(TransactionalEvictorI.this); - assert stale == false; - stale = true; - - if(unpin) - { - _store.cache().unpin(_identity); - } - - if(_evictPosition != null) - { - _evictPosition.remove(); - _evictPosition = null; - _currentEvictorSize--; - } - else - { - assert(!unpin); - } - } - - void - fixEvictPosition() - { - assert Thread.holdsLock(TransactionalEvictorI.this); - assert stale == false; - - if(_evictPosition == null) - { - // - // New element - // - _currentEvictorSize++; - } - else - { - _evictPosition.remove(); - } - _evictorList.addFirst(this); - _evictPosition = _evictorList.iterator(); - // - // Position the iterator "on" the element. - // - _evictPosition.next(); - } - - final Ice.Object servant; - - // - // Protected by the TransactionEvictorI mutex - // - boolean stale = false; // stale = true means no longer in the cache - private java.util.Iterator<EvictorElement> _evictPosition; - - // - // These two fields are only needed for eviction - // - final private ObjectStore _store; - final private Ice.Identity _identity; - } - - // - // List of EvictorElement with stable iterators - // - private final Freeze.LinkedList<EvictorElement> _evictorList = new Freeze.LinkedList<EvictorElement>(); - private int _currentEvictorSize = 0; - - // - // A simple adapter - // - private Ice.DispatchInterceptor _interceptor = new Ice.DispatchInterceptor() - { - @Override - public Ice.DispatchStatus - dispatch(Ice.Request request) - { - return TransactionalEvictorI.this.dispatch(request); - } - }; - - private boolean _rollbackOnUserException; -} diff --git a/java/src/Freeze/src/main/java/Freeze/Util.java b/java/src/Freeze/src/main/java/Freeze/Util.java deleted file mode 100644 index a6838743163..00000000000 --- a/java/src/Freeze/src/main/java/Freeze/Util.java +++ /dev/null @@ -1,236 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -package Freeze; - -public class Util -{ - /** - * Returns a new background save evictor. The Berkeley DB database with the - * name given in <code>filename</code> is opened if it already exists. - * If the database does not exist yet and <code>createDb</code> - * is true, the database is created automatically; otherwise, - * <code>DatabaseException</code> is raised. - * - * @param adapter The object adapter to associate with this evictor. - * @param envName The name of the Berkeley DB database environment. The - * environment must already exist. - * @param filename The name of the Berkeley DB database file. - * @param initializer An object that is used to initialize a servant after its - * state is restored from the database, or <code>null</code> if no initializer - * is necessary. - * @param indices An array of Freeze indices, or <code>null</code if no indices - * are defined. - * @param createDb True if the Berkeley DB database should be created if it does - * not exist, false otherwise. - * @return A new background save evictor. - * @throws Freeze.DatabaseException If an error occurs during database operations. - */ - public static BackgroundSaveEvictor - createBackgroundSaveEvictor(Ice.ObjectAdapter adapter, String envName, String filename, - ServantInitializer initializer, Index[] indices, boolean createDb) - { - return new BackgroundSaveEvictorI(adapter, envName, filename, initializer, indices, createDb); - } - - /** - * Returns a new background save evictor. The Berkeley DB database with the - * name given in <code>filename</code> is opened if it already exists. - * If the database does not exist yet and <code>createDb</code> - * is true, the database is created automatically; otherwise, - * <code>DatabaseException</code> is raised. - * - * @param adapter The object adapter to associate with this evictor. - * @param envName The name of the Berkeley DB database environment. The - * environment must already exist. - * @param dbEnv The database environment in which to open the database. - * @param filename The name of the Berkeley DB database file. - * @param initializer An object that is used to initialize a servant after its - * state is restored from the database, or <code>null</code> if no initializer - * is necessary. - * @param indices An array of Freeze indices, or <code>null</code if no indices - * are defined. - * @param createDb True if the Berkeley DB database should be created if it does - * not exist, false otherwise. - * @return A new background save evictor. - * @throws Freeze.DatabaseException If an error occurs during database operations. - */ - public static BackgroundSaveEvictor - createBackgroundSaveEvictor(Ice.ObjectAdapter adapter, String envName, com.sleepycat.db.Environment dbEnv, - String filename, ServantInitializer initializer, Index[] indices, boolean createDb) - { - return new BackgroundSaveEvictorI(adapter, envName, dbEnv, filename, initializer, indices, createDb); - } - - /** - * Returns a new transactional evictor. The Berkeley DB database with the - * name given in <code>filename</code> is opened if it already exists. - * If the database does not exist yet and <code>createDb</code> - * is true, the database is created automatically; otherwise, - * <code>DatabaseException</code> is raised. - * - * @param adapter The object adapter to associate with this evictor. - * @param envName The name of the Berkeley DB database environment. The - * environment must already exist. - * @param filename The name of the Berkeley DB database file. - * @param facetTypes A map of facet name to Slice type id, or <code>null</code if - * no map is necessary. - * @param initializer An object that is used to initialize a servant after its - * state is restored from the database, or <code>null</code> if no initializer - * is necessary. - * @param indices An array of Freeze indices, or <code>null</code if no indices - * are defined. - * @param createDb True if the Berkeley DB database should be created if it does - * not exist, false otherwise. - * @return A new transactional evictor. - * @throws Freeze.DatabaseException If an error occurs during database operations. - */ - public static TransactionalEvictor - createTransactionalEvictor(Ice.ObjectAdapter adapter, String envName, String filename, - java.util.Map<String, String> facetTypes, ServantInitializer initializer, - Index[] indices, boolean createDb) - { - return new TransactionalEvictorI(adapter, envName, filename, facetTypes, initializer, indices, createDb); - } - - /** - * Returns a new transactional evictor. The Berkeley DB database with the - * name given in <code>filename</code> is opened if it already exists. - * If the database does not exist yet and <code>createDb</code> - * is true, the database is created automatically; otherwise, - * <code>DatabaseException</code> is raised. - * - * @param adapter The object adapter to associate with this evictor. - * @param envName The name of the Berkeley DB database environment. The - * environment must already exist. - * @param dbEnv The database environment in which to open the database. - * @param filename The name of the Berkeley DB database file. - * @param facetTypes A map of facet name to Slice type id, or <code>null</code if - * no map is necessary. - * @param initializer An object that is used to initialize a servant after its - * state is restored from the database, or <code>null</code> if no initializer - * is necessary. - * @param indices An array of Freeze indices, or <code>null</code if no indices - * are defined. - * @param createDb True if the Berkeley DB database should be created if it does - * not exist, false otherwise. - * @return A new transactional evictor. - * @throws Freeze.DatabaseException If an error occurs during database operations. - */ - public static TransactionalEvictor - createTransactionalEvictor(Ice.ObjectAdapter adapter, String envName, com.sleepycat.db.Environment dbEnv, - String filename, java.util.Map<String, String> facetTypes, - ServantInitializer initializer, Index[] indices, boolean createDb) - { - return new TransactionalEvictorI(adapter, envName, dbEnv, filename, facetTypes, initializer, indices, createDb); - } - - /** - * Creates a new Freeze connection using the given communicator and database - * environment. - * - * @param communicator The communicator associated with this connection. - * @param envName The name of the Berkeley DB database environment. - * @return A new Freeze connection. - * @throws Freeze.DatabaseException If an error occurs during database operations. - */ - public static Connection - createConnection(Ice.Communicator communicator, String envName) - { - return new ConnectionI(communicator, envName, null); - } - - /** - * Creates a new Freeze connection using the given communicator and database - * environment. - * - * @param communicator The communicator associated with this connection. - * @param envName The name of the Berkeley DB database environment. - * @param dbEnv The database environment associated with this connection. - * @return A new Freeze connection. - * @throws Freeze.DatabaseException If an error occurs during database operations. - */ - public static Connection - createConnection(Ice.Communicator communicator, String envName, com.sleepycat.db.Environment dbEnv) - { - return new ConnectionI(communicator, envName, dbEnv); - } - - /** - * Returns the database file name of the Freeze catalog. - * - * @return The database file name. - */ - public static String - catalogName() - { - return _catalogName; - } - - /** - * Returns the database file name of the Freeze catalog index list. - * - * @return The database file name. - */ - public static String - catalogIndexListName() - { - return _catalogIndexListName; - } - - /** - * Returns the Berkeley DB transaction object associated with a Freeze transaction. - * - * @param tx The Freeze transaction. - * @return The Berkeley DB transaction. - */ - public static com.sleepycat.db.Transaction - getTxn(Transaction tx) - { - try - { - return ((TransactionI)tx).dbTxn(); - } - catch(ClassCastException e) - { - return null; - } - } - - /** - * Registers a handler for fatal errors encountered by the background save evictor. - * - * @param cb The error callback. - */ - public static synchronized FatalErrorCallback - registerFatalErrorCallback(FatalErrorCallback cb) - { - FatalErrorCallback result = _fatalErrorCallback; - _fatalErrorCallback = cb; - return result; - } - - static synchronized void - handleFatalError(BackgroundSaveEvictor evictor, Ice.Communicator communicator, RuntimeException ex) - { - if(_fatalErrorCallback != null) - { - _fatalErrorCallback.handleError(evictor, communicator, ex); - } - else - { - communicator.getLogger().error("*** Halting JVM ***"); - Runtime.getRuntime().halt(1); - } - } - - private static String _catalogName = "__catalog"; - private static String _catalogIndexListName = "__catalogIndexList"; - private static FatalErrorCallback _fatalErrorCallback = null; -} |