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

            Throw(error);

            if (v != IntPtr.Zero)
            {
                try
                {
                    var bytes = new byte[(long)length];

                    // TODO: consider copy loop, as Marshal.Copy has 2GB-1 limit, or native pointers
                    Marshal.Copy(v, bytes, 0, checked ((int)length));
                    return(bytes);
                }
                finally
                {
                    LevelDBInterop.leveldb_free(v);
                }
            }
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// If the database contains an entry for "key" return the value,
        /// otherwise return null.
        /// </summary>
        public int[] Get(int key, ReadOptions options)
        {
            IntPtr error;
            IntPtr length;
            IntPtr v;

            v = LevelDBInterop.leveldb_get(this.Handle, options.Handle, ref key, (IntPtr)sizeof(int), out length, out error);
            Throw(error);

            if (v != IntPtr.Zero)
            {
                try
                {
                    var bytes = new int[(long)length / 4];

                    // TODO: consider >2GB-1
                    Marshal.Copy(v, bytes, 0, checked ((int)bytes.LongLength));
                    return(bytes);
                }
                finally
                {
                    LevelDBInterop.leveldb_free(v);
                }
            }
            return(null);
        }
Exemple #3
0
 override protected bool ReleaseHandle()
 {
     if (this.handle != default(IntPtr))
     {
         LevelDBInterop.leveldb_free(this.handle);
     }
     this.handle = default(IntPtr);
     return(true);
 }
Exemple #4
0
 static void Throw(IntPtr error, Func <string, Exception> exception)
 {
     if (error != IntPtr.Zero)
     {
         try
         {
             var msg = Marshal.PtrToStringAnsi(error);
             throw exception(msg);
         }
         finally
         {
             LevelDBInterop.leveldb_free(error);
         }
     }
 }
Exemple #5
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)
        {
            string result = null;
            var    ptr    = LevelDBInterop.leveldb_property_value(this.Handle, name);

            if (ptr != IntPtr.Zero)
            {
                try
                {
                    return(Marshal.PtrToStringAnsi(ptr));
                }
                finally
                {
                    LevelDBInterop.leveldb_free(ptr);
                }
            }
            return(result);
        }