PST File Format SDK v0.4
Loading...
Searching...
No Matches
context.h
Go to the documentation of this file.
1
2#ifndef PSTSDK_NDB_CONTEXT_H
3#define PSTSDK_NDB_CONTEXT_H
4
5#include <map>
6
7#include "pstsdk/ndb/node.h"
9
10namespace pstsdk
11{
12
13class db_context_impl : public db_context
14{
15public:
16 db_context_impl(const shared_db_ptr& parent)
17 : m_db(parent), m_bbt_root(parent->read_bbt_root(shared_from_this())), m_nbt_root(parent->read_nbt_root(shared_from_this())) { }
18
19 // database implementation
20 node lookup_node(node_id nid)
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);
45
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);
53private:
54 shared_db_ptr m_db;
55 std::shared_ptr<bbt_page> m_bbt_root;
56 block_id m_base_bid;
57 std::shared_ptr<nbt_page> m_nbt_root;
58 node_id m_base_node_id;
59
60 std::map<node_id, node_info> m_nodes; // nodes commited to this context
61 std::vector<node_id> m_deleted_nodes; // nodes deleted in this context
62 std::map<block_id, std::shared_ptr<block>> m_blocks; // blocks commited to this context (null db pointers)
63
64 template<typename T>
65 bool lookup_saved_block(block_id bid, std::shared_ptr<T>& pcached_block);
66 template<typename T>
67 bool add_saved_block(const std::shared_ptr<T>& pblock);
68};
69
70} // end pstsdk namespace
71
72template<typename T>
73bool pstsdk::db_context_impl::lookup_saved_block(block_id bid, std::shared_ptr<T>& pcached_block)
74{
75 auto pos = m_blocks.find(bid);
76 if(pos != m_blocks.end())
77 {
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;
81 return true;
82 }
83 return false;
84}
85
86template<typename T>
87bool pstsdk::db_context_impl::add_saved_block(const std::shared_ptr<T>& pblock)
88{
89 auto pos = m_blocks.find(pblock->get_id());
90 if(pos == m_blocks.end())
91 {
92 std::shared_ptr<block> pcopy(new T(*pblock));
93 pcopy->set_db_ptr(shared_db_ptr());
94 m_blocks[pcopy->get_id()] = pcopy;
95 return true;
96 }
97 return false;
98}
99
100pstsdk::node_info pstsdk::db_context_impl::lookup_node_info(node_id nid)
101{
102 auto pos = m_nodes.find(nid);
103 if(pos != m_nodes.end())
104 return pos->second;
105
106 return read_nbt_root()->lookup_node_info(nid);
107}
108
109pstsdk::block_info pstsdk::db_context_impl::lookup_block_info(block_id bid)
110{
111 std::shared_ptr<block> pblock;
112 if(lookup_cached_block(bid, pblock))
113 {
114 block_info bi;
115 bi.address = 0;
116 bi.ref_count = 0;
117 bi.id = pblock->get_id();
118 bi.size = pblock->get_disk_size();
119 return bi;
120 }
121
122 return read_bbt_root()->lookup_block_info(bid);
123}
124
125std::shared_ptr<pstsdk::block> pstsdk::db_context_impl::read_block(block_id bid)
126{
127 std::shared_ptr<pstsdk::block> presult;
128 if(lookup_saved_block(bid, presult))
129 return presult;
130 return m_db->read_block(shared_from_this(), lookup_block_info(bid));
131}
132
133std::shared_ptr<pstsdk::data_block> pstsdk::db_context_impl::read_data_block(block_id bid)
134{
135 std::shared_ptr<pstsdk::data_block> presult;
136 if(lookup_saved_block(bid, presult))
137 return presult;
138 return m_db->read_data_block(shared_from_this(), lookup_block_info(bid));
139}
140
141std::shared_ptr<pstsdk::extended_block> pstsdk::db_context_impl::read_extended_block(block_id bid)
142{
143 std::shared_ptr<pstsdk::extended_block> presult;
144 if(lookup_saved_block(bid, presult))
145 return presult;
146 return m_db->read_extended_block(shared_from_this(), lookup_block_info(bid));
147}
148
149std::shared_ptr<pstsdk::external_block> pstsdk::db_context_impl::read_external_block(block_id bid)
150{
151 std::shared_ptr<pstsdk::external_block> presult;
152 if(lookup_saved_block(bid, presult))
153 return presult;
154 return m_db->read_external_block(shared_from_this(), lookup_block_info(bid));
155}
156
157std::shared_ptr<pstsdk::subnode_block> pstsdk::db_context_impl::read_subnode_block(block_id bid)
158{
159 std::shared_ptr<pstsdk::subnode_block> presult;
160 if(lookup_saved_block(bid, presult))
161 return presult;
162 return m_db->read_subnode_block(shared_from_this(), lookup_block_info(bid));
163}
164
165std::shared_ptr<pstsdk::subnode_leaf_block> pstsdk::db_context_impl::read_subnode_leaf_block(block_id bid)
166{
167 std::shared_ptr<pstsdk::subnode_leaf_block> presult;
168 if(lookup_saved_block(bid, presult))
169 return presult;
170 return m_db->read_subnode_leaf_block(shared_from_this(), lookup_block_info(bid));
171}
172
173std::shared_ptr<pstsdk::subnode_nonleaf_block> pstsdk::db_context_impl::read_subnode_nonleaf_block(block_id bid)
174{
175 std::shared_ptr<pstsdk::subnode_nonleaf_block> presult;
176 if(lookup_saved_block(bid, presult))
177 return presult;
178 return m_db->read_subnode_nonleaf_block(shared_from_this(), lookup_block_info(bid));
179}
180
181#endif
Database interface.
boost::uint64_t ulonglong
Definition primitives.h:70
ulong node_id
Definition primitives.h:86
ulonglong block_id
Definition primitives.h:87
Contains the definition of all in memory representations of disk structures.
Definition disk.h:19
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.