예제 #1
0
        public byte[] GetValue(DataAddr addr)
        {
            var buf = new byte[addr.len];

            lock (LOCK)
            {
                _fileStream.Position = (long)addr.addr;
                _fileStream.Read(buf, 0, (int)addr.len);
            }
            return(buf);
        }
예제 #2
0
        public bool HasKey(HashBin key, out DataAddr dataAddr, bool getDataAddr = false, KeyHint hint = default(KeyHint), byte skipBytes = 0)
        {
            if (key.Length != _keyLength)
            {
                throw new ArgumentException($"Key must be {_keyLength} bytes");
            }

            // bool hasHint = hint.StartAddr >= _keyStart &&
            //                hint.EndAddr >= hint.StartAddr
            //                && hint.EndAddr <= _keyEnd;

            int compareLength = _keyLength - skipBytes;

            if (!(hint.StartAddr >= _keyStart &&
                  hint.EndAddr >= hint.StartAddr &&
                  hint.EndAddr <= _keyEnd))
            {
                hint.StartAddr = _keyStart;
                hint.EndAddr   = _keyEnd;
                compareLength  = _keyLength;
            }

            FileStream.Position = (long)hint.StartAddr;
            var  remaining = (long)(hint.EndAddr - hint.StartAddr + _keyLength);
            long lastRead;
            int  amountToRead;
            var  bufReadPos = 0;
            int  compareRes;

            fixed(byte *ptrKey = &key.Hash[0])
            {
                while (remaining > 0)
                {
                    amountToRead = (int)(remaining < _readBuffer.Length ? remaining : _readBuffer.Length);
                    lastRead     = FileStream.Read(_readBuffer, 0, amountToRead);
                    remaining   -= lastRead;
                    bufReadPos   = 0;
                    //loop until buffer read
                    while (bufReadPos < lastRead)
                    {
                        //_readBinBuffer.SetPointer(_readBufferPtr+bufReadPos, _keyLength);
                        //compareRes = _readBinBuffer.CompareTo(key);
                        compareRes = HashBin.ArrayPtrCompare(_readBufferPtr + bufReadPos, ptrKey, compareLength);

                        if (compareRes == 0)
                        {
                            if (getDataAddr)
                            {
                                var foundLocation = FileStream.Position - lastRead + bufReadPos;
                                //FileStream.Position = ((((foundLocation) / _keyLength) << 4) + (long)_keyEnd + (long)HeaderLength);
                                AddrsFileStream.Position = (foundLocation / _keyLength) << 4;
                                dataAddr = new DataAddr
                                {
                                    addr = _addrReader.ReadUInt64(),
                                    len  = _addrReader.ReadUInt64()
                                };
                                return(true);
                            }

                            dataAddr = default;
                            return(true);
                        }

                        if (compareRes == 1)
                        {
                            dataAddr = default;
                            return(false);
                        }
                        //
                        // if (HashBin.ArrayPtrEqualCompare(ptrKey, _readBufferPtr+bufReadPos, compareLength))
                        // {
                        //     if (getDataAddr)
                        //     {
                        //         var foundLocation = FileStream.Position - lastRead + bufReadPos;
                        //         FileStream.Position = ((((foundLocation) / _keyLength) << 4) + (long)_keyEnd + (long)HeaderLength);
                        //         dataAddr = new DataAddr();
                        //         dataAddr.addr = _reader.ReadUInt64();
                        //         dataAddr.len = _reader.ReadUInt64();
                        //         return true;
                        //     }
                        //
                        //     dataAddr = default;
                        //     return true;
                        // }



                        bufReadPos += _keyLength;
                    }
                }
            }

            dataAddr = default;
            return(false);
        }
예제 #3
0
 public bool GetKeyDataAddr(HashBin key, out DataAddr dataAddr, KeyHint hint = default(KeyHint), byte skipBytes = 0)
 {
     return(HasKey(key, out dataAddr, true, hint, skipBytes));
 }