Beispiel #1
0
        public PtrSize Get(long id)
        {
            var readResult = _parent.Read(_nestedValueKey);

            if (readResult == null)
            {
                throw new InvalidOperationException($"Could not find a map result wit id '{id}' within a nested values section stored under '{_nestedValueKey}' key");
            }

            var reader = readResult.Reader;
            var entry  = (ResultHeader *)reader.Base;
            var end    = reader.Base + reader.Length;

            while (entry < end)
            {
                if (entry->Id == id)
                {
                    return(PtrSize.Create((byte *)entry + sizeof(ResultHeader), entry->Size));
                }

                entry = (ResultHeader *)((byte *)entry + sizeof(ResultHeader) + entry->Size);
            }

            throw new InvalidOperationException($"Could not find a map result wit id '{id}' within a nested values section stored under '{_nestedValueKey}' key");
        }
Beispiel #2
0
        public unsafe void Can_seek_and_read_from_chunked_mmap_file(int totalSize, int chunkSize, int seed)
        {
            var random = new Random(seed);

            var buffer = new byte[totalSize];

            random.NextBytes(buffer);

            fixed(byte *ptr = buffer)
            {
                var ptrSize = new List <PtrSize>();

                var numberOfBuffers = totalSize / chunkSize + (totalSize % chunkSize != 0 ? 1 : 0);

                for (int i = 0; i < numberOfBuffers; i++)
                {
                    ptrSize.Add(PtrSize.Create(
                                    ptr + (i * chunkSize),
                                    i == numberOfBuffers - 1 ? totalSize % chunkSize : chunkSize));
                }

                var sut = new ChunkedMmapStream(ptrSize.ToArray(), chunkSize);

                // read all bytes
                var allBytes = sut.ReadEntireBlock(totalSize);

                Assert.Equal(buffer, allBytes);

                // seek to beginning, read to end
                sut.Position = 0;
                allBytes     = sut.ReadData();
                Assert.Equal(buffer, allBytes);

                // read all bytes one by one
                sut.Position = 0;
                for (int i = 0; i < allBytes.Length; i++)
                {
                    Assert.Equal(allBytes[i], sut.ReadByte());
                }

                // random seeks
                for (int i = 0; i < 1000; i++)
                {
                    var pos = random.Next(0, totalSize - 1);
                    sut.Position = pos;

                    var result = new byte[Math.Min(totalSize - pos, random.Next(1, totalSize))];

                    sut.ReadEntireBlock(result, 0, result.Length);
                    Assert.Equal(buffer.Skip(pos).Take(result.Length).ToArray(), result);
                }
            }
        }
Beispiel #3
0
        private void OpenInternal()
        {
            var fileTree = _currentTransaction.Value.ReadTree(_name);

            if (fileTree == null)
            {
                throw new FileNotFoundException("Could not find index input", _name);
            }

            var numberOfChunks = fileTree.State.NumberOfEntries;

            _ptrs = new PtrSize[numberOfChunks];

            int index = 0;

            using (var it = fileTree.Iterate(prefetch: false))
            {
                if (it.Seek(Slices.BeforeAllKeys) == false)
                {
                    throw new InvalidDataException("Could not seek to any chunk of this file");
                }

                do
                {
                    var readResult = fileTree.Read(it.CurrentKey);

                    _ptrs[index] = PtrSize.Create(readResult.Reader.Base, readResult.Reader.Length);

                    index++;
                } while (it.MoveNext());
            }

            if (numberOfChunks != index)
            {
                throw new InvalidDataException($"Read invalid number of file chunks. Expected {numberOfChunks}, read {index}.");
            }

            _stream = new ChunkedMmapStream(_ptrs, VoronIndexOutput.MaxFileChunkSize);
        }
        public ReadMapEntryScope Get(long id)
        {
            id = Bits.SwapBytes(id);

            switch (Type)
            {
            case MapResultsStorageType.Tree:
                Slice entrySlice;
                using (Slice.External(_indexContext.Allocator, (byte *)&id, sizeof(long), out entrySlice))
                {
                    if (Tree.IsLeafCompressionSupported)
                    {
                        var read = Tree.ReadDecompressed(entrySlice);
                        if (read == null)
                        {
                            throw new InvalidOperationException($"Could not find a map result with id '{id}' in '{Tree.Name}' tree");
                        }
                        return(new ReadMapEntryScope(read));
                    }
                    else
                    {
                        var read = Tree.Read(entrySlice);
                        if (read == null)
                        {
                            throw new InvalidOperationException($"Could not find a map result with id '{id}' in '{Tree.Name}' tree");
                        }
                        return(new ReadMapEntryScope(PtrSize.Create(read.Reader.Base, read.Reader.Length)));
                    }
                }

            case MapResultsStorageType.Nested:
                var section = GetNestedResultsSection();
                return(new ReadMapEntryScope(section.Get(id)));

            default:
                throw new ArgumentOutOfRangeException(Type.ToString());
            }
        }
Beispiel #5
0
        public void Add <T>(T value) where T : struct
        {
            PtrSize ptr = PtrSize.Create(value);

            Add(ref ptr);
        }
Beispiel #6
0
        public void Add(void *pointer, int size)
        {
            PtrSize ptr = PtrSize.Create(pointer, size);

            Add(ref ptr);
        }
Beispiel #7
0
        public void Add(Slice buffer)
        {
            PtrSize ptr = PtrSize.Create(buffer.Content.Ptr, buffer.Content.Length);

            Add(ref ptr);
        }
 public ReadMapEntryScope(DecompressedReadResult read)
 {
     _read = read;
     Data  = PtrSize.Create(read.Reader.Base, read.Reader.Length);
 }
Beispiel #9
0
 public void Add(void *ptr, int size)
 {
     Add(PtrSize.Create(ptr, size));
 }
Beispiel #10
0
 public void Add <T>(T value) where T : struct
 {
     Add(PtrSize.Create(value));
 }