forked from springmeyer/mapnik-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_manager.hpp
124 lines (99 loc) · 3.88 KB
/
connection_manager.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
112
113
114
115
116
117
118
119
120
121
122
123
124
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
* 2013 Oleksandr Novychenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MONGODB_CONNECTION_MANAGER_HPP
#define MONGODB_CONNECTION_MANAGER_HPP
#include "connection.hpp"
// mapnik
#include <mapnik/pool.hpp>
#include <mapnik/utils.hpp>
// boost
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/optional.hpp>
// stl
#include <string>
#include <sstream>
using mapnik::Pool;
using mapnik::singleton;
using mapnik::CreateStatic;
template <typename T>
class ConnectionCreator {
boost::optional<std::string> host_, port_;
boost::optional<std::string> dbname_, collection_;
boost::optional<std::string> user_, pass_;
public:
ConnectionCreator(const boost::optional<std::string> &host,
const boost::optional<std::string> &port,
const boost::optional<std::string> &dbname,
const boost::optional<std::string> &collection,
const boost::optional<std::string> &user,
const boost::optional<std::string> &pass)
: host_(host), port_(port),
dbname_(dbname), collection_(collection),
user_(user), pass_(pass) {}
T* operator()() const {
return new T(connection_string(), namespace_string());
}
inline std::string id() const {
return connection_string() + " " + namespace_string();
}
inline std::string connection_string() const {
std::string rs = *host_;
if (port_ && !port_->empty())
rs += ":" + *port_;
return rs;
}
inline std::string namespace_string() const {
return *dbname_ + "." + *collection_;
}
};
class ConnectionManager : public singleton <ConnectionManager,CreateStatic> {
friend class CreateStatic<ConnectionManager>;
typedef Pool<Connection, ConnectionCreator> PoolType;
typedef std::map<std::string, boost::shared_ptr<PoolType> > ContType;
typedef boost::shared_ptr<Connection> HolderType;
ContType pools_;
public:
bool registerPool(const ConnectionCreator<Connection> &creator, size_t initialSize, size_t maxSize) {
ContType::const_iterator itr = pools_.find(creator.id());
if (itr != pools_.end()) {
itr->second->set_initial_size(initialSize);
itr->second->set_max_size(maxSize);
} else
return pools_.insert(
std::make_pair(creator.id(), boost::make_shared<PoolType>(creator, initialSize,maxSize))).second;
return false;
}
boost::shared_ptr<PoolType> getPool(std::string const& key) {
ContType::const_iterator itr = pools_.find(key);
if (itr != pools_.end())
return itr->second;
static const boost::shared_ptr<PoolType> emptyPool;
return emptyPool;
}
ConnectionManager() {}
private:
ConnectionManager(const ConnectionManager&);
ConnectionManager &operator=(const ConnectionManager);
};
#endif // MONGODB_CONNECTION_MANAGER_HPP