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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lora_ruby/version.rb +1 -1
  3. data/src/lib.rs +64 -19
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc78ad82af9cdeddda40129742a043a0a2671a4671ace27e6ed250f10bb81f47
4
- data.tar.gz: 409372914f30e7c4d69ad3a4fb70dc328020a0b903c2a0c1d69b64aa61c2358f
3
+ metadata.gz: c27b39620c0d4484f27a0d1d80ca4bfe85fbf4a7cc9ee85866cdbc15a1f028f0
4
+ data.tar.gz: 6474c3321d2fef98b602deb92d184778861b4330217ebb882569cd3b828790cc
5
5
  SHA512:
6
- metadata.gz: 7fb025acfebc7f57a98cb07f745b9ad9ede8cae1532abc4a157d2fd8c0cfc875754299e714b4456dc4f93bed76b10954edb8bc6f5b41012d02f2076510a71ed0
7
- data.tar.gz: 884ffd034cd6b174b936bc0c02019e073c4d3106822db1f9f8fa067fd3903a8c9dc619648508d855ee2ab26f522dab6b619c89f35be2d5c9f5fcbbfb2c5496dc
6
+ metadata.gz: adad8d60ecf6a6df5f7156d0176d43b2981170a57bf7d2239554b94666dd06dcb8ff551c3487025cf2f3812d4465c0f4e5ecd912ed70af97f98474104e742e99
7
+ data.tar.gz: 63d8a74a82179146ef6dc0c9fc7ff2b30902cee5cda9a44d157953fd61a36b74c4a89a466d4a6f1b4336b2bd6ebcf6ea5dbc14c87a7e527ca999660497f2a90f
@@ -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.2.0" unless const_defined?(:VERSION)
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::{Arc, Mutex};
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
- GraphStorage, LoraDate, LoraDateTime, LoraDuration, LoraLocalDateTime, LoraLocalTime,
31
- LoraPoint, LoraTime, LoraVector, RawCoordinate, VectorCoordinateType, VectorValues,
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<Mutex<InMemoryGraph>>` gives us cheap cloning so the mutex guard
118
- /// can be sent across the GVL-release boundary without borrowing any Ruby
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
- store: Arc<Mutex<InMemoryGraph>>,
124
+ db: Arc<InnerDatabase<InMemoryGraph>>,
123
125
  }
124
126
 
125
127
  impl Database {
126
128
  fn empty() -> Self {
127
129
  Self {
128
- store: Arc::new(Mutex::new(InMemoryGraph::new())),
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
- let mut guard = rb_self.store.lock().unwrap_or_else(|p| p.into_inner());
146
- *guard = InMemoryGraph::new();
147
+ rb_self.db.clear();
147
148
  }
148
149
 
149
150
  fn database_node_count(rb_self: &Database) -> u64 {
150
- let guard = rb_self.store.lock().unwrap_or_else(|p| p.into_inner());
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
- let guard = rb_self.store.lock().unwrap_or_else(|p| p.into_inner());
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
- guard.node_count(),
164
- guard.relationship_count(),
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 store = Arc::clone(&rb_self.store);
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
  };
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lora-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - LoraDB, Inc.