A library to scatter things accross a cluster depending on weights
Você não pode selecionar mais de 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.

76 linhas
2.3 KiB

  1. #include <stdint.h>
  2. #ifdef COMPILE_TIME
  3. #include "9float.hpp"
  4. using scatter_key_t = ninefloat::Q<uint64_t,64>;
  5. static const scatter_key_t scatter_key_increment = ninefloat::Q<uint64_t,64>::min_increment();
  6. #else
  7. using scatter_key_t = uint64_t;
  8. static const scatter_key_t scatter_key_increment = 1;
  9. #endif
  10. #include <vector>
  11. namespace StorageTree{
  12. namespace _impl{
  13. template<class childs>
  14. struct subdivision
  15. {
  16. uint64_t weight;
  17. std::vector<childs> content;
  18. scatter_key_t begin;
  19. scatter_key_t end;
  20. static const bool divisible = true;
  21. };
  22. struct daemon_impl{
  23. uint64_t weight;
  24. scatter_key_t begin;
  25. scatter_key_t end;
  26. static const bool divisible = false;
  27. };
  28. template<class T>
  29. constexpr T max(T any = 0)
  30. {
  31. if(any>any+scatter_key_increment)
  32. return any
  33. return max(any+scatter_key_increment);
  34. }
  35. }
  36. using daemon = _impl::daemon_impl;
  37. using server = _impl::subdivision<daemon>;
  38. using subrack = _impl::subdivision<server>;
  39. using rack = _impl::subdivision<subrack>;
  40. using room = _impl::subdivision<rack>;
  41. using datacenter = _impl::subdivision<room>;
  42. using root = _impl::subdivision<datacenter>;
  43. template<class T, std::enable_if_t<T::divisible>>
  44. void update_weights(T& root, uint64_t sub_ratio=1, scatter_key_t begin=0, scatter_key_t end=_impl::max<scatter_key_t>())
  45. {
  46. scatter_key_t address_space = end-begin;
  47. scatter_key_t base = begin
  48. for(int idx=0:idx<root.content.size();idx++)
  49. {
  50. auto& elem = root.content[idx];
  51. if(idx!=root.content.size()-1)
  52. {
  53. update_weights(elem, root.content.size()*sub_ratio, base, base+address_space/root.content.size());
  54. base = base+address_space/root.content.size()+scatter_key_increment;
  55. }
  56. else
  57. update_weights(elem, root.content.size()*sub_ratio, base, end);
  58. }
  59. }
  60. template<class T, std::enable_if_t<!T::divisible>>
  61. void update_weights(T& root, scatter_key_t begin, scatter_key_t end)
  62. {
  63. this->begin = begin;
  64. this->end = end;
  65. }
  66. }