1 module tagion.dart.BlockSegment; 2 3 import std.stdio : File; 4 5 import LEB128 = tagion.utils.LEB128; 6 import std.typecons : Typedef; 7 import tagion.basic.Types : Buffer; 8 import tagion.dart.BlockFile; 9 import tagion.hibon.Document; 10 11 @safe 12 struct BlockSegment { 13 Index index; /// Block index where the document is stored or should be stored 14 Document doc; /// Document stored in the segment 15 16 void write(BlockFile blockfile) const { 17 blockfile.seek(index); 18 blockfile.file.rawWrite(doc.serialize); 19 } 20 21 @disable this(); 22 this(const Document doc, const Index index) pure nothrow @nogc { 23 this.index = index; 24 this.doc = doc; 25 } 26 27 import tagion.hibon.HiBONFile : fread; 28 29 this(BlockFile blockfile, const Index index) { 30 blockfile.seek(index); 31 const max_size = blockfile.headerBlock.max_size * blockfile.headerBlock.block_size; 32 doc = blockfile.file.fread(max_size); 33 this.index = index; 34 } 35 36 } 37 38 version (unittest) { 39 import tagion.basic.Types : FileExtension; 40 import basic = tagion.basic.basic; 41 42 const(basic.FileNames) fileId(T = BlockSegment)(string prefix = null) @safe { 43 return basic.fileId!T(FileExtension.block, prefix); 44 } 45 46 enum SMALL_BLOCK_SIZE = 0x40; 47 } 48 49 /// 50 @safe 51 unittest { 52 import std.algorithm.iteration : map; 53 import std.array : array; 54 import std.range : iota; 55 import std.stdio; 56 57 immutable filename = fileId("blocksegment").fullpath; 58 // writefln("filename=%s", filename); 59 auto file = File(filename, "w"); 60 scope (exit) { 61 file.close; 62 } 63 file.rawWrite(iota(SMALL_BLOCK_SIZE).map!(i => cast(ubyte) i).array); 64 65 }