private bool TryGetInternal(UInt32 dbi, byte[] key, out Func<byte[]> valueFactory)
        {
            valueFactory = null;

            using (var keyMarshalStruct = new MarshalValueStructure(key))
            {
                var valueStruct = default(ValueStructure);
                var keyStructure = keyMarshalStruct.ValueStructure;

                var res = Native.Read(() => Native.mdb_get(_handle, dbi, ref keyStructure, out valueStruct));

                var exists = res != Native.MDB_NOTFOUND;
                if (exists)
                    valueFactory = () => valueStruct.ToByteArray(res);

                return exists;
            }
        }
 public KeyValuePair<byte[], byte[]> MoveToFirstAfter(byte[] key)
 {
     using(var marshalKey = new MarshalValueStructure(key))
         return this.Get(CursorOperation.SetRange, marshalKey.ValueStructure);
 }
 public KeyValuePair<byte[], byte[]> MoveToFirstValueAfter(byte[] key, byte[] value)
 {
     using (var marshalKey = new MarshalValueStructure(key))
     using (var marshalValue = new MarshalValueStructure(value))
         return this.Get(CursorOperation.GetBothRange, marshalKey.ValueStructure, marshalValue.ValueStructure);
 }
        //TODO: tests
        public void Put(byte[] key, byte[] value, PutOptions options)
        {
            using(var keyMarshalStruct = new MarshalValueStructure(key))
            using (var valueMarshalStruct = new MarshalValueStructure(value))
            {
                var keyStruct = keyMarshalStruct.ValueStructure;
                var valueStruct = valueMarshalStruct.ValueStructure;

                Native.Execute(lib => lib.mdb_cursor_put(_handle, keyStruct, valueStruct, options));
            }
        }
        public void Delete(LightningDatabase db, byte[] key, byte[] value = null)
        {
            if (db == null)
                throw new ArgumentNullException("db");

            using (var keyMarshalStruct = new MarshalValueStructure(key))
            {
                var keyStructure = keyMarshalStruct.ValueStructure;
                if (value != null)
                {
                    using (var valueMarshalStruct = new MarshalValueStructure(value))
                    {
                        var valueStructure = valueMarshalStruct.ValueStructure;
                        Native.Execute(() => Native.mdb_del(_handle, db._handle, ref keyStructure, ref valueStructure));
                        return;
                    }
                }
                Native.Execute(() => Native.mdb_del(_handle, db._handle, ref keyStructure, IntPtr.Zero));
            }
        }
        public void Put(LightningDatabase db, byte[] key, byte[] value, PutOptions options = PutOptions.None)
        {
            if (db == null)
                throw new ArgumentNullException("db");

            using (var keyStructureMarshal = new MarshalValueStructure(key))
            using (var valueStructureMarshal = new MarshalValueStructure(value))
            {
                var keyStruct = keyStructureMarshal.ValueStructure;
                var valueStruct = valueStructureMarshal.ValueStructure;

                Native.Execute(() => Native.mdb_put(_handle, db._handle, ref keyStruct, ref valueStruct, options));
            }
        }