예제 #1
0
        /// <summary>
        /// If the database contains an entry for "key" return the value,
        /// otherwise return null.
        /// </summary>
        public unsafe byte[] Get(byte[] key, ReadOptions options)
        {
            IntPtr error;
            IntPtr lengthPtr;
            var    valuePtr = LevelDBInterop.leveldb_get(this.Handle, options.Handle, key, (IntPtr)key.Length, out lengthPtr, out error);

            LevelDBException.Check(error);
            if (valuePtr == IntPtr.Zero)
            {
                return(null);
            }
            try {
                var length      = (long)lengthPtr;
                var value       = new byte[length];
                var valueNative = (byte *)valuePtr.ToPointer();
                for (long i = 0; i < length; ++i)
                {
                    value[i] = valueNative[i];
                }
                return(value);
            } finally {
                LevelDBInterop.leveldb_free(valuePtr);
                GC.KeepAlive(options);
                GC.KeepAlive(this);
            }
        }
예제 #2
0
        /// <summary>
        /// If an error has occurred, throw it.
        /// </summary>
        void Throw()
        {
            IntPtr error;

            LevelDBInterop.leveldb_iter_get_error(Handle, out error);
            LevelDBException.Check(error);
            GC.KeepAlive(this);
        }
예제 #3
0
        /// <summary>
        /// Destroy the contents of the specified database.
        /// Be very careful using this method.
        /// Options should not be modified after calling this method.
        /// </summary>
        public static void Destroy(string name, Options options)
        {
            IntPtr error;

            LevelDBInterop.leveldb_destroy_db(options.Handle, name, out error);
            LevelDBException.Check(error);
            GC.KeepAlive(options);
        }
예제 #4
0
        /// <summary>
        /// Open the database with the specified "name".
        /// Options should not be modified after calling this method.
        /// </summary>
        public DB(string name, Options options)
        {
            Options = options ?? new Options();
            IntPtr error;

            Handle = LevelDBInterop.leveldb_open(Options.Handle, name, out error);
            LevelDBException.Check(error);
            GC.KeepAlive(Options);
        }
예제 #5
0
        /// <summary>
        /// Remove the database entry (if any) for "key".
        /// It is not an error if "key" did not exist in the database.
        /// </summary>
        public void Delete(byte[] key, WriteOptions options)
        {
            IntPtr error;

            LevelDBInterop.leveldb_delete(this.Handle, options.Handle, key, (IntPtr)key.Length, out error);
            LevelDBException.Check(error);
            GC.KeepAlive(options);
            GC.KeepAlive(this);
        }
예제 #6
0
        /// <summary>
        /// Set the database entry for "key" to "value".
        /// </summary>
        public void Put(byte[] key, byte[] value, WriteOptions options)
        {
            IntPtr error;

            LevelDBInterop.leveldb_put(this.Handle, options.Handle, key, (IntPtr)key.Length, value, (IntPtr)value.LongLength, out error);
            LevelDBException.Check(error);
            GC.KeepAlive(options);
            GC.KeepAlive(this);
        }
예제 #7
0
        public void Write(WriteBatch batch, WriteOptions options)
        {
            IntPtr error;

            LevelDBInterop.leveldb_write(this.Handle, options.Handle, batch.Handle, out error);
            LevelDBException.Check(error);
            GC.KeepAlive(batch);
            GC.KeepAlive(options);
            GC.KeepAlive(this);
        }