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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use super::network::cluster::{Cluster, Server};
use super::client::OrientDBClientInternal;
use super::statement::Statement;
use crate::common::protocol::messages::request::{Close, Query};
use crate::common::protocol::messages::response;
use crate::sync::types::resultset::{PagedResultSet, ResultSet};
use crate::{OrientError, OrientResult};
use r2d2::{ManageConnection, Pool, PooledConnection};
use std::sync::Arc;
pub struct OSession {
pub client_id: i32,
pub session_id: i32,
pub token: Option<Vec<u8>>,
#[allow(unused_variables)]
#[allow(dead_code)]
cluster: Arc<Cluster>,
server: Arc<Server>,
pooled: bool,
}
impl OSession {
pub(crate) fn new(
client_id: i32,
session_id: i32,
token: Option<Vec<u8>>,
cluster: Arc<Cluster>,
server: Arc<Server>,
pooled: bool,
) -> OSession {
OSession {
client_id,
session_id,
token,
cluster,
server,
pooled,
}
}
pub fn query<T: Into<String>>(&self, query: T) -> Statement {
Statement::new(self, query.into())
}
pub fn command<T: Into<String>>(&self, command: T) -> Statement {
Statement::new(self, command.into()).mode(0)
}
pub fn script_sql<T: Into<String>>(&self, script: T) -> Statement {
Statement::new(self, script.into())
.mode(2)
.language(String::from("SQL"))
}
pub fn script<T: Into<String>, S: Into<String>>(&self, script: T, language: S) -> Statement {
Statement::new(self, script.into())
.mode(2)
.language(language.into())
}
pub(crate) fn run(&self, query: Query) -> OrientResult<impl ResultSet> {
let mut conn = self.server.connection()?;
let page_size = query.page_size;
let q: response::Query = conn.send(query.into())?.payload();
Ok(PagedResultSet::new(
self.server.clone(),
q,
self.session_id,
self.token.clone(),
page_size,
))
}
pub fn close(self) -> OrientResult<()> {
if !self.pooled {
return self.force_close();
}
Ok(())
}
fn force_close(mut self) -> OrientResult<()> {
let mut conn = self.server.connection()?;
self.session_id = -1;
self.token = None;
conn.send_and_forget(Close::new(self.session_id, self.token).into())?;
Ok(())
}
}
pub struct SessionPoolManager {
db: String,
user: String,
password: String,
server: Arc<Server>,
client: OrientDBClientInternal,
}
impl SessionPoolManager {
pub(crate) fn new(
client: OrientDBClientInternal,
server: Arc<Server>,
db_name: &str,
user: &str,
password: &str,
) -> SessionPoolManager {
SessionPoolManager {
db: String::from(db_name),
user: String::from(user),
password: String::from(password),
server,
client,
}
}
pub(crate) fn managed(self, size: Option<u32>) -> OrientResult<SessionPool> {
let pool = Pool::builder().max_size(size.unwrap_or(20)).build(self)?;
Ok(SessionPool(pool))
}
}
impl ManageConnection for SessionPoolManager {
type Connection = OSession;
type Error = OrientError;
fn connect(&self) -> OrientResult<OSession> {
self.client._server_session(
self.server.clone(),
&self.db,
&self.user,
&self.password,
true,
)
}
fn is_valid(&self, _conn: &mut OSession) -> OrientResult<()> {
Ok(())
}
fn has_broken(&self, _conn: &mut OSession) -> bool {
false
}
}
#[derive(Clone)]
pub struct SessionPool(Pool<SessionPoolManager>);
pub type SessionPooled = PooledConnection<SessionPoolManager>;
impl SessionPool {
pub fn get(&self) -> OrientResult<SessionPooled> {
self.0.get().map_err(OrientError::from)
}
pub fn size(&self) -> u32 {
self.0.state().connections
}
pub fn idle(&self) -> u32 {
self.0.state().idle_connections
}
}