internal BlockSeeker(ReadOnlySpan <byte> blockData) { _blockData = blockData; _reader = new SpanReader(blockData); Key = null; _restartOffset = 0; _restartCount = 0; _comparator = new BytewiseComparator(); _lastValue = null; Initialize(); }
public List <FileMetadata> GetOverlappingFiles(int level, byte[] smallestKey, byte[] largestKey) { if (!Levels.ContainsKey(level)) { return(new List <FileMetadata>()); } var overlappingFiles = new List <FileMetadata>(); var comparator = new BytewiseComparator(); foreach (FileMetadata metadata in GetFiles(level)) { if (comparator.Compare(metadata.SmallestKey, largestKey) <= 0 && comparator.Compare(metadata.LargestKey, smallestKey) >= 0) { overlappingFiles.Add(metadata); } } return(overlappingFiles); }
public void BytewiseComparatorTest() { var comparator = new BytewiseComparator(); // Basic Assert.AreEqual(Equal, comparator.Compare(new Span <byte>(), new Span <byte>())); Assert.AreEqual(Greater, comparator.Compare(new Span <byte>(new byte[] { 0 }), new Span <byte>())); Assert.AreEqual(Less, comparator.Compare(new Span <byte>(), new Span <byte>(new byte[] { 0 }))); Assert.AreEqual(Equal, comparator.Compare(new Span <byte>(new byte[] { 0 }), new Span <byte>(new byte[] { 0 }))); Assert.AreEqual(Equal, comparator.Compare(new Span <byte>(new byte[] { 0, 1, 2 }), new Span <byte>(new byte[] { 0, 1, 2 }))); Assert.AreEqual(Greater, comparator.Compare(new Span <byte>(new byte[] { 1 }), new Span <byte>(new byte[] { 0 }))); Assert.AreEqual(Greater, comparator.Compare(new Span <byte>(new byte[] { 1, 1 }), new Span <byte>(new byte[] { 1, 0 }))); Assert.AreEqual(Greater, comparator.Compare(new Span <byte>(new byte[] { 1, 1 }), new Span <byte>(new byte[] { 1, 0, 1 }))); Assert.AreEqual(Greater, comparator.Compare(new Span <byte>(new byte[] { 1, 1, 1 }), new Span <byte>(new byte[] { 1, 1 }))); Assert.AreEqual(Less, comparator.Compare(new Span <byte>(new byte[] { 0 }), new Span <byte>(new byte[] { 1 }))); Assert.AreEqual(Less, comparator.Compare(new Span <byte>(new byte[] { 1, 0 }), new Span <byte>(new byte[] { 1, 1 }))); Assert.AreEqual(Less, comparator.Compare(new Span <byte>(new byte[] { 1, 0, 1 }), new Span <byte>(new byte[] { 1, 1 }))); Assert.AreEqual(Less, comparator.Compare(new Span <byte>(new byte[] { 1, 1 }), new Span <byte>(new byte[] { 1, 1, 1 }))); Assert.AreEqual(Greater, comparator.Compare(new Span <byte>(new byte[] { 1, 0, 2 }), new Span <byte>(new byte[] { 1, 0, 1, 2, 3 }))); //"SmallestKey": { // "Key": "00 00 00 00 10 00 00 00 31 01 1f 34 00 00 00 00 00 ........1..4....." //}, //"LargestKey": { // "Key": "ff ff ff ff fc ff ff ff 76 01 a7 42 00 00 00 00 00 ÿÿÿÿüÿÿÿv.§B....." //} Assert.AreEqual(Less, comparator.Compare( new byte[] { 0x00, 0x00, 0x00, 0x00, 0x10, 0x00 }, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x31, 0x01, 0x1f, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, })); Assert.AreEqual(Greater, comparator.Compare( new byte[] { 0x00, 0x00, 0x00, 0x00, 0x10, 0x01 }, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x31, 0x01, 0x1f, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, })); }
public void LevelDbReadLogTest() { // https://github.com/google/leveldb/blob/master/doc/log_format.md DirectoryInfo directory = TestUtils.GetTestDirectory(); LogReader logReader = new LogReader(new FileInfo(Path.Combine(directory.FullName, "000047.log"))); BytewiseComparator comparator = new BytewiseComparator(); bool found = false; while (true) { ReadOnlySpan <byte> data = logReader.ReadData(); if (logReader.Eof) { break; } var dataReader = new SpanReader(data); long sequenceNumber = dataReader.ReadInt64(); long size = dataReader.ReadInt32(); while (!dataReader.Eof) { byte recType = dataReader.ReadByte(); ulong v1 = dataReader.ReadVarLong(); var currentKey = dataReader.Read(v1); //CurrentKey = f5 ff ff ff eb ff ff ff 36 if (comparator.Compare(new byte[] { 0xf5, 0xff, 0xff, 0xff, 0xeb, 0xff, 0xff, 0xff, 0x36 }, currentKey) == 0) { Assert.False(found); found = true; } ulong v2 = 0; ReadOnlySpan <byte> currentVal = ReadOnlySpan <byte> .Empty; switch (recType) { case 1: // value { if (recType == 1) { v2 = dataReader.ReadVarLong(); currentVal = dataReader.Read(v2); } break; } case 0: // delete { //Assert.Fail("Unexpected delete key"); break; } default: throw new Exception("Unknown record format"); } if (Log.IsDebugEnabled) { Log.Debug($"RecType={recType}, Sequence={sequenceNumber}, Size={size}, v1={v1}, v2={v2}\nCurrentKey={currentKey.HexDump(currentKey.Length, false, false)}\nCurrentVal=\n{currentVal.HexDump(cutAfterFive: true)} "); } } } Assert.True(found); }