| | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.NoSuchElementException; |
| | | |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.i18n.slf4j.LocalizedLogger; |
| | |
| | | { |
| | | private ByteString currentKey; |
| | | private ByteString currentValue; |
| | | private final Exchange ex; |
| | | private final Exchange exchange; |
| | | |
| | | private CursorImpl(final Exchange exchange) |
| | | { |
| | | this.ex = exchange; |
| | | this.exchange = exchange; |
| | | } |
| | | |
| | | @Override |
| | | public void close() |
| | | { |
| | | // Release immediately because this exchange did not come from the txn cache |
| | | db.releaseExchange(ex); |
| | | db.releaseExchange(exchange); |
| | | } |
| | | |
| | | @Override |
| | | public boolean isDefined() { |
| | | return exchange.getValue().isDefined(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | { |
| | | if (currentKey == null) |
| | | { |
| | | currentKey = keyToBytes(ex.getKey()); |
| | | throwIfUndefined(); |
| | | currentKey = ByteString.wrap(exchange.getKey().reset().decodeByteArray()); |
| | | } |
| | | return currentKey; |
| | | } |
| | |
| | | { |
| | | if (currentValue == null) |
| | | { |
| | | currentValue = valueToBytes(ex.getValue()); |
| | | throwIfUndefined(); |
| | | currentValue = ByteString.wrap(exchange.getValue().getByteArray()); |
| | | } |
| | | return currentValue; |
| | | } |
| | |
| | | clearCurrentKeyAndValue(); |
| | | try |
| | | { |
| | | return ex.next(); |
| | | return exchange.next(); |
| | | } |
| | | catch (final PersistitException e) |
| | | { |
| | |
| | | public boolean positionToKey(final ByteSequence key) |
| | | { |
| | | clearCurrentKeyAndValue(); |
| | | bytesToKey(ex.getKey(), key); |
| | | bytesToKey(exchange.getKey(), key); |
| | | try |
| | | { |
| | | ex.fetch(); |
| | | return ex.getValue().isDefined(); |
| | | exchange.fetch(); |
| | | return exchange.getValue().isDefined(); |
| | | } |
| | | catch (final PersistitException e) |
| | | { |
| | |
| | | public boolean positionToKeyOrNext(final ByteSequence key) |
| | | { |
| | | clearCurrentKeyAndValue(); |
| | | bytesToKey(ex.getKey(), key); |
| | | bytesToKey(exchange.getKey(), key); |
| | | try |
| | | { |
| | | ex.fetch(); |
| | | return ex.getValue().isDefined() || ex.next(); |
| | | exchange.fetch(); |
| | | return exchange.getValue().isDefined() || exchange.next(); |
| | | } |
| | | catch (final PersistitException e) |
| | | { |
| | |
| | | { |
| | | // There doesn't seem to be a way to optimize this using Persistit. |
| | | clearCurrentKeyAndValue(); |
| | | ex.getKey().to(Key.BEFORE); |
| | | exchange.getKey().to(Key.BEFORE); |
| | | try |
| | | { |
| | | for (int i = 0; i <= index; i++) |
| | | { |
| | | if (!ex.next()) |
| | | if (!exchange.next()) |
| | | { |
| | | return false; |
| | | } |
| | |
| | | public boolean positionToLastKey() |
| | | { |
| | | clearCurrentKeyAndValue(); |
| | | ex.getKey().to(Key.AFTER); |
| | | exchange.getKey().to(Key.AFTER); |
| | | try |
| | | { |
| | | return ex.previous(); |
| | | return exchange.previous(); |
| | | } |
| | | catch (final PersistitException e) |
| | | { |
| | |
| | | currentKey = null; |
| | | currentValue = null; |
| | | } |
| | | |
| | | private void throwIfUndefined() { |
| | | if (!isDefined()) { |
| | | throw new NoSuchElementException(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** PersistIt implementation of the {@link Importer} interface. */ |
| | |
| | | return value; |
| | | } |
| | | |
| | | private ByteString keyToBytes(final Key key) |
| | | { |
| | | return ByteString.wrap(key.reset().decodeByteArray()); |
| | | } |
| | | |
| | | private ByteString valueToBytes(final Value value) |
| | | { |
| | | if (value.isDefined()) |