@@ -46,6 +46,25 @@ state(File&& df_, File&& kf_, File&& lf_,
4646 " File requirements not met" );
4747}
4848
49+ template <class Hasher , class File >
50+ basic_store<Hasher, File>::state::
51+ state (File&& df_, File&& kf_,
52+ path_type const & dp_, path_type const & kp_,
53+ detail::key_file_header const & kh_)
54+ : df(std::move(df_))
55+ , kf(std::move(kf_))
56+ , dp(dp_)
57+ , kp(kp_)
58+ , hasher(kh_.salt)
59+ , p0(kh_.key_size, " p0" )
60+ , p1(kh_.key_size, " p1" )
61+ , c1(kh_.key_size, kh_.block_size, " c1" )
62+ , kh(kh_)
63+ {
64+ BOOST_STATIC_ASSERT_MSG (is_File<File>::value,
65+ " File requirements not met" );
66+ }
67+
4968// ------------------------------------------------------------------------------
5069
5170template <class Hasher , class File >
@@ -122,9 +141,12 @@ open(
122141 path_type const & dat_path,
123142 path_type const & key_path,
124143 path_type const & log_path,
144+ open_mode mode,
125145 error_code& ec,
126146 Args&&... args)
127147{
148+ bool writable = mode == open_mode::read_write;
149+
128150 static_assert (is_Hasher<Hasher>::value,
129151 " Hasher requirements not met" );
130152 using namespace detail ;
@@ -138,17 +160,23 @@ open(
138160 File df (args...);
139161 File kf (args...);
140162 File lf (args...);
141- df.open (file_mode::append, dat_path, ec);
142- if (ec)
143- return ;
144- kf.open (file_mode::write, key_path, ec);
163+ df.open (writable ? file_mode::append : file_mode::read,
164+ dat_path, ec);
145165 if (ec)
146166 return ;
147- lf.create (file_mode::append, log_path, ec);
167+ kf.open (writable ? file_mode::write : file_mode::read,
168+ key_path, ec);
148169 if (ec)
149170 return ;
150- // VFALCO TODO Erase empty log file if this
151- // function subsequently fails.
171+
172+ if (writable){
173+ lf.create (file_mode::append, log_path, ec);
174+ if (ec)
175+ return ;
176+ // VFALCO TODO Erase empty log file if this
177+ // function subsequently fails.
178+ }
179+
152180 dat_file_header dh;
153181 read (df, dh, ec);
154182 if (ec)
@@ -167,8 +195,12 @@ open(
167195 if (ec)
168196 return ;
169197 boost::optional<state> s;
170- s.emplace (std::move (df), std::move (kf), std::move (lf),
171- dat_path, key_path, log_path, kh);
198+ if (writable)
199+ s.emplace (std::move (df), std::move (kf), std::move (lf),
200+ dat_path, key_path, log_path, kh);
201+ else
202+ s.emplace (std::move (df), std::move (kf),
203+ dat_path, key_path, kh);
172204 thresh_ = std::max<std::size_t >(65536UL ,
173205 kh.load_factor * kh.capacity );
174206 frac_ = thresh_ / 2 ;
@@ -180,10 +212,13 @@ open(
180212 ec = error::short_key_file;
181213 return ;
182214 }
183- dataWriteSize_ = 32 * nudb::block_size (dat_path);
184- logWriteSize_ = 32 * nudb::block_size (log_path);
215+ if (writable){
216+ dataWriteSize_ = 32 * nudb::block_size (dat_path);
217+ logWriteSize_ = 32 * nudb::block_size (log_path);
218+ }
185219 s_.emplace (std::move (*s));
186220 open_ = true ;
221+ is_writable_ = writable;
187222 ctx_->insert (*this );
188223}
189224
0 commit comments