pigeonhole-db 1.2.3 → 1.3.0

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.
Files changed (132) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +16 -16
  3. data/Cargo.toml +5 -5
  4. data/lib/pigeonhole/version.rb +1 -1
  5. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/IO_Event.so +0 -0
  6. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/gem_make.out +6 -6
  7. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/mkmf.log +26 -26
  8. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/gem_make.out +6 -6
  9. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/json/ext/generator.so +0 -0
  10. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/json/ext/parser.so +0 -0
  11. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/mkmf.log +19 -19
  12. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/gem_make.out +9 -9
  13. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/mkmf.log +6 -6
  14. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/prism/prism.so +0 -0
  15. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/gem_make.out +8 -8
  16. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/mkmf.log +7 -7
  17. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/rbs_extension.so +0 -0
  18. data/vendor/bundle/ruby/3.4.0/gems/io-event-1.22.3/ext/Makefile +3 -3
  19. data/vendor/bundle/ruby/3.4.0/gems/io-event-1.22.3/lib/IO_Event.so +0 -0
  20. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/ext/json/ext/generator/Makefile +3 -3
  21. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/ext/json/ext/parser/Makefile +3 -3
  22. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/lib/json/ext/generator.so +0 -0
  23. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/lib/json/ext/parser.so +0 -0
  24. data/vendor/bundle/ruby/3.4.0/gems/prism-1.9.0/ext/prism/Makefile +3 -3
  25. data/vendor/bundle/ruby/3.4.0/gems/prism-1.9.0/lib/prism/prism.so +0 -0
  26. data/vendor/bundle/ruby/3.4.0/gems/rbs-4.2.0/ext/rbs_extension/Makefile +3 -3
  27. data/vendor/bundle/ruby/3.4.0/gems/rbs-4.2.0/lib/rbs_extension.so +0 -0
  28. data/vendor/cache/Cargo.toml +2 -2
  29. data/vendor/cache/src/lib.rs +37 -0
  30. data/vendor/cache/src/loom_tests.rs +32 -0
  31. data/vendor/cache/src/s3fifo.rs +239 -11
  32. data/vendor/cache/src/sync.rs +4 -4
  33. data/vendor/compaction/Cargo.toml +6 -6
  34. data/vendor/compaction/src/cursor.rs +86 -4
  35. data/vendor/compaction/src/job.rs +24 -2
  36. data/vendor/engine/Cargo.toml +11 -11
  37. data/vendor/engine/src/admission.rs +3 -1
  38. data/vendor/engine/src/catalog.rs +39 -4
  39. data/vendor/engine/src/check.rs +59 -20
  40. data/vendor/engine/src/compact.rs +2 -2
  41. data/vendor/engine/src/engine/hooks.rs +144 -2
  42. data/vendor/engine/src/engine/info.rs +6 -0
  43. data/vendor/engine/src/engine.rs +1492 -50
  44. data/vendor/engine/src/external_registry.rs +447 -0
  45. data/vendor/engine/src/feed.rs +313 -84
  46. data/vendor/engine/src/feed_reader/catalog.rs +490 -0
  47. data/vendor/engine/src/feed_reader/history.rs +236 -0
  48. data/vendor/engine/src/feed_reader/read_ahead.rs +241 -0
  49. data/vendor/engine/src/feed_reader/token.rs +232 -0
  50. data/vendor/engine/src/feed_reader.rs +970 -852
  51. data/vendor/engine/src/feed_stop_loom.rs +73 -0
  52. data/vendor/engine/src/flush.rs +29 -4
  53. data/vendor/engine/src/io_count.rs +190 -0
  54. data/vendor/engine/src/large.rs +4 -1
  55. data/vendor/engine/src/lib.rs +16 -2
  56. data/vendor/engine/src/maintenance.rs +2 -2
  57. data/vendor/engine/src/manifest.rs +38 -6
  58. data/vendor/engine/src/nonblocking.rs +3 -3
  59. data/vendor/engine/src/options.rs +26 -0
  60. data/vendor/engine/src/punch.rs +282 -0
  61. data/vendor/engine/src/range_copy.rs +365 -0
  62. data/vendor/engine/src/read/keys.rs +37 -5
  63. data/vendor/engine/src/read/reduce.rs +1 -0
  64. data/vendor/engine/src/read.rs +7 -0
  65. data/vendor/engine/src/row_cache.rs +24 -0
  66. data/vendor/engine/src/shard/tablets.rs +127 -31
  67. data/vendor/engine/src/shard.rs +1006 -33
  68. data/vendor/engine/src/source.rs +105 -12
  69. data/vendor/engine/src/write.rs +310 -4
  70. data/vendor/format/Cargo.toml +1 -1
  71. data/vendor/format/src/shm.rs +10 -0
  72. data/vendor/format/src/superblock.rs +9 -2
  73. data/vendor/format/src/wal.rs +818 -16
  74. data/vendor/io/Cargo.toml +3 -3
  75. data/vendor/io/src/count.rs +76 -0
  76. data/vendor/io/src/file.rs +70 -0
  77. data/vendor/io/src/lib.rs +2 -0
  78. data/vendor/io/src/os/unix.rs +124 -0
  79. data/vendor/io/src/os/windows.rs +51 -0
  80. data/vendor/io/src/pread.rs +26 -0
  81. data/vendor/io/src/sim.rs +81 -9
  82. data/vendor/io/src/sys.rs +53 -0
  83. data/vendor/io/src/uring.rs +28 -0
  84. data/vendor/io/src/vfs.rs +4 -0
  85. data/vendor/memory/Cargo.toml +4 -4
  86. data/vendor/memory/src/hooks.rs +6 -0
  87. data/vendor/memory/src/ivf.rs +67 -1
  88. data/vendor/memory/src/lib.rs +4 -1
  89. data/vendor/memory/src/recall/pool.rs +872 -0
  90. data/vendor/memory/src/recall.rs +351 -273
  91. data/vendor/memory/src/store.rs +15 -0
  92. data/vendor/memtable/Cargo.toml +3 -3
  93. data/vendor/pager/Cargo.toml +3 -3
  94. data/vendor/pager/src/alloc.rs +430 -0
  95. data/vendor/pager/src/lib.rs +114 -0
  96. data/vendor/pigeonhole/Cargo.toml +6 -5
  97. data/vendor/pigeonhole/src/db.rs +91 -2
  98. data/vendor/pigeonhole/src/error.rs +2 -1
  99. data/vendor/pigeonhole/src/feed.rs +210 -6
  100. data/vendor/pigeonhole/src/lib.rs +3 -0
  101. data/vendor/pigeonhole/src/metrics.rs +37 -0
  102. data/vendor/pigeonhole/src/nonblocking.rs +94 -111
  103. data/vendor/pigeonhole/src/options.rs +69 -0
  104. data/vendor/pigeonhole/src/replication.rs +601 -11
  105. data/vendor/pigeonhole/src/write.rs +81 -0
  106. data/vendor/runtime/Cargo.toml +2 -2
  107. data/vendor/shm/Cargo.toml +3 -3
  108. data/vendor/shm/src/region.rs +20 -2
  109. data/vendor/sst/Cargo.toml +4 -4
  110. data/vendor/sst/src/blob.rs +85 -7
  111. data/vendor/sst/src/iter.rs +98 -2
  112. data/vendor/sst/src/lib.rs +11 -2
  113. data/vendor/sst/src/writer.rs +12 -2
  114. data/vendor/text/Cargo.toml +2 -2
  115. data/vendor/text/src/block.rs +638 -0
  116. data/vendor/text/src/error.rs +8 -3
  117. data/vendor/text/src/fuzzy.rs +1 -1
  118. data/vendor/text/src/index.rs +1055 -80
  119. data/vendor/text/src/layout.rs +63 -2
  120. data/vendor/text/src/lib.rs +5 -1
  121. data/vendor/text/src/maxscore.rs +481 -94
  122. data/vendor/text/src/merged.rs +470 -0
  123. data/vendor/text/src/search.rs +1278 -341
  124. data/vendor/text/src/stats.rs +431 -0
  125. data/vendor/vector/Cargo.toml +2 -2
  126. data/vendor/vector/src/hooks.rs +33 -4
  127. data/vendor/vector/src/index.rs +498 -69
  128. data/vendor/wal/Cargo.toml +3 -3
  129. data/vendor/wal/src/feed.rs +313 -19
  130. data/vendor/wal/src/recovery.rs +31 -1
  131. data/vendor/wal/src/stream.rs +28 -0
  132. metadata +15 -1
