/rust/registry/src/index.crates.io-1949cf8c6b5b557f/surrealmx-0.18.0/src/inner.rs
Line | Count | Source |
1 | | // Copyright © SurrealDB Ltd |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | //! This module stores the inner in-memory database type. |
16 | | |
17 | | use crate::oracle::Oracle; |
18 | | #[cfg(not(target_arch = "wasm32"))] |
19 | | use crate::persistence::Persistence; |
20 | | use crate::queue::{Commit, Merge}; |
21 | | use crate::versions::Versions; |
22 | | use crate::DatabaseOptions; |
23 | | use bytes::Bytes; |
24 | | use crossbeam_skiplist::SkipMap; |
25 | | use parking_lot::RwLock; |
26 | | use std::sync::atomic::{AtomicBool, AtomicU64}; |
27 | | use std::sync::Arc; |
28 | | #[cfg(not(target_arch = "wasm32"))] |
29 | | use std::thread::JoinHandle; |
30 | | use std::time::Duration; |
31 | | |
32 | | /// The inner structure of the transactional in-memory database |
33 | | pub struct Inner { |
34 | | /// The timestamp version oracle |
35 | | pub(crate) oracle: Arc<Oracle>, |
36 | | /// The underlying lock-free Skip Map datastructure |
37 | | pub(crate) datastore: SkipMap<Bytes, RwLock<Versions>>, |
38 | | /// A count of total transactions grouped by oracle version |
39 | | pub(crate) counter_by_oracle: SkipMap<u64, Arc<AtomicU64>>, |
40 | | /// A count of total transactions grouped by commit id |
41 | | pub(crate) counter_by_commit: SkipMap<u64, Arc<AtomicU64>>, |
42 | | /// The transaction commit queue attempt sequence number |
43 | | pub(crate) transaction_queue_id: AtomicU64, |
44 | | /// The transaction commit queue success sequence number |
45 | | pub(crate) transaction_commit_id: AtomicU64, |
46 | | /// The transaction merge queue attempt sequence number |
47 | | pub(crate) transaction_merge_id: AtomicU64, |
48 | | /// The transaction commit queue list of modifications |
49 | | pub(crate) transaction_commit_queue: SkipMap<u64, Arc<Commit>>, |
50 | | /// Transaction updates which are committed but not yet applied |
51 | | pub(crate) transaction_merge_queue: SkipMap<u64, Arc<Merge>>, |
52 | | /// The epoch duration to determine how long to store versioned data |
53 | | pub(crate) garbage_collection_epoch: RwLock<Option<Duration>>, |
54 | | /// Optional persistence handler |
55 | | #[cfg(not(target_arch = "wasm32"))] |
56 | | pub(crate) persistence: RwLock<Option<Arc<Persistence>>>, |
57 | | /// Specifies whether background worker threads are enabled |
58 | | pub(crate) background_threads_enabled: AtomicBool, |
59 | | /// Stores a handle to the current transaction cleanup background thread |
60 | | #[cfg(not(target_arch = "wasm32"))] |
61 | | pub(crate) transaction_cleanup_handle: RwLock<Option<JoinHandle<()>>>, |
62 | | /// Stores a handle to the current garbage collection background thread |
63 | | #[cfg(not(target_arch = "wasm32"))] |
64 | | pub(crate) garbage_collection_handle: RwLock<Option<JoinHandle<()>>>, |
65 | | /// Threshold after which transaction state is reset |
66 | | pub(crate) reset_threshold: usize, |
67 | | } |
68 | | |
69 | | impl Inner { |
70 | | /// Create a new [`Inner`] structure with the given oracle resync interval. |
71 | 195 | pub fn new(opts: &DatabaseOptions) -> Self { |
72 | 195 | Self { |
73 | 195 | oracle: Oracle::new(opts.resync_interval), |
74 | 195 | datastore: SkipMap::new(), |
75 | 195 | counter_by_oracle: SkipMap::new(), |
76 | 195 | counter_by_commit: SkipMap::new(), |
77 | 195 | transaction_queue_id: AtomicU64::new(0), |
78 | 195 | transaction_commit_id: AtomicU64::new(0), |
79 | 195 | transaction_merge_id: AtomicU64::new(0), |
80 | 195 | transaction_commit_queue: SkipMap::new(), |
81 | 195 | transaction_merge_queue: SkipMap::new(), |
82 | 195 | garbage_collection_epoch: RwLock::new(None), |
83 | 195 | #[cfg(not(target_arch = "wasm32"))] |
84 | 195 | persistence: RwLock::new(None), |
85 | 195 | background_threads_enabled: AtomicBool::new(true), |
86 | 195 | #[cfg(not(target_arch = "wasm32"))] |
87 | 195 | transaction_cleanup_handle: RwLock::new(None), |
88 | 195 | #[cfg(not(target_arch = "wasm32"))] |
89 | 195 | garbage_collection_handle: RwLock::new(None), |
90 | 195 | reset_threshold: opts.reset_threshold, |
91 | 195 | } |
92 | 195 | } |
93 | | } |
94 | | |
95 | | impl Default for Inner { |
96 | 0 | fn default() -> Self { |
97 | 0 | Self::new(&DatabaseOptions::default()) |
98 | 0 | } |
99 | | } |