You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.8 KiB

11 months ago
12 months ago
12 months ago
11 months ago
11 months ago
11 months ago
11 months ago
12 months ago
11 months ago
11 months ago
12 months ago
12 months ago
11 months ago
11 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
12 months ago
11 months ago
11 months ago
12 months ago
11 months ago
11 months ago
12 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
12 months ago
12 months ago
12 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  1. /*
  2. ISC License
  3. Copyright (c) 2019, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #pragma once
  16. // This uses a hashed key to limit the size of the data
  17. // structure. This could lead to choosing the wrong value but given
  18. // its usecase that really isn't a big deal.
  19. #include "nonstd/optional.hpp"
  20. #include "boost/unordered/concurrent_flat_map.hpp"
  21. #include "boost/flyweight.hpp"
  22. #include "boost/flyweight/no_tracking.hpp"
  23. #include "wyhash.h"
  24. #include "strpool.h"
  25. #include "fmt/core.h"
  26. #include <cstdint>
  27. #include <string>
  28. class PolicyCache
  29. {
  30. public:
  31. typedef uint64_t Key;
  32. typedef boost::flyweight<std::string,boost::flyweights::no_tracking> Val;
  33. typedef boost::concurrent_flat_map<Key,Val> Map;
  34. public:
  35. PolicyCache(unsigned const max_size_ = 256)
  36. : _max_size(max_size_)
  37. {
  38. }
  39. ~PolicyCache()
  40. {
  41. }
  42. public:
  43. Val
  44. insert(char const *key_,
  45. size_t const keylen_,
  46. std::string const &val_)
  47. {
  48. Val val;
  49. uint64_t hash;
  50. val = val_;
  51. hash = wyhash(key_,keylen_,0xdeadbeef,_wyp);
  52. _cache.insert_or_assign(hash,val_);
  53. fmt::print("insert {}={}\n",
  54. key_,
  55. val.get());
  56. return val;
  57. }
  58. Val
  59. insert(char const *key_,
  60. std::string const &val_)
  61. {
  62. return insert(key_,
  63. strlen(key_),
  64. val_);
  65. }
  66. Val
  67. insert(std::string const &key_,
  68. std::string const &val_)
  69. {
  70. return insert(key_.c_str(),
  71. key_.size(),
  72. val_);
  73. }
  74. Val
  75. find(std::string const &key_) const
  76. {
  77. Val rv;
  78. uint64_t hash;
  79. hash = wyhash(key_.c_str(),key_.size(),0xdeadbeef,_wyp);
  80. _cache.cvisit(hash,
  81. [&](Map::value_type const &v_)
  82. {
  83. fmt::print("get {}={}\n",
  84. key_,
  85. v_.second.get());
  86. rv = v_.second;
  87. });
  88. return rv;
  89. }
  90. void
  91. erase(std::string const &key_)
  92. {
  93. uint64_t hash;
  94. hash = wyhash(key_.c_str(),key_.size(),0xdeadbeef,_wyp);
  95. _cache.erase(hash);
  96. }
  97. private:
  98. unsigned _max_size;
  99. Map _cache;
  100. };