file_structs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #ifndef FILE_NODE_STRUCTS_H
  2. #define FILE_NODE_STRUCTS_H
  3. #include "../utils/types.h"
  4. namespace binom {
  5. class BitIterator;
  6. //! Real index in file
  7. typedef ui64 real_index;
  8. //! Virtual index for translation to real index
  9. typedef ui64 virtual_index;
  10. //! Size of data block in file
  11. typedef ui64 block_size;
  12. //! Element count in the file
  13. typedef ui64 element_cnt;
  14. struct VMemoryBlock {
  15. virtual_index v_index = 0;
  16. block_size size = 0;
  17. inline bool isEmpty() const {return !v_index && !size;}
  18. };
  19. struct RMemoryBlock {
  20. real_index r_index;
  21. block_size size;
  22. };
  23. #pragma pack(push, 1)
  24. class BitMap {
  25. union {
  26. ui64 number = 0;
  27. struct {
  28. bool _0:1;
  29. bool _1:1;
  30. bool _2:1;
  31. bool _3:1;
  32. bool _4:1;
  33. bool _5:1;
  34. bool _6:1;
  35. bool _7:1;
  36. bool _8:1;
  37. bool _9:1;
  38. bool _10:1;
  39. bool _11:1;
  40. bool _12:1;
  41. bool _13:1;
  42. bool _14:1;
  43. bool _15:1;
  44. bool _16:1;
  45. bool _17:1;
  46. bool _18:1;
  47. bool _19:1;
  48. bool _20:1;
  49. bool _21:1;
  50. bool _22:1;
  51. bool _23:1;
  52. bool _24:1;
  53. bool _25:1;
  54. bool _26:1;
  55. bool _27:1;
  56. bool _28:1;
  57. bool _29:1;
  58. bool _30:1;
  59. bool _31:1;
  60. bool _32:1;
  61. bool _33:1;
  62. bool _34:1;
  63. bool _35:1;
  64. bool _36:1;
  65. bool _37:1;
  66. bool _38:1;
  67. bool _39:1;
  68. bool _40:1;
  69. bool _41:1;
  70. bool _42:1;
  71. bool _43:1;
  72. bool _44:1;
  73. bool _45:1;
  74. bool _46:1;
  75. bool _47:1;
  76. bool _48:1;
  77. bool _49:1;
  78. bool _50:1;
  79. bool _51:1;
  80. bool _52:1;
  81. bool _53:1;
  82. bool _54:1;
  83. bool _55:1;
  84. bool _56:1;
  85. bool _57:1;
  86. bool _58:1;
  87. bool _59:1;
  88. bool _60:1;
  89. bool _61:1;
  90. bool _62:1;
  91. bool _63:1;
  92. } bits;
  93. } data;
  94. public:
  95. bool get(ui8 index) {
  96. switch (index) {
  97. case 0: return data.bits._0;
  98. case 1: return data.bits._1;
  99. case 2: return data.bits._2;
  100. case 3: return data.bits._3;
  101. case 4: return data.bits._4;
  102. case 5: return data.bits._5;
  103. case 6: return data.bits._6;
  104. case 7: return data.bits._7;
  105. case 8: return data.bits._8;
  106. case 9: return data.bits._9;
  107. case 10: return data.bits._10;
  108. case 11: return data.bits._11;
  109. case 12: return data.bits._12;
  110. case 13: return data.bits._13;
  111. case 14: return data.bits._14;
  112. case 15: return data.bits._15;
  113. case 16: return data.bits._16;
  114. case 17: return data.bits._17;
  115. case 18: return data.bits._18;
  116. case 19: return data.bits._19;
  117. case 20: return data.bits._20;
  118. case 21: return data.bits._21;
  119. case 22: return data.bits._22;
  120. case 23: return data.bits._23;
  121. case 24: return data.bits._24;
  122. case 25: return data.bits._25;
  123. case 26: return data.bits._26;
  124. case 27: return data.bits._27;
  125. case 28: return data.bits._28;
  126. case 29: return data.bits._29;
  127. case 30: return data.bits._30;
  128. case 31: return data.bits._31;
  129. case 32: return data.bits._32;
  130. case 33: return data.bits._33;
  131. case 34: return data.bits._34;
  132. case 35: return data.bits._35;
  133. case 36: return data.bits._36;
  134. case 37: return data.bits._37;
  135. case 38: return data.bits._38;
  136. case 39: return data.bits._39;
  137. case 40: return data.bits._40;
  138. case 41: return data.bits._41;
  139. case 42: return data.bits._42;
  140. case 43: return data.bits._43;
  141. case 44: return data.bits._44;
  142. case 45: return data.bits._45;
  143. case 46: return data.bits._46;
  144. case 47: return data.bits._47;
  145. case 48: return data.bits._48;
  146. case 49: return data.bits._49;
  147. case 50: return data.bits._50;
  148. case 51: return data.bits._51;
  149. case 52: return data.bits._52;
  150. case 53: return data.bits._53;
  151. case 54: return data.bits._54;
  152. case 55: return data.bits._55;
  153. case 56: return data.bits._56;
  154. case 57: return data.bits._57;
  155. case 58: return data.bits._58;
  156. case 59: return data.bits._59;
  157. case 60: return data.bits._60;
  158. case 61: return data.bits._61;
  159. case 62: return data.bits._62;
  160. case 63: return data.bits._63;
  161. default: return false;
  162. }
  163. }
  164. bool set(ui8 index, bool value) {
  165. switch (index) {
  166. case 0: return data.bits._0 = value;
  167. case 1: return data.bits._1 = value;
  168. case 2: return data.bits._2 = value;
  169. case 3: return data.bits._3 = value;
  170. case 4: return data.bits._4 = value;
  171. case 5: return data.bits._5 = value;
  172. case 6: return data.bits._6 = value;
  173. case 7: return data.bits._7 = value;
  174. case 8: return data.bits._8 = value;
  175. case 9: return data.bits._9 = value;
  176. case 10: return data.bits._10 = value;
  177. case 11: return data.bits._11 = value;
  178. case 12: return data.bits._12 = value;
  179. case 13: return data.bits._13 = value;
  180. case 14: return data.bits._14 = value;
  181. case 15: return data.bits._15 = value;
  182. case 16: return data.bits._16 = value;
  183. case 17: return data.bits._17 = value;
  184. case 18: return data.bits._18 = value;
  185. case 19: return data.bits._19 = value;
  186. case 20: return data.bits._20 = value;
  187. case 21: return data.bits._21 = value;
  188. case 22: return data.bits._22 = value;
  189. case 23: return data.bits._23 = value;
  190. case 24: return data.bits._24 = value;
  191. case 25: return data.bits._25 = value;
  192. case 26: return data.bits._26 = value;
  193. case 27: return data.bits._27 = value;
  194. case 28: return data.bits._28 = value;
  195. case 29: return data.bits._29 = value;
  196. case 30: return data.bits._30 = value;
  197. case 31: return data.bits._31 = value;
  198. case 32: return data.bits._32 = value;
  199. case 33: return data.bits._33 = value;
  200. case 34: return data.bits._34 = value;
  201. case 35: return data.bits._35 = value;
  202. case 36: return data.bits._36 = value;
  203. case 37: return data.bits._37 = value;
  204. case 38: return data.bits._38 = value;
  205. case 39: return data.bits._39 = value;
  206. case 40: return data.bits._40 = value;
  207. case 41: return data.bits._41 = value;
  208. case 42: return data.bits._42 = value;
  209. case 43: return data.bits._43 = value;
  210. case 44: return data.bits._44 = value;
  211. case 45: return data.bits._45 = value;
  212. case 46: return data.bits._46 = value;
  213. case 47: return data.bits._47 = value;
  214. case 48: return data.bits._48 = value;
  215. case 49: return data.bits._49 = value;
  216. case 50: return data.bits._50 = value;
  217. case 51: return data.bits._51 = value;
  218. case 52: return data.bits._52 = value;
  219. case 53: return data.bits._53 = value;
  220. case 54: return data.bits._54 = value;
  221. case 55: return data.bits._55 = value;
  222. case 56: return data.bits._56 = value;
  223. case 57: return data.bits._57 = value;
  224. case 58: return data.bits._58 = value;
  225. case 59: return data.bits._59 = value;
  226. case 60: return data.bits._60 = value;
  227. case 61: return data.bits._61 = value;
  228. case 62: return data.bits._62 = value;
  229. case 63: return data.bits._63 = value;
  230. default: return false;
  231. }
  232. }
  233. ui64 value() {return data.number;}
  234. BitIterator begin();
  235. BitIterator end();
  236. };
  237. //! Version of data base
  238. struct DBVersion {
  239. const char file_type[9] = "BinOM.FS"; // File_Storage
  240. const ui16 major = 0;
  241. const ui16 minor = 0;
  242. };
  243. //! Current version of data base
  244. constexpr DBVersion current{"BinOM.FS", 1, 0};
  245. //! Descriptor of node page
  246. struct NodePageDescriptor {
  247. real_index next_node_page = 0;
  248. BitMap node_map;
  249. };
  250. //! Descriptor of heap page
  251. struct HeapPageDescriptor {
  252. real_index next_heap_page = 0;
  253. };
  254. //! Descriptor of BinOM node
  255. struct NodeDescriptor {
  256. VarType type = VarType::end;
  257. virtual_index index = 0; ///< Value for primitive types
  258. block_size size = 0;
  259. static inline NodeDescriptor null() {return {VarType::invalid_type, 0xFFFFFFFFFFFFFFFF, 0};}
  260. bool isNull() const {return type == VarType::invalid_type && index == 0xFFFFFFFFFFFFFFFF && !size;}
  261. bool isFree() const {return type == VarType::end && !index && !size;}
  262. bool isNoData() const {return !index && !size;}
  263. bool isEmpty() const {return !size;}
  264. VMemoryBlock toVMemoryBlock() const {return {index, size};}
  265. };
  266. struct ObjectNameLength {
  267. ValType char_type = ValType::invalid_type;
  268. block_size name_length = 0;
  269. element_cnt name_count = 0;
  270. };
  271. struct ObjectDescriptor {
  272. element_cnt length_element_count = 0;
  273. block_size name_block_size = 0;
  274. element_cnt index_count = 0;
  275. };
  276. //! Data Base descriptor
  277. struct DBHeader {
  278. enum class VersionDifference {
  279. identical = 0,
  280. minor = 1,
  281. major = 2,
  282. file_type = 3,
  283. };
  284. DBVersion version = current;
  285. real_index first_node_page_index = 0;
  286. real_index first_heap_page_index = 0;
  287. NodeDescriptor root_node;
  288. VersionDifference checkFileVersion() const {
  289. if(memcmp(version.file_type, current.file_type, sizeof(current.file_type)))
  290. return VersionDifference::file_type;
  291. if(version.major != current.major)
  292. return VersionDifference::major;
  293. if(version.minor != current.minor)
  294. return VersionDifference::minor;
  295. return VersionDifference::identical;
  296. }
  297. };
  298. #pragma pack(pop)
  299. class BitIterator {
  300. friend class BitMap;
  301. BitMap* map;
  302. ui8 bit_index;
  303. BitIterator(BitMap* map, ui8 bit_index = 0) : map(map), bit_index(bit_index) {}
  304. public:
  305. BitIterator(BitIterator& other) : map(other.map), bit_index(other.bit_index) {}
  306. BitIterator(BitIterator&& other) : map(other.map), bit_index(other.bit_index) {}
  307. bool get() {return map->get(bit_index);}
  308. ui8 getBitIndex() {return bit_index;}
  309. bool set(bool value) {return map->set(bit_index, value);}
  310. BitIterator& operator*() {return *this;}
  311. BitIterator& operator++() {if(bit_index < 64)++bit_index;return *this;}
  312. BitIterator operator++(int) {BitIterator prev(*this);if(bit_index < 64)++bit_index;return prev;}
  313. BitIterator& operator--() {if(bit_index > 0)--bit_index;return *this;}
  314. BitIterator operator--(int) {BitIterator next(*this);if(bit_index > 0)--bit_index;return next;}
  315. BitIterator& operator+=(ui8 add) {if((bit_index += add) > 64) bit_index = 64;return *this;}
  316. BitIterator& operator-=(ui8 sub) {if((bit_index -= sub) > 64) bit_index = 0;return *this;}
  317. BitIterator operator+(ui8 add) {return BitIterator(*this) += add;}
  318. BitIterator operator-(ui8 sub) {return BitIterator(*this) -= sub;}
  319. BitIterator& operator=(BitIterator other) {map = other.map; bit_index = other.bit_index; return *this;}
  320. BitIterator& operator=(BitMap* map) {this->map = map; bit_index = 0; return *this;}
  321. bool operator==(BitIterator other) {return map == other.map && bit_index == other.bit_index;}
  322. bool operator!=(BitIterator other) {return map != other.map || bit_index != other.bit_index;}
  323. bool operator<(BitIterator other) {return bit_index < other.bit_index;}
  324. bool operator<=(BitIterator other) {return bit_index <= other.bit_index;}
  325. bool operator>(BitIterator other) {return bit_index > other.bit_index;}
  326. bool operator>=(BitIterator other) {return bit_index >= other.bit_index;}
  327. bool isBegin() const {return bit_index == 0;}
  328. bool isEnd() const {return bit_index >= 64;}
  329. };
  330. inline BitIterator BitMap::begin() {return BitIterator(this);}
  331. inline BitIterator BitMap::end() {return BitIterator(this, 64);}
  332. constexpr element_cnt node_page_node_count = 64;
  333. constexpr block_size node_page_size = sizeof(NodePageDescriptor) + sizeof(NodeDescriptor)*node_page_node_count;
  334. constexpr block_size heap_page_data_size = 16_mb;
  335. constexpr block_size heap_page_size = sizeof(HeapPageDescriptor) + heap_page_data_size;
  336. //constexpr block_size byte_page_size = sizeof(BytePageDescriptor) + 64;
  337. }
  338. #endif