protected override bool ReleaseHandle() { if (handle != default(IntPtr)) { LevelDbInterop.leveldb_free(handle); } handle = default(IntPtr); return(true); }
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); } } }
/// <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(Handle, name); if (ptr == IntPtr.Zero) { return(null); } try { return(Marshal.PtrToStringAnsi(ptr)); } finally { LevelDbInterop.leveldb_free(ptr); } }
/// <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(Handle, options.Handle, key, (IntPtr)key.LongLength, out length, out error); Throw(error); if (v == IntPtr.Zero) { return(null); } 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, (int)length); return(bytes); } finally { LevelDbInterop.leveldb_free(v); } }
/// <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(Handle, options.Handle, ref key, (IntPtr)sizeof(int), out length, out error); Throw(error); if (v == IntPtr.Zero) { return(null); } 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); } }