@@ -13,12 +13,12 @@ NULLCMD = :
13
13
  #### Start of system configuration section. ####
14
14
 
15
15
  srcdir = .
16
- topdir = /opt/hostedtoolcache/Ruby/3.4.12/x64/include/ruby-3.4.0
16
+ topdir = /opt/hostedtoolcache/Ruby/3.4.13/x64/include/ruby-3.4.0
17
17
  hdrdir = $(topdir)
18
- arch_hdrdir = /opt/hostedtoolcache/Ruby/3.4.12/x64/include/ruby-3.4.0/x86_64-linux
18
+ arch_hdrdir = /opt/hostedtoolcache/Ruby/3.4.13/x64/include/ruby-3.4.0/x86_64-linux
19
19
  PATH_SEPARATOR = :
20
20
  VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby:$(srcdir)/../../src:$(srcdir)/../../src/util:$(srcdir)/ext/rbs_extension
21
- prefix = $(DESTDIR)/opt/hostedtoolcache/Ruby/3.4.12/x64
21
+ prefix = $(DESTDIR)/opt/hostedtoolcache/Ruby/3.4.13/x64
22
22
  rubysitearchprefix = $(rubylibprefix)/$(sitearch)
23
23
  rubyarchprefix = $(rubylibprefix)/$(arch)
24
24
  rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
@@ -2,7 +2,7 @@
2
2
 
3
3
  [dependencies.pigeonhole-io]
4
4
  path = "../io"
5
- version = "=3.0.2"
5
+ version = "=3.1.0"
6
6
  [lints.rust.unexpected_cfgs]
7
7
  check-cfg = ["cfg(loom)", "cfg(engine_loom)", "cfg(io_loom)", "cfg(pigeonhole_sim)"]
