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 struct pool_state;
57
58 public:
59 using connection_factory = std::function<std::shared_ptr<connection>()>;
60
63 class lease {
64 public:
65 lease() = default;
66 lease(connection_pool *pool, std::shared_ptr<connection> conn);
67 lease(lease &&other) noexcept;
68 lease &operator=(lease &&other) noexcept;
69 lease(lease const &) = delete;
70 lease &operator=(lease const &) = delete;
72
75 std::shared_ptr<connection> shared() const;
76
77 private:
78 struct shared_state;
79 void release();
80 std::shared_ptr<shared_state> state_;
81 std::shared_ptr<connection> connection_;
82 };
83
89
94 static connection_factory make_factory(std::string db,
97
102
104 std::size_t capacity() const;
105
107 std::size_t idle_count() const;
108
110 std::size_t created_count() const;
111
112 private:
113 friend class lease;
114 void release(std::shared_ptr<connection> conn);
115
116 std::shared_ptr<pool_state> state_;
117 };
118
119} // namespace v2
120} // namespace sqlite
121
122#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.
std::shared_ptr< pool_state > state_
lease acquire()
Blocks until a connection is available and returns a scoped lease.
void release(std::shared_ptr< connection > conn)
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...