1use std::sync::Arc;
23use rayon::ThreadPool;
45use cuprate_database::{ConcreteEnv, InitError};
67use crate::{
8 service::{
9 read::{init_read_service, init_read_service_with_pool},
10 types::{TxpoolReadHandle, TxpoolWriteHandle},
11 write::init_write_service,
12 },
13 Config,
14};
1516//---------------------------------------------------------------------------------------------------- Init
17#[cold]
18#[inline(never)] // Only called once (?)
19/// Initialize a database & thread-pool, and return a read/write handle to it.
20///
21/// Once the returned handles are [`Drop::drop`]ed, the reader
22/// thread-pool and writer thread will exit automatically.
23///
24/// # Errors
25/// This will forward the error if [`crate::open`] failed.
26pub fn init(
27 config: Config,
28) -> Result<(TxpoolReadHandle, TxpoolWriteHandle, Arc<ConcreteEnv>), InitError> {
29let reader_threads = config.reader_threads;
3031// Initialize the database itself.
32let db = Arc::new(crate::open(config)?);
3334// Spawn the Reader thread pool and Writer.
35let readers = init_read_service(Arc::clone(&db), reader_threads);
36let writer = init_write_service(Arc::clone(&db));
3738Ok((readers, writer, db))
39}
4041#[cold]
42#[inline(never)] // Only called once (?)
43/// Initialize a database, and return a read/write handle to it.
44///
45/// Unlike [`init`] this will not create a thread-pool, instead using
46/// the one passed in.
47///
48/// Once the returned handles are [`Drop::drop`]ed, the reader
49/// thread-pool and writer thread will exit automatically.
50///
51/// # Errors
52/// This will forward the error if [`crate::open`] failed.
53pub fn init_with_pool(
54 config: Config,
55 pool: Arc<ThreadPool>,
56) -> Result<(TxpoolReadHandle, TxpoolWriteHandle, Arc<ConcreteEnv>), InitError> {
57// Initialize the database itself.
58let db = Arc::new(crate::open(config)?);
5960// Spawn the Reader thread pool and Writer.
61let readers = init_read_service_with_pool(Arc::clone(&db), pool);
62let writer = init_write_service(Arc::clone(&db));
6364Ok((readers, writer, db))
65}