Пример #1
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);
        }
Пример #2
0
        public NativeArray <T> GetRaw <T>(NativeArray key, ReadOptions options)
            where T : struct
        {
            IntPtr error;
            IntPtr length;

            var handle = new LevelDbFreeHandle();

            // todo: remove typecast to int
            var v = (Ptr <T>)LevelDBInterop.leveldb_get(
                this.Handle,
                options.Handle,
                key.baseAddr,
                key.byteLength,
                out length,
                out error);

            handle.SetHandle((IntPtr)v);

            // round down, truncating the array slightly if needed
            var count = (IntPtr)((ulong)length / Ptr <T> .sizeof_T);

            return(new NativeArray <T> {
                baseAddr = v, count = count, handle = handle
            });
        }
Пример #3
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);
        }