Represents a transaction.
Inheritance: IDisposable
Ejemplo n.º 1
0
        /// <summary>
        /// Creates new instance of LightningCursor
        /// </summary>
        /// <param name="db">Database</param>
        /// <param name="txn">Transaction</param>
        internal LightningCursor(LightningDatabase db, LightningTransaction txn)
        {
            if (db == null)
            {
                throw new ArgumentNullException(nameof(db));
            }

            if (txn == null)
            {
                throw new ArgumentNullException(nameof(txn));
            }

            _currentWithOptimizedPair = () => _currentPair;
            _currentDefault           = () =>
            {
                if (_currentKeyStructure.size == IntPtr.Zero)
                {
                    return(default(KeyValuePair <byte[], byte[]>));
                }

                return(new KeyValuePair <byte[], byte[]>(_currentKeyStructure.GetBytes(),
                                                         _currentValueStructure.GetBytes()));
            };

            _getCurrent = _currentDefault;

            mdb_cursor_open(txn.Handle(), db.Handle(), out _handle);

            Database               = db;
            Transaction            = txn;
            Transaction.Disposing += Dispose;
        }
        public static void Put <TKey, TValue>(this LightningTransaction txn, LightningDatabase db, TKey key, TValue value, PutOptions options = PutOptions.None)
        {
            var keyBytes   = db.ToBytes(key);
            var valueBytes = db.ToBytes(value);

            txn.Put(db, keyBytes, valueBytes, options);
        }
        public static void Delete <TKey, TValue>(this LightningTransaction txn, LightningDatabase db, TKey key, TValue value)
        {
            var keyBytes   = db.ToBytes(key);
            var valueBytes = db.ToBytes(value);

            txn.Delete(db, keyBytes, valueBytes);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Created new instance of LightningTransaction
        /// </summary>
        /// <param name="environment">Environment.</param>
        /// <param name="parent">Parent transaction or null.</param>
        /// <param name="flags">Transaction open options.</param>
        public LightningTransaction(LightningEnvironment environment, LightningTransaction parent, TransactionBeginFlags flags)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }

            this.Environment       = environment;
            this.ParentTransaction = parent;
            this.IsReadOnly        = flags == TransactionBeginFlags.ReadOnly;

            var parentHandle = parent != null
                ? parent._handle
                : IntPtr.Zero;

            IntPtr handle = default(IntPtr);

            NativeMethods.Execute(lib => lib.mdb_txn_begin(environment._handle, parentHandle, flags, out handle));

            _handle = handle;

            this.State = LightningTransactionState.Active;

            if (parent == null)
            {
                this.Environment.Closing += EnvironmentOrParentTransactionClosing;
            }
            else
            {
                parent.Closing += EnvironmentOrParentTransactionClosing;
            }
        }
 public MerkleTreePruningCursor(UInt256 blockHash, LightningTransaction txn, LightningDatabase db, LightningCursor cursor)
 {
     this.blockHash = blockHash;
     this.db = db;
     this.txn = txn;
     this.cursor = cursor;
 }
        public LightningTransaction(LightningEnvironment environment, LightningTransaction parent, TransactionBeginFlags flags)
        {
            if (environment == null)
                throw new ArgumentNullException("environment");

            this.Environment = environment;
            this.ParentTransaction = parent;
            this.IsReadOnly = flags == TransactionBeginFlags.ReadOnly;
            
            var parentHandle = parent != null
                ? parent._handle
                : IntPtr.Zero;

            IntPtr handle = default(IntPtr);
            Native.Execute(() => Native.mdb_txn_begin(environment._handle, parentHandle, flags, out handle));

            _handle = handle;

            this.State = LightningTransacrionState.Active;

            if (parent == null)
                this.Environment.Closing += EnvironmentOrParentTransactionClosing;
            else
                parent.Closing += EnvironmentOrParentTransactionClosing;
        }
        public static TValue Get <TKey, TValue>(this LightningTransaction txn, LightningDatabase db, TKey key)
        {
            var keyBytes   = db.ToBytes(key);
            var valueBytes = txn.Get(db, keyBytes);

            return(db.FromBytes <TValue>(valueBytes));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates a LightningDatabase instance.
        /// </summary>
        /// <param name="name">Database name.</param>
        /// <param name="flags">Database open flags/</param>
        /// <param name="tran">Active transaction.</param>
        /// <param name="encoding">Default strings encoding.</param>
        public LightningDatabase(string name, LightningTransaction tran, DatabaseOpenFlags?flags, Encoding encoding = null)
        {
            if (tran == null)
            {
                throw new ArgumentNullException("tran");
            }

            if (!flags.HasValue)
            {
                flags = LightningConfig.Database.DefaultOpenFlags;
            }

            encoding = encoding ?? LightningConfig.Database.DefaultEncoding;

            UInt32 handle = default(UInt32);

            NativeMethods.Execute(lib => lib.mdb_dbi_open(tran._handle, name, flags.Value, out handle));

            _name = name ?? DefaultDatabaseName;

            _handle        = handle;
            _shouldDispose = true;

            this.IsOpened    = true;
            this.Encoding    = encoding;
            this.OpenFlags   = flags.Value;
            this.Environment = tran.Environment;
        }
Ejemplo n.º 9
0
        public LightningCursor(LightningDatabase db, LightningTransaction txn)
        {
            if (db == null)
                throw new ArgumentNullException("db");

            if (txn == null)
                throw new ArgumentNullException("txn");

            if (db.Environment != txn.Environment)
                throw new ArgumentException("db and txn belong to different environments");

            IntPtr handle = default(IntPtr);
            Native.Execute(lib => lib.mdb_cursor_open(txn._handle, db._handle, out handle));

            _handle = handle;

            this.Database = db;
            this.Transaction = txn;

            _shouldDispose = true;

            if (txn.IsReadOnly)
                this.Environment.Closing += EnvironmentOrTransactionClosing;
            else
                this.Transaction.Closing += EnvironmentOrTransactionClosing;
        }
Ejemplo n.º 10
0
 internal void SetComparer(LightningTransaction tran, LightningDatabase db)
 {
     SetNativeCompareFunction(
         tran,
         db,
         Compare,
         (lib, func) => lib.mdb_set_compare(tran._handle, db._handle, func));
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Drops the database.
        /// </summary>
        public MDBResultCode Drop(LightningTransaction transaction)
        {
            var result = mdb_drop(transaction.Handle(), _handle, true);

            IsOpened = false;
            _handle  = default;
            return(result);
        }
        /// <summary>
        /// Create a transaction for use with the environment.
        /// The transaction handle may be discarded using Abort() or Commit().
        /// Note:
        /// Transactions may not span threads; a transaction must only be used by a single thread. Also, a thread may only have a single transaction.
        /// Cursors may not span transactions; each cursor must be opened and closed within a single transaction.
        /// </summary>
        /// <param name="parent">
        /// If this parameter is non-NULL, the new transaction will be a nested transaction, with the transaction indicated by parent as its parent.
        /// Transactions may be nested to any level.
        /// A parent transaction may not issue any other operations besides BeginTransaction, Abort, or Commit while it has active child transactions.
        /// </param>
        /// <param name="beginFlags">
        /// Special options for this transaction.
        /// </param>
        /// <returns>
        /// New LightningTransaction
        /// </returns>
        public LightningTransaction BeginTransaction(LightningTransaction parent = null, TransactionBeginFlags beginFlags = LightningTransaction.DefaultTransactionBeginFlags)
        {
            if (!IsOpened)
            {
                throw new InvalidOperationException("Environment must be opened before starting a transaction");
            }

            return(new LightningTransaction(this, parent, beginFlags));
        }
Ejemplo n.º 13
0
 public static bool TryGet(this LightningTransaction tx, LightningDatabase db, ReadOnlySpan <byte> key, out byte[] value)
 {
     var(resultCode, _, mdbValue) = tx.Get(db, key);
     if (resultCode == MDBResultCode.Success)
     {
         value = mdbValue.CopyToNewArray();
         return(true);
     }
     value = default;
     return(false);
 }
 internal void ConfigureDatabase(LightningTransaction tx, LightningDatabase db)
 {
     if (_comparer != null)
     {
         mdb_set_compare(tx.Handle(), db.Handle(), Compare);
     }
     if (_duplicatesComparer != null)
     {
         mdb_set_dupsort(tx.Handle(), db.Handle(), IsDuplicate);
     }
 }
Ejemplo n.º 15
0
        internal void SetDuplicatesSort(LightningTransaction tran, LightningDatabase db)
        {
            if (!db.OpenFlags.HasFlag(DatabaseOpenFlags.DuplicatesSort))
                return;

            SetNativeCompareFunction(
                tran,
                db,
                DuplicatesSort,
                (lib, func) => lib.mdb_set_dupsort(tran._handle, db._handle, func));
        }
        public static bool TryGet <TKey, TValue>(this LightningTransaction txn, LightningDatabase db, TKey key, out TValue value)
        {
            GetByOperation operation;
            var            result = txn.TryGetBy(db, key, out operation);

            value = result
                ? operation.Value <TValue>()
                : default(TValue);

            return(result);
        }
        public static bool TryGetBy <TKey>(this LightningTransaction txn, LightningDatabase db, TKey key, out GetByOperation value)
        {
            byte[] valueBytes;

            var keyBytes = db.ToBytes(key);
            var result   = txn.TryGet(db, keyBytes, out valueBytes);

            value = result
                ? new GetByOperation(db, valueBytes)
                : null;

            return(result);
        }
Ejemplo n.º 18
0
        //TODO: tests
        /// <summary>
        /// Renew a cursor handle.
        /// Cursors are associated with a specific transaction and database and may not span threads.
        /// Cursors that are only used in read-only transactions may be re-used, to avoid unnecessary malloc/free overhead.
        /// The cursor may be associated with a new read-only transaction, and referencing the same database handle as it was created with.
        /// </summary>
        /// <param name="txn">Transaction to renew in.</param>
        public void Renew(LightningTransaction txn)
        {
            if (txn == null)
            {
                throw new ArgumentNullException(nameof(txn));
            }

            if (!txn.IsReadOnly)
            {
                throw new InvalidOperationException("Can't renew cursor on non-readonly transaction");
            }

            mdb_cursor_renew(txn.Handle(), _handle);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Created new instance of LightningTransaction
        /// </summary>
        /// <param name="environment">Environment.</param>
        /// <param name="parent">Parent transaction or null.</param>
        /// <param name="flags">Transaction open options.</param>
        internal LightningTransaction(LightningEnvironment environment, IntPtr handle, LightningTransaction parent, TransactionBeginFlags flags)
        {
            if (environment == null)
                throw new ArgumentNullException("environment");

            this.Environment = environment;
            this.ParentTransaction = parent;
            this.IsReadOnly = flags == TransactionBeginFlags.ReadOnly;
            this.State = LightningTransactionState.Active;

            _handle = handle;
            _subTransactionsManager = new TransactionManager(environment, this);
            _cursorManager = new CursorManager(this);
        }
Ejemplo n.º 20
0
        //TODO: tests
        public void Renew(LightningTransaction txn)
        {
            if (txn == null)
            {
                throw new ArgumentNullException("txn");
            }

            if (!txn.IsReadOnly)
            {
                throw new InvalidOperationException("Can't renew cursor on non-readonly transaction");
            }

            NativeMethods.Execute(lib => lib.mdb_cursor_renew(txn._handle, _handle));
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Creates a LightningDatabase instance.
        /// </summary>
        /// <param name="name">Database name.</param>
        /// <param name="transaction">Active transaction.</param>
        /// <param name="configuration">Options for the database, like encoding, option flags, and comparison logic.</param>
        internal LightningDatabase(string name, LightningTransaction transaction, DatabaseConfiguration configuration)
        {
            if (transaction == null)
                throw new ArgumentNullException(nameof(transaction));

            if (configuration == null)
                throw new ArgumentNullException(nameof(configuration));

            Name = name;
            _configuration = configuration;
            Environment = transaction.Environment;
            Environment.Disposing += Dispose;
            mdb_dbi_open(transaction.Handle(), name, _configuration.Flags, out _handle);
            _configuration.ConfigureDatabase(transaction, this);
            IsOpened = true;
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Creates a LightningDatabase instance.
        /// </summary>
        /// <param name="name">Database name.</param>
        /// <param name="transaction">Active transaction.</param>
        /// <param name="configuration">Options for the database, like encoding, option flags, and comparison logic.</param>
        internal LightningDatabase(string name, LightningTransaction transaction, DatabaseConfiguration configuration)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            Name                   = name;
            _configuration         = configuration ?? throw new ArgumentNullException(nameof(configuration));
            Environment            = transaction.Environment;
            _transaction           = transaction;
            Environment.Disposing += Dispose;
            mdb_dbi_open(transaction.Handle(), name, _configuration.Flags, out _handle).ThrowOnError();
            _pinnedConfig = _configuration.ConfigureDatabase(transaction, this);
            IsOpened      = true;
        }
Ejemplo n.º 23
0
        public static bool TryGet(this LightningTransaction tx, LightningDatabase db, ReadOnlySpan <byte> key, byte[] destinationValueBuffer)
        {
            var(resultCode, _, mdbValue) = tx.Get(db, key);
            if (resultCode != MDBResultCode.Success)
            {
                return(false);
            }

            var valueSpan = mdbValue.AsSpan();

            if (valueSpan.TryCopyTo(destinationValueBuffer))
            {
                return(true);
            }
            throw new LightningException("Incorrect buffer size given in destinationValueBuffer", (int)MDBResultCode.BadValSize);
        }
 internal IDisposable ConfigureDatabase(LightningTransaction tx, LightningDatabase db)
 {
     var pinnedComparer = new ComparerKeepAlive();
     if (_comparer != null)
     {
         CompareFunction compare = Compare;
         pinnedComparer.AddComparer(compare);
         mdb_set_compare(tx.Handle(), db.Handle(), compare);
     }
     if (_duplicatesComparer != null)
     {
         CompareFunction dupCompare = IsDuplicate;
         pinnedComparer.AddComparer(dupCompare);
         mdb_set_dupsort(tx.Handle(), db.Handle(), dupCompare);
     }
     return pinnedComparer;
 }
Ejemplo n.º 25
0
        /// <summary>
        /// Creates new instance of LightningCursor
        /// </summary>
        /// <param name="db">Database</param>
        /// <param name="txn">Transaction</param>
        internal LightningCursor(LightningDatabase db, LightningTransaction txn)
        {
            if (db == null)
            {
                throw new ArgumentNullException(nameof(db));
            }

            if (txn == null)
            {
                throw new ArgumentNullException(nameof(txn));
            }

            mdb_cursor_open(txn.Handle(), db.Handle(), out _handle).ThrowOnError();

            Transaction            = txn;
            Transaction.Disposing += Dispose;
        }
Ejemplo n.º 26
0
        private static void SetNativeCompareFunction(
            LightningTransaction tran, 
            LightningDatabase db,
            Func<CompareFunctionBuilder, LightningCompareDelegate> delegateFactory,
            Func<INativeLibraryFacade, CompareFunction, int> setter)
        {
            if (delegateFactory == null)
                return;

            var comparer = delegateFactory.Invoke(new CompareFunctionBuilder());
            if (comparer == null)
                return;

            var compareFunction = CreateNativeCompareFunction(db, comparer);

            NativeMethods.Execute(lib => setter.Invoke(lib, compareFunction));
            tran.SubTransactionsManager.StoreCompareFunction(compareFunction);
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Created new instance of LightningTransaction
        /// </summary>
        /// <param name="environment">Environment.</param>
        /// <param name="parent">Parent transaction or null.</param>
        /// <param name="flags">Transaction open options.</param>
        internal LightningTransaction(LightningEnvironment environment, LightningTransaction parent, TransactionBeginFlags flags)
        {
            Environment            = environment ?? throw new ArgumentNullException(nameof(environment));
            ParentTransaction      = parent;
            IsReadOnly             = flags == TransactionBeginFlags.ReadOnly;
            State                  = LightningTransactionState.Active;
            Environment.Disposing += Dispose;
            if (parent != null)
            {
                parent.Disposing     += Dispose;
                parent.StateChanging += OnParentStateChanging;
            }

            var parentHandle = parent?.Handle() ?? IntPtr.Zero;

            mdb_txn_begin(environment.Handle(), parentHandle, flags, out _handle).ThrowOnError();
            _originalHandle = _handle;
        }
Ejemplo n.º 28
0
        public LightningDatabase(string name, DatabaseOpenFlags flags, LightningTransaction tran, Encoding encoding)
        {
            if (tran == null)
                throw new ArgumentNullException("tran");

            UInt32 handle = default(UInt32);
            NativeMethods.Execute(lib => lib.mdb_dbi_open(tran._handle, name, flags, out handle));

            _name = name ?? DefaultDatabaseName;

            _handle = handle;
            _shouldDispose = true;
                        
            this.IsOpened = true;
            this.Encoding = encoding;
            this.OpenFlags = flags;
            this.Environment = tran.Environment;
        }
Ejemplo n.º 29
0
        internal IDisposable ConfigureDatabase(LightningTransaction tx, LightningDatabase db)
        {
            var pinnedComparer = new ComparerKeepAlive();

            if (_comparer != null)
            {
                CompareFunction compare = Compare;
                pinnedComparer.AddComparer(compare);
                mdb_set_compare(tx.Handle(), db.Handle(), compare);
            }
            if (_duplicatesComparer != null)
            {
                CompareFunction dupCompare = IsDuplicate;
                pinnedComparer.AddComparer(dupCompare);
                mdb_set_dupsort(tx.Handle(), db.Handle(), dupCompare);
            }
            return(pinnedComparer);
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Creates a LightningDatabase instance.
        /// </summary>
        /// <param name="name">Database name.</param>
        /// <param name="flags">Database open flags/</param>
        /// <param name="tran">Active transaction.</param>
        /// <param name="encoding">Default strings encoding.</param>
        internal LightningDatabase(string name, LightningTransaction tran, DatabaseHandleCacheEntry entry, Encoding encoding)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            if (tran == null)
                throw new ArgumentNullException("tran");

            if (encoding == null)
                throw new ArgumentNullException("encoding");

            _name = name;
            _handle = entry.Handle;
            _shouldDispose = true;
                        
            this.Encoding = encoding;
            this.OpenFlags = entry.OpenFlags;
            this.Environment = tran.Environment;
        }
        /// <summary>
        /// Created new instance of LightningTransaction
        /// </summary>
        /// <param name="environment">Environment.</param>
        /// <param name="parent">Parent transaction or null.</param>
        /// <param name="flags">Transaction open options.</param>
        internal LightningTransaction(LightningEnvironment environment, LightningTransaction parent, TransactionBeginFlags flags)
        {
            if (environment == null)
                throw new ArgumentNullException(nameof(environment));

            Environment = environment;
            ParentTransaction = parent;
            IsReadOnly = (flags & TransactionBeginFlags.ReadOnly) == TransactionBeginFlags.ReadOnly;
            State = LightningTransactionState.Active;
            Environment.Disposing += Dispose;
            if (parent != null)
            {
                parent.Disposing += Dispose;
                parent.StateChanging += OnParentStateChanging;
            }

            var parentHandle = parent?.Handle() ?? IntPtr.Zero;
            mdb_txn_begin(environment.Handle(), parentHandle, flags, out _handle);
            _originalHandle = _handle;
        }
Ejemplo n.º 32
0
        public LightningDatabase(string name, DatabaseOpenFlags flags, LightningTransaction tran, Encoding encoding)
        {
            if (tran == null)
            {
                throw new ArgumentNullException("tran");
            }

            UInt32 handle = default(UInt32);

            Native.Execute(() => Native.mdb_dbi_open(tran._handle, name, flags, out handle));

            _name = name ?? DefaultDatabaseName;

            _handle        = handle;
            _shouldDispose = true;

            this.IsOpened    = true;
            this.Encoding    = encoding;
            this.OpenFlags   = flags;
            this.Environment = tran.Environment;
        }
Ejemplo n.º 33
0
        //TODO: Upgrade db flags?
        internal LightningDatabase OpenDatabase(string name, LightningTransaction tran, DatabaseOpenFlags?flags, Encoding encoding)
        {
            var internalName = name ?? LightningDatabase.DefaultDatabaseName;
            var db           = _openedDatabases.GetOrAdd(internalName, n =>
            {
                var ldb = new LightningDatabase(name, tran, flags, encoding);
                _databasesForReuse.Add(ldb._handle);

                return(ldb);
            });

            if (db.OpenFlags != flags.GetValueOrDefault())
            {
                throw new InvalidOperationException("Database " + internalName + " already opened with different flags");
            }

            if (db.Encoding != (encoding ?? LightningConfig.Database.DefaultEncoding))
            {
                throw new InvalidOperationException("Can not change encoding of already opened database");
            }

            return(db);
        }
Ejemplo n.º 34
0
        public LightningCursor(LightningDatabase db, LightningTransaction txn)
        {
            if (db == null)
            {
                throw new ArgumentNullException("db");
            }

            if (txn == null)
            {
                throw new ArgumentNullException("txn");
            }

            if (db.Environment != txn.Environment)
            {
                throw new ArgumentException("db and txn belong to different environments");
            }

            IntPtr handle = default(IntPtr);

            NativeMethods.Execute(lib => lib.mdb_cursor_open(txn._handle, db._handle, out handle));

            _handle = handle;

            this.Database    = db;
            this.Transaction = txn;

            _shouldDispose = true;

            if (txn.IsReadOnly)
            {
                this.Environment.Closing += EnvironmentOrTransactionClosing;
            }
            else
            {
                this.Transaction.Closing += EnvironmentOrTransactionClosing;
            }
        }
Ejemplo n.º 35
0
 public LightningDatabase(string name, DatabaseOpenFlags flags, LightningTransaction tran)
     : this(name, flags, tran, Encoding.UTF8)
 {
 }
 public static TType Get <TType>(this LightningTransaction txn, LightningDatabase db, TType key)
 {
     return(txn.Get <TType, TType>(db, key));
 }
Ejemplo n.º 37
0
        public LightningTransaction BeginTransaction(LightningTransaction parent, TransactionBeginFlags beginFlags)
        {
            this.EnsureOpened();

            return new LightningTransaction(this, parent, beginFlags);
        }
Ejemplo n.º 38
0
 public LightningDatabase(string name, DatabaseOpenFlags flags, LightningTransaction tran)
     : this (name, flags, tran, Encoding.UTF8)
 {
 }
Ejemplo n.º 39
0
        public void BeginTransaction(bool readOnly, bool pruneOnly)
        {
            if (this.txn != null)
                throw new InvalidOperationException();

            this.txn = this.jetInstance.BeginTransaction(readOnly ? TransactionBeginFlags.ReadOnly : TransactionBeginFlags.None);
        }
Ejemplo n.º 40
0
 public void Dispose()
 {
     if (this.txn != null)
     {
         this.txn.Dispose();
         this.txn = null;
     }
 }
        public static GetByOperation GetBy <TKey>(this LightningTransaction txn, LightningDatabase db, TKey key)
        {
            var valueBytes = txn.GetRawValue(db, key);

            return(new GetByOperation(db, valueBytes));
        }
        public static void Delete <TKey>(this LightningTransaction txn, LightningDatabase db, TKey key)
        {
            var keyBytes = db.ToBytes(key);

            txn.Delete(db, keyBytes);
        }
 /// <summary>
 /// Create a transaction for use with the environment.
 /// The transaction handle may be discarded using Abort() or Commit().
 /// Note:
 /// Transactions may not span threads; a transaction must only be used by a single thread. Also, a thread may only have a single transaction.
 /// Cursors may not span transactions; each cursor must be opened and closed within a single transaction.
 /// </summary>
 /// <param name="parent">
 /// If this parameter is non-NULL, the new transaction will be a nested transaction, with the transaction indicated by parent as its parent. 
 /// Transactions may be nested to any level. 
 /// A parent transaction may not issue any other operations besides BeginTransaction, Abort, or Commit while it has active child transactions.
 /// </param>
 /// <param name="beginFlags">
 /// Special options for this transaction. 
 /// </param>
 /// <returns>
 /// New LightningTransaction
 /// </returns>
 public LightningTransaction BeginTransaction(LightningTransaction parent, TransactionBeginFlags beginFlags)
 {
     return new LightningTransaction(this, parent, beginFlags);
 }
        public static byte[] GetRawValue <TKey>(this LightningTransaction txn, LightningDatabase db, TKey key)
        {
            var keyBytes = db.ToBytes(key);

            return(txn.Get(db, keyBytes));
        }
Ejemplo n.º 45
0
        public void RollbackTransaction()
        {
            CheckTransaction();

            this.txn.Abort();
            this.txn.Dispose();
            this.txn = null;
        }
Ejemplo n.º 46
0
        //TODO: tests
        public void Renew(LightningTransaction txn)
        {
            if(txn == null)
                throw new ArgumentNullException("txn");

            if (!txn.IsReadOnly)
                throw new InvalidOperationException("Can't renew cursor on non-readonly transaction");

            Native.Execute(lib => lib.mdb_cursor_renew(txn._handle, _handle));
        }
Ejemplo n.º 47
0
 /// <summary>
 /// Truncates all data from the database.
 /// </summary>
 public void Truncate(LightningTransaction transaction)
 {
     mdb_drop(transaction.Handle(), _handle, false);
 }
Ejemplo n.º 48
0
        public void CommitTransaction()
        {
            CheckTransaction();

            this.txn.Commit();
            this.txn.Dispose();
            this.txn = null;
        }
Ejemplo n.º 49
0
        /// <summary>
        /// Create a transaction for use with the environment.
        /// The transaction handle may be discarded using Abort() or Commit().
        /// Note:
        /// Transactions may not span threads; a transaction must only be used by a single thread. Also, a thread may only have a single transaction.
        /// Cursors may not span transactions; each cursor must be opened and closed within a single transaction.
        /// </summary>
        /// <param name="parent">
        /// If this parameter is non-NULL, the new transaction will be a nested transaction, with the transaction indicated by parent as its parent.
        /// Transactions may be nested to any level.
        /// A parent transaction may not issue any other operations besides BeginTransaction, Abort, or Commit while it has active child transactions.
        /// </param>
        /// <param name="beginFlags">
        /// Special options for this transaction.
        /// </param>
        /// <returns>
        /// New LightningTransaction
        /// </returns>
        public LightningTransaction BeginTransaction(LightningTransaction parent, TransactionBeginFlags beginFlags)
        {
            this.EnsureOpened();

            return(new LightningTransaction(this, parent, beginFlags));
        }
Ejemplo n.º 50
0
 /// <summary>
 /// Truncates all data from the database.
 /// </summary>
 public MDBResultCode Truncate(LightningTransaction transaction)
 {
     return(mdb_drop(transaction.Handle(), _handle, false));
 }
Ejemplo n.º 51
0
 /// <summary>
 /// Drops the database.
 /// </summary>
 public void Drop(LightningTransaction transaction)
 {
     mdb_drop(transaction.Handle(), _handle, true);
     IsOpened = false;
     _handle = default(uint);
 }
        /// <summary>
        /// Create a transaction for use with the environment.
        /// The transaction handle may be discarded using Abort() or Commit().
        /// Note:
        /// Transactions may not span threads; a transaction must only be used by a single thread. Also, a thread may only have a single transaction.
        /// Cursors may not span transactions; each cursor must be opened and closed within a single transaction.
        /// </summary>
        /// <param name="parent">
        /// If this parameter is non-NULL, the new transaction will be a nested transaction, with the transaction indicated by parent as its parent. 
        /// Transactions may be nested to any level. 
        /// A parent transaction may not issue any other operations besides BeginTransaction, Abort, or Commit while it has active child transactions.
        /// </param>
        /// <param name="beginFlags">
        /// Special options for this transaction. 
        /// </param>
        /// <returns>
        /// New LightningTransaction
        /// </returns>
        public LightningTransaction BeginTransaction(LightningTransaction parent, TransactionBeginFlags beginFlags)
        {
            if (!IsOpened)
                throw new InvalidOperationException("Environment must be opened before starting a transaction");

            return new LightningTransaction(this, parent, beginFlags);
        }
        //TODO: Upgrade db flags?
        internal LightningDatabase OpenDatabase(string name, LightningTransaction tran, DatabaseOpenFlags? flags, Encoding encoding)
        {
            var internalName = name ?? LightningDatabase.DefaultDatabaseName;
            var db = _openedDatabases.GetOrAdd(internalName, n => 
            {
                var ldb = new LightningDatabase(name, tran, flags, encoding);
                _databasesForReuse.Add(ldb._handle);

                return ldb;
            });

            if (db.OpenFlags != flags.GetValueOrDefault())
                throw new InvalidOperationException("Database " + internalName + " already opened with different flags");

            if (db.Encoding != (encoding ?? LightningConfig.Database.DefaultEncoding))
                throw new InvalidOperationException("Can not change encoding of already opened database");

            return db;
        }
Ejemplo n.º 54
0
        //TODO: Upgrade db flags?
        internal LightningDatabase OpenDatabase(string name, DatabaseOpenFlags flags, LightningTransaction tran)
        {
            var internalName = name ?? LightningDatabase.DefaultDatabaseName;
            var db = _openedDatabases.GetOrAdd(internalName, n => 
            {
                var ldb = new LightningDatabase(name, flags, tran);
                _databasesForReuse.Add(ldb._handle);

                return ldb;
            });

            if (db.OpenFlags != flags)
                throw new InvalidOperationException("Database " + internalName + " already opened with different flags");

            return db;
        }
        public static bool ContainsKey <TKey>(this LightningTransaction txn, LightningDatabase db, TKey key)
        {
            var keyBytes = db.ToBytes(key);

            return(txn.ContainsKey(db, keyBytes));
        }