bitcoinleveldb_table/
leveldb_db_table_cache.rs

1/*!
2  | Thread-safe (provides internal synchronization)
3  |
4  */
5
6crate::ix!();
7
8//-------------------------------------------[.cpp/bitcoin/src/leveldb/db/table_cache.h]
9
10pub struct TableCache {
11    env:     Box<dyn Env>,
12    dbname:  String,
13    options: Rc<Options>,
14    cache:   *mut Cache,
15}
16
17impl Drop for TableCache {
18    fn drop(&mut self) {
19        todo!();
20        /*
21            delete cache_;
22        */
23    }
24}
25
26impl TableCache {
27
28    pub fn new(
29        dbname:  &String,
30        options: &Options,
31        entries: i32) -> Self {
32    
33        todo!();
34        /*
35        : env(options.env),
36        : dbname(dbname),
37        : options(options),
38        : cache(NewLRUCache(entries)),
39        */
40    }
41    
42    pub fn find_table(&mut self, 
43        file_number: u64,
44        file_size:   u64,
45        handle:      *mut *mut CacheHandle) -> crate::Status {
46        
47        todo!();
48        /*
49            Status s;
50      char buf[sizeof(file_number)];
51      EncodeFixed64(buf, file_number);
52      Slice key(buf, sizeof(buf));
53      *handle = cache_->Lookup(key);
54      if (*handle == nullptr) {
55        std::string fname = TableFileName(dbname_, file_number);
56        RandomAccessFile* file = nullptr;
57        Table* table = nullptr;
58        s = env_->NewRandomAccessFile(fname, &file);
59        if (!s.ok()) {
60          std::string old_fname = SSTTableFileName(dbname_, file_number);
61          if (env_->NewRandomAccessFile(old_fname, &file).ok()) {
62            s = Status::OK();
63          }
64        }
65        if (s.ok()) {
66          s = Table::Open(options_, file, file_size, &table);
67        }
68
69        if (!s.ok()) {
70          assert(table == nullptr);
71          delete file;
72          // We do not cache error results so that if the error is transient,
73          // or somebody repairs the file, we recover automatically.
74        } else {
75          TableAndFile* tf = new TableAndFile;
76          tf->file = file;
77          tf->table = table;
78          *handle = cache_->Insert(key, tf, 1, &DeleteEntry);
79        }
80      }
81      return s;
82        */
83    }
84    
85    /**
86      | Return an iterator for the specified file
87      | number (the corresponding file length must be
88      | exactly "file_size" bytes).  If "tableptr" is
89      | non-null, also sets "*tableptr" to point to
90      | the Table object underlying the returned
91      | iterator, or to nullptr if no Table object
92      | underlies the returned iterator.  The
93      | returned "*tableptr" object is owned by the
94      | cache and should not be deleted, and is valid
95      | for as long as the returned iterator is live.
96      */
97    pub fn new_iterator(&mut self, 
98        options:     &ReadOptions,
99        file_number: u64,
100        file_size:   u64,
101        tableptr:    *mut *mut crate::table::Table) -> *mut LevelDBIterator {
102        
103        todo!();
104        /*
105            if (tableptr != nullptr) {
106        *tableptr = nullptr;
107      }
108
109      CacheHandle* handle = nullptr;
110      Status s = FindTable(file_number, file_size, &handle);
111      if (!s.ok()) {
112        return NewErrorIterator(s);
113      }
114
115      Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
116      Iterator* result = table->NewIterator(options);
117      result->RegisterCleanup(&UnrefEntry, cache_, handle);
118      if (tableptr != nullptr) {
119        *tableptr = table;
120      }
121      return result;
122        */
123    }
124    
125    /**
126      | If a seek to internal key "k" in specified
127      | file finds an entry, call
128      | (*handle_result)(arg, found_key,
129      | found_value).
130      */
131    pub fn get(&mut self, 
132        options:       &ReadOptions,
133        file_number:   u64,
134        file_size:     u64,
135        k:             &Slice,
136        arg:           *mut c_void,
137        handle_result: fn(
138                _0: *mut c_void,
139                _1: &Slice,
140                _2: &Slice
141        ) -> c_void) -> crate::Status {
142        
143        todo!();
144        /*
145            CacheHandle* handle = nullptr;
146      Status s = FindTable(file_number, file_size, &handle);
147      if (s.ok()) {
148        Table* t = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
149        s = t->InternalGet(options, k, arg, handle_result);
150        cache_->Release(handle);
151      }
152      return s;
153        */
154    }
155    
156    /**
157      | Evict any entry for the specified file
158      | number
159      |
160      */
161    pub fn evict(&mut self, file_number: u64)  {
162        
163        todo!();
164        /*
165            char buf[sizeof(file_number)];
166      EncodeFixed64(buf, file_number);
167      cache_->Erase(Slice(buf, sizeof(buf)));
168        */
169    }
170}
171
172//-------------------------------------------[.cpp/bitcoin/src/leveldb/db/table_cache.cc]
173
174pub struct TableAndFile {
175    file:  *mut dyn RandomAccessFile,
176    table: *mut table::Table,
177}
178
179pub fn delete_entry(
180        key_:  &Slice,
181        value: *mut c_void)  {
182    
183    todo!();
184        /*
185            TableAndFile* tf = reinterpret_cast<TableAndFile*>(value);
186      delete tf->table;
187      delete tf->file;
188      delete tf;
189        */
190}
191
192pub fn unref_entry(
193        arg1: *mut c_void,
194        arg2: *mut c_void)  {
195    
196    todo!();
197        /*
198            Cache* cache = reinterpret_cast<Cache*>(arg1);
199      CacheHandle* h = reinterpret_cast<CacheHandle*>(arg2);
200      cache->Release(h);
201        */
202}