-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdatabase.hpp
111 lines (93 loc) · 4.02 KB
/
database.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (C) 2014-2015 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef FOONATHAN_STRING_ID_DATABASE_HPP_INCLUDED
#define FOONATHAN_STRING_ID_DATABASE_HPP_INCLUDED
#include <memory>
#include <mutex>
#include "basic_database.hpp"
#include "config.hpp"
namespace foonathan { namespace string_id
{
/// \brief A database that doesn't store the string-values.
/// \detail It does not detect collisions or allows retrieving,
/// \c lookup() returns "string_id database disabled".
class dummy_database : public basic_database
{
public:
insert_status insert(hash_type, const char *, std::size_t) FOONATHAN_OVERRIDE
{
return new_string;
}
insert_status insert_prefix(hash_type, hash_type, const char *, std::size_t) FOONATHAN_OVERRIDE
{
return new_string;
}
const char* lookup(hash_type) const FOONATHAN_NOEXCEPT FOONATHAN_OVERRIDE
{
return "string_id database disabled";
}
};
/// \brief A database that uses a highly optimized hash table.
class map_database : public basic_database
{
public:
/// \brief Creates a new database with given number of buckets and maximum load factor.
explicit map_database(std::size_t size = 1024, double max_load_factor = 1.0);
~map_database() FOONATHAN_NOEXCEPT;
insert_status insert(hash_type hash, const char *str, std::size_t length) FOONATHAN_OVERRIDE;
insert_status insert_prefix(hash_type hash, hash_type prefix,
const char *str, std::size_t length) FOONATHAN_OVERRIDE;
const char* lookup(hash_type hash) const FOONATHAN_NOEXCEPT FOONATHAN_OVERRIDE;
private:
void rehash();
class node_list;
std::unique_ptr<node_list[]> buckets_;
std::size_t no_items_, no_buckets_;
double max_load_factor_;
std::size_t next_resize_;
};
/// \brief A thread-safe database adapter.
/// \detail It derives from any database type and synchronizes access via \c std::mutex.
template <class Database>
class thread_safe_database : public Database
{
public:
/// \brief The base database.
typedef Database base_database;
// workaround of lacking inheriting constructors
template <typename ... Args>
explicit thread_safe_database(Args&&... args)
: base_database(std::forward<Args>(args)...) {}
typename Database::insert_status
insert(hash_type hash, const char *str, std::size_t length) FOONATHAN_OVERRIDE
{
std::lock_guard<std::mutex> lock(mutex_);
return Database::insert(hash, str, length);
}
typename Database::insert_status
insert_prefix(hash_type hash, hash_type prefix, const char *str, std::size_t length) FOONATHAN_OVERRIDE
{
std::lock_guard<std::mutex> lock(mutex_);
return Database::insert_prefix(hash, prefix, str, length);
}
const char* lookup(hash_type hash) const FOONATHAN_NOEXCEPT FOONATHAN_OVERRIDE
{
std::lock_guard<std::mutex> lock(mutex_);
return Database::lookup(hash);
}
private:
mutable std::mutex mutex_;
};
/// \brief The default database where the strings are stored.
/// \detail Its exact type is one of the previous listed databases.
/// You can control its selection via the macros listed in config.hpp.in.
#if FOONATHAN_STRING_ID_DATABASE && FOONATHAN_STRING_ID_MULTITHREADED
typedef thread_safe_database<map_database> default_database;
#elif FOONATHAN_STRING_ID_DATABASE
typedef map_database default_database;
#else
typedef dummy_database default_database;
#endif
}} // namespace foonathan::string_id
#endif // FOONATHAN_STRING_ID_DATABASE_HPP_INCLUDED