ndb-rails 0.3.2 → 0.4.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/README.md +3 -2
- data/lib/ndb/rails/persistence.rb +8 -2
- data/lib/ndb/rails/record.rb +48 -2
- data/lib/ndb/rails/relation.rb +9 -0
- data/lib/ndb/rails/version.rb +1 -1
- 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: d7fe2bcbea6268a30dae29bfa0457dcd8078ae7b485064871a77b6cf970bf173
|
|
4
|
+
data.tar.gz: cfdde7db03a7c51e58fadf9e4216743aafb8dedfc4dfb7666568c8a9d394cefa
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cdba61392f9801da6d52a1c73bebfd64306b570a20817281cfa0b21b04689401dfe571b115e6cbddf37ae2279b4dd1bc17a461e296b28de74f18537c41669df9
|
|
7
|
+
data.tar.gz: bc12a571537f11c22fcaabb1346c32bc98bcf4403a7cbb789503d1e91e61f4453d37d6365a0f9c39e17936ee3a3ec29f11691df86cc066a2c842e42c7e138a33
|
data/README.md
CHANGED
|
@@ -141,7 +141,8 @@ Call.where(status: "retry").destroy_all # loads and destroys each record with i
|
|
|
141
141
|
|
|
142
142
|
- Every load asks for the version (`_v`). `save`, `update` and `destroy` put the id and `_v` in the filter of the write. If another writer changed the document first, the filter matches nothing and you get `Ndb::StaleObjectError` (code `stale_object`).
|
|
143
143
|
- `find_or_create_by` is atomic. It checks validations and runs the `save` and `create` callbacks, then sends one upsert: the attributes you pass are the filter, and the other attributes go in `$setOnInsert`, so a writer that created the document first is never overwritten and you get that document. An invalid record is returned unsaved (the `!` form raises `Ndb::RecordInvalid`). Only the attributes you pass must be equality values.
|
|
144
|
-
- `delete_all` and `destroy_all` on a model without a `where` are refused by the daemon (`Ndb::EmptyFilter`); `delete_all` needs the id or an indexed field in the `where`.
|
|
144
|
+
- `delete_all` and `destroy_all` on a model without a `where` are refused by the daemon (`Ndb::EmptyFilter`); `delete_all` needs the id or an indexed field in the `where`. To empty the whole collection, use `Call.truncate!`.
|
|
145
|
+
- `Call.truncate!` removes every document, with no callbacks, and keeps the definition and indexes. `Call.drop!` removes the collection, its definition and its watch consumers; the next use of the model declares it again. Both take the same time for 10 or 10 million documents, and watch consumers get one `"truncate"` event.
|
|
145
146
|
- Setting an attribute to `nil` and saving removes the field from the document.
|
|
146
147
|
- A duplicate id raises `RecordNotUnique` (code `duplicate_id`).
|
|
147
148
|
- `persisted?`, `new_record?`, `destroyed?`, `id` and `version` work as in ActiveRecord.
|
|
@@ -302,7 +303,7 @@ client = Ndb::Rails.client
|
|
|
302
303
|
|
|
303
304
|
begin
|
|
304
305
|
client.watch(Call.collection_name, "relay", where: { status: "answered" }) do |event|
|
|
305
|
-
# event has "pos", "op" ("put" or "
|
|
306
|
+
# event has "pos", "op" ("put", "del" or "truncate"), "_id", "v" and "doc"
|
|
306
307
|
handle(event)
|
|
307
308
|
|
|
308
309
|
client.ack(Call.collection_name, "relay", event["pos"])
|
|
@@ -42,9 +42,10 @@ module Ndb
|
|
|
42
42
|
def instantiate(row)
|
|
43
43
|
row = row.dup
|
|
44
44
|
version = row.delete("_v")
|
|
45
|
+
source = row.delete("_coll")
|
|
45
46
|
record = new(row.slice(*attribute_names))
|
|
46
47
|
|
|
47
|
-
record.send(:loaded!, version)
|
|
48
|
+
record.send(:loaded!, version, source)
|
|
48
49
|
|
|
49
50
|
record
|
|
50
51
|
end
|
|
@@ -52,6 +53,10 @@ module Ndb
|
|
|
52
53
|
|
|
53
54
|
attr_reader :version
|
|
54
55
|
|
|
56
|
+
# The collection a record was read from, when its model reads a
|
|
57
|
+
# collection pattern (self.collection_name = "logs-2026-*"); nil otherwise.
|
|
58
|
+
attr_reader :source_collection
|
|
59
|
+
|
|
55
60
|
# id is an alias of _id.
|
|
56
61
|
def id
|
|
57
62
|
_id
|
|
@@ -244,9 +249,10 @@ module Ndb
|
|
|
244
249
|
self
|
|
245
250
|
end
|
|
246
251
|
|
|
247
|
-
def loaded!(version)
|
|
252
|
+
def loaded!(version, source = nil)
|
|
248
253
|
@persisted = true
|
|
249
254
|
@version = version
|
|
255
|
+
@source_collection = source if source
|
|
250
256
|
|
|
251
257
|
changes_applied
|
|
252
258
|
end
|
data/lib/ndb/rails/record.rb
CHANGED
|
@@ -18,9 +18,12 @@ module Ndb
|
|
|
18
18
|
|
|
19
19
|
attribute :_id, :string
|
|
20
20
|
|
|
21
|
+
# The answer of Model.search.
|
|
22
|
+
SearchResult = Struct.new(:records, :next_cursor, :total)
|
|
23
|
+
|
|
21
24
|
class << self
|
|
22
25
|
QUERY_METHODS = %i[
|
|
23
|
-
where order limit offset page after group find find_by find_by! first count pluck exists?
|
|
26
|
+
where where_raw order limit offset page after group find find_by find_by! first count pluck exists?
|
|
24
27
|
find_each in_batches sum average minimum maximum to_a each destroy_all delete_all
|
|
25
28
|
].freeze
|
|
26
29
|
|
|
@@ -36,6 +39,19 @@ module Ndb
|
|
|
36
39
|
Relation.new(self)
|
|
37
40
|
end
|
|
38
41
|
|
|
42
|
+
# Runs a find with a whole body built elsewhere, such as from a search
|
|
43
|
+
# form: { "where" => {...}, "sort" => {...}, "limit" => 50, "after" =>
|
|
44
|
+
# token, "count" => true }. The body is sent as is (the daemon
|
|
45
|
+
# validates it). Returns the records, the next cursor (nil on the last
|
|
46
|
+
# page) and the total (with "count" => true).
|
|
47
|
+
def search(body)
|
|
48
|
+
raise ArgumentError, "search takes a Hash, got #{body.class}" unless body.is_a?(Hash)
|
|
49
|
+
|
|
50
|
+
page = ndb_find(body.transform_keys(&:to_s))
|
|
51
|
+
|
|
52
|
+
SearchResult.new(page.rows.map { |row| instantiate(row) }, page.next_cursor, page.total)
|
|
53
|
+
end
|
|
54
|
+
|
|
39
55
|
# Finds the record with these attributes, or builds an unsaved one.
|
|
40
56
|
def find_or_initialize_by(attrs, &block)
|
|
41
57
|
find_by(attrs) || new(attrs).tap { |record| block&.call(record) }
|
|
@@ -68,6 +84,27 @@ module Ndb
|
|
|
68
84
|
where(primary_key => ids).delete_all
|
|
69
85
|
end
|
|
70
86
|
|
|
87
|
+
# Removes every document of the collection, with no callbacks, and
|
|
88
|
+
# keeps its definition and indexes. The cost does not depend on the
|
|
89
|
+
# number of documents. Watch consumers get one "truncate" event.
|
|
90
|
+
def truncate!
|
|
91
|
+
ensure_collection!
|
|
92
|
+
|
|
93
|
+
Rails.client.truncate(collection_name)
|
|
94
|
+
|
|
95
|
+
true
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Removes the collection: documents, definition and watch consumers.
|
|
99
|
+
# The next use of the model declares it again, empty.
|
|
100
|
+
def drop!
|
|
101
|
+
Rails.client.drop(collection_name)
|
|
102
|
+
|
|
103
|
+
@ensured_client = nil
|
|
104
|
+
|
|
105
|
+
true
|
|
106
|
+
end
|
|
107
|
+
|
|
71
108
|
def collection_name
|
|
72
109
|
@collection_name || model_name.plural
|
|
73
110
|
end
|
|
@@ -137,8 +174,17 @@ module Ndb
|
|
|
137
174
|
definition
|
|
138
175
|
end
|
|
139
176
|
|
|
140
|
-
#
|
|
177
|
+
# A collection pattern ("logs-2026-*") reads many collections and
|
|
178
|
+
# writes none.
|
|
179
|
+
def collection_pattern?
|
|
180
|
+
collection_name.include?("*")
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# Declares the collection once per process (and once per client). A
|
|
184
|
+
# pattern declares nothing.
|
|
141
185
|
def ensure_collection!
|
|
186
|
+
return if collection_pattern?
|
|
187
|
+
|
|
142
188
|
client = Rails.client
|
|
143
189
|
|
|
144
190
|
return if @ensured_client.equal?(client)
|
data/lib/ndb/rails/relation.rb
CHANGED
|
@@ -28,6 +28,15 @@ module Ndb
|
|
|
28
28
|
add_clause(Conditions.equality(attrs))
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
# A where Hash built elsewhere, such as from a search form, sent as is:
|
|
32
|
+
# no Rails conventions (an Array stays an Array, not $in). The daemon
|
|
33
|
+
# validates it. ANDed with the other clauses.
|
|
34
|
+
def where_raw(where)
|
|
35
|
+
raise ArgumentError, "where_raw takes a Hash, got #{where.class}" unless where.is_a?(Hash)
|
|
36
|
+
|
|
37
|
+
add_clause(Conditions.json_value(where))
|
|
38
|
+
end
|
|
39
|
+
|
|
31
40
|
def or(other)
|
|
32
41
|
clauses = [Conditions.combine(values[:where]), Conditions.combine(other.values[:where])]
|
|
33
42
|
|
data/lib/ndb/rails/version.rb
CHANGED