Example #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);
            }
        }
Example #2
0
 public static void Check(IntPtr error)
 {
     if (error != IntPtr.Zero)
     {
         try
         {
             var message = Marshal.PtrToStringAnsi(error);
             throw new LevelDBException(message);
         }
         finally
         {
             LevelDBInterop.leveldb_free(error);
         }
     }
 }
Example #3
0
        /// <summary>
        /// DB implementations can export properties about their state
        /// via this method.  If "property" is a valid property understood by this
        /// DB implementation, fills "*value" with its current value and returns
        /// true.  Otherwise returns false.
        ///
        /// Valid property names include:
        ///
        ///  "leveldb.num-files-at-level<N>" - return the number of files at level <N>,
        ///     where <N> is an ASCII representation of a level number (e.g. "0").
        ///  "leveldb.stats" - returns a multi-line string that describes statistics
        ///     about the internal operation of the DB.
        /// </summary>
        public string PropertyValue(string name)
        {
            var ptr = LevelDBInterop.leveldb_property_value(this.Handle, name);

            if (ptr == IntPtr.Zero)
            {
                return(null);
            }
            try {
                return(Marshal.PtrToStringAnsi(ptr));
            } finally {
                LevelDBInterop.leveldb_free(ptr);
                GC.KeepAlive(this);
            }
        }