VSQLite++ 0.3
Loading...
Searching...
No Matches
connection_pool.hpp
Go to the documentation of this file.
1/*##############################################################################
2 VSQLite++ - virtuosic bytes SQLite3 C++ wrapper
3
4 Copyright (c) 2024
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15 * Neither the name of virtuosic bytes nor the names of its contributors may
16 be used to endorse or promote products derived from this software without
17 specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30
31##############################################################################*/
32#ifndef GUARD_SQLITE_CONNECTION_POOL_HPP_INCLUDED
33#define GUARD_SQLITE_CONNECTION_POOL_HPP_INCLUDED
34
35#include <condition_variable>
36#include <cstddef>
37#include <functional>
38#include <memory>
39#include <mutex>
40#include <vector>
41
42#include <sqlite/connection.hpp>
43
51namespace sqlite {
52inline namespace v2 {
53
56 public:
57 using connection_factory = std::function<std::shared_ptr<connection>()>;
58
61 class lease {
62 public:
63 lease() = default;
64 lease(connection_pool *pool, std::shared_ptr<connection> conn);
65 lease(lease &&other) noexcept;
66 lease &operator=(lease &&other) noexcept;
67 lease(lease const &) = delete;
68 lease &operator=(lease const &) = delete;
70
73 std::shared_ptr<connection> shared() const;
74
75 private:
76 struct shared_state;
77 void release();
78 std::shared_ptr<shared_state> state_;
79 std::shared_ptr<connection> connection_;
80 };
81
87
92 static connection_factory make_factory(std::string db,
95
100
102 std::size_t capacity() const;
103
105 std::size_t idle_count() const;
106
108 std::size_t created_count() const;
109
110 private:
111 friend class lease;
112 void release(std::shared_ptr<connection> conn);
113
115 std::size_t capacity_;
116 std::size_t created_ = 0;
117 mutable std::mutex mutex_;
118 std::condition_variable cv_;
119 std::vector<std::shared_ptr<connection>> idle_;
120 };
121
122} // namespace v2
123} // namespace sqlite
124
125#endif
std::shared_ptr< connection > connection_
std::shared_ptr< connection > shared() const
lease(lease &&other) noexcept
lease(connection_pool *pool, std::shared_ptr< connection > conn)
lease & operator=(lease &&other) noexcept
std::shared_ptr< shared_state > state_
lease & operator=(lease const &)=delete
connection * operator->() const
connection & operator*() const
Thread-safe pool for leasing reusable SQLite connections.
std::size_t created_count() const
Number of connections that have been created so far.
static connection_factory make_factory(std::string db, open_mode mode=open_mode::open_or_create, filesystem_adapter_ptr fs={})
Helper that captures the parameters for creating connections inside a pool factory.
lease acquire()
Blocks until a connection is available and returns a scoped lease.
void release(std::shared_ptr< connection > conn)
std::condition_variable cv_
std::vector< std::shared_ptr< connection > > idle_
std::function< std::shared_ptr< connection >()> connection_factory
connection_pool(std::size_t capacity, connection_factory factory)
Constructs a pool with a maximum capacity and a factory used to create new connections.
std::size_t idle_count() const
Number of idle connections currently waiting in the pool.
std::size_t capacity() const
Maximum number of concurrent connections the pool will create.
Owning RAII wrapper for sqlite3* handles plus attachment helpers and statement caching.
std::shared_ptr< filesystem_adapter > filesystem_adapter_ptr
@ open_or_create
Opens an existing database or creates it on demand.
connection is used to open, close, attach and detach a database. Further it has to be passed to all c...