/// <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 valuePtr = LevelDBInterop.leveldb_get(this.Handle, options.Handle, key, (IntPtr)key.Length, out IntPtr lengthPtr, out IntPtr error); LevelDBException.Check(error); if (valuePtr == IntPtr.Zero) { return(null); } try { Int64 length = (Int64)lengthPtr; Byte[] value = new Byte[length]; Byte * valueNative = (Byte *)valuePtr.ToPointer(); for (Int64 i = 0; i < length; ++i) { value[i] = valueNative[i]; } return(value); } finally { LevelDBInterop.leveldb_free(valuePtr); GC.KeepAlive(options); GC.KeepAlive(this); } }
/// <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); }
/// <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); }
override protected bool ReleaseHandle() { if (this.handle != default(IntPtr)) { LevelDBInterop.leveldb_free(this.handle); } this.handle = default(IntPtr); return(true); }
///DOLATER <summary>Add Description</summary> /// <param name="error">FILL IN</param> public static void Check(IntPtr error) { if (error != IntPtr.Zero) { try { String message = Marshal.PtrToStringAnsi(error); throw new LevelDBException(message); } finally { LevelDBInterop.leveldb_free(error); } } }
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) { IntPtr 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); } }
/// <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); }
public bool TryGet(ReadOptions options, Slice key, out Slice value) { IntPtr length; IntPtr error; IntPtr v = LevelDBInterop.leveldb_get(Handle, options.Handle, key.buffer, (IntPtr)key.buffer.Length, out length, out error); if (error != IntPtr.Zero) { LevelDBInterop.leveldb_free(error); value = default(Slice); return(false); } if (v == IntPtr.Zero) { value = default(Slice); return(false); } value = new Slice(v, length); LevelDBInterop.leveldb_free(v); return(true); }
public Slice Get(ReadOptions options, Slice key) { IntPtr length; IntPtr error; IntPtr value = LevelDBInterop.leveldb_get(this.Handle, options.Handle, key.buffer, (IntPtr)key.buffer.Length, out length, out error); try { if (value == IntPtr.Zero) { throw new LevelDBException("not found"); } return(new Slice(value, length)); } finally { if (value != IntPtr.Zero) { LevelDBInterop.leveldb_free(value); } } }
public unsafe long Get(byte[] key, byte[] buffer, 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(0); } try { var length = (long)lengthPtr; var valueNative = (byte *)valuePtr.ToPointer(); Marshal.Copy((IntPtr)valuePtr, buffer, 0, (int)length); return(length); } finally { LevelDBInterop.leveldb_free(valuePtr); } }