|
42 | 42 |
|
43 | 43 | import java.nio.ByteBuffer;
|
44 | 44 | import java.util.Collection;
|
| 45 | +import java.util.LinkedHashMap; |
45 | 46 | import java.util.Map;
|
46 | 47 | import java.util.Map.Entry;
|
47 | 48 | import java.util.Objects;
|
48 | 49 | import java.util.Optional;
|
| 50 | +import java.util.SequencedMap; |
49 | 51 | import java.util.function.Consumer;
|
50 | 52 | import java.util.function.Function;
|
51 | 53 | import java.util.function.Predicate;
|
| 54 | +import java.util.stream.Collectors; |
52 | 55 | import java.util.stream.Stream;
|
53 | 56 | import java.util.stream.StreamSupport;
|
54 | 57 |
|
@@ -138,8 +141,8 @@ public AbstractLmdbDb(final LmdbEnv lmdbEnvironment,
|
138 | 141 | int envMaxKeySize = lmdbEnvironment.getMaxKeySize();
|
139 | 142 | if (keySerdeCapacity > envMaxKeySize) {
|
140 | 143 | LAMBDA_LOGGER.debug(() -> LogUtil.message("Key serde {} capacity {} is greater than the maximum " +
|
141 |
| - "key size for the environment {}. " + |
142 |
| - "The max environment key size {} will be used instead.", |
| 144 | + "key size for the environment {}. " + |
| 145 | + "The max environment key size {} will be used instead.", |
143 | 146 | keySerde.getClass().getName(), keySerdeCapacity, envMaxKeySize, envMaxKeySize));
|
144 | 147 | }
|
145 | 148 | this.keyBufferCapacity = Math.min(envMaxKeySize, keySerdeCapacity);
|
@@ -344,6 +347,21 @@ public <T> T streamEntries(final Txn<ByteBuffer> txn,
|
344 | 347 | }
|
345 | 348 | }
|
346 | 349 |
|
| 350 | + /** |
| 351 | + * Get all entries found in keyRange in the order they are found in the DB. |
| 352 | + * |
| 353 | + * @return The result of the stream mapping function. |
| 354 | + */ |
| 355 | + public SequencedMap<K, V> asSequencedMap(final Txn<ByteBuffer> txn, |
| 356 | + final KeyRange<K> keyRange) { |
| 357 | + return streamEntries(txn, keyRange, stream -> stream |
| 358 | + .collect(Collectors.toMap( |
| 359 | + Entry::getKey, |
| 360 | + Entry::getValue, |
| 361 | + (o, o2) -> o, // Merge shouldn't be an issue as the db is essentially a map |
| 362 | + LinkedHashMap::new))); |
| 363 | + } |
| 364 | + |
347 | 365 | /**
|
348 | 366 | * Find the first entry matching the supplied key predicate in the supplied key range.
|
349 | 367 | * Not very efficient as it will scan over all entries in the range (de-serialising
|
|
0 commit comments