small-cache 0.0.3__tar.gz → 0.0.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -92,7 +92,7 @@ FetchContent_Declare(
92
92
  find_package(
93
93
  absl
94
94
  REQUIRED
95
- COMPONENTS flat_hash_map flat_hash_set hash
95
+ COMPONENTS flat_hash_map hash
96
96
  )
97
97
 
98
98
  Set(FETCHCONTENT_QUIET FALSE)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: small-cache
3
- Version: 0.0.3
3
+ Version: 0.0.5
4
4
  Summary: Small cache
5
5
  Author: kam1k4dze
6
6
  Classifier: License :: OSI Approved :: BSD License
@@ -1,10 +1,10 @@
1
1
  [build-system]
2
- requires = ["scikit-build-core >=0.10", "nanobind >=1.3.2"]
2
+ requires = ["scikit-build-core >=0.10", "nanobind >=2.10.2"]
3
3
  build-backend = "scikit_build_core.build"
4
4
 
5
5
  [project]
6
6
  name = "small-cache"
7
- version = "0.0.3"
7
+ version = "0.0.5"
8
8
  description = "Small cache"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -2,13 +2,11 @@
2
2
  #include <nanobind/stl/variant.h>
3
3
  #include <nanobind/stl/string.h>
4
4
  #include <nanobind/stl/vector.h>
5
- #include <nanobind/stl/pair.h>
6
5
  #include <nanobind/stl/optional.h>
7
6
  #include <nanobind/stl/unordered_map.h>
8
7
  #include <tsl/sparse_map.h>
9
8
  #include <absl/hash/hash.h>
10
9
  #include <absl/container/flat_hash_map.h>
11
- #include <absl/container/flat_hash_set.h>
12
10
  #include <boost/flyweight.hpp>
13
11
  #include <optional>
14
12
  #include <algorithm>
@@ -66,7 +64,7 @@ public:
66
64
  if (attributes.empty()) {
67
65
  throw std::runtime_error("No attributes provided");
68
66
  }
69
- if (attributes.size() > 255) {
67
+ if (attributes.size() > MarkedItem::maxAttributes) {
70
68
  throw std::runtime_error("Too many attributes provided");
71
69
  }
72
70
  attrMap.reserve(attributes.size());
@@ -85,6 +83,9 @@ public:
85
83
  struct MarkedItem {
86
84
  bool isNew = true;
87
85
  std::array<uint32_t, 3> attrs_flags{}; // 96 bits total
86
+ static constexpr std::size_t maxAttributes =
87
+ std::tuple_size_v<decltype(attrs_flags)> *
88
+ std::numeric_limits<std::remove_reference_t<decltype(attrs_flags)>::value_type>::digits;
88
89
  std::vector<AttributeValue> value;
89
90
 
90
91
  [[nodiscard]] std::vector<size_t> getIdxs() const {
@@ -293,7 +294,6 @@ public:
293
294
  }
294
295
  auto &marked_attrs = cache[item_id];
295
296
  setMarkedItem(marked_attrs, attributes);
296
- increaseIteratorVersion();
297
297
  }
298
298
 
299
299
  std::vector<pyAttrValue> get_one(const str &id, const strVec &attributes) {
@@ -317,51 +317,19 @@ public:
317
317
  return {};
318
318
  }
319
319
 
320
- struct getManyResponse {
321
- uint64_t iterator_version{};
322
- uint64_t iterator{};
323
- std::vector<std::pair<str, std::vector<pyAttrValue>>> result{};
324
- };
325
-
326
- str get_many(const strVec &ids, const strVec &attributes, uint64_t per_iteration = 10000,
327
- uint64_t iterator_version = 0, uint64_t iterator = 0) {
328
- if (transactionOpened)
329
- throw std::runtime_error("Can't get many when transaction is opened");
330
- if (iterator && iterator_version == 0) {
331
- throw std::runtime_error("Invalid iterator version");
332
- }
333
- if (iterator && iterator_version != this->iterator_version)
334
- throw std::runtime_error("Iterator already invalidated");
335
- getManyResponse resp{};
336
- std::vector<str> keys;
337
- if (ids.empty()) {
338
- keys = cache | std::views::keys | std::ranges::to<std::vector>();
339
- std::ranges::sort(keys);
340
- } else {
341
- absl::flat_hash_set<str> set_of_ids(ids.begin(), ids.end());
342
- keys = cache | std::views::keys |
343
- std::views::filter([&](const auto &key) { return set_of_ids.contains(key); }) |
344
- std::ranges::to<std::vector>();
345
- }
346
- std::ranges::sort(keys);
347
-
348
- const uint64_t start = iterator;
349
- const uint64_t available = keys.size() > start ? keys.size() - start : 0;
350
- const uint64_t to_take = std::min<uint64_t>(available, per_iteration);
351
-
352
- resp.result.reserve(to_take);
353
-
354
- auto i = iterator;
355
- while (i < iterator + per_iteration && i < keys.size()) {
356
- const auto &key = keys[i];
357
- resp.result.emplace_back(key, get_one(key, attributes));
358
- i++;
320
+ std::vector<std::vector<pyAttrValue>> get_many(const strVec &ids, const strVec &attributes) {
321
+ std::vector<std::vector<pyAttrValue>> out;
322
+ out.resize(ids.size());
323
+ for (size_t i = 0; i < ids.size(); ++i) {
324
+ out[i] = get_one(ids[i], attributes);
359
325
  }
360
- resp.iterator_version = this->iterator_version;
361
- resp.iterator = i < keys.size() ? i : 0;
362
- return glz::write_json(resp).value_or("error"); // TODO don't use json
326
+ return out;
363
327
  }
364
328
 
329
+ std::vector<str> get_all_ids() {
330
+ std::vector<str> keys = cache | std::views::keys | std::ranges::to<std::vector>();
331
+ return keys;
332
+ }
365
333
 
366
334
  void begin_transaction(uint64_t estimated_number_of_items = 0, bool remove_old_items = true) {
367
335
  if (transactionOpened) {
@@ -369,7 +337,6 @@ public:
369
337
  }
370
338
  if (estimated_number_of_items != 0) {
371
339
  cache.reserve(estimated_number_of_items);
372
- increaseIteratorVersion();
373
340
  }
374
341
  oldCacheSize = cache.size();
375
342
  transactionOpened = true;
@@ -392,7 +359,6 @@ public:
392
359
  }
393
360
  }
394
361
  }
395
- increaseIteratorVersion();
396
362
  transactionOpened = false;
397
363
  transactionShouldRemoveOldItems = true;
398
364
  }
@@ -413,15 +379,9 @@ public:
413
379
 
414
380
  setMarkedItem(marked_item, attrs);
415
381
  }
