Exemplo n.º 1
0
        public static void Put(this LMDBTransaction tx, LMDBDatabase db, string key, string value)
        {
            var enc = System.Text.Encoding.UTF8;

            byte[] bufferKey   = enc.GetBytes(key);
            byte[] bufferValue = enc.GetBytes(value);
            tx.Put(db, bufferKey, bufferValue);
        }
Exemplo n.º 2
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 LMDBTransaction
        /// </returns>
        public LMDBTransaction BeginTransaction(LMDBTransaction parent, TransactionBeginFlags beginFlags)
        {
            if (!IsOpened)
            {
                throw new InvalidOperationException("Environment must be opened before starting a transaction");
            }

            return(new LMDBTransaction(this, parent, beginFlags));
        }
Exemplo n.º 3
0
        public static bool TryGet(this LMDBTransaction tx, LMDBDatabase db, string key, out string value)
        {
            var enc = System.Text.Encoding.UTF8;

            byte[] result;
            var    found = tx.TryGet(db, enc.GetBytes(key), out result);

            value = enc.GetString(result);
            return(found);
        }
Exemplo n.º 4
0
        public static string Get(this LMDBTransaction tx, LMDBDatabase db, string key)
        {
            var enc = System.Text.Encoding.UTF8;

            byte[] bufferKey = enc.GetBytes(key);
            var    result    = tx.Get(db, bufferKey);
            var    ret       = enc.GetString(result);

            return(ret);
        }
Exemplo n.º 5
0
        public void GetData(string key, out string value)
        {
            LMDBTransaction tx = _env.BeginTransaction();
            LMDBDatabase    db = tx.OpenDatabase(_dbName, new DatabaseConfiguration {
                Flags = DatabaseOpenFlags.Create
            });

            value = tx.Get(db, key);
            tx.Commit();
            db.Dispose();
        }
Exemplo n.º 6
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(LMDBTransaction txn)
        {
            if (txn == null)
            {
                throw new ArgumentNullException(nameof(txn));
            }

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

            Lmdb.mdb_cursor_renew(txn.Handle(), _handle);
        }
Exemplo n.º 7
0
        internal IDisposable ConfigureDatabase(LMDBTransaction tx, LMDBDatabase db)
        {
            var pinnedComparer = new ComparerKeepAlive();

            if (_comparer != null)
            {
                CompareFunction compare = Compare;
                pinnedComparer.AddComparer(compare);
            }
            if (_duplicatesComparer != null)
            {
                CompareFunction dupCompare = IsDuplicate;
                pinnedComparer.AddComparer(dupCompare);
            }
            return(pinnedComparer);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Creates new instance of LMDBCursor
        /// </summary>
        /// <param name="db">Database</param>
        /// <param name="txn">Transaction</param>
        internal LMDBCursor(LMDBDatabase db, LMDBTransaction txn)
        {
            if (db == null)
            {
                throw new ArgumentNullException(nameof(db));
            }

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

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

            Transaction            = txn;
            Transaction.Disposing += Dispose;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a LMDBDatabase 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 LMDBDatabase(string name, LMDBTransaction 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;
            Lmdb.mdb_dbi_open(transaction.Handle(), name, _configuration.Flags, out _handle);
            _pinnedConfig = _configuration.ConfigureDatabase(transaction, this);
            IsOpened      = true;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Created new instance of LMDBTransaction
        /// </summary>
        /// <param name="environment">Environment.</param>
        /// <param name="parent">Parent transaction or null.</param>
        /// <param name="flags">Transaction open options.</param>
        internal LMDBTransaction(LMDBEnvironment environment, LMDBTransaction parent, TransactionBeginFlags flags)
        {
            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

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

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

            Lmdb.mdb_txn_begin(environment.Handle(), parentHandle, flags, out _handle);
            _originalHandle = _handle;
        }
Exemplo n.º 11
0
 /// <summary>
 /// Truncates all data from the database.
 /// </summary>
 public void Truncate(LMDBTransaction transaction)
 {
     Lmdb.mdb_drop(transaction.Handle(), _handle, false);
 }
Exemplo n.º 12
0
 /// <summary>
 /// Drops the database.
 /// </summary>
 public void Drop(LMDBTransaction transaction)
 {
     Lmdb.mdb_drop(transaction.Handle(), _handle, true);
     IsOpened = false;
     _handle  = default(uint);
 }
Exemplo n.º 13
0
        public static bool ContainsKey(this LMDBTransaction tx, LMDBDatabase db, string key)
        {
            var enc = System.Text.Encoding.UTF8;

            return(tx.ContainsKey(db, enc.GetBytes(key)));
        }
Exemplo n.º 14
0
        public static void Delete(this LMDBTransaction tx, LMDBDatabase db, string key)
        {
            var enc = System.Text.Encoding.UTF8;

            tx.Delete(db, enc.GetBytes(key));
        }