8
8
  level = "warn"
@@ -17,5 +17,5 @@ name = "pigeonhole-cache"
17
17
  readme = "README.md"
18
18
  repository = "https://github.com/CodingAnarchy/pigeonhole"
19
19
  rust-version = "1.96"
20
- version = "3.0.2"
20
+ version = "3.1.0"
21
21
  [target."cfg(loom)"]
@@ -328,6 +328,31 @@ impl BlockCache {
328
328
  pub fn capacity(&self) -> usize {
329
329
  self.shards.capacity()
330
330
  }
331
+
332
+ /// Resizes the cache to `capacity` bytes, over the shards it was built with (#1761).
333
+ ///
334
+ /// A shrink evicts unpinned blocks down to each shard's new share before it returns, a
335
+ /// few at a time with the shard's lock released between them, so gets go on meanwhile. It
336
+ /// can't evict a pinned block (one a reader holds, or an SST's top index and filters,
337
+ /// [`add_pinned`](Self::add_pinned)), so a shard's usage can stay over its share by what
338
+ /// is pinned until those handles drop, as at any capacity. A grow evicts nothing. A
339
+ /// [`disabled`](Self::disabled) cache stays disabled.
340
+ ///
341
+ /// ```
342
+ /// use pigeonhole_cache::{BlockCache, BlockData, BlockKey, Priority};
343
+ ///
344
+ /// let cache = BlockCache::new(1 << 20, 1);
345
+ /// for i in 0..64 {
346
+ /// let key = BlockKey { file: 1, offset: i * 4096 };
347
+ /// drop(cache.insert(key, BlockData::from(vec![0u8; 4096]), Priority::Normal));
348
+ /// }
349
+ /// cache.set_capacity(64 << 10);
350
+ /// assert_eq!(cache.capacity(), 64 << 10);
351
+ /// assert!(cache.usage() <= 64 << 10);
352
+ /// ```
353
+ pub fn set_capacity(&self, capacity: usize) {
354
+ self.shards.set_capacity(capacity);
355
+ }
331
356
  }
332
357
 
333
358
  impl fmt::Debug for BlockCache {
@@ -735,6 +760,18 @@ impl RowCache {
735
760
  pub fn usage(&self) -> usize {
736
761
  self.shards.usage()
737
762
  }
763
+
764
+ /// Configured capacity.
765
+ pub fn capacity(&self) -> usize {
766
+ self.shards.capacity()
767
+ }
768
+
769
+ /// Resizes the cache to `capacity` bytes, as [`BlockCache::set_capacity`] does: a shrink
770
+ /// evicts unpinned rows down to it before returning, a few at a time outside the lock; a
771
+ /// grow evicts nothing (#1761).
772
+ pub fn set_capacity(&self, capacity: usize) {
773
+ self.shards.set_capacity(capacity);
774
+ }
738
775
  }
739
776
 
740
777
  impl fmt::Debug for RowCache {
@@ -98,6 +98,38 @@ fn loom_handle_dropped_while_evicting() {
98
98
  });
99
99
  }
100
100
 
101
+ /// #1761 (simp724's S3): a shrink racing a get and an insert on the same shard. The shrink
102
+ /// evicts in passes with the lock released between them, so the other two interleave with
103
+ /// it. A handle a get returned keeps its bytes. The capacity ends at the shrink's. Once
104
+ /// every handle is gone, the shard is within it.
105
+ #[test]
106
+ fn loom_resize_races_get_and_insert() {
107
+ loom::model(|| {
108
+ let cache = Arc::new(BlockCache::new(128, 1));
109
+ drop(cache.insert(key(0), block(1), Priority::Normal));
110
+ drop(cache.insert(key(1), block(2), Priority::Normal));
111
+
112
+ let c = Arc::clone(&cache);
113
+ let resizer = thread::spawn(move || c.set_capacity(64));
114
+ let c = Arc::clone(&cache);
115
+ let user = thread::spawn(move || {
116
+ if let Some(h) = c.get(key(0)) {
117
+ thread::yield_now();
118
+ assert_eq!(h.bytes(), &[1; 64]);
119
+ }
120
+ let h = c.insert(key(2), block(3), Priority::Normal);
121
+ assert_eq!(h.bytes(), &[3; 64]);
122
+ });
123
+ resizer.join().unwrap();
124
+ user.join().unwrap();
125
+ assert_eq!(cache.capacity(), 64);
126
+ // Nothing is pinned now: one more resize to the same capacity evicts any excess an
127
+ // interleaving left (an entry pinned during the shrink).
128
+ cache.set_capacity(64);
129
+ assert!(cache.usage() <= 64, "usage {}", cache.usage());
130
+ });
131
+ }
132
+
101
133
  #[test]
