2015-01-12 07:59:11 -08:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
/*
|
|
|
|
|
This file is part of Beast: https://github.com/vinniefalco/Beast
|
|
|
|
|
Copyright 2014, Vinnie Falco <vinnie.falco@gmail.com>
|
|
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
|
|
#ifndef BEAST_NUDB_CREATE_H_INCLUDED
|
|
|
|
|
#define BEAST_NUDB_CREATE_H_INCLUDED
|
|
|
|
|
|
|
|
|
|
#include <beast/nudb/file.h>
|
|
|
|
|
#include <beast/nudb/detail/bucket.h>
|
|
|
|
|
#include <beast/nudb/detail/format.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstring>
|
2015-02-03 07:46:24 -08:00
|
|
|
#include <random>
|
2015-01-12 07:59:11 -08:00
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
namespace beast {
|
|
|
|
|
namespace nudb {
|
|
|
|
|
|
2015-02-03 07:46:24 -08:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
|
|
template <class = void>
|
|
|
|
|
std::uint64_t
|
|
|
|
|
make_uid()
|
|
|
|
|
{
|
|
|
|
|
std::random_device rng;
|
|
|
|
|
std::mt19937_64 gen {rng()};
|
|
|
|
|
std::uniform_int_distribution <std::size_t> dist;
|
|
|
|
|
return dist(gen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Generate a random salt. */
|
|
|
|
|
template <class = void>
|
|
|
|
|
std::uint64_t
|
|
|
|
|
make_salt()
|
|
|
|
|
{
|
|
|
|
|
std::random_device rng;
|
|
|
|
|
std::mt19937_64 gen {rng()};
|
|
|
|
|
std::uniform_int_distribution <std::size_t> dist;
|
|
|
|
|
return dist(gen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns the best guess at the volume's block size. */
|
|
|
|
|
inline
|
|
|
|
|
std::size_t
|
|
|
|
|
block_size (path_type const& /*path*/)
|
|
|
|
|
{
|
|
|
|
|
return 4096;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-12 07:59:11 -08:00
|
|
|
/** Create a new database.
|
|
|
|
|
Preconditions:
|
|
|
|
|
The files must not exist
|
|
|
|
|
Throws:
|
|
|
|
|
|
|
|
|
|
@param args Arguments passed to File constructors
|
|
|
|
|
@return `false` if any file could not be created.
|
|
|
|
|
*/
|
2015-02-03 07:46:24 -08:00
|
|
|
template <
|
|
|
|
|
class Hasher,
|
|
|
|
|
class Codec,
|
|
|
|
|
class File,
|
|
|
|
|
class... Args
|
|
|
|
|
>
|
2015-01-12 07:59:11 -08:00
|
|
|
bool
|
|
|
|
|
create (
|
|
|
|
|
path_type const& dat_path,
|
|
|
|
|
path_type const& key_path,
|
|
|
|
|
path_type const& log_path,
|
|
|
|
|
std::uint64_t appnum,
|
|
|
|
|
std::uint64_t salt,
|
|
|
|
|
std::size_t key_size,
|
|
|
|
|
std::size_t block_size,
|
2015-02-03 07:46:24 -08:00
|
|
|
float load_factor,
|
|
|
|
|
Args&&... args)
|
2015-01-12 07:59:11 -08:00
|
|
|
{
|
|
|
|
|
using namespace detail;
|
|
|
|
|
if (key_size < 1)
|
|
|
|
|
throw std::domain_error(
|
|
|
|
|
"invalid key size");
|
|
|
|
|
if (block_size > field<std::uint16_t>::max)
|
|
|
|
|
throw std::domain_error(
|
|
|
|
|
"nudb: block size too large");
|
|
|
|
|
if (load_factor <= 0.f)
|
|
|
|
|
throw std::domain_error(
|
|
|
|
|
"nudb: load factor too small");
|
|
|
|
|
if (load_factor >= 1.f)
|
|
|
|
|
throw std::domain_error(
|
|
|
|
|
"nudb: load factor too large");
|
|
|
|
|
auto const capacity =
|
2015-02-03 07:46:24 -08:00
|
|
|
bucket_capacity(block_size);
|
2015-01-12 07:59:11 -08:00
|
|
|
if (capacity < 1)
|
|
|
|
|
throw std::domain_error(
|
|
|
|
|
"nudb: block size too small");
|
2015-02-03 07:46:24 -08:00
|
|
|
File df(args...);
|
|
|
|
|
File kf(args...);
|
|
|
|
|
File lf(args...);
|
|
|
|
|
if (df.create(
|
|
|
|
|
file_mode::append, dat_path))
|
2015-01-12 07:59:11 -08:00
|
|
|
{
|
2015-02-03 07:46:24 -08:00
|
|
|
if (kf.create (
|
|
|
|
|
file_mode::append, key_path))
|
2015-01-12 07:59:11 -08:00
|
|
|
{
|
2015-02-03 07:46:24 -08:00
|
|
|
if (lf.create(
|
|
|
|
|
file_mode::append, log_path))
|
|
|
|
|
goto success;
|
|
|
|
|
File::erase (dat_path);
|
2015-01-12 07:59:11 -08:00
|
|
|
}
|
2015-02-03 07:46:24 -08:00
|
|
|
File::erase (key_path);
|
2015-01-12 07:59:11 -08:00
|
|
|
}
|
2015-02-03 07:46:24 -08:00
|
|
|
return false;
|
|
|
|
|
success:
|
2015-01-12 07:59:11 -08:00
|
|
|
dat_file_header dh;
|
|
|
|
|
dh.version = currentVersion;
|
2015-02-03 07:46:24 -08:00
|
|
|
dh.uid = make_uid();
|
2015-01-12 07:59:11 -08:00
|
|
|
dh.appnum = appnum;
|
|
|
|
|
dh.key_size = key_size;
|
|
|
|
|
|
|
|
|
|
key_file_header kh;
|
|
|
|
|
kh.version = currentVersion;
|
2015-02-03 07:46:24 -08:00
|
|
|
kh.uid = dh.uid;
|
2015-01-12 07:59:11 -08:00
|
|
|
kh.appnum = appnum;
|
2015-02-03 07:46:24 -08:00
|
|
|
kh.key_size = key_size;
|
2015-01-12 07:59:11 -08:00
|
|
|
kh.salt = salt;
|
|
|
|
|
kh.pepper = pepper<Hasher>(salt);
|
|
|
|
|
kh.block_size = block_size;
|
|
|
|
|
// VFALCO Should it be 65536?
|
|
|
|
|
// How do we set the min?
|
|
|
|
|
kh.load_factor = std::min<std::size_t>(
|
|
|
|
|
65536.0 * load_factor, 65535);
|
|
|
|
|
write (df, dh);
|
|
|
|
|
write (kf, kh);
|
|
|
|
|
buffer buf(block_size);
|
|
|
|
|
std::memset(buf.get(), 0, block_size);
|
2015-02-03 07:46:24 -08:00
|
|
|
bucket b (block_size, buf.get(), empty);
|
2015-01-12 07:59:11 -08:00
|
|
|
b.write (kf, block_size);
|
|
|
|
|
// VFALCO Leave log file empty?
|
|
|
|
|
df.sync();
|
|
|
|
|
kf.sync();
|
|
|
|
|
lf.sync();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // nudb
|
|
|
|
|
} // beast
|
|
|
|
|
|
|
|
|
|
#endif
|