small-cache 0.0.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: small-cache
3
- Version: 0.0.4
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 ==2.9.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.4"
7
+ version = "0.0.5"
8
8
  description = "Small cache"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -2,7 +2,6 @@
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>
@@ -65,7 +64,7 @@ public:
65
64
  if (attributes.empty()) {
66
65
  throw std::runtime_error("No attributes provided");
67
66
  }
68
- if (attributes.size() > 255) {
67
+ if (attributes.size() > MarkedItem::maxAttributes) {
69
68
  throw std::runtime_error("Too many attributes provided");
70
69
  }
71
70
  attrMap.reserve(attributes.size());
@@ -84,6 +83,9 @@ public:
84
83
  struct MarkedItem {
85
84
  bool isNew = true;
86
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;
87
89
  std::vector<AttributeValue> value;
88
90
 
89
91
  [[nodiscard]] std::vector<size_t> getIdxs() const {
@@ -292,7 +294,6 @@ public:
292
294
  }
293
295
  auto &marked_attrs = cache[item_id];
294
296
  setMarkedItem(marked_attrs, attributes);
295
- increaseIteratorVersion();
296
297
  }
297
298
 
298
299
  std::vector<pyAttrValue> get_one(const str &id, const strVec &attributes) {
@@ -325,50 +326,17 @@ public:
325
326
  return out;
326
327
  }
327
328
 
328
- struct getAllResponse {
329
- uint64_t iterator_version{};
330
- uint64_t iterator{};
331
- std::vector<std::pair<str, std::vector<pyAttrValue>>> result{};
332
- };
333
-
334
- str get_all(const strVec &attributes, uint64_t per_iteration = 10000, uint64_t iterator_version = 0,
335
- uint64_t iterator = 0) {
336
- if (transactionOpened)
337
- throw std::runtime_error("Can't get all when transaction is opened");
338
- if (iterator && iterator_version == 0) {
339
- throw std::runtime_error("Invalid iterator version");
340
- }
341
- if (iterator && iterator_version != this->iterator_version)
342
- throw std::runtime_error("Iterator already invalidated");
343
- getAllResponse resp{};
329
+ std::vector<str> get_all_ids() {
344
330
  std::vector<str> keys = cache | std::views::keys | std::ranges::to<std::vector>();
345
- std::ranges::sort(keys);
346
-
347
- uint64_t start = iterator;
348
- uint64_t available = keys.size() > start ? keys.size() - start : 0;
349
- uint64_t to_take = std::min<uint64_t>(available, per_iteration);
350
-
351
- resp.result.reserve(to_take);
352
-
353
- auto i = iterator;
354
- while (i < iterator + per_iteration && i < keys.size()) {
355
- const auto &key = keys[i];
356
- resp.result.emplace_back(key, get_one(key, attributes));
357
- i++;
358
- }
359
- resp.iterator_version = this->iterator_version;
360
- resp.iterator = i < keys.size() ? i : 0;
361
- return glz::write_json(resp).value_or("error"); // TODO don't use json
331
+ return keys;
362
332
  }
363
333
 
364
-
365
334
  void begin_transaction(uint64_t estimated_number_of_items = 0, bool remove_old_items = true) {
366
335
  if (transactionOpened) {
367
336
  throw std::runtime_error("Transaction already open");
368
337
  }
369
338
  if (estimated_number_of_items != 0) {
370
339
  cache.reserve(estimated_number_of_items);
371
- increaseIteratorVersion();
372
340
  }
373
341
  oldCacheSize = cache.size();
374
342
  transactionOpened = true;
@@ -391,7 +359,6 @@ public:
391
359
  }
392
360
  }
393
361
  }
394
- increaseIteratorVersion();
395
362
  transactionOpened = false;
396
363
  transactionShouldRemoveOldItems = true;
397
364
  }
@@ -412,15 +379,9 @@ public:
412
379
 
413
380
  setMarkedItem(marked_item, attrs);
414
381
  }
415
- increaseIteratorVersion();
416
382
  return resp.result.pagination.pages;
417
383
  }
418
384
 
419
- void increaseIteratorVersion() {
420
- iterator_version++;
421
- iterator_version++;
422
- }
423
-
424
385
  tsl::sparse_map<str, MarkedItem> cache;
425
386
  absl::flat_hash_map<str, uint8_t, absl::Hash<str>> attrMap;
426
387
  strVec attrIdx;
@@ -428,7 +389,6 @@ public:
428
389
  size_t oldCacheSize = 0;
429
390
  bool transactionOpened = false;
430
391
  bool transactionShouldRemoveOldItems = true;
431
- uint64_t iterator_version = 1; // for get_all()
432
392
  };
433
393
 
434
394
 
@@ -443,9 +403,7 @@ NB_MODULE(_small_cache_impl, m) {
443
403
  .def("add", &SmallCache::add_item, nb::arg("item_id"), nb::arg("attributes"))
444
404
  .def("get_one", &SmallCache::get_one, nb::arg("id"), nb::arg("attributes"))
445
405
  .def("get_many", &SmallCache::get_many, nb::arg("ids"), nb::arg("attributes"))
446
- .def("get_all", &SmallCache::get_all, nb::arg("attributes"), nb::arg("per_iteration") = 10000,
447
- nb::arg("iterator_version") = 0,
448
- nb::arg("iterator") = 0)
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
File without changes