scc/tree_index.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
//! [`TreeIndex`] is a read-optimized concurrent and asynchronous B-plus tree.
mod internal_node;
mod leaf;
mod leaf_node;
mod node;
use crate::ebr::{AtomicShared, Guard, Ptr, Shared, Tag};
use crate::wait_queue::AsyncWait;
use crate::Comparable;
use leaf::{InsertResult, Leaf, RemoveResult, Scanner};
use node::Node;
use std::fmt::{self, Debug};
use std::iter::FusedIterator;
use std::marker::PhantomData;
use std::ops::Bound::{Excluded, Included, Unbounded};
use std::ops::RangeBounds;
use std::panic::UnwindSafe;
use std::pin::Pin;
use std::sync::atomic::Ordering::{AcqRel, Acquire};
/// Scalable concurrent B-plus tree.
///
/// [`TreeIndex`] is a concurrent and asynchronous B-plus tree variant that is optimized for read
/// operations. Read operations, such as read, iteration over entries, are neither blocked nor
/// interrupted by other threads or tasks. Write operations, such as insert, remove, do not block
/// if structural changes are not required.
///
/// ## Notes
///
/// [`TreeIndex`] methods are linearizable, however its iterator methods are not; [`Iter`] and
/// [`Range`] are only guaranteed to observe events happened before the first call to
/// [`Iterator::next`].
///
/// ## The key features of [`TreeIndex`]
///
/// * Lock-free-read: read and scan operations do not modify shared data and are never blocked.
/// * Near lock-free write: write operations do not block unless a structural change is needed.
/// * No busy waiting: each node has a wait queue to avoid spinning.
/// * Immutability: the data in the container is immutable until it becomes unreachable.
///
/// ## The key statistics for [`TreeIndex`]
///
/// * The maximum number of entries that a leaf can contain: 14.
/// * The maximum number of leaves or child nodes that a node can point to: 15.
///
/// ## Locking behavior
///
/// Read access is always lock-free and non-blocking. Write access to an entry is also lock-free
/// and non-blocking as long as no structural changes are required. However, when nodes are being
/// split or merged by a write operation, other write operations on keys in the affected range are
/// blocked.
///
/// ### Unwind safety
///
/// [`TreeIndex`] is impervious to out-of-memory errors and panics in user specified code on one
/// condition; `K::drop` and `V::drop` must not panic.
pub struct TreeIndex<K, V> {
root: AtomicShared<Node<K, V>>,
}
/// An iterator over the entries of a [`TreeIndex`].
///
/// An [`Iter`] iterates over all the entries that survive the [`Iter`] in monotonically increasing
/// order.
pub struct Iter<'t, 'g, K, V> {
root: &'t AtomicShared<Node<K, V>>,
leaf_scanner: Option<Scanner<'g, K, V>>,
guard: &'g Guard,
}
/// An iterator over a sub-range of entries in a [`TreeIndex`].
pub struct Range<'t, 'g, K, V, Q: ?Sized, R: RangeBounds<Q>> {
root: &'t AtomicShared<Node<K, V>>,
leaf_scanner: Option<Scanner<'g, K, V>>,
range: R,
check_lower_bound: bool,
check_upper_bound: bool,
guard: &'g Guard,
query: PhantomData<fn() -> Q>,
}
impl<K, V> TreeIndex<K, V> {
/// Creates an empty [`TreeIndex`].
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
/// ```
#[cfg(not(feature = "loom"))]
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
root: AtomicShared::null(),
}
}
/// Creates an empty [`TreeIndex`].
#[cfg(feature = "loom")]
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
root: AtomicShared::null(),
}
}
/// Clears the [`TreeIndex`].
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
///
/// treeindex.clear();
/// assert_eq!(treeindex.len(), 0);
/// ```
#[inline]
pub fn clear(&self) {
if let (Some(root), _) = self.root.swap((None, Tag::None), Acquire) {
root.clear(&Guard::new());
}
}
/// Returns the depth of the [`TreeIndex`].
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
/// assert_eq!(treeindex.depth(), 0);
/// ```
#[inline]
pub fn depth(&self) -> usize {
let guard = Guard::new();
self.root
.load(Acquire, &guard)
.as_ref()
.map_or(0, |root_ref| root_ref.depth(1, &guard))
}
}
impl<K, V> TreeIndex<K, V>
where
K: 'static + Clone + Ord,
V: 'static + Clone,
{
/// Inserts a key-value pair.
///
/// # Errors
///
/// Returns an error along with the supplied key-value pair if the key exists.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
///
/// assert!(treeindex.insert(1, 10).is_ok());
/// assert_eq!(treeindex.insert(1, 11).err().unwrap(), (1, 11));
/// assert_eq!(treeindex.peek_with(&1, |k, v| *v).unwrap(), 10);
/// ```
#[inline]
pub fn insert(&self, mut key: K, mut val: V) -> Result<(), (K, V)> {
let mut new_root = None;
loop {
let guard = Guard::new();
let root_ptr = self.root.load(Acquire, &guard);
if let Some(root_ref) = root_ptr.as_ref() {
match root_ref.insert(key, val, &mut (), &guard) {
Ok(r) => match r {
InsertResult::Success => return Ok(()),
InsertResult::Frozen(k, v) | InsertResult::Retry(k, v) => {
key = k;
val = v;
root_ref.cleanup_link(&key, false, &guard);
}
InsertResult::Duplicate(k, v) => return Err((k, v)),
InsertResult::Full(k, v) => {
let (k, v) = Node::split_root(root_ptr, &self.root, k, v, &guard);
key = k;
val = v;
continue;
}
InsertResult::Retired(k, v) => {
key = k;
val = v;
let _result = Node::cleanup_root(&self.root, &mut (), &guard);
}
},
Err((k, v)) => {
key = k;
val = v;
}
}
}
let node = if let Some(new_root) = new_root.take() {
new_root
} else {
Shared::new(Node::new_leaf_node())
};
if let Err((node, _)) = self.root.compare_exchange(
Ptr::null(),
(Some(node), Tag::None),
AcqRel,
Acquire,
&guard,
) {
new_root = node;
}
}
}
/// Inserts a key-value pair.
///
/// It is an asynchronous method returning an `impl Future` for the caller to await.
///
/// # Errors
///
/// Returns an error along with the supplied key-value pair if the key exists.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
/// let future_insert = treeindex.insert_async(1, 10);
/// ```
#[inline]
pub async fn insert_async(&self, mut key: K, mut val: V) -> Result<(), (K, V)> {
let mut new_root = None;
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
let need_await = {
let guard = Guard::new();
let root_ptr = self.root.load(Acquire, &guard);
if let Some(root_ref) = root_ptr.as_ref() {
match root_ref.insert(key, val, &mut async_wait_pinned, &guard) {
Ok(r) => match r {
InsertResult::Success => return Ok(()),
InsertResult::Frozen(k, v) | InsertResult::Retry(k, v) => {
key = k;
val = v;
root_ref.cleanup_link(&key, false, &guard);
true
}
InsertResult::Duplicate(k, v) => return Err((k, v)),
InsertResult::Full(k, v) => {
let (k, v) = Node::split_root(root_ptr, &self.root, k, v, &guard);
key = k;
val = v;
continue;
}
InsertResult::Retired(k, v) => {
key = k;
val = v;
!Node::cleanup_root(&self.root, &mut async_wait_pinned, &guard)
}
},
Err((k, v)) => {
key = k;
val = v;
true
}
}
} else {
false
}
};
if need_await {
async_wait_pinned.await;
}
let node = if let Some(new_root) = new_root.take() {
new_root
} else {
Shared::new(Node::new_leaf_node())
};
if let Err((node, _)) = self.root.compare_exchange(
Ptr::null(),
(Some(node), Tag::None),
AcqRel,
Acquire,
&Guard::new(),
) {
new_root = node;
}
}
}
/// Removes a key-value pair.
///
/// Returns `false` if the key does not exist.
///
/// Returns `true` if the key existed and the condition was met after marking the entry
/// unreachable; the memory will be reclaimed later.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
///
/// assert!(!treeindex.remove(&1));
/// assert!(treeindex.insert(1, 10).is_ok());
/// assert!(treeindex.remove(&1));
/// ```
#[inline]
pub fn remove<Q>(&self, key: &Q) -> bool
where
Q: Comparable<K> + ?Sized,
{
self.remove_if(key, |_| true)
}
/// Removes a key-value pair.
///
/// Returns `false` if the key does not exist. It is an asynchronous method returning an
/// `impl Future` for the caller to await.
///
/// Returns `true` if the key existed and the condition was met after marking the entry
/// unreachable; the memory will be reclaimed later.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
/// let future_remove = treeindex.remove_async(&1);
/// ```
#[inline]
pub async fn remove_async<Q>(&self, key: &Q) -> bool
where
Q: Comparable<K> + ?Sized,
{
self.remove_if_async(key, |_| true).await
}
/// Removes a key-value pair if the given condition is met.
///
/// Returns `false` if the key does not exist or the condition was not met.
///
/// Returns `true` if the key existed and the condition was met after marking the entry
/// unreachable; the memory will be reclaimed later.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
///
/// assert!(treeindex.insert(1, 10).is_ok());
/// assert!(!treeindex.remove_if(&1, |v| *v == 0));
/// assert!(treeindex.remove_if(&1, |v| *v == 10));
/// ```
#[inline]
pub fn remove_if<Q, F: FnMut(&V) -> bool>(&self, key: &Q, mut condition: F) -> bool
where
Q: Comparable<K> + ?Sized,
{
let mut removed = false;
loop {
let guard = Guard::new();
if let Some(root_ref) = self.root.load(Acquire, &guard).as_ref() {
if let Ok(result) =
root_ref.remove_if::<_, _, _>(key, &mut condition, &mut (), &guard)
{
if matches!(result, RemoveResult::Cleanup) {
root_ref.cleanup_link(key, false, &guard);
}
match result {
RemoveResult::Success => return true,
RemoveResult::Cleanup | RemoveResult::Retired => {
if Node::cleanup_root(&self.root, &mut (), &guard) {
return true;
}
removed = true;
}
RemoveResult::Fail => {
if removed {
if Node::cleanup_root(&self.root, &mut (), &guard) {
return true;
}
} else {
return false;
}
}
RemoveResult::Frozen => (),
}
}
} else {
return removed;
}
}
}
/// Removes a key-value pair if the given condition is met.
///
/// Returns `false` if the key does not exist or the condition was not met. It is an
/// asynchronous method returning an `impl Future` for the caller to await.
///
/// Returns `true` if the key existed and the condition was met after marking the entry
/// unreachable; the memory will be reclaimed later.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
/// let future_remove = treeindex.remove_if_async(&1, |v| *v == 0);
/// ```
#[inline]
pub async fn remove_if_async<Q, F: FnMut(&V) -> bool>(&self, key: &Q, mut condition: F) -> bool
where
Q: Comparable<K> + ?Sized,
{
let mut removed = false;
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let guard = Guard::new();
if let Some(root_ref) = self.root.load(Acquire, &guard).as_ref() {
if let Ok(result) = root_ref.remove_if::<_, _, _>(
key,
&mut condition,
&mut async_wait_pinned,
&guard,
) {
if matches!(result, RemoveResult::Cleanup) {
root_ref.cleanup_link(key, false, &guard);
}
match result {
RemoveResult::Success => return true,
RemoveResult::Cleanup | RemoveResult::Retired => {
if Node::cleanup_root(&self.root, &mut async_wait_pinned, &guard) {
return true;
}
removed = true;
}
RemoveResult::Fail => {
if removed {
if Node::cleanup_root(
&self.root,
&mut async_wait_pinned,
&guard,
) {
return true;
}
} else {
return false;
}
}
RemoveResult::Frozen => (),
}
}
} else {
return removed;
}
}
async_wait_pinned.await;
}
}
/// Removes keys in the specified range.
///
/// This method removes internal nodes that are definitely contained in the specified range
/// first, and then removes remaining entries individually.
///
/// # Notes
///
/// Internally, multiple internal node locks need to be acquired, thus making this method
/// susceptible to lock starvation.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
///
/// for k in 2..8 {
/// assert!(treeindex.insert(k, 1).is_ok());
/// }
///
/// treeindex.remove_range(3..8);
///
/// assert!(treeindex.contains(&2));
/// assert!(!treeindex.contains(&3));
/// ```
#[inline]
pub fn remove_range<Q, R: RangeBounds<Q>>(&self, range: R)
where
Q: Comparable<K> + ?Sized,
{
let start_unbounded = matches!(range.start_bound(), Unbounded);
let guard = Guard::new();
// Remove internal nodes, and individual entries in affected leaves.
//
// It takes O(N) to traverse sub-trees on the range border.
while let Some(root_ref) = self.root.load(Acquire, &guard).as_ref() {
if let Ok(num_children) =
root_ref.remove_range(&range, start_unbounded, None, None, &mut (), &guard)
{
if num_children < 2 && !Node::cleanup_root(&self.root, &mut (), &guard) {
continue;
}
break;
}
}
}
/// Removes keys in the specified range.
///
/// This method removes internal nodes that are definitely contained in the specified range
/// first, and then removes remaining entries individually.
///
/// It is an asynchronous method returning an `impl Future` for the caller to await.
///
/// # Notes
///
/// Internally, multiple internal node locks need to be acquired, thus making this method
/// susceptible to lock starvation.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
///
/// for k in 2..8 {
/// assert!(treeindex.insert(k, 1).is_ok());
/// }
///
/// let future_remove_range = treeindex.remove_range_async(3..8);
/// ```
#[inline]
pub async fn remove_range_async<Q, R: RangeBounds<Q>>(&self, range: R)
where
Q: Comparable<K> + ?Sized,
{
let start_unbounded = matches!(range.start_bound(), Unbounded);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let guard = Guard::new();
// Remove internal nodes, and individual entries in affected leaves.
//
// It takes O(N) to traverse sub-trees on the range border.
if let Some(root_ref) = self.root.load(Acquire, &guard).as_ref() {
if let Ok(num_children) = root_ref.remove_range(
&range,
start_unbounded,
None,
None,
&mut async_wait_pinned,
&guard,
) {
if num_children >= 2
|| Node::cleanup_root(&self.root, &mut async_wait_pinned, &guard)
{
// Completed removal and cleaning up the root.
return;
}
}
} else {
// Nothing to remove.
return;
}
}
async_wait_pinned.await;
}
}
/// Returns a guarded reference to the value for the specified key without acquiring locks.
///
/// Returns `None` if the key does not exist. The returned reference can survive as long as the
/// associated [`Guard`] is alive.
///
/// # Examples
///
/// ```
/// use scc::ebr::Guard;
/// use std::sync::Arc;
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<Arc<str>, u32> = TreeIndex::new();
///
/// let guard = Guard::new();
/// assert!(treeindex.peek("foo", &guard).is_none());
///
/// treeindex.insert("foo".into(), 1).expect("insert in empty TreeIndex");
/// ```
#[inline]
pub fn peek<'g, Q>(&self, key: &Q, guard: &'g Guard) -> Option<&'g V>
where
Q: Comparable<K> + ?Sized,
{
if let Some(root_ref) = self.root.load(Acquire, guard).as_ref() {
return root_ref.search_value(key, guard);
}
None
}
/// Peeks a key-value pair without acquiring locks.
///
/// Returns `None` if the key does not exist.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<Arc<str>, u32> = TreeIndex::new();
///
/// assert!(treeindex.peek_with("foo", |k, v| *v).is_none());
///
/// treeindex.insert("foo".into(), 1).expect("insert in empty TreeIndex");
///
/// let key: Arc<str> = treeindex
/// .peek_with("foo", |k, _v| Arc::clone(k))
/// .expect("peek_with by borrowed key");
/// ```
#[inline]
pub fn peek_with<Q, R, F: FnOnce(&K, &V) -> R>(&self, key: &Q, reader: F) -> Option<R>
where
Q: Comparable<K> + ?Sized,
{
let guard = Guard::new();
self.peek_entry(key, &guard).map(|(k, v)| reader(k, v))
}
/// Returns a guarded reference to the key-value pair for the specified key without acquiring locks.
///
/// Returns `None` if the key does not exist. The returned reference can survive as long as the
/// associated [`Guard`] is alive.
///
/// # Examples
///
/// ```
/// use scc::ebr::Guard;
/// use std::sync::Arc;
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<Arc<str>, u32> = TreeIndex::new();
///
/// let guard = Guard::new();
/// assert!(treeindex.peek_entry("foo", &guard).is_none());
///
/// treeindex.insert("foo".into(), 1).expect("insert in empty TreeIndex");
///
/// let key: Arc<str> = treeindex
/// .peek_entry("foo", &guard)
/// .map(|(k, _v)| Arc::clone(k))
/// .expect("peek_entry by borrowed key");
/// ```
#[inline]
pub fn peek_entry<'g, Q>(&self, key: &Q, guard: &'g Guard) -> Option<(&'g K, &'g V)>
where
Q: Comparable<K> + ?Sized,
{
if let Some(root_ref) = self.root.load(Acquire, guard).as_ref() {
return root_ref.search_entry(key, guard);
}
None
}
/// Returns `true` if the [`TreeIndex`] contains the key.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::default();
///
/// assert!(!treeindex.contains(&1));
/// assert!(treeindex.insert(1, 0).is_ok());
/// assert!(treeindex.contains(&1));
/// ```
#[inline]
pub fn contains<Q>(&self, key: &Q) -> bool
where
Q: Comparable<K> + ?Sized,
{
self.peek(key, &Guard::new()).is_some()
}
/// Returns the size of the [`TreeIndex`].
///
/// It internally scans all the leaf nodes, and therefore the time complexity is O(N).
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
/// assert_eq!(treeindex.len(), 0);
/// ```
#[inline]
pub fn len(&self) -> usize {
let guard = Guard::new();
self.iter(&guard).count()
}
/// Returns `true` if the [`TreeIndex`] is empty.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
///
/// assert!(treeindex.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
let guard = Guard::new();
!self.iter(&guard).any(|_| true)
}
/// Returns an [`Iter`].
///
/// The returned [`Iter`] starts scanning from the minimum key-value pair. Key-value pairs
/// are scanned in ascending order, and key-value pairs that have existed since the invocation
/// of the method are guaranteed to be visited if they are not removed. However, it is possible
/// to visit removed key-value pairs momentarily.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
/// use scc::ebr::Guard;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
///
/// let guard = Guard::new();
/// let mut iter = treeindex.iter(&guard);
/// assert!(iter.next().is_none());
/// ```
#[inline]
pub fn iter<'t, 'g>(&'t self, guard: &'g Guard) -> Iter<'t, 'g, K, V> {
Iter::new(&self.root, guard)
}
/// Returns a [`Range`] that scans keys in the given range.
///
/// Key-value pairs in the range are scanned in ascending order, and key-value pairs that have
/// existed since the invocation of the method are guaranteed to be visited if they are not
/// removed. However, it is possible to visit removed key-value pairs momentarily.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
/// use scc::ebr::Guard;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::new();
///
/// let guard = Guard::new();
/// assert_eq!(treeindex.range(4..=8, &guard).count(), 0);
/// ```
#[inline]
pub fn range<'t, 'g, Q, R: RangeBounds<Q>>(
&'t self,
range: R,
guard: &'g Guard,
) -> Range<'t, 'g, K, V, Q, R>
where
Q: Comparable<K> + ?Sized,
{
Range::new(&self.root, range, guard)
}
}
impl<K, V> Clone for TreeIndex<K, V>
where
K: 'static + Clone + Ord,
V: 'static + Clone,
{
#[inline]
fn clone(&self) -> Self {
let self_clone = Self::default();
for (k, v) in self.iter(&Guard::new()) {
let _reuslt = self_clone.insert(k.clone(), v.clone());
}
self_clone
}
}
impl<K, V> Debug for TreeIndex<K, V>
where
K: 'static + Clone + Debug + Ord,
V: 'static + Clone + Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let guard = Guard::new();
f.debug_map().entries(self.iter(&guard)).finish()
}
}
impl<K, V> Default for TreeIndex<K, V> {
/// Creates a [`TreeIndex`] with the default parameters.
///
/// # Examples
///
/// ```
/// use scc::TreeIndex;
///
/// let treeindex: TreeIndex<u64, u32> = TreeIndex::default();
/// ```
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<K, V> Drop for TreeIndex<K, V> {
#[inline]
fn drop(&mut self) {
self.clear();
}
}
impl<K, V> PartialEq for TreeIndex<K, V>
where
K: 'static + Clone + Ord,
V: 'static + Clone + PartialEq,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
// The key order is preserved, therefore comparing iterators suffices.
let guard = Guard::new();
Iterator::eq(self.iter(&guard), other.iter(&guard))
}
}
impl<K, V> UnwindSafe for TreeIndex<K, V> {}
impl<'t, 'g, K, V> Iter<'t, 'g, K, V> {
#[inline]
fn new(root: &'t AtomicShared<Node<K, V>>, guard: &'g Guard) -> Iter<'t, 'g, K, V> {
Iter::<'t, 'g, K, V> {
root,
leaf_scanner: None,
guard,
}
}
}
impl<'t, 'g, K, V> Debug for Iter<'t, 'g, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Iter")
.field("root", &self.root)
.field("leaf_scanner", &self.leaf_scanner)
.finish()
}
}
impl<'t, 'g, K, V> Iterator for Iter<'t, 'g, K, V>
where
K: 'static + Clone + Ord,
V: 'static + Clone,
{
type Item = (&'g K, &'g V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
// Starts scanning.
if self.leaf_scanner.is_none() {
let root_ptr = self.root.load(Acquire, self.guard);
if let Some(root_ref) = root_ptr.as_ref() {
if let Some(scanner) = root_ref.min(self.guard) {
self.leaf_scanner.replace(scanner);
}
} else {
return None;
}
}
// Go to the next entry.
if let Some(mut scanner) = self.leaf_scanner.take() {
let min_allowed_key = scanner.get().map(|(key, _)| key);
if let Some(result) = scanner.next() {
self.leaf_scanner.replace(scanner);
return Some(result);
}
// Go to the next leaf node.
if let Some(new_scanner) = scanner.jump(min_allowed_key, self.guard) {
if let Some(entry) = new_scanner.get() {
self.leaf_scanner.replace(new_scanner);
return Some(entry);
}
}
}
None
}
}
impl<'t, 'g, K, V> FusedIterator for Iter<'t, 'g, K, V>
where
K: 'static + Clone + Ord,
V: 'static + Clone,
{
}
impl<'t, 'g, K, V> UnwindSafe for Iter<'t, 'g, K, V> {}
impl<'t, 'g, K, V, Q: ?Sized, R: RangeBounds<Q>> Range<'t, 'g, K, V, Q, R> {
#[inline]
fn new(
root: &'t AtomicShared<Node<K, V>>,
range: R,
guard: &'g Guard,
) -> Range<'t, 'g, K, V, Q, R> {
Range::<'t, 'g, K, V, Q, R> {
root,
leaf_scanner: None,
range,
check_lower_bound: true,
check_upper_bound: false,
guard,
query: PhantomData,
}
}
}
impl<'t, 'g, K, V, Q, R> Range<'t, 'g, K, V, Q, R>
where
K: 'static + Clone + Ord,
V: 'static + Clone,
Q: Comparable<K> + ?Sized,
R: RangeBounds<Q>,
{
#[inline]
fn next_unbounded(&mut self) -> Option<(&'g K, &'g V)> {
if self.leaf_scanner.is_none() {
// Start scanning.
let root_ptr = self.root.load(Acquire, self.guard);
if let Some(root_ref) = root_ptr.as_ref() {
let min_allowed_key = match self.range.start_bound() {
Excluded(key) | Included(key) => Some(key),
Unbounded => {
self.check_lower_bound = false;
None
}
};
let mut leaf_scanner = min_allowed_key
.and_then(|min_allowed_key| root_ref.max_le_appr(min_allowed_key, self.guard));
if leaf_scanner.is_none() {
// No `min_allowed_key` is supplied, or no keys smaller than or equal to
// `min_allowed_key` found.
if let Some(mut scanner) = root_ref.min(self.guard) {
// It's possible that the leaf has just been emptied, so go to the next.
scanner.next();
while scanner.get().is_none() {
scanner = scanner.jump(None, self.guard)?;
}
leaf_scanner.replace(scanner);
}
}
if let Some(leaf_scanner) = leaf_scanner {
if let Some(result) = leaf_scanner.get() {
self.set_check_upper_bound(&leaf_scanner);
self.leaf_scanner.replace(leaf_scanner);
return Some(result);
}
}
}
}
// Go to the next entry.
if let Some(mut leaf_scanner) = self.leaf_scanner.take() {
let min_allowed_key = leaf_scanner.get().map(|(key, _)| key);
if let Some(result) = leaf_scanner.next() {
self.leaf_scanner.replace(leaf_scanner);
return Some(result);
}
// Go to the next leaf node.
if let Some(new_scanner) = leaf_scanner.jump(min_allowed_key, self.guard) {
if let Some(entry) = new_scanner.get() {
self.set_check_upper_bound(&new_scanner);
self.leaf_scanner.replace(new_scanner);
return Some(entry);
}
}
}
None
}
#[inline]
fn set_check_upper_bound(&mut self, scanner: &Scanner<K, V>) {
self.check_upper_bound = match self.range.end_bound() {
Excluded(key) => scanner
.max_key()
.map_or(false, |max_key| key.compare(max_key).is_le()),
Included(key) => scanner
.max_key()
.map_or(false, |max_key| key.compare(max_key).is_lt()),
Unbounded => false,
};
}
}
impl<'t, 'g, K, V, Q: ?Sized, R: RangeBounds<Q>> Debug for Range<'t, 'g, K, V, Q, R> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Range")
.field("root", &self.root)
.field("leaf_scanner", &self.leaf_scanner)
.field("check_lower_bound", &self.check_lower_bound)
.field("check_upper_bound", &self.check_upper_bound)
.finish()
}
}
impl<'t, 'g, K, V, Q, R> Iterator for Range<'t, 'g, K, V, Q, R>
where
K: 'static + Clone + Ord,
V: 'static + Clone,
Q: Comparable<K> + ?Sized,
R: RangeBounds<Q>,
{
type Item = (&'g K, &'g V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
while let Some((k, v)) = self.next_unbounded() {
if self.check_lower_bound {
match self.range.start_bound() {
Excluded(key) => {
if key.compare(k).is_ge() {
continue;
}
}
Included(key) => {
if key.compare(k).is_gt() {
continue;
}
}
Unbounded => (),
}
}
self.check_lower_bound = false;
if self.check_upper_bound {
match self.range.end_bound() {
Excluded(key) => {
if key.compare(k).is_gt() {
return Some((k, v));
}
}
Included(key) => {
if key.compare(k).is_ge() {
return Some((k, v));
}
}
Unbounded => {
return Some((k, v));
}
}
break;
}
return Some((k, v));
}
None
}
}
impl<'t, 'g, K, V, Q, R> FusedIterator for Range<'t, 'g, K, V, Q, R>
where
K: 'static + Clone + Ord,
V: 'static + Clone,
Q: Comparable<K> + ?Sized,
R: RangeBounds<Q>,
{
}
impl<'t, 'g, K, V, Q, R> UnwindSafe for Range<'t, 'g, K, V, Q, R>
where
Q: ?Sized,
R: RangeBounds<Q> + UnwindSafe,
{
}