diff options
author | Mark Spruiell <mes@zeroc.com> | 2014-08-07 16:08:30 -0700 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2014-08-07 16:08:30 -0700 |
commit | 9c4b185a6c83418caa5296f07ec1f08e4964867e (patch) | |
tree | dee966c65c144f20e02583110fac6569fe7680bc | |
parent | fix hello demo configuration unintended change (diff) | |
download | ice-9c4b185a6c83418caa5296f07ec1f08e4964867e.tar.bz2 ice-9c4b185a6c83418caa5296f07ec1f08e4964867e.tar.xz ice-9c4b185a6c83418caa5296f07ec1f08e4964867e.zip |
ICE-5311 - use ByteBuffer in Freeze maps
-rw-r--r-- | cpp/src/slice2freezej/Main.cpp | 49 | ||||
-rw-r--r-- | java/src/Freeze/Index.java | 15 | ||||
-rw-r--r-- | java/src/Freeze/MapInternal/EntryI.java | 18 | ||||
-rw-r--r-- | java/src/Freeze/MapInternal/Index.java | 107 | ||||
-rw-r--r-- | java/src/Freeze/MapInternal/IndexedSubMap.java | 39 | ||||
-rw-r--r-- | java/src/Freeze/MapInternal/IteratorI.java | 6 | ||||
-rw-r--r-- | java/src/Freeze/MapInternal/KeyCodec.java | 4 | ||||
-rw-r--r-- | java/src/Freeze/MapInternal/MapI.java | 231 | ||||
-rw-r--r-- | java/src/Freeze/MapInternal/Search.java | 15 | ||||
-rw-r--r-- | java/src/Freeze/MapInternal/SubMap.java | 31 | ||||
-rw-r--r-- | java/src/Freeze/MapInternal/UtilI.java | 35 |
11 files changed, 344 insertions, 206 deletions
diff --git a/cpp/src/slice2freezej/Main.cpp b/cpp/src/slice2freezej/Main.cpp index 07f2d0eecee..a81cd47c0f2 100644 --- a/cpp/src/slice2freezej/Main.cpp +++ b/cpp/src/slice2freezej/Main.cpp @@ -898,11 +898,9 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) // // encode // - out << sp << nl << "public byte[]" << nl << "encode" << keyValue << "(" << typeS - << " v, Ice.Communicator communicator, Ice.EncodingVersion encoding)"; + out << sp << nl << "public void" << nl << "encode" << keyValue << "(" << typeS + << " v, IceInternal.BasicStream __os)"; out << sb; - out << nl << "IceInternal.BasicStream __os = " - << "new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, true, false);"; if(encaps) { out << nl << "__os.startWriteEncaps();"; @@ -917,20 +915,13 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) { out << nl << "__os.endWriteEncaps();"; } - out << nl << "IceInternal.Buffer __buf = __os.prepareWrite();"; - out << nl << "byte[] __r = new byte[__buf.size()];"; - out << nl << "__buf.b.get(__r);"; - out << nl << "return __r;"; out << eb; // // decode // - out << sp << nl << "public " << typeS << nl << "decode" << keyValue - << "(byte[] b, Ice.Communicator communicator, Ice.EncodingVersion encoding)"; + out << sp << nl << "public " << typeS << nl << "decode" << keyValue << "(IceInternal.BasicStream __is)"; out << sb; - out << nl << "IceInternal.BasicStream __is = " - << "new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, b);"; if(type->usesClasses()) { out << nl << "__is.sliceObjects(false);"; @@ -1041,9 +1032,8 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) // // encodeKey // - out << sp << nl << "public byte[]"; - out << nl << "encodeKey(" << indexKeyTypeS << " key, Ice.Communicator communicator, " - << "Ice.EncodingVersion encoding)"; + out << sp << nl << "public void"; + out << nl << "encodeKey(" << indexKeyTypeS << " key, IceInternal.BasicStream __os)"; out << sb; if(dict.indices[i].member.empty()) { @@ -1056,7 +1046,7 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) keyS = "key.toLowerCase()"; } - out << nl << "return encodeValue(" << keyS << ", communicator, encoding);"; + out << nl << "encodeValue(" << keyS << ", __os);"; } else { @@ -1067,16 +1057,9 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) keyS = objectToVar(indexTypes[i], keyS); - out << nl << "IceInternal.BasicStream __os = " - << "new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, true, false);"; int iter = 0; writeMarshalUnmarshalCode(out, "", indexTypes[i], OptionalNone, false, 0, keyS, true, iter, false); assert(!indexTypes[i]->usesClasses()); - - out << nl << "IceInternal.Buffer buf = __os.prepareWrite();"; - out << nl << "byte[] r = new byte[buf.size()];"; - out << nl << "buf.b.get(r);"; - out << nl << "return r;"; } out << eb; @@ -1084,20 +1067,17 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) // decodeKey // out << sp << nl << "public " << indexKeyTypeS; - out << nl << "decodeKey(byte[] bytes, Ice.Communicator communicator, Ice.EncodingVersion encoding)"; + out << nl << "decodeKey(IceInternal.BasicStream __is)"; out << sb; if(dict.indices[i].member.empty()) { // // Decode the full value (with an encaps!) // - out << nl << "return decodeValue(bytes, communicator, encoding);"; + out << nl << "return decodeValue(__is);"; } else { - out << nl << "IceInternal.BasicStream __is = " - << "new IceInternal.BasicStream(IceInternal.Util.getInstance(communicator), encoding, bytes);"; - int iter = 0; list<string> metaData; string patchParams; @@ -1197,8 +1177,8 @@ FreezeGenerator::generate(UnitPtr& u, const Dict& dict) // if(dict.indices[i].member.empty() && dict.indices[i].caseSensitive) { - out << sp << nl << "protected byte[]"; - out << nl << "marshalKey(byte[] value)"; + out << sp << nl << "protected java.nio.ByteBuffer"; + out << nl << "marshalKey(java.nio.ByteBuffer value)"; out << sb; out << nl << "return value;"; out << eb; @@ -1383,7 +1363,7 @@ FreezeGenerator::generate(UnitPtr& u, const Index& index) // string typeString = typeToString(type, TypeModeIn); - out << sp << nl << "protected byte[]" << nl + out << sp << nl << "protected java.nio.ByteBuffer" << nl << "marshalKey(Ice.Object __servant)"; out << sb; out << nl << "if(__servant instanceof " << typeString << ")"; @@ -1399,7 +1379,7 @@ FreezeGenerator::generate(UnitPtr& u, const Index& index) string valueS = index.caseSensitive ? "__key" : "__key.toLowerCase()"; - out << sp << nl << "private byte[]" << nl + out << sp << nl << "private java.nio.ByteBuffer" << nl << "marshalKey(" << memberTypeString << " __key)"; out << sb; out << nl << "IceInternal.BasicStream __os = " @@ -1410,10 +1390,7 @@ FreezeGenerator::generate(UnitPtr& u, const Index& index) { out << nl << "__os.writePendingObjects();"; } - out << nl << "IceInternal.Buffer __buf = __os.prepareWrite();"; - out << nl << "byte[] __r = new byte[__buf.size()];"; - out << nl << "__buf.b.get(__r);"; - out << nl << "return __r;"; + out << nl << "return __os.prepareWrite().b;"; out << eb; out << eb; diff --git a/java/src/Freeze/Index.java b/java/src/Freeze/Index.java index 19dbf0d7c18..3b84522ea7d 100644 --- a/java/src/Freeze/Index.java +++ b/java/src/Freeze/Index.java @@ -9,6 +9,8 @@ package Freeze; +import java.nio.ByteBuffer; + public abstract class Index implements com.sleepycat.db.SecondaryKeyCreator { // @@ -26,11 +28,10 @@ public abstract class Index implements com.sleepycat.db.SecondaryKeyCreator Ice.EncodingVersion encoding = _store.encoding(); ObjectRecord rec = ObjectStore.unmarshalValue(value, communicator, encoding, _store.keepStats()); - byte[] secondaryKey = marshalKey(rec.servant); + ByteBuffer secondaryKey = marshalKey(rec.servant); if(secondaryKey != null) { - result.setData(secondaryKey); - result.setSize(secondaryKey.length); + result.setDataNIO(secondaryKey); return true; } else @@ -61,10 +62,10 @@ public abstract class Index implements com.sleepycat.db.SecondaryKeyCreator _facet = facet; } - protected abstract byte[] marshalKey(Ice.Object servant); + protected abstract ByteBuffer marshalKey(Ice.Object servant); protected Ice.Identity[] - untypedFindFirst(byte[] k, int firstN) + untypedFindFirst(ByteBuffer k, int firstN) { EvictorI.DeactivateController deactivateController = _store.evictor().deactivateController(); deactivateController.lock(); @@ -208,13 +209,13 @@ public abstract class Index implements com.sleepycat.db.SecondaryKeyCreator } protected Ice.Identity[] - untypedFind(byte[] key) + untypedFind(ByteBuffer key) { return untypedFindFirst(key, 0); } protected int - untypedCount(byte[] k) + untypedCount(ByteBuffer k) { EvictorI.DeactivateController deactivateController = _store.evictor().deactivateController(); deactivateController.lock(); diff --git a/java/src/Freeze/MapInternal/EntryI.java b/java/src/Freeze/MapInternal/EntryI.java index cb006c7cb13..f7de9fa6551 100644 --- a/java/src/Freeze/MapInternal/EntryI.java +++ b/java/src/Freeze/MapInternal/EntryI.java @@ -9,17 +9,17 @@ 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, byte[] valueBytes, byte[] indexBytes) + EntryI(MapI<K, V> map, K key, com.sleepycat.db.DatabaseEntry dbKey, ByteBuffer valueBytes, ByteBuffer indexBytes) { _map = map; _dbKey = dbKey; _valueBytes = valueBytes; _indexBytes = indexBytes; - _communicator = map.connection().getCommunicator(); - _encoding = map.connection().getEncoding(); _key = key; _haveKey = key != null; } @@ -30,7 +30,7 @@ class EntryI<K, V> implements java.util.Map.Entry<K, V> if(!_haveKey) { assert(_dbKey != null); - _key = _map.decodeKey(_dbKey.getData(), _communicator, _encoding); + _key = _map.decodeKey(_dbKey); _haveKey = true; } return _key; @@ -42,7 +42,7 @@ class EntryI<K, V> implements java.util.Map.Entry<K, V> if(!_haveValue) { assert(_valueBytes != null); - _value = _map.decodeValue(_valueBytes, _communicator, _encoding); + _value = _map.decodeValue(_valueBytes); _haveValue = true; // // Not needed anymore @@ -52,7 +52,7 @@ class EntryI<K, V> implements java.util.Map.Entry<K, V> return _value; } - public byte[] + public ByteBuffer getIndexBytes() { return _indexBytes; @@ -120,11 +120,9 @@ class EntryI<K, V> implements java.util.Map.Entry<K, V> private MapI<K, V> _map; private com.sleepycat.db.DatabaseEntry _dbKey; - private byte[] _valueBytes; - private byte[] _indexBytes; + private ByteBuffer _valueBytes; + private ByteBuffer _indexBytes; - private Ice.Communicator _communicator; - private Ice.EncodingVersion _encoding; private K _key; private boolean _haveKey = false; private V _value; diff --git a/java/src/Freeze/MapInternal/Index.java b/java/src/Freeze/MapInternal/Index.java index 6c9938db844..63d6250d2ef 100644 --- a/java/src/Freeze/MapInternal/Index.java +++ b/java/src/Freeze/MapInternal/Index.java @@ -15,9 +15,10 @@ import Freeze.ConnectionI; import Freeze.Map; import Freeze.MapIndex; import Freeze.NavigableMap; +import java.nio.ByteBuffer; public abstract class Index<K, V, I> - implements MapIndex, com.sleepycat.db.SecondaryKeyCreator, java.util.Comparator<byte[]>, KeyCodec<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) @@ -59,7 +60,14 @@ public abstract class Index<K, V, I> config.setType(com.sleepycat.db.DatabaseType.BTREE); if(_comparator != null) { - config.setBtreeComparator(this); + java.util.Comparator<byte[]> c = new java.util.Comparator<byte[]>() + { + public int compare(byte[] a1, byte[] a2) + { + return Index.this.compare(ByteBuffer.wrap(a1), ByteBuffer.wrap(a2)); + } + }; + config.setBtreeComparator(c); } config.setKeyCreator(this); @@ -149,25 +157,20 @@ public abstract class Index<K, V, I> throws com.sleepycat.db.DatabaseException { Ice.Communicator communicator = _map.connection().getCommunicator(); - byte[] secondaryKey = marshalKey(value.getData()); + ByteBuffer secondaryKey = marshalKey(UtilI.getBuffer(value)); assert(secondaryKey != null); - result.setData(secondaryKey); - result.setSize(secondaryKey.length); + result.setDataNIO(secondaryKey); return true; } // - // java.util.Comparator<byte[]> methods + // java.util.Comparator<ByteBuffer>.compare() // - - public int - compare(byte[] k1, byte[] k2) + public int compare(ByteBuffer b1, ByteBuffer b2) { assert(_comparator != null); - Ice.Communicator communicator = _map.connection().getCommunicator(); - Ice.EncodingVersion encoding = _map.connection().getEncoding(); - return _comparator.compare(decodeKey(k1, communicator, encoding), decodeKey(k2, communicator, encoding)); + return _comparator.compare(decodeKey(b1), decodeKey(b2)); } private class FindModel implements IteratorModel<K, V> @@ -230,7 +233,7 @@ public abstract class Index<K, V, I> public int count(I key) { - byte[] k = encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + 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(); @@ -384,6 +387,51 @@ public abstract class Index<K, V, I> return new IndexedSubMap<K, V, I>(this, null, false, null, false); } + ByteBuffer encodeKey(I k) + { + IceInternal.BasicStream str = createWriteStream(); + encodeKey(k, str); + return str.prepareWrite().b; + } + + I decodeKey(ByteBuffer buf) + { + return decodeKey(createReadStream(buf)); + } + + I 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)); + } + } + + IceInternal.BasicStream createWriteStream() + { + return new IceInternal.BasicStream(IceInternal.Util.getInstance(_map.connection().getCommunicator()), + _map.connection().getEncoding(), true, false); + } + + IceInternal.BasicStream createReadStream(byte[] arr) + { + return new IceInternal.BasicStream(IceInternal.Util.getInstance(_map.connection().getCommunicator()), + _map.connection().getEncoding(), arr); + } + + IceInternal.BasicStream createReadStream(ByteBuffer buf) + { + return new IceInternal.BasicStream(IceInternal.Util.getInstance(_map.connection().getCommunicator()), + _map.connection().getEncoding(), buf); + } + com.sleepycat.db.SecondaryDatabase db() { @@ -420,7 +468,7 @@ public abstract class Index<K, V, I> @SuppressWarnings("unchecked") I key = (I)o; - byte[] k = encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + 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(); @@ -475,7 +523,7 @@ public abstract class Index<K, V, I> com.sleepycat.db.SecondaryCursor c = (com.sleepycat.db.SecondaryCursor)cursor; assert(fromKey != null); - byte[] k = encodeKey(fromKey, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer k = encodeKey(fromKey); com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); @@ -483,7 +531,7 @@ public abstract class Index<K, V, I> if(c.getSearchKey(dbIKey, dbKey, dbValue, null) == com.sleepycat.db.OperationStatus.SUCCESS) { - return new EntryI<K, V>(_map, null, dbKey, dbValue.getData(), dbIKey.getData()); + return new EntryI<K, V>(_map, null, dbKey, UtilI.getBuffer(dbValue), UtilI.getBuffer(dbIKey)); } return null; @@ -515,7 +563,7 @@ public abstract class Index<K, V, I> if(status == com.sleepycat.db.OperationStatus.SUCCESS) { - return new EntryI<K, V>(_map, null, dbKey, dbValue.getData(), dbIKey.getData()); + return new EntryI<K, V>(_map, null, dbKey, UtilI.getBuffer(dbValue), UtilI.getBuffer(dbIKey)); } return null; @@ -538,15 +586,15 @@ public abstract class Index<K, V, I> if(fromKey != null) { - byte[] k = encodeKey(fromKey, _map.connection().getCommunicator(), _map.connection().getEncoding()); - dbIKey.setData(k); + 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(dbIKey.getData(), k); + int cmp = compare(UtilI.getBuffer(dbIKey), k); assert(cmp >= 0); if(cmp == 0) { @@ -605,15 +653,15 @@ public abstract class Index<K, V, I> if(fromKey != null) { - byte[] k = encodeKey(fromKey, _map.connection().getCommunicator(), _map.connection().getEncoding()); - dbIKey.setData(k); + 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(dbIKey.getData(), k); + int cmp = compare(UtilI.getBuffer(dbIKey), k); assert(cmp >= 0); if(cmp == 0) { @@ -658,12 +706,11 @@ public abstract class Index<K, V, I> // // marshalKey may be overridden by subclasses as an optimization. // - protected byte[] - marshalKey(byte[] value) + protected ByteBuffer + marshalKey(ByteBuffer value) { - V decodedValue = _map.decodeValue(value, _map.connection().getCommunicator(), _map.connection().getEncoding()); - return encodeKey(extractKey(decodedValue), _map.connection().getCommunicator(), - _map.connection().getEncoding()); + V decodedValue = _map.decodeValue(value); + return encodeKey(extractKey(decodedValue)); } private EntryI<K, V> @@ -673,14 +720,14 @@ public abstract class Index<K, V, I> I key = null; if(fromKey != null || toKey != null) { - key = decodeKey(dbIKey.getData(), _map.connection().getCommunicator(), _map.connection().getEncoding()); + key = decodeKey(dbIKey); if(!checkRange(key, fromKey, fromInclusive, toKey, toInclusive)) { return null; } } - return new EntryI<K, V>(_map, null, dbKey, dbValue.getData(), dbIKey.getData()); + return new EntryI<K, V>(_map, null, dbKey, UtilI.getBuffer(dbValue), UtilI.getBuffer(dbIKey)); } private boolean diff --git a/java/src/Freeze/MapInternal/IndexedSubMap.java b/java/src/Freeze/MapInternal/IndexedSubMap.java index fd775d29927..2a35174003e 100644 --- a/java/src/Freeze/MapInternal/IndexedSubMap.java +++ b/java/src/Freeze/MapInternal/IndexedSubMap.java @@ -13,6 +13,7 @@ import Freeze.ConnectionI; import Freeze.DatabaseException; import Freeze.Map; import Freeze.NavigableMap; +import java.nio.ByteBuffer; // // Indexed submap of a Freeze Map or of another submap @@ -138,8 +139,7 @@ class IndexedSubMap<K, V, I> next() { EntryI<K, V> entry = (EntryI<K, V>)_iterator.next(); - return new Entry(_index.decodeKey(entry.getIndexBytes(), _map.connection().getCommunicator(), - _map.connection().getEncoding())); + return new Entry(_index.decodeKey(entry.getIndexBytes())); } public void @@ -504,7 +504,7 @@ class IndexedSubMap<K, V, I> } private Entry - entrySearch(Search.Type type, byte[] key) + entrySearch(Search.Type type, ByteBuffer key) { if(type != Search.Type.FIRST && type != Search.Type.LAST && key == null) { @@ -523,8 +523,7 @@ class IndexedSubMap<K, V, I> if(Search.search(type, _map.connection(), _index.dbName(), _index.db(), dbKey, null, _index, _view, _index.traceLevels())) { - I k = _index.decodeKey(dbKey.getData(), _map.connection().getCommunicator(), - _map.connection().getEncoding()); + I k = _index.decodeKey(dbKey); return new Entry(k); } @@ -578,7 +577,7 @@ class IndexedSubMap<K, V, I> first() { Search.Type type; - byte[] key = null; + ByteBuffer key = null; if(_fromKey != null) { type = _fromInclusive ? mapSearchType(Search.Type.CEILING) : mapSearchType(Search.Type.HIGHER); @@ -595,7 +594,7 @@ class IndexedSubMap<K, V, I> last() { Search.Type type; - byte[] key = null; + ByteBuffer key = null; if(_toKey != null) { type = _toInclusive ? mapSearchType(Search.Type.FLOOR) : mapSearchType(Search.Type.LOWER); @@ -611,28 +610,28 @@ class IndexedSubMap<K, V, I> final Entry ceiling(I key) { - byte[] k = _index.encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer k = _index.encodeKey(key); return entrySearch(mapSearchType(Search.Type.CEILING), k); } final Entry floor(I key) { - byte[] k = _index.encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer k = _index.encodeKey(key); return entrySearch(mapSearchType(Search.Type.FLOOR), k); } final Entry higher(I key) { - byte[] k = _index.encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer k = _index.encodeKey(key); return entrySearch(mapSearchType(Search.Type.HIGHER), k); } final Entry lower(I key) { - byte[] k = _index.encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer k = _index.encodeKey(key); return entrySearch(mapSearchType(Search.Type.LOWER), k); } @@ -680,9 +679,9 @@ class IndexedSubMap<K, V, I> // final public boolean - keyInRange(byte[] key) + keyInRange(ByteBuffer key) { - I k = _index.decodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + I k = _index.decodeKey(key); return inRange(k, true); } @@ -699,24 +698,22 @@ class IndexedSubMap<K, V, I> return _comparator; } - final protected byte[] + final protected ByteBuffer fromKeyBytes() { if(_fromKey != null && _fromKeyBytes == null) { - _fromKeyBytes = _index.encodeKey(_fromKey, _map.connection().getCommunicator(), - _map.connection().getEncoding()); + _fromKeyBytes = _index.encodeKey(_fromKey); } return _fromKeyBytes; } - final protected byte[] + final protected ByteBuffer toKeyBytes() { if(_toKey != null && _toKeyBytes == null) { - _toKeyBytes = _index.encodeKey(_toKey, _map.connection().getCommunicator(), - _map.connection().getEncoding()); + _toKeyBytes = _index.encodeKey(_toKey); } return _toKeyBytes; } @@ -754,8 +751,8 @@ class IndexedSubMap<K, V, I> final boolean _fromInclusive; final I _toKey; final boolean _toInclusive; - private byte[] _fromKeyBytes; - private byte[] _toKeyBytes; + private ByteBuffer _fromKeyBytes; + private ByteBuffer _toKeyBytes; } private class AscendingView extends View diff --git a/java/src/Freeze/MapInternal/IteratorI.java b/java/src/Freeze/MapInternal/IteratorI.java index e3bb40a7b0f..08ca5e12b47 100644 --- a/java/src/Freeze/MapInternal/IteratorI.java +++ b/java/src/Freeze/MapInternal/IteratorI.java @@ -15,6 +15,7 @@ import Freeze.DatabaseException; import Freeze.DeadlockException; import Freeze.NotFoundException; import Freeze.Map; +import java.nio.ByteBuffer; class IteratorI<K, V> implements Freeze.Map.EntryIterator<java.util.Map.Entry<K, V>> { @@ -280,7 +281,7 @@ class IteratorI<K, V> implements Freeze.Map.EntryIterator<java.util.Map.Entry<K, // // Yes, update it directly // - byte[] v = _map.encodeValue(value, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer v = _map.encodeValue(value); com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(v); try @@ -326,8 +327,7 @@ class IteratorI<K, V> implements Freeze.Map.EntryIterator<java.util.Map.Entry<K, throw ex; } - byte[] v = _map.encodeValue(value, _map.connection().getCommunicator(), - _map.connection().getEncoding()); + ByteBuffer v = _map.encodeValue(value); com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(v); clone.putCurrent(dbValue); } diff --git a/java/src/Freeze/MapInternal/KeyCodec.java b/java/src/Freeze/MapInternal/KeyCodec.java index ad43dddf8da..f633efd8340 100644 --- a/java/src/Freeze/MapInternal/KeyCodec.java +++ b/java/src/Freeze/MapInternal/KeyCodec.java @@ -11,6 +11,6 @@ package Freeze.MapInternal; interface KeyCodec<K> { - public abstract byte[] encodeKey(K k, Ice.Communicator communicator, Ice.EncodingVersion encoding); - public abstract K decodeKey(byte[] b, Ice.Communicator communicator, Ice.EncodingVersion encoding); + public abstract void encodeKey(K k, IceInternal.BasicStream str); + public abstract K decodeKey(IceInternal.BasicStream str); } diff --git a/java/src/Freeze/MapInternal/MapI.java b/java/src/Freeze/MapInternal/MapI.java index 460bb5eff04..9e30a2ad3b0 100644 --- a/java/src/Freeze/MapInternal/MapI.java +++ b/java/src/Freeze/MapInternal/MapI.java @@ -23,12 +23,13 @@ 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 byte[] encodeValue(V o, Ice.Communicator communicator, Ice.EncodingVersion encoding); - public abstract V decodeValue(byte[] b, Ice.Communicator communicator, Ice.EncodingVersion encoding); + 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, @@ -37,7 +38,10 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> _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); @@ -49,7 +53,10 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> _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); } @@ -92,7 +99,8 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> if(trace.level >= 2) { - trace.logger.trace("Freeze.MapInternal.MapI", "Removing all existing indices for \"" + dbName + "\""); + trace.logger.trace("Freeze.MapInternal.MapI", "Removing all existing indices for \"" + dbName + + "\""); } CatalogIndexList catalogIndexList = new CatalogIndexList(connection, Util.catalogIndexListName(), true); @@ -122,7 +130,8 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> if(trace.level >= 2) { - trace.logger.trace("Freeze.MapInternal.MapI", "Renaming \"" + dbName + "\" to \"" + oldDbName + "\""); + trace.logger.trace("Freeze.MapInternal.MapI", "Renaming \"" + dbName + "\" to \"" + oldDbName + + "\""); } connection.dbEnv().getEnv().renameDatabase(txn, dbName, null, oldDbName); @@ -132,13 +141,13 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> oldDb = connection.dbEnv().getEnv().openDatabase(txn, oldDbName, null, oldDbConfig); - newDb = new MapDb(connection, dbName, key, value, map._comparator, indices, true); + 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 + "\""); + trace.logger.trace("Freeze.MapInternal.MapI", "Writing contents of \"" + oldDbName + + "\" to fresh \"" + dbName + "\""); } // @@ -188,7 +197,8 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> { if(trace.deadlockWarning) { - trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.recreate on Db \"" + dbName + "\"; retrying ..."); + trace.logger.warning("Deadlock in Freeze.MapInternal.MapI.recreate on Db \"" + dbName + + "\"; retrying ..."); } // @@ -252,7 +262,7 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> protected void init(MapIndex[] indices, String dbName, String key, String value, boolean createDb) { - init(_connection.dbEnv().getSharedMapDb(dbName, key, value, _comparator, indices, createDb), indices); + init(_connection.dbEnv().getSharedMapDb(dbName, key, value, _dbComparator, indices, createDb), indices); } protected void @@ -277,17 +287,13 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> public void fastPut(K key, V value) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(k); - putImpl(dbKey, value); + putImpl(new com.sleepycat.db.DatabaseEntry(encodeKey(key)), value); } public boolean fastRemove(K key) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(k); - return removeImpl(dbKey); + return removeImpl(new com.sleepycat.db.DatabaseEntry(encodeKey(key))); } public void @@ -455,60 +461,52 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> public java.util.Map.Entry<K, V> ceilingEntry(K key) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - return entrySearch(Search.Type.CEILING, k, true); + return entrySearch(Search.Type.CEILING, encodeKey(key), true); } public java.util.Map.Entry<K, V> floorEntry(K key) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - return entrySearch(Search.Type.FLOOR, k, true); + return entrySearch(Search.Type.FLOOR, encodeKey(key), true); } public java.util.Map.Entry<K, V> higherEntry(K key) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - return entrySearch(Search.Type.HIGHER, k, true); + return entrySearch(Search.Type.HIGHER, encodeKey(key), true); } public java.util.Map.Entry<K, V> lowerEntry(K key) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - return entrySearch(Search.Type.LOWER, k, true); + return entrySearch(Search.Type.LOWER, encodeKey(key), true); } public K ceilingKey(K key) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - java.util.Map.Entry<K, V> e = entrySearch(Search.Type.CEILING, k, false); + java.util.Map.Entry<K, V> e = entrySearch(Search.Type.CEILING, encodeKey(key), false); return e != null ? e.getKey() : null; } public K floorKey(K key) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - java.util.Map.Entry<K, V> e = entrySearch(Search.Type.FLOOR, k, false); + java.util.Map.Entry<K, V> e = entrySearch(Search.Type.FLOOR, encodeKey(key), false); return e != null ? e.getKey() : null; } public K higherKey(K key) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - java.util.Map.Entry<K, V> e = entrySearch(Search.Type.HIGHER, k, false); + java.util.Map.Entry<K, V> e = entrySearch(Search.Type.HIGHER, encodeKey(key), false); return e != null ? e.getKey() : null; } public K lowerKey(K key) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - java.util.Map.Entry<K, V> e = entrySearch(Search.Type.LOWER, k, false); + java.util.Map.Entry<K, V> e = entrySearch(Search.Type.LOWER, encodeKey(key), false); return e != null ? e.getKey() : null; } @@ -789,9 +787,7 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> throw ex; } - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(k); + 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); @@ -839,29 +835,26 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> @SuppressWarnings("unchecked") K key = (K)o; - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(k); - byte[] v = getImpl(dbKey); + ByteBuffer v = getImpl(new com.sleepycat.db.DatabaseEntry(encodeKey(key))); if(v == null) { return null; } else { - return decodeValue(v, _connection.getCommunicator(), _connection.getEncoding()); + return decodeValue(v); } } public V put(K key, V value) { - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(k); - byte[] v = getImpl(dbKey); + 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, _connection.getCommunicator(), _connection.getEncoding()); + old = decodeValue(v); } putImpl(dbKey, value); return old; @@ -873,13 +866,12 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> @SuppressWarnings("unchecked") K key = (K)o; - byte[] k = encodeKey(key, _connection.getCommunicator(), _connection.getEncoding()); - com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(k); - byte[] v = getImpl(dbKey); + com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(encodeKey(key)); + ByteBuffer v = getImpl(dbKey); if(v != null && removeImpl(dbKey)) { - return decodeValue(v, _connection.getCommunicator(), _connection.getEncoding()); + return decodeValue(v); } else { @@ -999,9 +991,8 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> EntryI<K, V> entry = (EntryI<K, V>)o; V value = entry.getValue(); - byte[] v = getImpl(entry.getDbKey()); - return v != null && valEquals(decodeValue(v, _connection.getCommunicator(), - _connection.getEncoding()), value); + ByteBuffer v = getImpl(entry.getDbKey()); + return v != null && valEquals(decodeValue(v), value); } public boolean @@ -1015,9 +1006,8 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> EntryI<K, V> entry = (EntryI<K, V>)o; V value = entry.getValue(); - byte[] v = getImpl(entry.getDbKey()); - if(v != null && valEquals(decodeValue(v, _connection.getCommunicator(), _connection.getEncoding()), - value)) + ByteBuffer v = getImpl(entry.getDbKey()); + if(v != null && valEquals(decodeValue(v), value)) { return removeImpl(entry.getDbKey()); } @@ -1078,6 +1068,75 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> 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, true, false); + } + + IceInternal.BasicStream createReadStream(byte[] arr) + { + return new IceInternal.BasicStream(IceInternal.Util.getInstance(_communicator), _encoding, arr); + } + + IceInternal.BasicStream createReadStream(ByteBuffer buf) + { + return new IceInternal.BasicStream(IceInternal.Util.getInstance(_communicator), _encoding, buf); + } + com.sleepycat.db.Database db() { @@ -1132,7 +1191,7 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> // Convenience method for use in this class. // private EntryI<K, V> - entrySearch(Search.Type type, byte[] key, boolean includeValue) + entrySearch(Search.Type type, ByteBuffer key, boolean includeValue) { return entrySearch(type, key, includeValue, null); } @@ -1141,7 +1200,7 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> // Also used by SubMap. // EntryI<K, V> - entrySearch(Search.Type type, byte[] key, boolean includeValue, Search.KeyValidator validator) + entrySearch(Search.Type type, ByteBuffer key, boolean includeValue, Search.KeyValidator validator) { if(type != Search.Type.FIRST && type != Search.Type.LAST && key == null) { @@ -1160,7 +1219,7 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> if(Search.search(type, _connection, _dbName, _db.db(), dbKey, dbValue, _comparator, validator, _trace)) { - return new EntryI<K, V>(this, null, dbKey, dbValue != null ? dbValue.getData() : null, null); + return new EntryI<K, V>(this, null, dbKey, dbValue != null ? UtilI.getBuffer(dbValue) : null, null); } return null; @@ -1178,15 +1237,15 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> com.sleepycat.db.DatabaseEntry dbKey = new com.sleepycat.db.DatabaseEntry(); com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); - byte[] k = encodeKey(fromKey, _connection.getCommunicator(), _connection.getEncoding()); - dbKey.setData(k); + 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(dbKey.getData(), k); + int cmp = _comparator.compare(UtilI.getBuffer(dbKey), k); assert(cmp >= 0); if(cmp == 0) { @@ -1238,15 +1297,15 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> if(fromKey != null) { - byte[] k = encodeKey(fromKey, _connection.getCommunicator(), _connection.getEncoding()); - dbKey.setData(k); + 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(dbKey.getData(), k); + int cmp = _comparator.compare(UtilI.getBuffer(dbKey), k); assert(cmp >= 0); if(cmp == 0) { @@ -1295,7 +1354,7 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> throw ex; } - byte[] v = encodeValue(value, _connection.getCommunicator(), _connection.getEncoding()); + ByteBuffer v = encodeValue(value); com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(v); if(_trace.level >= 2) @@ -1398,7 +1457,7 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> } } - private byte[] + private ByteBuffer getImpl(com.sleepycat.db.DatabaseEntry dbKey) { if(_db == null) @@ -1422,7 +1481,7 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> com.sleepycat.db.OperationStatus rc = _db.db().get(_connection.dbTxn(), dbKey, dbValue, null); if(rc == com.sleepycat.db.OperationStatus.SUCCESS) { - return dbValue.getData(); + return UtilI.getBuffer(dbValue); } else { @@ -1463,14 +1522,14 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> K key = null; if(fromKey != null || toKey != null) { - key = decodeKey(dbKey.getData(), _connection.getCommunicator(), _connection.getEncoding()); + key = decodeKey(dbKey); if(!checkRange(key, fromKey, fromInclusive, toKey, toInclusive)) { return null; } } - return new EntryI<K, V>(this, key, dbKey, dbValue.getData(), null); + return new EntryI<K, V>(this, key, dbKey, UtilI.getBuffer(dbValue), null); } private boolean @@ -1501,25 +1560,46 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> return (v1 == null ? v2 == null : v1.equals(v2)); } - private class Comparator implements java.util.Comparator<byte[]> + private class DbComparator implements java.util.Comparator<byte[]> + { + DbComparator(java.util.Comparator<K> comparator) + { + _comparator = comparator; + } + + public java.util.Comparator<K> comparator() + { + return _comparator; + } + + 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() + public java.util.Comparator<K> comparator() { return _comparator; } - public int - compare(byte[] d1, byte[] d2) + public int compare(ByteBuffer b1, ByteBuffer b2) { - Ice.Communicator communicator = _connection.getCommunicator(); - Ice.EncodingVersion encoding = _connection.getEncoding(); - return _comparator.compare(decodeKey(d1, communicator, encoding), decodeKey(d2, communicator, encoding)); + return _comparator.compare(decodeKey(b1), decodeKey(b2)); } // @@ -1560,8 +1640,11 @@ public abstract class MapI<K, V> extends java.util.AbstractMap<K, V> 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; diff --git a/java/src/Freeze/MapInternal/Search.java b/java/src/Freeze/MapInternal/Search.java index a57dbed23d9..7146bd5661c 100644 --- a/java/src/Freeze/MapInternal/Search.java +++ b/java/src/Freeze/MapInternal/Search.java @@ -12,6 +12,7 @@ package Freeze.MapInternal; import Freeze.ConnectionI; import Freeze.DatabaseException; import Freeze.DeadlockException; +import java.nio.ByteBuffer; class Search { @@ -47,13 +48,13 @@ class Search interface KeyValidator { - boolean keyInRange(byte[] key); + 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<byte[]> comparator, KeyValidator validator, TraceLevels trace) + java.util.Comparator<ByteBuffer> comparator, KeyValidator validator, TraceLevels trace) { if(type != Type.FIRST && type != Type.LAST && comparator == null) { @@ -79,7 +80,7 @@ class Search try { com.sleepycat.db.DatabaseEntry dbcKey = - new com.sleepycat.db.DatabaseEntry(key != null ? key.getData() : null); + new com.sleepycat.db.DatabaseEntry(key != null ? UtilI.getBuffer(key) : null); dbcKey.setReuseBuffer(false); dbc = db.openCursor(connection.dbTxn(), null); @@ -116,7 +117,7 @@ class Search // 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(dbcKey.getData(), key.getData()); + int cmp = comparator.compare(UtilI.getBuffer(dbcKey), UtilI.getBuffer(key)); assert(cmp >= 0); if(cmp > 0) { @@ -143,7 +144,7 @@ class Search // 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(dbcKey.getData(), key.getData()); + int cmp = comparator.compare(UtilI.getBuffer(dbcKey), UtilI.getBuffer(key)); assert(cmp >= 0); if(cmp == 0) { @@ -178,9 +179,9 @@ class Search if(status == com.sleepycat.db.OperationStatus.SUCCESS) { - if(validator == null || validator.keyInRange(dbcKey.getData())) + if(validator == null || validator.keyInRange(UtilI.getBuffer(dbcKey))) { - key.setData(dbcKey.getData()); + key.setDataNIO(UtilI.getBuffer(dbcKey)); return true; } } diff --git a/java/src/Freeze/MapInternal/SubMap.java b/java/src/Freeze/MapInternal/SubMap.java index f886b38b056..270f72ca949 100644 --- a/java/src/Freeze/MapInternal/SubMap.java +++ b/java/src/Freeze/MapInternal/SubMap.java @@ -12,6 +12,7 @@ package Freeze.MapInternal; import Freeze.ConnectionI; import Freeze.Map; import Freeze.NavigableMap; +import java.nio.ByteBuffer; // // Submap of a Freeze Map or of another submap @@ -404,7 +405,7 @@ class SubMap<K, V> extends java.util.AbstractMap<K, V> implements NavigableMap<K first() { Search.Type type; - byte[] key = null; + ByteBuffer key = null; if(_fromKey != null) { type = _fromInclusive ? mapSearchType(Search.Type.CEILING) : mapSearchType(Search.Type.HIGHER); @@ -421,7 +422,7 @@ class SubMap<K, V> extends java.util.AbstractMap<K, V> implements NavigableMap<K last() { Search.Type type; - byte[] key = null; + ByteBuffer key = null; if(_toKey != null) { type = _toInclusive ? mapSearchType(Search.Type.FLOOR) : mapSearchType(Search.Type.LOWER); @@ -437,28 +438,28 @@ class SubMap<K, V> extends java.util.AbstractMap<K, V> implements NavigableMap<K final EntryI<K, V> ceiling(K key) { - byte[] k = _map.encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer k = _map.encodeKey(key); return _map.entrySearch(mapSearchType(Search.Type.CEILING), k, true, this); } final EntryI<K, V> floor(K key) { - byte[] k = _map.encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer k = _map.encodeKey(key); return _map.entrySearch(mapSearchType(Search.Type.FLOOR), k, true, this); } final EntryI<K, V> higher(K key) { - byte[] k = _map.encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer k = _map.encodeKey(key); return _map.entrySearch(mapSearchType(Search.Type.HIGHER), k, true, this); } final EntryI<K, V> lower(K key) { - byte[] k = _map.encodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + ByteBuffer k = _map.encodeKey(key); return _map.entrySearch(mapSearchType(Search.Type.LOWER), k, true, this); } @@ -506,9 +507,9 @@ class SubMap<K, V> extends java.util.AbstractMap<K, V> implements NavigableMap<K // final public boolean - keyInRange(byte[] key) + keyInRange(ByteBuffer key) { - K k = _map.decodeKey(key, _map.connection().getCommunicator(), _map.connection().getEncoding()); + K k = _map.decodeKey(key); return inRange(k, true); } @@ -525,24 +526,22 @@ class SubMap<K, V> extends java.util.AbstractMap<K, V> implements NavigableMap<K return _comparator; } - final protected byte[] + final protected ByteBuffer fromKeyBytes() { if(_fromKey != null && _fromKeyBytes == null) { - _fromKeyBytes = _map.encodeKey(_fromKey, _map.connection().getCommunicator(), - _map.connection().getEncoding()); + _fromKeyBytes = _map.encodeKey(_fromKey); } return _fromKeyBytes; } - final protected byte[] + final protected ByteBuffer toKeyBytes() { if(_toKey != null && _toKeyBytes == null) { - _toKeyBytes = _map.encodeKey(_toKey, _map.connection().getCommunicator(), - _map.connection().getEncoding()); + _toKeyBytes = _map.encodeKey(_toKey); } return _toKeyBytes; } @@ -580,8 +579,8 @@ class SubMap<K, V> extends java.util.AbstractMap<K, V> implements NavigableMap<K final boolean _fromInclusive; final K _toKey; final boolean _toInclusive; - private byte[] _fromKeyBytes; - private byte[] _toKeyBytes; + private ByteBuffer _fromKeyBytes; + private ByteBuffer _toKeyBytes; } private class AscendingView extends View diff --git a/java/src/Freeze/MapInternal/UtilI.java b/java/src/Freeze/MapInternal/UtilI.java new file mode 100644 index 00000000000..433324308cc --- /dev/null +++ b/java/src/Freeze/MapInternal/UtilI.java @@ -0,0 +1,35 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2014 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; + } +} |