102
134
  fn loom_row_handle_survives_invalidate() {
103
135
  loom::model(|| {
@@ -43,7 +43,7 @@ use crate::Priority;
43
43
  use crate::hash::BuildKeyHasher;
44
44
  use std::sync::atomic::Ordering;
45
45
 
46
- use crate::sync::{Arc, AtomicU8, AtomicU64, RwLock, lock};
46
+ use crate::sync::{Arc, AtomicU8, AtomicU64, AtomicUsize, Mutex, RwLock, lock};
47
47
 
48
48
  /// Maximum hit count per priority: S3-FIFO's 2 bits for `Normal`, one life for `Low`, and
49
49
  /// more lives for `High`, so a hot `High` block outlasts an equally hot `Normal` one in main.
@@ -341,6 +341,14 @@ impl<K: Copy + Eq + Hash, V> Shard<K, V> {
341
341
  self.usage
342
342
  }
343
343
 
344
+ /// Sets the shard's capacity (and its small queue's share of it), evicting nothing:
345
+ /// [`Sharded::set_capacity`] evicts down to it in batches, outside the lock between them
346
+ /// (#1761, simp724's S1).
347
+ fn set_capacity(&mut self, capacity: usize) {
348
+ self.capacity = capacity;
349
+ self.small_target = capacity / (100 / SMALL_PERCENT);
350
+ }
351
+
344
352
  /// Eviction passes that stopped early with a full [`Taken`] ([`TAKEN_MAX`]).
345
353
  pub(crate) fn deferred_evictions(&self) -> u64 {
346
354
  self.deferred_evictions
@@ -381,6 +389,15 @@ impl<K: Copy + Eq + Hash, V> Shard<K, V> {
381
389
  }
382
390
 
383
391
  fn evict(&mut self, taken: &mut Taken<V>) {
392
+ if self.evict_pass(taken) {
393
+ self.deferred_evictions += 1;
394
+ }
395
+ }
396
+
397
+ /// Evicts down to capacity, or until `taken` is full: then it returns true, with the
398
+ /// shard still over capacity. False when it got under capacity, or can make no further
399
+ /// progress (every entry left pinned).
400
+ fn evict_pass(&mut self, taken: &mut Taken<V>) -> bool {
384
401
  // A pinned entry is rotated to its queue's tail; once a whole queue has been skipped
385
402
  // without any progress, stop using it. Progress is an eviction or anything else that
386
403
  // changes what a later pass finds (aging, promotion, dropping a stale entry); each of
@@ -390,8 +407,7 @@ impl<K: Copy + Eq + Hash, V> Shard<K, V> {
390
407
  let mut skipped_main = 0;
391
408
  while self.usage > self.capacity {
392
409
  if taken.is_full() {
393
- self.deferred_evictions += 1;
394
- break;
410
+ return true;
395
411
  }
396
412
  let small_ok = !self.small.is_empty() && skipped_small < self.small.len();
397
413
  let main_ok = !self.main.is_empty() && skipped_main < self.main.len();
@@ -419,6 +435,7 @@ impl<K: Copy + Eq + Hash, V> Shard<K, V> {
419
435
  }
420
436
  }
421
437
  }
438
+ false
422
439
  }
423
440
 
424
441
  fn evict_small(&mut self, taken: &mut Taken<V>) -> Step {
@@ -515,10 +532,23 @@ pub(crate) struct Padded<K, V> {
515
532
  pub(crate) lock: RwLock<Shard<K, V>>,
516
533
  }
517
534
 
535
+ /// Shard `i`'s part of `capacity` over `n` shards: equal, the remainder one byte each to the
536
+ /// first.
537
+ fn share(capacity: usize, n: usize, i: usize) -> usize {
538
+ capacity / n + usize::from(i < capacity % n)
539
+ }
540
+
518
541
  /// A fixed set of shards, picked by key hash.
519
542
  pub(crate) struct Sharded<K, V> {
520
543
  shards: Box<[Padded<K, V>]>,
521
- capacity: usize,
544
+ /// The shards' capacities together: read without a lock (the block cache's pinned-bytes
545
+ /// thresholds, metrics), changed only by [`Sharded::set_capacity`] (#1761). Stored before
546
+ /// the shards are resized, so it leads them while a resize runs.
547
+ capacity: AtomicUsize,
548
+ /// Held across a whole [`Sharded::set_capacity`], so two resizes can't interleave their
549
+ /// per-shard passes and leave the shards at a mix of both totals (perf2's S1 on #1890).
550
+ /// Off the get and insert paths.
551
+ resizing: Mutex<()>,
522
552
  }
523
553
 
524
554
  impl<K: Copy + Eq + Hash, V> Sharded<K, V> {
@@ -539,20 +569,22 @@ impl<K: Copy + Eq + Hash, V> Sharded<K, V> {
539
569
  .map(|i| Padded {
540
570
  hits: AtomicU64::new(0),
541
571
  misses: AtomicU64::new(0),
542
- lock: RwLock::new(Shard::new(
543
- capacity / n + usize::from(i < capacity % n),
544
- entry_hint,
545
- )),
572
+ lock: RwLock::new(Shard::new(share(capacity, n, i), entry_hint)),
546
573
  })
547
574
  .collect();
548
- Self { shards, capacity }
575
+ Self {
576
+ shards,
577
+ capacity: AtomicUsize::new(capacity),
578
+ resizing: Mutex::new(()),
579
+ }
549
580
  }
550
581
 
551
582
  /// No shards: stores nothing.
552
583
  pub(crate) fn empty() -> Self {
553
584
  Self {
554
585
  shards: Box::new([]),
555
- capacity: 0,
586
+ capacity: AtomicUsize::new(0),
587
+ resizing: Mutex::new(()),
556
588
  }
557
589
  }
558
590
 
@@ -608,7 +640,46 @@ impl<K: Copy + Eq + Hash, V> Sharded<K, V> {
608
640
  }
609
641
 
610
642
  pub(crate) fn capacity(&self) -> usize {
611
- self.capacity
643
+ self.capacity.load(Ordering::Relaxed)
644
+ }
645
+
646
+ /// Resizes to `capacity` bytes over the same shards, each its share as at
647
+ /// [`new`](Self::new) (#1761: a node re-splitting its engine memory as partitions open and
648
+ /// close). A shard over its new share evicts down to it in passes of at most
649
+ /// [`TAKEN_MAX`] entries, its lock released and the taken values dropped between them, so
650
+ /// a large shrink never holds one shard's lock for the whole eviction (simp724's S1). It
651
+ /// returns once every shard is at or under its share, or holds only pinned entries over
652
+ /// it: those stay until their handles drop, as at any capacity. A grow evicts nothing.
653
+ /// No shards (a disabled cache): nothing changes. Concurrent resizes run one after the
654
+ /// other, the last to take the lock deciding the capacity.
655
+ pub(crate) fn set_capacity(&self, capacity: usize) {
656
+ let n = self.shards.len();
657
+ if n == 0 {
658
+ return;
659
+ }
660
+ let _one = self
661
+ .resizing
662
+ .lock()
663
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
664
+ self.capacity.store(capacity, Ordering::Relaxed);
665
+ for (i, s) in self.shards.iter().enumerate() {
666
+ let mut first = true;
667
+ loop {
668
+ // Declared before the guard, so the values drop after it (#1559).
669
+ let mut taken = Taken::new();
670
+ let mut shard = lock(&s.lock);
671
+ if first {
672
+ shard.set_capacity(share(capacity, n, i));
673
+ first = false;
674
+ }
675
+ let more = shard.evict_pass(&mut taken);
676
+ drop(shard);
677
+ drop(taken);
678
+ if !more {
679
+ break;
680
+ }
681
+ }
682
+ }
612
683
  }
613
684
 
614
685
  pub(crate) fn len(&self) -> usize {
@@ -816,4 +887,161 @@ mod tests {
816
887
  s.for_each(|sh, _| total += sh.capacity);
817
888
  assert_eq!(total, 10);
818
889
  }
890
+
891
+ /// A `Sharded` of `shards` shards and `cap` bytes, filled with `entries` 10-byte entries
892
+ /// (keys 0..entries), none pinned.
893
+ fn filled(cap: usize, shards: usize, entries: u32) -> Sharded<u32, Vec<u8>> {
894
+ let s: Sharded<u32, Vec<u8>> = Sharded::new(cap, shards, 1, 0);
895
+ for k in 0..entries {
896
+ let mut taken = Taken::new();
897
+ let h = crate::hash::hash_of(&k);
898
+ let shard = s.shard(h).unwrap();
899
+ lock(shard).insert(k, Arc::new(vec![0; 10]), 10, Priority::Normal, &mut taken);
900
+ }
901
+ s
902
+ }
903
+
904
+ /// #1761 (simp724's S1): a shrink far past one pass's `TAKEN_MAX` evicts every shard down
905
+ /// to its new share before it returns, in passes that are not counted as deferred.
906
+ #[test]
907
+ fn a_shrink_evicts_every_shard_down_to_its_share_in_passes() {
908
+ let entries = n(2000);
909
+ // Room for every entry however the keys hash over the shards.
910
+ let s = filled(40 * entries as usize, 4, entries);
911
+ assert_eq!(s.usage(), 10 * entries as usize);
912
+ let deferred = s.deferred_evictions();
913
+ // About a quarter of the entries per shard, down to 2 or 3: many passes each.
914
+ s.set_capacity(100);
915
+ assert_eq!(s.capacity(), 100);
916
+ let mut caps = Vec::new();
917
+ s.for_each(|sh, _| {
918
+ assert!(
919
+ sh.usage() <= sh.capacity,
920
+ "{} over {}",
921
+ sh.usage(),
922
+ sh.capacity
923
+ );
924
+ caps.push(sh.capacity);
925
+ });
926
+ assert_eq!(caps, [25, 25, 25, 25], "each shard its share");
927
+ assert!(s.usage() <= 100);
928
+ assert_eq!(
929
+ s.deferred_evictions(),
930
+ deferred,
931
+ "a resize's passes aren't deferrals"
932
+ );
933
+ }
934
+
935
+ /// #1761 (simp724's S2): the small queue's target follows the capacity.
936
+ #[test]
937
+ fn a_resize_recomputes_the_small_target() {
938
+ let s: Sharded<u32, Vec<u8>> = Sharded::new(1000, 1, 1, 0);
939
+ s.set_capacity(5000);
940
+ s.for_each(|sh, _| assert_eq!((sh.capacity, sh.small_target), (5000, 500)));
941
+ s.set_capacity(100);
942
+ s.for_each(|sh, _| assert_eq!((sh.capacity, sh.small_target), (100, 10)));
943
+ }
944
+
945
+ /// #1761 (simp724's S2): an entry larger than the new capacity is evicted with the rest,
946
+ /// and one inserted afterwards isn't cached, as for any oversized entry.
947
+ #[test]
948
+ fn a_shrink_below_an_entry_evicts_it_and_a_later_one_isnt_cached() {
949
+ let s: Sharded<u32, Vec<u8>> = Sharded::new(1000, 1, 1, 0);
950
+ let shard = s.shard(0).unwrap();
951
+ let mut taken = Taken::new();
952
+ lock(shard).insert(1, Arc::new(vec![0; 500]), 500, Priority::Normal, &mut taken);
953
+ drop(taken);
954
+ s.set_capacity(100);
955
+ assert!(
956
+ lock(shard).peek(&1).is_none(),
957
+ "the 500-byte entry is evicted"
958
+ );
959
+ assert_eq!(s.usage(), 0);
960
+ let mut taken = Taken::new();
961
+ lock(shard).insert(2, Arc::new(vec![0; 500]), 500, Priority::Normal, &mut taken);
962
+ assert!(lock(shard).peek(&2).is_none(), "nor is a new one cached");
963
+ }
964
+
965
+ /// #1761: a shrink can't evict what's pinned; those entries stay over the new share until
966
+ /// their handles drop, and the resize returns rather than spinning on them.
967
+ #[test]
968
+ fn a_shrink_stops_at_pinned_entries() {
969
+ let s: Sharded<u32, Vec<u8>> = Sharded::new(1000, 1, 1, 0);
970
+ let shard = s.shard(0).unwrap();
971
+ let mut held = Vec::new();
972
+ for k in 0..10u32 {
973
+ let v = Arc::new(vec![0u8; 50]);
974
+ held.push(Arc::clone(&v));
975
+ let mut taken = Taken::new();
976
+ lock(shard).insert(k, v, 50, Priority::Normal, &mut taken);
977
+ }
978
+ for k in 10..20u32 {
979
+ let mut taken = Taken::new();
980
+ lock(shard).insert(k, Arc::new(vec![0u8; 50]), 50, Priority::Normal, &mut taken);
981
+ }
982
+ assert_eq!(s.usage(), 1000);
983
+ s.set_capacity(100);
984
+ assert_eq!(s.usage(), 500, "only the ten pinned entries are left");
985
+ drop(held);
986
+ // The next change evicts down to the new capacity.
987
+ let mut taken = Taken::new();
988
+ lock(shard).insert(
989
+ 99,
990
+ Arc::new(vec![0u8; 10]),
991
+ 10,
992
+ Priority::Normal,
993
+ &mut taken,
994
+ );
995
+ drop(taken);
996
+ assert!(s.usage() <= 100, "usage {}", s.usage());
997
+ }
998
+
999
+ /// #1761: a grow evicts nothing, and the cache fills to the new capacity.
1000
+ #[test]
1001
+ fn a_grow_keeps_everything_and_fills_further() {
1002
+ let s = filled(500, 1, 100);
1003
+ assert_eq!(s.usage(), 500);
1004
+ s.set_capacity(2000);
1005
+ assert_eq!(s.usage(), 500, "nothing evicted");
1006
+ let shard = s.shard(0).unwrap();
1007
+ for k in 100..300u32 {
1008
+ let mut taken = Taken::new();
1009
+ lock(shard).insert(k, Arc::new(vec![0; 10]), 10, Priority::Normal, &mut taken);
1010
+ }
1011
+ assert_eq!(s.usage(), 2000);
1012
+ }
1013
+
1014
+ /// perf2's S1 on #1890: two resizes at once end at one of the two totals, every shard at
1015
+ /// its share of it, never a mix.
1016
+ #[test]
1017
+ fn concurrent_resizes_end_at_one_total() {
1018
+ for _ in 0..n(200) {
1019
+ let s = Arc::new(filled(4000, 4, 100));
1020
+ let (a, b) = (Arc::clone(&s), Arc::clone(&s));
1021
+ let one = std::thread::spawn(move || a.set_capacity(400));
1022
+ let two = std::thread::spawn(move || b.set_capacity(2000));
1023
+ one.join().unwrap();
1024
+ two.join().unwrap();
1025
+ let total = s.capacity();
1026
+ assert!(total == 400 || total == 2000, "{total}");
1027
+ let mut sum = 0;
1028
+ s.for_each(|sh, _| {
1029
+ assert_eq!(
1030
+ sh.capacity,
1031
+ total / 4,
1032
+ "every shard at its share of {total}"
1033
+ );
1034
+ sum += sh.capacity;
1035
+ });
1036
+ assert_eq!(sum, total);
1037
+ }
1038
+ }
1039
+
1040
+ /// A disabled (shardless) cache stays disabled.
1041
+ #[test]
1042
+ fn a_resize_of_no_shards_changes_nothing() {
1043
+ let s: Sharded<u32, Vec<u8>> = Sharded::empty();
1044
+ s.set_capacity(1 << 20);
1045
+ assert_eq!(s.capacity(), 0);
1046
+ }
819
1047
  }
@@ -2,13 +2,13 @@
2
2
  //! (`loom` is a dev-dependency, so only the `--cfg loom` test build switches.)
3
3
 
4
4
  #[cfg(all(loom, test))]
5
- pub(crate) use loom::sync::atomic::{AtomicU8, AtomicU64};
5
+ pub(crate) use loom::sync::atomic::{AtomicU8, AtomicU64, AtomicUsize};
6
6
  #[cfg(all(loom, test))]
7
- pub(crate) use loom::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
7
+ pub(crate) use loom::sync::{Arc, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
8
8
  #[cfg(not(all(loom, test)))]
9
- pub(crate) use std::sync::atomic::{AtomicU8, AtomicU64};
9
+ pub(crate) use std::sync::atomic::{AtomicU8, AtomicU64, AtomicUsize};
10
10
  #[cfg(not(all(loom, test)))]
11
- pub(crate) use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
11
+ pub(crate) use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
12
12
 
13
13
  /// Locks a shard for changes (insert, eviction, removal), ignoring poison. A panic inside a
14
14
  /// shard's critical section (in practice a debug-build arithmetic overflow in the usage
@@ -2,19 +2,19 @@
2
2
 
3
3
  [dependencies.pigeonhole-cache]
4
4
  path = "../cache"
5
- version = "=3.0.2"
5
+ version = "=3.1.0"
6
6
  [dependencies.pigeonhole-format]
7
7
  path = "../format"
8
- version = "=3.0.2"
8
+ version = "=3.1.0"
9
9
  [dependencies.pigeonhole-io]
10
10
  path = "../io"
11
- version = "=3.0.2"
11
+ version = "=3.1.0"
12
12
  [dependencies.pigeonhole-pager]
13
13
  path = "../pager"
14
- version = "=3.0.2"
14
+ version = "=3.1.0"
15
15
  [dependencies.pigeonhole-sst]
16
16
  path = "../sst"
17
- version = "=3.0.2"
17
+ version = "=3.1.0"
18
18
  [lints.rust.unexpected_cfgs]
19
19
  check-cfg = ["cfg(loom)", "cfg(engine_loom)", "cfg(io_loom)", "cfg(pigeonhole_sim)"]
20
20
  level = "warn"
@@ -29,4 +29,4 @@ name = "pigeonhole-compaction"
29
29
  readme = "README.md"
30
30
  repository = "https://github.com/CodingAnarchy/pigeonhole"
31
31
  rust-version = "1.96"
32
- version = "3.0.2"
32
+ version = "3.1.0"
@@ -447,6 +447,13 @@ pub struct FilteredCursor<C> {
447
447
  /// While parked, the current entry's verdict (with its hint in `hint`), so settling
448
448
  /// does not check it again.
449
449
  verdict: Verdict,
450
+ /// A one-row read (#1924): a jump (a seek to the hint, a row skip) that lands past the
451
+ /// row ends the settle there, valid but not admitted, where the read ends, instead of
452
+ /// jumping on row by row. Beside the other flags, so the cursor is no larger.
453
+ one_row: bool,
454
+ /// Such a jump stop left the cursor on an entry it didn't admit, since
455
+ /// [`FilteredCursor::take_stopped`] last asked (#1931). Set only where a jump stops.
456
+ stopped: bool,
450
457
  }
451
458
 
452
459
  impl<C: Cursor> FilteredCursor<C> {
@@ -460,9 +467,57 @@ impl<C: Cursor> FilteredCursor<C> {
460
467
  lazy: false,
461
468
  parked: false,
462
469
  verdict: Verdict::Admit,
470
+ one_row: false,
471
+ stopped: false,
463
472
  }
464
473
  }
465
474
 
475
+ /// Marks the cursor's reads as one-row (#1924): its caller stops at the first key past
476
+ /// the row, so a jump that leaves the row ends the settle there.
477
+ pub fn one_row(mut self, one_row: bool) -> Self {
478
+ self.one_row = one_row;
479
+ self
480
+ }
481
+
482
+ /// Whether a jump stop has left the cursor on an entry it didn't admit since this was last
483
+ /// asked, clearing it (#1931). A caller that moves on to a key the cursor already stands
484
+ /// at or past settles it there first ([`FilteredCursor::resettle`]).
485
+ #[inline]
486
+ pub fn take_stopped(&mut self) -> bool {
487
+ // Cleared only when set: the common answer is a load.
488
+ if !self.stopped {
489
+ return false;
490
+ }
491
+ self.stopped = false;
492
+ true
493
+ }
494
+
495
+ /// Settles the cursor from where it stands (#1931). In lazy mode it parks on the entry if
496
+ /// that isn't admitted, for a merge to settle under its bound; otherwise the filter's
497
+ /// jumps run from it (and may stop again, past its row).
498
+ pub fn resettle(&mut self) -> Result<(), C::Error> {
499
+ if self.all {
500
+ return Ok(());
501
+ }
502
+ if self.lazy {
503
+ self.land();
504
+ return Ok(());
505
+ }
506
+ self.settle_filtered()
507
+ }
508
+
509
+ /// Whether a jump of a one-row read just left the row it started in: past the row of
510
+ /// `hint` after a seek to it (`seek`), or after a row skip.
511
+ #[inline]
512
+ fn jumped_out(&self, seek: bool) -> bool {
513
+ self.one_row
514
+ && self.inner.valid()
515
+ && (!seek || {
516
+ let k = self.inner.key();
517
+ !k.starts_with(&self.hint) && left_row_of(k, &self.hint)
518
+ })
519
+ }
520
+
466
521
  /// Wraps `inner` in lazy mode (D288), for a source under a [`MergingCursor`]: a move
467
522
  /// lands on the next entry without filtering, and the merge settles the cursor
468
523
  /// ([`Cursor::settle_until`]) only when it reaches the merge's top, up to the next
@@ -537,12 +592,26 @@ impl<C: Cursor> FilteredCursor<C> {
537
592
 
538
593
  fn settle_filtered(&mut self) -> Result<(), C::Error> {
539
594
  while self.inner.valid() {
540
- match self.filter.check(self.inner.key(), &mut self.hint) {
595
+ let seek = match self.filter.check(self.inner.key(), &mut self.hint) {
541
596
  Verdict::Admit => break,
542
597
  // Nothing later in this row can be admitted.
543
- Verdict::SkipRow => self.inner.skip_row()?,
544
- Verdict::Seek => self.inner.seek(&self.hint)?,
545
- Verdict::Next => self.inner.next()?,
598
+ Verdict::SkipRow => {
599
+ self.inner.skip_row()?;
600
+ false
601
+ }
602
+ Verdict::Seek => {
603
+ self.inner.seek(&self.hint)?;
604
+ true
605
+ }
606
+ Verdict::Next => {
607
+ self.inner.next()?;
608
+ continue;
609
+ }
610
+ };
611
+ // A one-row read's jump past its row ends it there (#1924), as in `SstIter`.
612
+ if self.jumped_out(seek) {
613
+ self.stopped = true;
614
+ break;
546
615
  }
547
616
  }
548
617
  Ok(())
@@ -590,6 +659,12 @@ impl<C: Cursor> Cursor for FilteredCursor<C> {
590
659
  if !self.inner.valid() {
591
660
  break;
592
661
  }
662
+ // A one-row read's jump past its row ends it there, settled (#1924): alone in
663
+ // the merge (`bound` `None`) it would otherwise jump on through its later rows.
664
+ if verdict != Verdict::Next && self.jumped_out(verdict == Verdict::Seek) {
665
+ self.stopped = true;
666
+ break;
667
+ }
593
668
  verdict = self.filter.check(self.inner.key(), &mut self.hint);
594
669
  }
595
670
  self.parked = false;
@@ -698,3 +773,10 @@ impl Cursor for VecCursor {
698
773
  Ok(())
699
774
  }
700
775
  }
776
+
777
+ /// Whether `key` is past the row `hint` (a row prefix and a qualifier bound) starts with
778
+ /// ([`FilteredCursor::one_row`]'s slow case, once a key doesn't start with the whole hint).
779
+ #[cold]
780
+ fn left_row_of(key: &[u8], hint: &[u8]) -> bool {
781
+ pigeonhole_format::key::row_prefix_len(hint).is_ok_and(|n| !key.starts_with(&hint[..n]))
782
+ }
@@ -122,6 +122,28 @@ impl StreamGc {
122
122
  policy: &GcPolicy,
123
123
  family: &FamilyOptions,
124
124
  merge: Option<Arc<dyn MergeOperator>>,
125
+ ) -> Self {
126
+ Self::with(policy, family, merge, false, None)
127
+ }
128
+
129
+ /// GC as a compaction job runs it: also folding a counter family's operands into the
130
+ /// base below them (D298 §4.2), reading a separated base through `base_blobs`. A range
131
+ /// move's copy writes exactly what a compaction would (D331, simp724).
132
+ pub fn folding(
133
+ policy: &GcPolicy,
134
+ family: &FamilyOptions,
135
+ merge: Option<Arc<dyn MergeOperator>>,
136
+ base_blobs: Option<Arc<dyn BlobFetch>>,
137
+ ) -> Self {
138
+ Self::with(policy, family, merge, true, base_blobs)
139
+ }
140
+
141
+ fn with(
142
+ policy: &GcPolicy,
143
+ family: &FamilyOptions,
144
+ merge: Option<Arc<dyn MergeOperator>>,
145
+ fold_bases: bool,
146
+ base_blobs: Option<Arc<dyn BlobFetch>>,
125
147
  ) -> Self {
126
148
  Self {
127
149
  gc: Gc::new(GcConfig {
@@ -135,8 +157,8 @@ impl StreamGc {
135
157
  counter: family.kind.is_aggregate(),
136
158
  other_sources: policy.other_sources.clone(),
137
159
  no_outside_deletes: policy.no_outside_deletes,
138
- fold_bases: false,
139
- base_blobs: None,
160
+ fold_bases,
161
+ base_blobs,
140
162
  }),
141
163
  out: OutBuf::default(),
142
164
  }