lora-ruby 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/lora_ruby/version.rb +1 -1
- data/src/lib.rs +64 -19
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c27b39620c0d4484f27a0d1d80ca4bfe85fbf4a7cc9ee85866cdbc15a1f028f0
|
|
4
|
+
data.tar.gz: 6474c3321d2fef98b602deb92d184778861b4330217ebb882569cd3b828790cc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: adad8d60ecf6a6df5f7156d0176d43b2981170a57bf7d2239554b94666dd06dcb8ff551c3487025cf2f3812d4465c0f4e5ecd912ed70af97f98474104e742e99
|
|
7
|
+
data.tar.gz: 63d8a74a82179146ef6dc0c9fc7ff2b30902cee5cda9a44d157953fd61a36b74c4a89a466d4a6f1b4336b2bd6ebcf6ea5dbc14c87a7e527ca999660497f2a90f
|
data/lib/lora_ruby/version.rb
CHANGED
|
@@ -10,5 +10,5 @@ module LoraRuby
|
|
|
10
10
|
# Guard against redefinition so re-requiring this file (or loading
|
|
11
11
|
# both paths) doesn't emit a "warning: already initialized constant"
|
|
12
12
|
# when the native extension loads second with the identical value.
|
|
13
|
-
VERSION = "0.
|
|
13
|
+
VERSION = "0.3.0" unless const_defined?(:VERSION)
|
|
14
14
|
end
|
data/src/lib.rs
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
use std::collections::BTreeMap;
|
|
17
17
|
use std::ffi::c_void;
|
|
18
18
|
use std::mem::MaybeUninit;
|
|
19
|
-
use std::sync::
|
|
19
|
+
use std::sync::Arc;
|
|
20
20
|
|
|
21
21
|
use magnus::{
|
|
22
22
|
function, method, prelude::*, r_hash::ForEach, value::ReprValue, Error as MagnusError,
|
|
@@ -27,8 +27,8 @@ use lora_database::{
|
|
|
27
27
|
Database as InnerDatabase, ExecuteOptions, InMemoryGraph, LoraValue, QueryResult, ResultFormat,
|
|
28
28
|
};
|
|
29
29
|
use lora_store::{
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
LoraDate, LoraDateTime, LoraDuration, LoraLocalDateTime, LoraLocalTime, LoraPoint, LoraTime,
|
|
31
|
+
LoraVector, RawCoordinate, VectorCoordinateType, VectorValues,
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
// ============================================================================
|
|
@@ -71,6 +71,8 @@ fn init(ruby: &Ruby) -> Result<(), MagnusError> {
|
|
|
71
71
|
)?;
|
|
72
72
|
database.define_method("inspect", method!(database_inspect, 0))?;
|
|
73
73
|
database.define_method("to_s", method!(database_inspect, 0))?;
|
|
74
|
+
database.define_method("save_snapshot", method!(database_save_snapshot, 1))?;
|
|
75
|
+
database.define_method("load_snapshot", method!(database_load_snapshot, 1))?;
|
|
74
76
|
|
|
75
77
|
// `LoraRuby::VERSION` is owned by `lib/lora_ruby/version.rb` so the
|
|
76
78
|
// gem can expose a version before the native extension compiles
|
|
@@ -114,18 +116,18 @@ fn invalid_params(ruby: &Ruby, msg: impl Into<String>) -> MagnusError {
|
|
|
114
116
|
|
|
115
117
|
/// In-memory Lora graph database handle exposed to Ruby.
|
|
116
118
|
///
|
|
117
|
-
/// `Arc<
|
|
118
|
-
///
|
|
119
|
-
/// state.
|
|
119
|
+
/// Wraps an `Arc<Database<InMemoryGraph>>`; the same handle is cloned
|
|
120
|
+
/// across the GVL-release boundary for query execution without borrowing
|
|
121
|
+
/// any Ruby state.
|
|
120
122
|
#[magnus::wrap(class = "LoraRuby::Database", free_immediately, size)]
|
|
121
123
|
struct Database {
|
|
122
|
-
|
|
124
|
+
db: Arc<InnerDatabase<InMemoryGraph>>,
|
|
123
125
|
}
|
|
124
126
|
|
|
125
127
|
impl Database {
|
|
126
128
|
fn empty() -> Self {
|
|
127
129
|
Self {
|
|
128
|
-
|
|
130
|
+
db: Arc::new(InnerDatabase::in_memory()),
|
|
129
131
|
}
|
|
130
132
|
}
|
|
131
133
|
}
|
|
@@ -142,29 +144,73 @@ fn database_create() -> Database {
|
|
|
142
144
|
}
|
|
143
145
|
|
|
144
146
|
fn database_clear(rb_self: &Database) {
|
|
145
|
-
|
|
146
|
-
*guard = InMemoryGraph::new();
|
|
147
|
+
rb_self.db.clear();
|
|
147
148
|
}
|
|
148
149
|
|
|
149
150
|
fn database_node_count(rb_self: &Database) -> u64 {
|
|
150
|
-
|
|
151
|
-
guard.node_count() as u64
|
|
151
|
+
rb_self.db.node_count() as u64
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
fn database_relationship_count(rb_self: &Database) -> u64 {
|
|
155
|
-
|
|
156
|
-
guard.relationship_count() as u64
|
|
155
|
+
rb_self.db.relationship_count() as u64
|
|
157
156
|
}
|
|
158
157
|
|
|
159
158
|
fn database_inspect(rb_self: &Database) -> String {
|
|
160
|
-
let guard = rb_self.store.lock().unwrap_or_else(|p| p.into_inner());
|
|
161
159
|
format!(
|
|
162
160
|
"#<LoraRuby::Database nodes={} relationships={}>",
|
|
163
|
-
|
|
164
|
-
|
|
161
|
+
rb_self.db.node_count(),
|
|
162
|
+
rb_self.db.relationship_count(),
|
|
165
163
|
)
|
|
166
164
|
}
|
|
167
165
|
|
|
166
|
+
fn database_save_snapshot(
|
|
167
|
+
ruby: &Ruby,
|
|
168
|
+
rb_self: &Database,
|
|
169
|
+
path: RString,
|
|
170
|
+
) -> Result<RHash, MagnusError> {
|
|
171
|
+
let path = path.to_string()?;
|
|
172
|
+
let db = Arc::clone(&rb_self.db);
|
|
173
|
+
let meta = without_gvl(move || db.save_snapshot_to(&path))
|
|
174
|
+
.map_err(|e| query_error(ruby, format!("{e}")))?;
|
|
175
|
+
snapshot_meta_to_rhash(ruby, meta)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
fn database_load_snapshot(
|
|
179
|
+
ruby: &Ruby,
|
|
180
|
+
rb_self: &Database,
|
|
181
|
+
path: RString,
|
|
182
|
+
) -> Result<RHash, MagnusError> {
|
|
183
|
+
let path = path.to_string()?;
|
|
184
|
+
let db = Arc::clone(&rb_self.db);
|
|
185
|
+
let meta = without_gvl(move || db.load_snapshot_from(&path))
|
|
186
|
+
.map_err(|e| query_error(ruby, format!("{e}")))?;
|
|
187
|
+
snapshot_meta_to_rhash(ruby, meta)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
fn snapshot_meta_to_rhash(
|
|
191
|
+
ruby: &Ruby,
|
|
192
|
+
meta: lora_database::SnapshotMeta,
|
|
193
|
+
) -> Result<RHash, MagnusError> {
|
|
194
|
+
let h = ruby.hash_new();
|
|
195
|
+
h.aset(
|
|
196
|
+
ruby.str_new("formatVersion"),
|
|
197
|
+
ruby.integer_from_i64(meta.format_version as i64),
|
|
198
|
+
)?;
|
|
199
|
+
h.aset(
|
|
200
|
+
ruby.str_new("nodeCount"),
|
|
201
|
+
ruby.integer_from_i64(meta.node_count as i64),
|
|
202
|
+
)?;
|
|
203
|
+
h.aset(
|
|
204
|
+
ruby.str_new("relationshipCount"),
|
|
205
|
+
ruby.integer_from_i64(meta.relationship_count as i64),
|
|
206
|
+
)?;
|
|
207
|
+
match meta.wal_lsn {
|
|
208
|
+
Some(lsn) => h.aset(ruby.str_new("walLsn"), ruby.integer_from_i64(lsn as i64))?,
|
|
209
|
+
None => h.aset(ruby.str_new("walLsn"), ruby.qnil())?,
|
|
210
|
+
}
|
|
211
|
+
Ok(h)
|
|
212
|
+
}
|
|
213
|
+
|
|
168
214
|
/// `execute(query, params = nil)` — `-1` arity so `params` is optional and
|
|
169
215
|
/// we can distinguish "not passed" from `nil`/`{}` (both map to empty
|
|
170
216
|
/// params). Everything that touches Ruby values happens under the GVL;
|
|
@@ -202,9 +248,8 @@ fn database_execute(ruby: &Ruby, rb_self: &Database, args: &[Value]) -> Result<R
|
|
|
202
248
|
// Run the engine with the GVL released. Everything inside the closure
|
|
203
249
|
// is pure Rust — no Ruby values cross the boundary — which keeps this
|
|
204
250
|
// sound.
|
|
205
|
-
let
|
|
251
|
+
let db = Arc::clone(&rb_self.db);
|
|
206
252
|
let exec_result = without_gvl(move || {
|
|
207
|
-
let db = InnerDatabase::new(store);
|
|
208
253
|
let options = ExecuteOptions {
|
|
209
254
|
format: ResultFormat::RowArrays,
|
|
210
255
|
};
|