private byte[] readblockdata(AllocationBlock ab)
        {
            byte[] data   = new byte[ab.datalength];
            long   offset = 0;
            int    len    = ab.datalength;
            int    dbsize = _BlockSize - _blockheader.Length - ab.keylen;

            ab.Blocks.ForEach(x =>
            {
                byte[] b = _datastore.ReadBlock(x);
                int c    = len;
                if (c > dbsize)
                {
                    c = dbsize;
                }
                Buffer.BlockCopy(b, _blockheader.Length + ab.keylen, data, (int)offset, c);
                offset += c;
                len    -= c;
            });
            if (ab.isCompressed)
            {
                data = MiniLZO.Decompress(data);
            }
            return(data);
        }
        internal byte[] GetData(int blocknumber, List <int> usedblocks)
        {
            lock (_lock)
            {
                AllocationBlock ab = FillAllocationBlock(blocknumber);
                usedblocks = ab.Blocks;
                byte[] data   = new byte[ab.datalength];
                long   offset = 0;
                int    len    = ab.datalength;
                int    dbsize = _BlockSize - _blockheader.Length - ab.keylen;
                ab.Blocks.ForEach(x =>
                {
                    byte[] b = _datastore.ReadBlock(x);
                    int c    = len;
                    if (c > dbsize)
                    {
                        c = dbsize;
                    }
                    Buffer.BlockCopy(b, _blockheader.Length + ab.keylen, data, (int)offset, c);
                    offset += c;
                    len    -= c;
                });
                if (ab.isCompressed)
                {
                    data = MiniLZO.Decompress(data);
                }

                return(data);
            }
        }
Exemple #3
0
        private MGRB LoadBitmap(long offset)
        {
            MGRB bc = new MGRB();

            if (offset == -1)
            {
                return(bc);
            }
            FileStream bmp = _bitmapFileRead;

            bmp.Seek(offset, SeekOrigin.Begin);
            var hdr = new byte[_hdrlen];

            bmp.Read(hdr, 0, hdr.Length);
            if (hdr[0] == (byte)'b' && hdr[1] == (byte)'m')
            {
                int c = Helper.ToInt32(hdr, 3);
                var b = new byte[c];
                bmp.Read(b, 0, c);
                if (hdr[2] == 1)
                {
                    b = MiniLZO.Decompress(b);
                }
                bc.Deserialize(fastBinaryJSON.BJSON.ToObject <MGRBData>(b));
            }
            else
            {
                log.Error("bitmap not recognized");
            }

            return(bc);
        }
Exemple #4
0
        private long SaveBitmapToFile(MGRB bmp)
        {
            long off = _lastBitmapOffset;
            var  dat = bmp.Serialize();
            var  hdr = new byte[_hdrlen];
            var  b   = fastBinaryJSON.BJSON.ToBJSON(dat, new fastBinaryJSON.BJSONParameters {
                UseExtensions = false
            });

            hdr[0] = (byte)'b';
            hdr[1] = (byte)'m';
            hdr[2] = 0; // uncompressed

            if (Global.CompressBitmapBytes)
            {
                hdr[2] = 1;
                b      = MiniLZO.Compress(b);
            }

            var s = Helper.GetBytes(b.Length, false);

            Buffer.BlockCopy(s, 0, hdr, 3, 4);

            _bitmapFileWrite.Write(hdr, 0, hdr.Length);
            _lastBitmapOffset += hdr.Length;

            _bitmapFileWrite.Write(b, 0, b.Length);
            _lastBitmapOffset += b.Length;

            return(off);
        }
Exemple #5
0
        public long WriteData(T key, byte[] data)
        {
            StorageItem <T> meta = new StorageItem <T>();

            meta.key = key;

            if (data.Length > (int)Global.CompressDocumentOverKiloBytes * _KILOBYTE)
            {
                meta.isCompressed = 1;
                data = MiniLZO.Compress(data);
            }

            return(internalWriteData(meta, data, false));
        }
Exemple #6
0
        internal byte[] ReadBytes(long recnum, out StorageItem <T> meta)
        {
            meta = null;
            if (recnum >= _lastRecordNum)
            {
                return(null);
            }
            lock (_readlock)
            {
                long       off  = ComputeOffset(recnum);
                FileStream fs   = GetReadFileStreamWithSeek(off);
                byte[]     data = internalReadBytes(fs, out meta);

                if (meta.isCompressed > 0)
                {
                    data = MiniLZO.Decompress(data);
                }

                return(data);
            }
        }
Exemple #7
0
        public long WriteObject(T key, object obj)
        {
            StorageItem <T> meta = new StorageItem <T>();

            meta.key      = key;
            meta.typename = fastJSON.Reflection.Instance.GetTypeAssemblyName(obj.GetType());
            byte[] data;
            if (_saveFormat == SF_FORMAT.BSON)
            {
                data = fastBinaryJSON.BJSON.ToBJSON(obj);
            }
            else
            {
                data = Helper.GetBytes(fastJSON.JSON.ToJSON(obj));
            }
            if (data.Length > (int)Global.CompressDocumentOverKiloBytes * _KILOBYTE)
            {
                meta.isCompressed = 1;
                data = MiniLZO.Compress(data); //MiniLZO
            }
            return(internalWriteData(meta, data, false));
        }
        public object GetObjectHF(string key)
        {
            lock (_lock)
            {
                int alloc;
                if (_keys.Get(key, out alloc))
                {
                    AllocationBlock ab = FillAllocationBlock(alloc);
                    if (ab.deleteKey == false)
                    {
                        byte[] data   = new byte[ab.datalength];
                        long   offset = 0;
                        int    len    = ab.datalength;
                        int    dbsize = _BlockSize - _blockheader.Length - ab.keylen;
                        ab.Blocks.ForEach(x =>
                        {
                            byte[] b = _datastore.ReadBlock(x);
                            int c    = len;
                            if (c > dbsize)
                            {
                                c = dbsize;
                            }
                            Buffer.BlockCopy(b, _blockheader.Length + ab.keylen, data, (int)offset, c);
                            offset += c;
                            len    -= c;
                        });
                        if (ab.isCompressed)
                        {
                            data = MiniLZO.Decompress(data);
                        }

                        return(fastBinaryJSON.BJSON.ToObject(data));
                    }
                }
            }

            return(null);
        }
        private void SaveNew(string key, byte[] keybytes, object obj)
        {
            byte[]          data;
            AllocationBlock ab = new AllocationBlock();

            ab.key    = key;
            ab.keylen = (byte)keybytes.Length;

            data            = fastBinaryJSON.BJSON.ToBJSON(obj);
            ab.isBinaryJSON = true;

            if (data.Length > (int)Global.CompressDocumentOverKiloBytes * _KILOBYTE)
            {
                ab.isCompressed = true;
                data            = MiniLZO.Compress(data);
            }
            ab.datalength = data.Length;

            int firstblock = internalSave(keybytes, data, ab);

            // save keys
            _keys.Set(key, firstblock);
        }