2#ifndef PSTSDK_NDB_CONTEXT_H
3#define PSTSDK_NDB_CONTEXT_H
13class db_context_impl :
public db_context
17 : m_db(parent), m_bbt_root(parent->read_bbt_root(shared_from_this())), m_nbt_root(parent->read_nbt_root(shared_from_this())) { }
21 {
return node(shared_from_this(), lookup_node_info(nid)); }
22 node_info lookup_node_info(
node_id nid);
23 block_info lookup_block_info(
block_id bid);
24 std::shared_ptr<bbt_page> read_bbt_root() {
return m_bbt_root; }
25 std::shared_ptr<nbt_page> read_nbt_root() {
return m_nbt_root; }
26 std::shared_ptr<bbt_page> read_bbt_page(
ulonglong location)
27 {
return m_db->read_bbt_page(shared_from_this(), location); }
28 std::shared_ptr<nbt_page> read_nbt_page(
ulonglong location)
29 {
return m_db->read_nbt_page(shared_from_this(), location); }
30 std::shared_ptr<nbt_leaf_page> read_nbt_leaf_page(
ulonglong location)
31 {
return m_db->read_nbt_leaf_page(shared_from_this(), location); }
32 std::shared_ptr<bbt_leaf_page> read_bbt_leaf_page(
ulonglong location)
33 {
return m_db->read_bbt_leaf_page(shared_from_this(), location); }
34 std::shared_ptr<nbt_nonleaf_page> read_nbt_nonleaf_page(
ulonglong location)
35 {
return m_db->read_nbt_nonleaf_page(shared_from_this(), location); }
36 std::shared_ptr<bbt_nonleaf_page> read_bbt_nonleaf_page(
ulonglong location)
37 {
return m_db->read_bbt_nonleaf_page(shared_from_this(), location); }
38 std::shared_ptr<block> read_block(
block_id bid);
39 std::shared_ptr<data_block> read_data_block(
block_id bid);
40 std::shared_ptr<extended_block> read_extended_block(
block_id bid);
41 std::shared_ptr<external_block> read_external_block(
block_id bid);
42 std::shared_ptr<subnode_block> read_subnode_block(
block_id bid);
43 std::shared_ptr<subnode_leaf_block> read_subnode_leaf_block(
block_id bid);
44 std::shared_ptr<subnode_nonleaf_block> read_subnode_nonleaf_block(
block_id bid);
46 std::shared_ptr<block> read_block(
const block_info& bi);
47 std::shared_ptr<data_block> read_data_block(
const block_info& bi);
48 std::shared_ptr<extended_block> read_extended_block(
const block_info& bi);
49 std::shared_ptr<external_block> read_external_block(
const block_info& bi);
50 std::shared_ptr<subnode_block> read_subnode_block(
const block_info& bi);
51 std::shared_ptr<subnode_leaf_block> read_subnode_leaf_block(
const block_info& bi);
52 std::shared_ptr<subnode_nonleaf_block> read_subnode_nonleaf_block(
const block_info& bi);
55 std::shared_ptr<bbt_page> m_bbt_root;
57 std::shared_ptr<nbt_page> m_nbt_root;
60 std::map<node_id, node_info> m_nodes;
61 std::vector<node_id> m_deleted_nodes;
62 std::map<block_id, std::shared_ptr<block>> m_blocks;
65 bool lookup_saved_block(
block_id bid, std::shared_ptr<T>& pcached_block);
67 bool add_saved_block(
const std::shared_ptr<T>& pblock);
73bool pstsdk::db_context_impl::lookup_saved_block(block_id bid, std::shared_ptr<T>& pcached_block)
75 auto pos = m_blocks.find(bid);
76 if(pos != m_blocks.end())
78 std::shared_ptr<T> pcopy(
new T(*std::static_pointer_cast<T>(pos->second)));
79 static_pointer_cast<block>(pcopy)->set_db_ptr(shared_from_this());
80 pcached_block = pcopy;
87bool pstsdk::db_context_impl::add_saved_block(
const std::shared_ptr<T>& pblock)
89 auto pos = m_blocks.find(pblock->get_id());
90 if(pos == m_blocks.end())
92 std::shared_ptr<block> pcopy(
new T(*pblock));
94 m_blocks[pcopy->get_id()] = pcopy;
102 auto pos = m_nodes.find(nid);
103 if(pos != m_nodes.end())
106 return read_nbt_root()->lookup_node_info(nid);
111 std::shared_ptr<block> pblock;
112 if(lookup_cached_block(bid, pblock))
117 bi.id = pblock->get_id();
118 bi.size = pblock->get_disk_size();
122 return read_bbt_root()->lookup_block_info(bid);
125std::shared_ptr<pstsdk::block> pstsdk::db_context_impl::read_block(
block_id bid)
127 std::shared_ptr<pstsdk::block> presult;
128 if(lookup_saved_block(bid, presult))
130 return m_db->read_block(shared_from_this(), lookup_block_info(bid));
133std::shared_ptr<pstsdk::data_block> pstsdk::db_context_impl::read_data_block(
block_id bid)
135 std::shared_ptr<pstsdk::data_block> presult;
136 if(lookup_saved_block(bid, presult))
138 return m_db->read_data_block(shared_from_this(), lookup_block_info(bid));
141std::shared_ptr<pstsdk::extended_block> pstsdk::db_context_impl::read_extended_block(
block_id bid)
143 std::shared_ptr<pstsdk::extended_block> presult;
144 if(lookup_saved_block(bid, presult))
146 return m_db->read_extended_block(shared_from_this(), lookup_block_info(bid));
149std::shared_ptr<pstsdk::external_block> pstsdk::db_context_impl::read_external_block(
block_id bid)
151 std::shared_ptr<pstsdk::external_block> presult;
152 if(lookup_saved_block(bid, presult))
154 return m_db->read_external_block(shared_from_this(), lookup_block_info(bid));
157std::shared_ptr<pstsdk::subnode_block> pstsdk::db_context_impl::read_subnode_block(
block_id bid)
159 std::shared_ptr<pstsdk::subnode_block> presult;
160 if(lookup_saved_block(bid, presult))
162 return m_db->read_subnode_block(shared_from_this(), lookup_block_info(bid));
165std::shared_ptr<pstsdk::subnode_leaf_block> pstsdk::db_context_impl::read_subnode_leaf_block(
block_id bid)
167 std::shared_ptr<pstsdk::subnode_leaf_block> presult;
168 if(lookup_saved_block(bid, presult))
170 return m_db->read_subnode_leaf_block(shared_from_this(), lookup_block_info(bid));
173std::shared_ptr<pstsdk::subnode_nonleaf_block> pstsdk::db_context_impl::read_subnode_nonleaf_block(
block_id bid)
175 std::shared_ptr<pstsdk::subnode_nonleaf_block> presult;
176 if(lookup_saved_block(bid, presult))
178 return m_db->read_subnode_nonleaf_block(shared_from_this(), lookup_block_info(bid));
boost::uint64_t ulonglong
Contains the definition of all in memory representations of disk structures.
std::shared_ptr< db_context > shared_db_ptr
Node and Block definitions.
An in memory, database format agnostic version of disk::bbt_leaf_entry.
An in memory, database format agnostic version of disk::nbt_leaf_entry.