File size: 4,354 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::{
    fs,
    path::Path,
    sync::atomic::{AtomicBool, Ordering},
};

use anyhow::Result;

use crate::database::{
    key_value_database::{KeySpace, KeyValueDatabase},
    write_batch::{
        BaseWriteBatch, ConcurrentWriteBatch, SerialWriteBatch, WriteBatch, WriteBuffer,
    },
};

pub fn is_fresh(path: &Path) -> bool {
    fs::exists(path).is_ok_and(|exists| !exists)
}

pub struct FreshDbOptimization<T: KeyValueDatabase> {
    database: T,
    fresh_db: AtomicBool,
}

impl<T: KeyValueDatabase> FreshDbOptimization<T> {
    pub fn new(database: T, fresh_db: bool) -> Self {
        Self {
            database,
            fresh_db: AtomicBool::new(fresh_db),
        }
    }
}

impl<T: KeyValueDatabase> KeyValueDatabase for FreshDbOptimization<T> {
    type ReadTransaction<'l>
        = T::ReadTransaction<'l>
    where
        Self: 'l;

    fn is_empty(&self) -> bool {
        self.fresh_db.load(Ordering::Acquire) || self.database.is_empty()
    }

    fn begin_read_transaction(&self) -> Result<Self::ReadTransaction<'_>> {
        self.database.begin_read_transaction()
    }

    type ValueBuffer<'l>
        = T::ValueBuffer<'l>
    where
        Self: 'l;

    fn get<'l, 'db: 'l>(
        &'l self,
        transaction: &'l Self::ReadTransaction<'db>,
        key_space: super::key_value_database::KeySpace,
        key: &[u8],
    ) -> anyhow::Result<Option<Self::ValueBuffer<'l>>> {
        if self.fresh_db.load(Ordering::Acquire) {
            // Performance optimization when the database was empty
            // It's assumed that no cache entries are removed from the memory cache, but we
            // might change that in future.
            return Ok(None);
        }
        self.database.get(transaction, key_space, key)
    }

    type SerialWriteBatch<'l>
        = FreshDbOptimizationWriteBatch<'l, T::SerialWriteBatch<'l>>
    where
        Self: 'l;

    type ConcurrentWriteBatch<'l>
        = FreshDbOptimizationWriteBatch<'l, T::ConcurrentWriteBatch<'l>>
    where
        Self: 'l;

    fn write_batch(
        &self,
    ) -> Result<WriteBatch<'_, Self::SerialWriteBatch<'_>, Self::ConcurrentWriteBatch<'_>>> {
        Ok(match self.database.write_batch()? {
            WriteBatch::Serial(write_batch) => WriteBatch::serial(FreshDbOptimizationWriteBatch {
                write_batch,
                fresh_db: &self.fresh_db,
            }),
            WriteBatch::Concurrent(write_batch, _) => {
                WriteBatch::concurrent(FreshDbOptimizationWriteBatch {
                    write_batch,
                    fresh_db: &self.fresh_db,
                })
            }
        })
    }
}

pub struct FreshDbOptimizationWriteBatch<'a, B> {
    write_batch: B,
    fresh_db: &'a AtomicBool,
}

impl<'a, B: BaseWriteBatch<'a>> BaseWriteBatch<'a> for FreshDbOptimizationWriteBatch<'a, B> {
    type ValueBuffer<'l>
        = B::ValueBuffer<'l>
    where
        Self: 'l,
        'a: 'l;

    fn get<'l>(&'l self, key_space: KeySpace, key: &[u8]) -> Result<Option<Self::ValueBuffer<'l>>>
    where
        'a: 'l,
    {
        self.write_batch.get(key_space, key)
    }

    fn commit(self) -> Result<()> {
        self.fresh_db.store(false, Ordering::Release);
        self.write_batch.commit()
    }
}

impl<'a, B: SerialWriteBatch<'a>> SerialWriteBatch<'a> for FreshDbOptimizationWriteBatch<'a, B> {
    fn put(
        &mut self,
        key_space: KeySpace,
        key: WriteBuffer<'_>,
        value: WriteBuffer<'_>,
    ) -> Result<()> {
        self.write_batch.put(key_space, key, value)
    }

    fn delete(&mut self, key_space: KeySpace, key: WriteBuffer<'_>) -> Result<()> {
        self.write_batch.delete(key_space, key)
    }

    fn flush(&mut self, key_space: KeySpace) -> Result<()> {
        self.write_batch.flush(key_space)
    }
}

impl<'a, B: ConcurrentWriteBatch<'a>> ConcurrentWriteBatch<'a>
    for FreshDbOptimizationWriteBatch<'a, B>
{
    fn put(&self, key_space: KeySpace, key: WriteBuffer<'_>, value: WriteBuffer<'_>) -> Result<()> {
        self.write_batch.put(key_space, key, value)
    }

    fn delete(&self, key_space: KeySpace, key: WriteBuffer<'_>) -> Result<()> {
        self.write_batch.delete(key_space, key)
    }

    unsafe fn flush(&self, key_space: KeySpace) -> Result<()> {
        unsafe { self.write_batch.flush(key_space) }
    }
}