416
- increaseIteratorVersion();
417
382
  return resp.result.pagination.pages;
418
383
  }
419
384
 
420
- void increaseIteratorVersion() {
421
- iterator_version++;
422
- iterator_version++;
423
- }
424
-
425
385
  tsl::sparse_map<str, MarkedItem> cache;
426
386
  absl::flat_hash_map<str, uint8_t, absl::Hash<str>> attrMap;
427
387
  strVec attrIdx;
@@ -429,7 +389,6 @@ public:
429
389
  size_t oldCacheSize = 0;
430
390
  bool transactionOpened = false;
431
391
  bool transactionShouldRemoveOldItems = true;
432
- uint64_t iterator_version = 1; // for get_all()
433
392
  };
434
393
 
435
394
 
@@ -443,9 +402,8 @@ NB_MODULE(_small_cache_impl, m) {
443
402
  .def("end_transaction", &SmallCache::end_transaction)
444
403
  .def("add", &SmallCache::add_item, nb::arg("item_id"), nb::arg("attributes"))
445
404
  .def("get_one", &SmallCache::get_one, nb::arg("id"), nb::arg("attributes"))
446
- .def("get_many", &SmallCache::get_many, nb::arg("ids"), nb::arg("attributes"), nb::arg("per_iteration") = 10000,
447
- nb::arg("iterator_version") = 0,
448
- nb::arg("iterator") = 0)
405
+ .def("get_many", &SmallCache::get_many, nb::arg("ids"), nb::arg("attributes"))
406
+ .def("get_all_ids", &SmallCache::get_all_ids)
449
407
  .def("load_page", &SmallCache::load_page, nb::arg("json_text"));
450
408
  // nb::class_<SmallCache::getAllResponse>(cache, "getAllResponse")
451
409
  // .def(nb::init<>())
File without changes
File without changes