Exemplo n.º 1
0
        private static Block ReadBlock(FileStream stream, BlockHandle handle)
        {
            uint n = (uint)handle.Size;

            stream.Position = (long)handle.Offset;
            using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
            {
                var buf      = reader.ReadBytes(unchecked ((int)n + 1));
                var encoding = buf[n];
                var crc32c   = reader.ReadInt32();

                // TODO: validate crc32c here?

                switch (encoding)
                {
                case kNoCompression:
                    return(new Block(new Slice(buf, 0, n)));

                case kSnappyCompression:
                    buf = SnappyDecompressor.Decompress(buf, 0, unchecked ((int)n));
                    return(new Block(new Slice(buf)));
                }
            }

            throw new Exception("bad block type");
        }
Exemplo n.º 2
0
        private Block GetBlock(byte[] key)
        {
            if (m_stream != null)
            {
                var handle = BlockHandle.DecodeFrom(new Slice(key));
                if (m_blockCache != null && m_blockCache.TryGetValue(handle, out var block))
                {
                    return(block);
                }

                block = ReadBlock(m_stream, handle);
                m_blockCache?.Add(handle, block);
                return(block);
            }
            throw new Exception("TableFile not open");
        }
Exemplo n.º 3
0
        public static Footer DecodeFrom(Slice slice)
        {
            uint startOffset = slice.Offset;
            uint startLength = slice.Length;

            uint  magic_lo = Coding.DecodeFixed32(slice.NewSlice(kEncodedLength - 8, 4));
            uint  magic_hi = Coding.DecodeFixed32(slice.NewSlice(kEncodedLength - 4, 4));
            ulong magic    = (unchecked ((ulong)magic_hi) << 32) | unchecked ((ulong)magic_lo);

            if (magic != kTableMagicNumber)
            {
                throw new Exception("not an sstable (bad magic number)");
            }

            var metaIndexHandle = BlockHandle.DecodeFrom(slice);
            var indexHandle     = BlockHandle.DecodeFrom(slice);

            slice.Update(startOffset + kEncodedLength, startLength - kEncodedLength);
            return(new Footer(metaIndexHandle, indexHandle));
        }
Exemplo n.º 4
0
 public Footer(BlockHandle metaIndexHandle, BlockHandle indexHandle)
 {
     MetaIndexHandle = metaIndexHandle;
     IndexHandle     = indexHandle;
 }