Ejemplo n.º 1
0
        public void CompareTest()
        {
            var comparer = new InternalKeyComparer(LeveldbDefaultKeyComparer.Comparer);

            Assert.IsTrue(comparer.Compare(
                              new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              , new InternalKey(new byte[] { 2 }, 0, InternalKey.ValueType.Value)
                              ) < 0);
            Assert.IsTrue(comparer.Compare(
                              new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              , new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              ) == 0);
            Assert.IsTrue(comparer.Compare(
                              new InternalKey(new byte[] { 2 }, 0, InternalKey.ValueType.Value)
                              , new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              ) > 0);

            Assert.IsTrue(comparer.Compare(
                              new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              , new InternalKey(new byte[] { 1, 1 }, 0, InternalKey.ValueType.Value)
                              ) < 0);
            Assert.IsTrue(comparer.Compare(
                              new InternalKey(new byte[] { 1, 1 }, 0, InternalKey.ValueType.Value)
                              , new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              ) > 0);


            Assert.IsTrue(comparer.Compare(
                              new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              , new InternalKey(new byte[] { 1 }, 1, InternalKey.ValueType.Value)
                              ) > 0);

            Assert.IsTrue(comparer.Compare(
                              new InternalKey(new byte[] { 1 }, 1, InternalKey.ValueType.Value)
                              , new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              ) < 0);


            Assert.IsTrue(comparer.Compare(
                              new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Deletion)
                              , new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              ) > 0);

            Assert.IsTrue(comparer.Compare(
                              new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Value)
                              , new InternalKey(new byte[] { 1 }, 0, InternalKey.ValueType.Deletion)
                              ) < 0);
        }
Ejemplo n.º 2
0
//        public Block(Stream stream, BlockHandle handle, IKeyComparer comparer)
//            : this(ReadBlock(stream, handle), comparer)
//        {
//        }

        internal Block(byte[] data, InternalKeyComparer comparer)
        {
            _data     = data;
            _comparer = comparer;
            if (_data.Length < sizeof(uint))
            {
                throw new InvalidDataException("bad block contents - block length");
            }

            _numRestarts = BitConverter.ToInt32(_data, _data.Length - sizeof(uint));

            var maxRestartsAllowed = (_data.Length - sizeof(uint)) / sizeof(uint);

            if (_numRestarts > maxRestartsAllowed)
            {
                throw new InvalidDataException("bad block contents - numRestarts");
            }

            _restartOffset = _data.Length - (1 + _numRestarts) * sizeof(uint);
        }
Ejemplo n.º 3
0
        public Table(IContentReader contentReader, ICache cache, InternalKeyComparer comparer,
                     ISnappyDecompressor decompressor)
        {
            _contentReader = contentReader;
            _comparer      = comparer;
            _decompressor  = decompressor;

            _cache   = cache;
            _cacheId = IdGenerator.NewId();

            var size = contentReader.ContentLength;

            if (size < Footer.EncodedLength)
            {
                throw new InvalidDataException("file is too short to be an sstable");
            }

            // TODO replace with read
            var footers = contentReader.ReadContent(size - Footer.EncodedLength, Footer.EncodedLength);

            var footer = new Footer(footers);

            _indexBlock = new Block(ReadBlock(footer.IndexHandle), comparer);
        }