示例#1
0
        public bool DeleteKeyHF(string key)
        {
            lock (_lock)
            {
                int alloc;
                if (_keys.Get(key, out alloc))
                {
                    if (_isDirty == false)
                    {
                        WriteDirtyFile();
                    }

                    byte[]          keybytes = Helper.GetBytes(key);
                    AllocationBlock ab       = FillAllocationBlock(alloc);

                    ab.keylen = (byte)keybytes.Length;

                    _keys.RemoveKey(key);// remove key from index

                    // write ab
                    ab.deleteKey  = true;
                    ab.datalength = 0;

                    byte[] header = CreateAllocHeader(ab, keybytes);

                    _datastore.SeekBlock(ab.blocknumber);
                    _datastore.WriteBlockBytes(header, 0, header.Length);

                    // free old data blocks
                    _datastore.FreeBlocks(ab.Blocks);

                    return(true);
                }
            }
            return(false);
        }
示例#2
0
        private int internalSave(byte[] keybytes, byte[] data, AllocationBlock ab)
        {
            ab.Blocks = new List <int>();
            int firstblock = _datastore.GetFreeBlockNumber();

            ab.Blocks.Add(firstblock);
            int blocknum = firstblock;

            byte[] header     = CreateAllocHeader(ab, keybytes);
            int    dblocksize = _BlockSize - header.Length;
            int    offset     = 0;
            // compute data block count
            int datablockcount = (data.Length / dblocksize) + 1;
            // save data blocks
            int counter = 0;
            int len     = data.Length;

            while (datablockcount > 0)
            {
                datablockcount--;
                int next = 0;
                if (datablockcount > 0)
                {
                    next = _datastore.GetFreeBlockNumber();
                }
                Buffer.BlockCopy(Helper.GetBytes(counter, false), 0, header, 0, 4); // set block number
                Buffer.BlockCopy(Helper.GetBytes(next, false), 0, header, 4, 4);    // set next pointer

                _datastore.SeekBlock(blocknum);
                _datastore.WriteBlockBytes(header, 0, header.Length);
                int c = len;
                if (c > dblocksize)
                {
                    c = dblocksize;
                }
                _datastore.WriteBlockBytes(data, offset, c);

                if (next > 0)
                {
                    blocknum = next;
                    ab.Blocks.Add(next);
                }
                offset += c;
                len    -= c;
                counter++;
            }
            return(firstblock);
        }