Goddess of Justice DB, the database used for storage on IzaroDFS
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

220 linhas
5.1 KiB

há 5 anos
  1. #pragma once
  2. #include "fsized_map.h"
  3. #include <chrono>
  4. struct metadata_t{
  5. bitops::regulated<uint64_t> record_cnt;
  6. bitops::regulated<uint64_t> page_cnt;
  7. bitops::regulated<uint64_t> delete_cnt;
  8. bitops::regulated<uint64_t> last_page;
  9. bitops::regulated<uint64_t> last_delete;
  10. };
  11. class database {
  12. database(){}
  13. public:
  14. mmap_array<std::pair<bitops::regulated<uint64_t>, record>> records;
  15. mmap_array<db_page> pages;
  16. mmap_array<metadata_t> metadata;
  17. mmap_array<size_t> delete_table;
  18. database(
  19. const std::string& records,
  20. const std::string& pages,
  21. const std::string& deletions,
  22. const std::string& meta,
  23. const size_t& record_cnt = 4096,
  24. const size_t& page_cnt = 4096,
  25. const size_t& delete_cnt = 512
  26. )
  27. : records{record_cnt, records}
  28. , pages{page_cnt, pages}
  29. , metadata{(size_t)1, meta}
  30. , delete_table{delete_cnt, deletions}
  31. {}
  32. static database&& create(const std::string dir, size_t page_nb) {
  33. database ret{
  34. dir+"records",
  35. dir+"pages",
  36. dir+"deleted",
  37. dir+"meta",
  38. page_nb+page_nb/2,
  39. page_nb,
  40. page_nb/8
  41. };
  42. for(auto& n : ret.records)
  43. {
  44. n.second.timestamp = 0;
  45. n.second.offset = 0;
  46. }
  47. for(auto& n : ret.delete_table)
  48. {
  49. n = std::numeric_limits<size_t>::max();
  50. }
  51. (*ret.metadata).last_page = 0;
  52. (*ret.metadata).last_delete = 0;
  53. (*ret.metadata).record_cnt = page_nb+page_nb/2;
  54. (*ret.metadata).page_cnt = page_nb;
  55. (*ret.metadata).delete_cnt = page_nb/8;
  56. #pragma GCC diagnostic push
  57. #pragma GCC diagnostic ignored "-Wreturn-local-addr"
  58. return std::move(ret);
  59. #pragma GCC diagnostic pop
  60. }
  61. static database&& open(const std::string dir) {
  62. mmap_array<metadata_t> tmp{(size_t)1, dir+"meta"};
  63. database ret{
  64. dir+"records",
  65. dir+"pages",
  66. dir+"deleted",
  67. dir+"meta",
  68. (*tmp).record_cnt,
  69. (*tmp).page_cnt,
  70. (*tmp).delete_cnt
  71. };
  72. tmp.clear();
  73. #pragma GCC diagnostic push
  74. #pragma GCC diagnostic ignored "-Wreturn-local-addr"
  75. return std::move(ret);
  76. #pragma GCC diagnostic pop
  77. }
  78. void write(const record_identifier& target, const db_page& value){
  79. uint64_t page = std::numeric_limits<uint64_t>::max();;
  80. size_t off = std::numeric_limits<size_t>::max();
  81. if(metadata[0].last_delete>0)
  82. {
  83. off = (*metadata).last_delete;
  84. page = delete_table[off-1];
  85. } else {
  86. page = (*metadata).last_page;
  87. if(page>=pages.size()) {
  88. throw std::runtime_error("PAGE STARVATION! MUST EXIT NOW");
  89. }
  90. }
  91. if(page == std::numeric_limits<uint64_t>::max())
  92. {
  93. throw std::runtime_error("PAGE ERROR! MUST EXIT NOW");
  94. }
  95. pages[page] = value;
  96. uint64_t hashed = std::hash<record_identifier>{}(target);
  97. uint64_t hashed_roll = hashed;
  98. bool succeed = false;
  99. uint64_t ts = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  100. std::pair<bitops::regulated<uint64_t>,record> tmp{0, record{}};
  101. tmp.first = hashed;
  102. tmp.second.record_head.split = target;
  103. tmp.second.timestamp = ts;
  104. tmp.second.offset = page;
  105. do{
  106. uint64_t pos = hashed_roll % records.size();
  107. switch (static_cast<uint64_t>(records[pos].second.timestamp)) {
  108. case 0:
  109. [[fallthrough]];
  110. case std::numeric_limits<uint64_t>::max():
  111. records[pos] = tmp;
  112. succeed = true;
  113. break;
  114. default:
  115. break;
  116. }
  117. hashed_roll++;
  118. }while(!succeed);
  119. if(off != std::numeric_limits<size_t>::max())
  120. {
  121. (*metadata).last_delete += -1;
  122. delete_table[off] = std::numeric_limits<size_t>::max();
  123. } else {
  124. (*metadata).last_page += (size_t)1;
  125. }
  126. }
  127. std::pair<uint64_t, db_page> read(const record_identifier& target) {
  128. std::pair<uint64_t, db_page> ret;
  129. ret.first = 0;
  130. ret.second.fill(0);
  131. uint64_t hashed = std::hash<record_identifier>{}(target);
  132. uint64_t hashed_roll = hashed;
  133. do{
  134. uint64_t pos = hashed_roll % records.size();
  135. auto& value = records[pos].second;
  136. switch (static_cast<uint64_t>(value.timestamp)) {
  137. case 0:
  138. return ret;
  139. case std::numeric_limits<uint64_t>::max():
  140. break;
  141. default:
  142. if(records[pos].first == hashed)
  143. if(std::hash<record_identifier>{}(value.record_head.split) == hashed)
  144. {
  145. if(ret.first<value.timestamp)
  146. {
  147. ret.first = value.timestamp;
  148. ret.second = pages[value.offset];
  149. }
  150. break;
  151. }
  152. }
  153. hashed_roll++;
  154. }while(true);
  155. return ret;
  156. }
  157. void remove(const record_identifier& target) {
  158. uint64_t hashed = std::hash<record_identifier>{}(target);
  159. uint64_t hashed_roll = hashed;
  160. do{
  161. uint64_t pos = hashed_roll % records.size();
  162. auto& value = records[pos].second;
  163. switch (static_cast<uint64_t>(value.timestamp)) {
  164. case 0:
  165. return;
  166. case std::numeric_limits<uint64_t>::max():
  167. break;
  168. default:
  169. if(records[pos].first == hashed)
  170. if(std::hash<record_identifier>{}(value.record_head.split) == hashed)
  171. {
  172. value.timestamp = std::numeric_limits<uint64_t>::max();
  173. (*metadata).last_delete+=1;
  174. delete_table[(*metadata).last_delete-1] = value.offset;
  175. value.offset = 0;
  176. break;
  177. }
  178. }
  179. hashed_roll++;
  180. }while(true); // return only happens on hitting a case 0
  181. }
  182. void rollback(const record_identifier&) {
  183. }
  184. };