GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — lmdbjava/src/test/java/org/lmdbjava/DbiBuilderTest.java at master · lmdbjava/lmdbjava Expand file tree/ Copy pathDbiBuilderTest.javaMore file actions 206 lines (181 loc) · 6.27 KB / Copy pathDbiBuilderTest.javaFile metadata and controls206 lines (181 loc) · 6.27 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 /* * Copyright © 2016-2025 The LmdbJava Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.lmdbjava; import static org.assertj.core.api.Assertions.assertThat; import static org.lmdbjava.Env.create; import static org.lmdbjava.EnvFlags.MDB_NOSUBDIR; import static org.lmdbjava.TestUtils.bb; import static org.lmdbjava.TestUtils.getString; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class DbiBuilderTest { private TempDir tempDir; private Env<ByteBuffer> env; @BeforeEach public void before() { tempDir = new TempDir(); env = create() .setMapSize(64, ByteUnit.MEBIBYTES) .setMaxReaders(2) .setMaxDbs(2) .setEnvFlags(MDB_NOSUBDIR) .open(tempDir.createTempFile()); } @AfterEach public void after() { env.close(); tempDir.cleanup(); } @Test public void unnamed() { final Dbi<ByteBuffer> dbi = env.createDbi() .withoutDbName() .withDefaultComparator() .setDbiFlags(DbiFlags.MDB_CREATE) .open(); assertThat(dbi.getName()).isNull(); assertThat(dbi.getNameAsString()).isEmpty(); assertThat(env.getDbiNames()).isEmpty(); assertPutAndGet(dbi); } @Test public void named() { final Dbi<ByteBuffer> dbi = env.createDbi() .setDbName("foo") .withDefaultComparator() .setDbiFlags(DbiFlags.MDB_CREATE) .open(); assertPutAndGet(dbi); assertThat(env.getDbiNames()).hasSize(1); assertThat(new String(env.getDbiNames().get(0), StandardCharsets.UTF_8)).isEqualTo("foo"); assertThat(dbi.getNameAsString()).isEqualTo("foo"); assertThat(dbi.getNameAsString(StandardCharsets.UTF_8)).isEqualTo("foo"); } @Test public void named2() { final Dbi<ByteBuffer> dbi = env.createDbi() .setDbName("foo".getBytes(StandardCharsets.US_ASCII)) .withDefaultComparator() .setDbiFlags(DbiFlags.MDB_CREATE) .open(); assertPutAndGet(dbi); assertThat(env.getDbiNames()).hasSize(1); assertThat(new String(env.getDbiNames().get(0), StandardCharsets.US_ASCII)).isEqualTo("foo"); assertThat(dbi.getNameAsString()).isEqualTo("foo"); assertThat(dbi.getNameAsString(StandardCharsets.US_ASCII)).isEqualTo("foo"); } @Test public void nativeComparator() { final Dbi<ByteBuffer> dbi = env.createDbi() .setDbName("foo") .withNativeComparator() .addDbiFlags(DbiFlags.MDB_CREATE) .open(); assertPutAndGet(dbi); assertThat(env.getDbiNames()).hasSize(1); } @Test public void callback() { final Comparator<ByteBuffer> proxyOptimal = ByteBufferProxy.PROXY_OPTIMAL.getComparator(); // Compare on key length, falling back to default final Comparator<ByteBuffer> comparator = (o1, o2) -> { final int res = Integer.compare(o1.remaining(), o2.remaining()); if (res == 0) { return proxyOptimal.compare(o1, o2); } else { return res; } }; final Dbi<ByteBuffer> dbi = env.createDbi() .setDbName("foo") .withCallbackComparator(ignored -> comparator) .addDbiFlags(DbiFlags.MDB_CREATE) .open(); TestUtils.doWithWriteTxn( env, txn -> { dbi.put(txn, bb("fox"), bb("val_1")); dbi.put(txn, bb("rabbit"), bb("val_2")); dbi.put(txn, bb("deer"), bb("val_3")); dbi.put(txn, bb("badger"), bb("val_4")); txn.commit(); }); final List<String> keys = new ArrayList<>(); TestUtils.doWithReadTxn( env, txn -> { try (CursorIterable<ByteBuffer> cursorIterable = dbi.iterate(txn)) { final Iterator<CursorIterable.KeyVal<ByteBuffer>> iterator = cursorIterable.iterator(); iterator.forEachRemaining( keyVal -> { keys.add(getString(keyVal.key())); }); } }); assertThat(keys).containsExactly("fox", "deer", "badger", "rabbit"); } @Test public void flags() { final Dbi<ByteBuffer> dbi = env.createDbi() .setDbName("foo") .withDefaultComparator() .setDbiFlags(DbiFlags.MDB_DUPSORT, DbiFlags.MDB_DUPFIXED) // Will get overwritten .setDbiFlags() // clear them .setDbiFlags(Collections.singletonList(DbiFlags.MDB_INTEGERDUP)) .setDbiFlags() // clear them .addDbiFlags(DbiFlags.MDB_CREATE) // Not a dbi flag as far as lmdb is concerned. .addDbiFlags(DbiFlags.MDB_INTEGERKEY) .addDbiFlags(DbiFlags.MDB_REVERSEKEY) .open(); assertPutAndGet(dbi); assertThat(env.getDbiNames()).hasSize(1); assertThat(new String(env.getDbiNames().get(0), StandardCharsets.UTF_8)).isEqualTo("foo"); TestUtils.doWithReadTxn( env, readTxn -> { assertThat(dbi.listFlags(readTxn)) .containsExactlyInAnyOrder(DbiFlags.MDB_INTEGERKEY, DbiFlags.MDB_REVERSEKEY); }); } private void assertPutAndGet(Dbi<ByteBuffer> dbi) { try (Txn<ByteBuffer> writeTxn = env.txnWrite()) { dbi.put(writeTxn, bb(123), bb(123_000)); writeTxn.commit(); } try (Txn<ByteBuffer> readTxn = env.txnRead()) { final ByteBuffer byteBuffer = dbi.get(readTxn, bb(123)); assertThat(byteBuffer).isNotNull(); final int val = byteBuffer.getInt(); assertThat(val).isEqualTo(123_000); } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |