Пример #1
0
        /// <summary>
        /// Open the environment.
        /// </summary>
        public void Open(EnvironmentOpenFlags openFlags = EnvironmentOpenFlags.None, UnixAccessMode accessMode = UnixAccessMode.Default)
        {
            if (IsOpened)
            {
                throw new InvalidOperationException("Environment is already opened.");
            }

            if (!openFlags.HasFlag(EnvironmentOpenFlags.NoSubDir) && !Directory.Exists(Path))
            {
                Directory.CreateDirectory(Path);
            }

            try
            {
                Lmdb.mdb_env_open(_handle, Path, openFlags, accessMode);
            }
            catch (Exception ex)
            {
                throw new LMDBException($"Failed to open environment at path {Path}", ex);
            }

            IsOpened = true;
        }
Пример #2
0
 /// <summary>
 /// Delete current key/data pair.
 /// This function deletes the key/data pair to which the cursor refers.
 /// </summary>
 /// <param name="option">Options for this operation. This parameter must be set to 0 or one of the values described here.
 ///     MDB_NODUPDATA - delete all of the data items for the current key. This flag may only be specified if the database was opened with MDB_DUPSORT.</param>
 private void Delete(CursorDeleteOption option)
 {
     Lmdb.mdb_cursor_del(_handle, option);
 }
Пример #3
0
 /// <summary>
 /// Store by cursor.
 /// This function stores key/data pairs into the database. The cursor is positioned at the new item, or on failure usually near it.
 /// Note: Earlier documentation incorrectly said errors would leave the state of the cursor unchanged.
 /// If the function fails for any reason, the state of the cursor will be unchanged.
 /// If the function succeeds and an item is inserted into the database, the cursor is always positioned to refer to the newly inserted item.
 /// </summary>
 /// <param name="key">The key operated on.</param>
 /// <param name="value">The data operated on.</param>
 /// <param name="options">
 /// Options for this operation. This parameter must be set to 0 or one of the values described here.
 ///     CursorPutOptions.Current - overwrite the data of the key/data pair to which the cursor refers with the specified data item. The key parameter is ignored.
 ///     CursorPutOptions.NoDuplicateData - enter the new key/data pair only if it does not already appear in the database. This flag may only be specified if the database was opened with MDB_DUPSORT. The function will return MDB_KEYEXIST if the key/data pair already appears in the database.
 ///     CursorPutOptions.NoOverwrite - enter the new key/data pair only if the key does not already appear in the database. The function will return MDB_KEYEXIST if the key already appears in the database, even if the database supports duplicates (MDB_DUPSORT).
 ///     CursorPutOptions.ReserveSpace - reserve space for data of the given size, but don't copy the given data. Instead, return a pointer to the reserved space, which the caller can fill in later. This saves an extra memcpy if the data is being generated later.
 ///     CursorPutOptions.AppendData - append the given key/data pair to the end of the database. No key comparisons are performed. This option allows fast bulk loading when keys are already known to be in the correct order. Loading unsorted keys with this flag will cause data corruption.
 ///     CursorPutOptions.AppendDuplicateData - as above, but for sorted dup data.
 /// </param>
 public void Put(byte[] key, byte[] value, CursorPutOptions options)
 {
     Lmdb.mdb_cursor_put(_handle, key, value, options);
 }
Пример #4
0
 private bool GetMultiple(CursorOperation operation)
 {
     return(Lmdb.mdb_cursor_get_multiple(_handle, ref _currentKeyStructure, ref _currentValueStructure, operation) == 0);
 }
Пример #5
0
 private bool Get(CursorOperation operation, byte[] key, byte[] value)
 {
     return(Lmdb.mdb_cursor_get(_handle, key, value, operation) == 0);
 }
Пример #6
0
 private bool Get(CursorOperation operation, byte[] key)
 {
     _currentValueStructure = default(ValueStructure);
     return(Lmdb.mdb_cursor_get(_handle, key, out _currentKeyStructure, out _currentValueStructure, operation) == 0);
 }
Пример #7
0
 /// <summary>
 /// Truncates all data from the database.
 /// </summary>
 public void Truncate(LMDBTransaction transaction)
 {
     Lmdb.mdb_drop(transaction.Handle(), _handle, false);
 }
Пример #8
0
 /// <summary>
 /// Drops the database.
 /// </summary>
 public void Drop(LMDBTransaction transaction)
 {
     Lmdb.mdb_drop(transaction.Handle(), _handle, true);
     IsOpened = false;
     _handle  = default(uint);
 }
Пример #9
0
 /// <summary>
 /// Abandon all the operations of the transaction instead of saving them.
 /// All cursors opened within the transaction will be closed by this call.
 /// The cursors and transaction handle will be freed and must not be used again after this call.
 /// </summary>
 public void Abort()
 {
     State = TransactionState.Aborted;
     StateChanging?.Invoke(State);
     Lmdb.mdb_txn_abort(_handle);
 }
Пример #10
0
 /// <summary>
 /// Commit all the operations of a transaction into the database.
 /// All cursors opened within the transaction will be closed by this call.
 /// The cursors and transaction handle will be freed and must not be used again after this call.
 /// </summary>
 public void Commit()
 {
     State = TransactionState.Commited;
     StateChanging?.Invoke(State);
     Lmdb.mdb_txn_commit(_handle);
 }
Пример #11
0
 /// <summary>
 /// Delete items from a database.
 /// This function removes key/data pairs from the database.
 /// If the database does not support sorted duplicate data items (MDB_DUPSORT) the data parameter is ignored.
 /// If the database supports sorted duplicates and the data parameter is NULL, all of the duplicate data items for the key will be deleted.
 /// Otherwise, if the data parameter is non-NULL only the matching data item will be deleted.
 /// This function will return MDB_NOTFOUND if the specified key/data pair is not in the database.
 /// </summary>
 /// <param name="db">A database handle returned by mdb_dbi_open()</param>
 /// <param name="key">The key to delete from the database</param>
 public void Delete(LMDBDatabase db, byte[] key)
 {
     Lmdb.mdb_del(_handle, db.Handle(), key);
 }
Пример #12
0
 //TODO: tests
 /// <summary>
 /// Flush the data buffers to disk.
 /// Data is always written to disk when LMDBTransaction.Commit is called, but the operating system may keep it buffered.
 /// MDB always flushes the OS buffers upon commit as well, unless the environment was opened with EnvironmentOpenFlags.NoSync or in part EnvironmentOpenFlags.NoMetaSync.
 /// </summary>
 /// <param name="force">If true, force a synchronous flush. Otherwise if the environment has the EnvironmentOpenFlags.NoSync flag set the flushes will be omitted, and with MDB_MAPASYNC they will be asynchronous.</param>
 public void Flush(bool force)
 {
     Lmdb.mdb_env_sync(_handle, force);
 }