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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright Rouven Bauer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Neo4j Bolt Driver
//!
//! This crate provides a driver for the Neo4j graph database.
//! It's designed to mirror many concepts of the official drivers while leveraging Rust's expressive
//! type system and lifetime management to provide a safer API that prevents many common pitfalls
//! already at compile time.
//!
//! ## Compatibility
// [bolt-version-bump] search tag when changing bolt version support
//! This driver supports bolt protocol version 4.4, and 5.0 - 5.8.
//! This corresponds to Neo4j versions 4.4, and the whole 5.x series.
//! Newer versions of Neo4j are supported as long as they keep support for at least one of the
//! protocol versions mentioned above.
//! For details of bolt protocol compatibility, see the
//! [official Neo4j documentation](https://7687.org/bolt-compatibility/).
//!
//! ## Basic Example
//! ```
//! use std::sync::Arc;
//!
//! use neo4j::address::Address;
//! use neo4j::driver::auth::AuthToken;
//! use neo4j::driver::{ConnectionConfig, Driver, DriverConfig, RoutingControl};
//! use neo4j::retry::ExponentialBackoff;
//! use neo4j::{value_map, ValueReceive};
//!
//! let host = "localhost";
//! # let host = doc_test_utils::get_host();
//! let port = 7687;
//! # let port = doc_test_utils::get_port();
//! let user = "neo4j";
//! # let user = doc_test_utils::get_user();
//! let password = "pass";
//! # let password = doc_test_utils::get_password();
//! let database = "neo4j";
//!
//! let database = Arc::new(String::from(database));
//! let address = Address::from((host, port));
//! let auth_token = AuthToken::new_basic_auth(user, password);
//! let driver = Driver::new(
//! // tell the driver where to connect to
//! ConnectionConfig::new(address),
//! // configure how the driver works locally (e.g., authentication)
//! DriverConfig::new().with_auth(Arc::new(auth_token)),
//! );
//!
//! // Driver::execute_query() is the easiest way to run a query.
//! // It will be sufficient for most use-cases and allows the driver to apply some optimizations.
//! // So it's recommended to use it whenever possible.
//! // For more control, see sessions and transactions.
//! let result = driver
//! // Run a CYPHER query against the DBMS.
//! .execute_query("RETURN $x AS x")
//! // Always specify the database when you can (also applies to using sessions).
//! // This will let the driver work more efficiently.
//! .with_database(database)
//! // Tell the driver to send the query to a read server.
//! // In a clustered environment, this will make sure that read queries don't overload
//! // the single write server.
//! .with_routing_control(RoutingControl::Read)
//! // Use query parameters (instead of string interpolation) to avoid injection attacks and
//! // improve performance.
//! .with_parameters(value_map!({"x": 123}))
//! // For more resilience, use retry policies.
//! // Especially in a clustered environment, this will help to recover from transient errors
//! // like those caused by leader elections, which are to be expected.
//! .run_with_retry(ExponentialBackoff::default());
//! println!("{:?}", result);
//!
//! let result = result.unwrap();
//! assert_eq!(result.records.len(), 1);
//! for mut record in result.records {
//! assert_eq!(record.values().count(), 1);
//! assert_eq!(record.take_value("x"), Some(ValueReceive::Integer(123)));
//! }
//! ```
//!
//! ## Concepts
//!
//! ### The Driver
//! The fundamental type of this crate is the [`Driver`].
//! Through it, all database interactions are performed.
//! See [`Driver::new()`].
//! The driver manages a connection pool. So there is no need to pool driver objects.
//! Usually, each application will use one global driver.
//!
//! ### Sessions
//! Sessions are spawned from the driver.
//! See [`Driver::session()`].
//! Session creation is cheap, it's recommended to create a new session for each piece of work
//! (unless when using [`Driver::execute_query()`]).
//! Sessions will borrow connections from the driver's pool as needed.
//!
//! ### Main Mechanisms for Query Execution
//! There are three main ways to execute queries:
//! - [`Driver::execute_query()`] is the easiest way to run a query.
//! Prefer it whenever possible as it most efficient.
//! - [`Session::transaction()`] gives you full control over the transaction.
//! - [`Session::auto_commit()`] is a special method for running queries that manage their own
//! transactions, such as `CALL {...} IN TRANSACTION`.
//!
//! ### Causal Consistency
//! By default, Neo4j clusters are eventually consistent:
//! a write transaction executed on the leader (write node) will sooner or later be visible to read
//! transactions on all followers (read nodes).
//! To provide stronger guarantees, the server sends a bookmark to the client after every
//! successful transaction that applies a write.
//! These bookmarks are abstract tokens that represent some state of the database.
//! By passing them back to the server along with a transaction, the client requests the server to
//! wait until the state(s) represented by the bookmark(s) have been established before executing
//! the transaction.
//!
//! To point out the obvious: relying on bookmarks can be slow because of the wait described above.
//! Not using them, however, can lead to stale reads which will be unacceptable in some cases.
//!
//! See also [`Bookmarks`].
//!
//! #### Methods for Managing Bookmarks
//! * The easiest way is to rely on the fact that [`Session`]s will automatically manage
//! bookmarks for you.
//! All work run in the same session will be part of the same causal chain.
//! * Manually passing [`Bookmarks`] between sessions.
//! See [`Session::last_bookmarks()`] for an example.
//! * Using a [`BookmarkManager`], which [`Driver::execute_query`] does by default.
//! See [`SessionConfig::with_bookmark_manager()`], [`Driver::execute_query_bookmark_manager()`].
//!
//! ## Logging
//! The driver uses the [`log`] crate for logging.
//!
//! **Important Notes on Usage:**
//! * Log messages are *not* considered part of the driver's API.
//! They may change at any time and don't follow semantic versioning.
//! * The driver's logs are meant for debugging the driver itself.
//! Log levels `ERROR` and `WARN` are used liberally to indicate (potential) problems within
//! abstraction layers of the driver.
//! If there are problems the user-code needs to be aware of, they will be reported via
//! [`Result`]s, not log messages.
//!
//! ### Logging Example
//! ```
//! use std::sync::Arc;
//!
//! use env_logger; // example using the env_logger crate
//! use log;
//! use neo4j::driver::{Driver, RoutingControl};
//! use neo4j::retry::ExponentialBackoff;
//!
//! # use doc_test_utils::get_driver;
//!
//! env_logger::builder()
//! .filter_level(log::LevelFilter::Debug)
//! .init();
//!
//! let driver: Driver = get_driver();
//! driver
//! .execute_query("RETURN 1")
//! .with_database(Arc::new(String::from("neo4j")))
//! .with_routing_control(RoutingControl::Read)
//! .run_with_retry(ExponentialBackoff::new())
//! .unwrap();
//! ```
// imports for docs
use ;
use RecordStream;
use Driver;
use ;
pub use ;
pub use ValueReceive;
pub use ValueSend;
/// Address and address resolution.
/// Bookmarks for [causal consistency](crate#causal-consistency).
/// Error and result types.
/// Retry policies.
/// Session and session configuration.
/// Query summary structs (metadata) received via [`RecordStream::consume()`].
/// Transactions and associated types.