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/main/java/org/lmdbjava/Txn.java at master · lmdbjava/lmdbjava Expand file tree/ Copy pathTxn.javaMore file actions 351 lines (297 loc) · 8.94 KB / Copy pathTxn.javaFile metadata and controls351 lines (297 loc) · 8.94 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 /* * 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 jnr.ffi.Memory.allocateDirect; import static jnr.ffi.NativeType.ADDRESS; import static org.lmdbjava.Env.SHOULD_CHECK; import static org.lmdbjava.Library.LIB; import static org.lmdbjava.Library.RUNTIME; import static org.lmdbjava.ResultCodeMapper.checkRc; import static org.lmdbjava.Txn.State.DONE; import static org.lmdbjava.Txn.State.READY; import static org.lmdbjava.Txn.State.RELEASED; import static org.lmdbjava.Txn.State.RESET; import static org.lmdbjava.TxnFlags.MDB_RDONLY_TXN; import java.util.Objects; import jnr.ffi.Pointer; /** * LMDB transaction. * * @param <T> buffer type */ public final class Txn<T> implements AutoCloseable { private final KeyVal<T> keyVal; private final Txn<T> parent; private final BufferProxy<T> proxy; private final Pointer ptr; private final boolean readOnly; private final Env<T> env; private State state; Txn(final Env<T> env, final Txn<T> parent, final BufferProxy<T> proxy, final TxnFlagSet flags) { if (SHOULD_CHECK) { Objects.requireNonNull(flags); } this.proxy = proxy; this.keyVal = proxy.keyVal(); this.readOnly = flags.isSet(MDB_RDONLY_TXN); if (env.isReadOnly() && !this.readOnly) { throw new EnvIsReadOnly(); } this.env = env; this.parent = parent; if (parent != null && parent.isReadOnly() != this.readOnly) { throw new IncompatibleParent(); } final Pointer txnPtr = allocateDirect(RUNTIME, ADDRESS); final Pointer txnParentPtr = parent == null ? null : parent.ptr; checkRc(LIB.mdb_txn_begin(env.pointer(), txnParentPtr, flags.getMask(), txnPtr)); ptr = txnPtr.getPointer(0); state = READY; } /** Aborts this transaction. */ public void abort() { if (SHOULD_CHECK) { env.checkNotClosed(); } checkReady(); state = DONE; LIB.mdb_txn_abort(ptr); } /** * Closes this transaction by aborting if not already committed. * * <p>Closing the transaction will invoke {@link BufferProxy#deallocate(java.lang.Object)} for * each read-only buffer (ie the key and value). */ @Override public void close() { if (SHOULD_CHECK) { env.checkNotClosed(); } if (state == RELEASED) { return; } if (state == READY) { LIB.mdb_txn_abort(ptr); } keyVal.close(); state = RELEASED; } /** Commits this transaction. */ public void commit() { if (SHOULD_CHECK) { env.checkNotClosed(); } checkReady(); state = DONE; checkRc(LIB.mdb_txn_commit(ptr)); } /** * Return the transaction's ID. * * @return A transaction ID, valid if input is an active transaction */ public long getId() { if (SHOULD_CHECK) { env.checkNotClosed(); } return LIB.mdb_txn_id(ptr); } /** * Obtains this transaction's parent. * * @return the parent transaction (may be null) */ public Txn<T> getParent() { return parent; } /** * Whether this transaction is read-only. * * @return if read-only */ public boolean isReadOnly() { return readOnly; } /** * Fetch the buffer which holds a read-only view of the LMDI allocated memory. Any use of this * buffer must comply with the standard LMDB C "mdb_get" contract (ie do not modify, do not * attempt to release the memory, do not use once the transaction or cursor closes, do not use * after a write etc). * * @return the key buffer (never null) */ public T key() { return keyVal.key(); } /** Renews a read-only transaction previously released by {@link #reset()}. */ public void renew() { if (SHOULD_CHECK) { env.checkNotClosed(); } if (state != RESET) { throw new NotResetException(); } state = DONE; checkRc(LIB.mdb_txn_renew(ptr)); state = READY; } /** * Aborts this read-only transaction and resets the transaction handle, so it can be reused upon * calling {@link #renew()}. */ public void reset() { if (SHOULD_CHECK) { env.checkNotClosed(); } checkReadOnly(); if (state != READY && state != DONE) { throw new ResetException(); } state = RESET; LIB.mdb_txn_reset(ptr); } /** * Fetch the buffer which holds a read-only view of the LMDI allocated memory. Any use of this * buffer must comply with the standard LMDB C "mdb_get" contract (ie do not modify, do not * attempt to release the memory, do not use once the transaction or cursor closes, do not use * after a write etc). * * @return the value buffer (never null) */ public T val() { return keyVal.val(); } void checkReadOnly() { if (!readOnly) { throw new ReadOnlyRequiredException(); } } void checkReady() { if (state != READY) { throw new NotReadyException(); } } void checkWritesAllowed() { if (readOnly) { throw new ReadWriteRequiredException(); } } /** * Return the state of the transaction. * * @return the state */ State getState() { return state; } KeyVal<T> kv() { return keyVal; } KeyVal<T> newKeyVal() { return proxy.keyVal(); } Pointer pointer() { return ptr; } /** Transaction must abort, has a child, or is invalid. */ public static final class BadException extends LmdbNativeException { static final int MDB_BAD_TXN = -30_782; private static final long serialVersionUID = 1L; BadException() { super(MDB_BAD_TXN, "Transaction must abort, has a child, or is invalid"); } } /** Invalid reuse of reader locktable slot. */ public static final class BadReaderLockException extends LmdbNativeException { static final int MDB_BAD_RSLOT = -30_783; private static final long serialVersionUID = 1L; BadReaderLockException() { super(MDB_BAD_RSLOT, "Invalid reuse of reader locktable slot"); } } /** The proposed R-W transaction is incompatible with a R-O Env. */ public static class EnvIsReadOnly extends LmdbException { private static final long serialVersionUID = 1L; /** Creates a new instance. */ public EnvIsReadOnly() { super("Read-write Txn incompatible with read-only Env"); } } /** The proposed transaction is incompatible with its parent transaction. */ public static class IncompatibleParent extends LmdbException { private static final long serialVersionUID = 1L; /** Creates a new instance. */ public IncompatibleParent() { super("Transaction incompatible with its parent transaction"); } } /** Transaction is not in a READY state. */ public static final class NotReadyException extends LmdbException { private static final long serialVersionUID = 1L; /** Creates a new instance. */ public NotReadyException() { super("Transaction is not in ready state"); } } /** The current transaction has not been reset. */ public static class NotResetException extends LmdbException { private static final long serialVersionUID = 1L; /** Creates a new instance. */ public NotResetException() { super("Transaction has not been reset"); } } /** The current transaction is not a read-only transaction. */ public static class ReadOnlyRequiredException extends LmdbException { private static final long serialVersionUID = 1L; /** Creates a new instance. */ public ReadOnlyRequiredException() { super("Not a read-only transaction"); } } /** The current transaction is not a read-write transaction. */ public static class ReadWriteRequiredException extends LmdbException { private static final long serialVersionUID = 1L; /** Creates a new instance. */ public ReadWriteRequiredException() { super("Not a read-write transaction"); } } /** The current transaction has already been reset. */ public static class ResetException extends LmdbException { private static final long serialVersionUID = 1L; /** Creates a new instance. */ public ResetException() { super("Transaction has already been reset"); } } /** Transaction has too many dirty pages. */ public static final class TxFullException extends LmdbNativeException { static final int MDB_TXN_FULL = -30_788; private static final long serialVersionUID = 1L; TxFullException() { super(MDB_TXN_FULL, "Transaction has too many dirty pages"); } } /** Transaction states. */ enum State { READY, DONE, RESET, RELEASED } } |
| You are visitor 01402520 — 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. |