Пример #1
0
        protected void RunChecked(Func <IntPtr, DbRetCode> libFunc)
        {
            var handle = CheckDisposed();
            var ret    = libFunc(handle);

            ErrorUtil.CheckRetCode(ret);
        }
Пример #2
0
        /// <summary>
        /// Renew a read-only transaction.
        /// This acquires a new reader lock for a transaction handle that had been released by mdb_txn_reset().
        /// It must be called before a reset transaction may be used again.
        /// </summary>
        public void Renew()
        {
            var handle = CheckDisposed();
            var ret    = DbLib.mdb_txn_renew(handle);

            ErrorUtil.CheckRetCode(ret);
        }
Пример #3
0
        /// <summary>
        /// Commit all the operations of a transaction into the database.
        /// The transaction handle is freed. It and its cursors must not be used again after this call, except with mdb_cursor_renew().
        /// Note: Earlier documentation incorrectly said all cursors would be freed. Only write-transactions free cursors.
        /// </summary>
        public void Commit()
        {
            var ret = DbRetCode.SUCCESS;
            // we check here, as we dont want to throw an exception in the CER, but won't use this handle.
            var handle = CheckDisposed();
            var txnId  = IntPtr.Zero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try { /* */ }
            finally {
                // now we use atomic access to the handle
                handle = Interlocked.CompareExchange(ref txn, IntPtr.Zero, txn);
                // if the txn handle was valid before we cleared it, lets close the handle
                if (handle != IntPtr.Zero)
                {
                    txnId = DbLib.mdb_txn_id(handle);
                    ret   = DbLib.mdb_txn_commit(handle);
                }
            }

            if (handle != IntPtr.Zero)
            {
                // weather the call succeeded or not, the transaction cannot be re-used.
                ReleaseManagedResources(true);
                disposed?.Invoke(txnId);

                ErrorUtil.CheckRetCode(ret);
                Committed();
            }
        }
Пример #4
0
        protected T GetChecked <T>(LibFunc <T, DbRetCode> libFunc)
        {
            var handle = CheckDisposed();
            var ret    = libFunc(handle, out T result);

            ErrorUtil.CheckRetCode(ret);
            return(result);
        }
Пример #5
0
        /// <summary>Constructor.</summary>
        /// <param name="config">Configuration to use.</param>
        public LmdbEnvironment(LmdbEnvironmentConfiguration config = null)
        {
            // so that we can refer back to the Environment instance
            instanceHandle = GCHandle.Alloc(this, GCHandleType.WeakTrackResurrection);

            DbRetCode ret;

            RuntimeHelpers.PrepareConstrainedRegions();
            try { /* */ }
            finally {
                IntPtr envHandle;
                ret = DbLib.mdb_env_create(out envHandle);
                if (ret == DbRetCode.SUCCESS)
                {
                    var ret2 = DbLib.mdb_env_set_userctx(envHandle, (IntPtr)instanceHandle);
                    if (ret2 == DbRetCode.SUCCESS)
                    {
                        this.env = envHandle;
                    }
                    else
                    {
                        ret = ret2;
                        DbLib.mdb_env_close(envHandle);
                    }
                }
            }
            ErrorUtil.CheckRetCode(ret);

            if (config != null)
            {
                this.autoReduceMapSizeIn32BitProcess = config.AutoReduceMapSizeIn32BitProcess;
                if (config.MapSize.HasValue)
                {
                    this.SetMapSize(config.MapSize.Value);
                }
                if (config.MaxDatabases.HasValue)
                {
                    this.MaxDatabases = config.MaxDatabases.Value;
                }
                if (config.MaxReaders.HasValue)
                {
                    this.MaxReaders = config.MaxReaders.Value;
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Commit all the operations of a transaction into the database.
        /// The transaction handle is freed. It and its cursors must not be used again after this call, except with mdb_cursor_renew().
        /// Note: Earlier documentation incorrectly said all cursors would be freed. Only write-transactions free cursors.
        /// </summary>
        public void Commit()
        {
            // we check here, as we dont want to throw an exception later, but won't use this handle.
            var currentHandle = CheckDisposed();

            // now we use atomic access to the handle
            var handle = Interlocked.Exchange(ref txn, IntPtr.Zero);

            // if the txn handle was valid before we cleared it, lets close the handle
            if (handle != IntPtr.Zero)
            {
                var txnId = DbLib.mdb_txn_id(handle);
                var ret   = DbLib.mdb_txn_commit(handle);
                // whether the call succeeded or not, the transaction cannot be re-used.
                ReleaseManagedResources(true);
                disposed?.Invoke(txnId);

                ErrorUtil.CheckRetCode(ret);
                Committed();
            }
        }