VSQLite++
0.3
Toggle main menu visibility
Loading...
Searching...
No Matches
include
sqlite
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
51
namespace
sqlite
{
52
inline
namespace
v2
{
53
55
class
connection_pool
{
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
;
71
~lease
();
72
73
connection
&
operator*
()
const
;
74
connection
*
operator->
()
const
;
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
88
connection_pool
(std::size_t
capacity
,
connection_factory
factory);
89
94
static
connection_factory
make_factory
(std::string db,
95
open_mode
mode =
open_mode::open_or_create
,
96
filesystem_adapter_ptr
fs = {});
97
101
lease
acquire
();
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
sqlite::v2::connection_pool::lease
Definition
connection_pool.hpp:63
sqlite::v2::connection_pool::lease::connection_
std::shared_ptr< connection > connection_
Definition
connection_pool.hpp:81
sqlite::v2::connection_pool::lease::shared
std::shared_ptr< connection > shared() const
sqlite::v2::connection_pool::lease::lease
lease(lease &&other) noexcept
sqlite::v2::connection_pool::lease::lease
lease(connection_pool *pool, std::shared_ptr< connection > conn)
sqlite::v2::connection_pool::lease::release
void release()
sqlite::v2::connection_pool::lease::operator=
lease & operator=(lease &&other) noexcept
sqlite::v2::connection_pool::lease::state_
std::shared_ptr< shared_state > state_
Definition
connection_pool.hpp:80
sqlite::v2::connection_pool::lease::lease
lease(lease const &)=delete
sqlite::v2::connection_pool::lease::operator=
lease & operator=(lease const &)=delete
sqlite::v2::connection_pool::lease::lease
lease()=default
sqlite::v2::connection_pool::lease::~lease
~lease()
sqlite::v2::connection_pool::lease::operator->
connection * operator->() const
sqlite::v2::connection_pool::lease::operator*
connection & operator*() const
sqlite::connection_pool
Thread-safe pool for leasing reusable SQLite connections.
Definition
connection_pool.hpp:55
sqlite::v2::connection_pool::created_count
std::size_t created_count() const
Number of connections that have been created so far.
sqlite::v2::connection_pool::make_factory
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.
sqlite::v2::connection_pool::state_
std::shared_ptr< pool_state > state_
Definition
connection_pool.hpp:116
sqlite::v2::connection_pool::acquire
lease acquire()
Blocks until a connection is available and returns a scoped lease.
sqlite::v2::connection_pool::release
void release(std::shared_ptr< connection > conn)
sqlite::v2::connection_pool::connection_factory
std::function< std::shared_ptr< connection >()> connection_factory
Definition
connection_pool.hpp:59
sqlite::v2::connection_pool::connection_pool
connection_pool(std::size_t capacity, connection_factory factory)
Constructs a pool with a maximum capacity and a factory used to create new connections.
sqlite::v2::connection_pool::idle_count
std::size_t idle_count() const
Number of idle connections currently waiting in the pool.
sqlite::v2::connection_pool::capacity
std::size_t capacity() const
Maximum number of concurrent connections the pool will create.
connection.hpp
Owning RAII wrapper for sqlite3* handles plus attachment helpers and statement caching.
sqlite::v2
Definition
backup.hpp:18
sqlite::v2::filesystem_adapter_ptr
std::shared_ptr< filesystem_adapter > filesystem_adapter_ptr
Definition
filesystem_adapter.hpp:76
sqlite::v2::open_mode
open_mode
Definition
connection.hpp:51
sqlite::v2::open_mode::open_or_create
@ open_or_create
Opens an existing database or creates it on demand.
Definition
connection.hpp:54
sqlite
Definition
backup.hpp:17
sqlite::v2::connection
connection is used to open, close, attach and detach a database. Further it has to be passed to all c...
Definition
connection.hpp:64
Generated by
1.17.0