예제 #1
0
파일: DB.cs 프로젝트: bkwcl/LevelDB
        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
            });
        }
예제 #2
0
        /// <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);
            }
        }
예제 #3
0
파일: DB.cs 프로젝트: bkwcl/LevelDB
        /// <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);
        }
예제 #4
0
파일: DB.cs 프로젝트: bkwcl/LevelDB
        /// <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);
        }
예제 #5
0
파일: DB.cs 프로젝트: vtchain/LevelDb.Net
        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);
        }
예제 #6
0
        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);
                }
            }
        }
예제 #7
0
파일: DB.cs 프로젝트: xubinhua888/CacheDB
        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);
            }
        }