ndb 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 317deb09dd8bbca4e1b76678129dbfa77d7f0c9b788a81250bb548d2ca56b1bf
4
- data.tar.gz: ca4d6e111625cfea52002931b9db8175282a8ba5c41c97f719a2c1181c691eec
3
+ metadata.gz: 440cb991f003e2ce9f918da9d9f40fe3ac07e77ca117ba1b3d7dbd801d6b5866
4
+ data.tar.gz: 8d48e93a33a65dc8df214ff57ac8ef77d0c6edbb5c9bfa12cb364400ab2492d5
5
5
  SHA512:
6
- metadata.gz: c0c12d078338582ad5044d352b8178667c63f7aa9339761fd64b82104743a7b9ba0649513e30f0f8700bd68fa28325d9a94246e89940846fe402b7e04a855c64
7
- data.tar.gz: b0050f79b594792a67b1e952745afc44b4e613fcb63cb94cb502a6efa8ce61a9b47c4fb7a2fdb965312c33a243d0a19830a2924e669cfc698d41971b8d798104
6
+ metadata.gz: 2375c7fdd4cca6e943e6a5c27e192d4e6ed3dda62722f935bd4ffaa0c606fa2d973101e11bc1c19907dce0c0068366c2cb5cefba0022086e0dd85232cb78eaf8
7
+ data.tar.gz: d382affec84704deee77750b458da1b524f6805d08ebf3f38b3411c5a7b64b002eb4256ebe94f99243253e2ecb13f438207317cfc419aeecce12115b7ba1a350
data/README.md CHANGED
@@ -103,6 +103,16 @@ ndb.delete("calls", { status: "failed" }, many: true)
103
103
  - The contract errors are `Ndb::UnknownOperator`, `NoUpdate`, `ConflictingUpdate`, `InvalidField`, `UnindexedMany` and `EmptyFilter`, all under `Ndb::InvalidUpdate`.
104
104
  - A duplicate `_id` raises `Ndb::UniqueViolation` (code `duplicate_id`). A broken unique field raises it too (code `unique_violation`).
105
105
 
106
+ ### Truncate and drop
107
+
108
+ ```ruby
109
+ ndb.truncate("calls") # every document goes; the definition and indexes stay => {"collection" => "calls", "truncated" => true}
110
+ ndb.drop("calls") # the collection, its definition and its watch consumers go
111
+ ```
112
+
113
+ - Both take the same time for 10 or 10 million documents. Watch consumers get one event with `"op" => "truncate"` (no `_id`), then the new changes.
114
+ - A direct-mode collection raises `Ndb::ReadOnlyCollection`. A collection declared in `ndb.yml` comes back, empty, when the daemon restarts after a drop.
115
+
106
116
  ## Expiry (TTL like Redis)
107
117
 
108
118
  The collection needs `expires_field`.
@@ -233,7 +243,7 @@ A watch streams the changes of a collection to a named consumer: first the event
233
243
  ```ruby
234
244
  begin
235
245
  ndb.watch("calls", "relay", where: { status: "failed" }) do |event|
236
- # event has "pos", "op" ("put" or "del"), "_id", "v" and "doc"
246
+ # event has "pos", "op" ("put", "del" or "truncate"), "_id", "v" and "doc"
237
247
  handle(event)
238
248
 
239
249
  ndb.ack("calls", "relay", event["pos"])
data/lib/ndb/client.rb CHANGED
@@ -145,6 +145,20 @@ module Ndb
145
145
  json(post(coll, "ack", consumer: consumer, pos: pos))
146
146
  end
147
147
 
148
+ # Removes every document of the collection and keeps its definition and
149
+ # indexes. The cost does not depend on the number of documents. Watch
150
+ # consumers get one event with op "truncate". {"collection", "truncated"}
151
+ def truncate(coll)
152
+ json(request("POST", "#{path(coll)}/truncate?confirm=#{confirm(coll)}", nil))
153
+ end
154
+
155
+ # Removes the collection: its documents, its definition and the positions
156
+ # of its watch consumers. A collection declared in ndb.yml comes back,
157
+ # empty, when the daemon restarts. {"collection", "dropped"}
158
+ def drop(coll)
159
+ json(request("DELETE", "#{path(coll)}?confirm=#{confirm(coll)}", nil))
160
+ end
161
+
148
162
  def ensure_collection(coll, definition = {})
149
163
  json(request("PUT", path(coll), definition))
150
164
  end
@@ -201,6 +215,10 @@ module Ndb
201
215
  "/v1/c/#{URI.encode_www_form_component(coll).gsub('+', '%20')}"
202
216
  end
203
217
 
218
+ def confirm(coll)
219
+ URI.encode_www_form_component(coll)
220
+ end
221
+
204
222
  def id_path(coll, id)
205
223
  "#{path(coll)}/#{URI.encode_www_form_component(id).gsub('+', '%20')}"
206
224
  end
data/lib/ndb/errors.rb CHANGED
@@ -52,6 +52,9 @@ module Ndb
52
52
  # consumer cannot resume from its position and must start over.
53
53
  class HistoryTruncated < Error; end
54
54
 
55
+ # truncate or drop without the collection name as confirm.
56
+ class ConfirmRequired < Error; end
57
+
55
58
  # The daemon error code -> the Ruby class.
56
59
  CODE_TO_ERROR = {
57
60
  "unauthorized" => Unauthorized,
@@ -77,7 +80,8 @@ module Ndb
77
80
  "conflicting_update" => ConflictingUpdate,
78
81
  "invalid_field" => InvalidField,
79
82
  "unindexed_many" => UnindexedMany,
80
- "empty_filter" => EmptyFilter
83
+ "empty_filter" => EmptyFilter,
84
+ "confirm_required" => ConfirmRequired
81
85
  }.freeze
82
86
 
83
87
  def self.error_for(code, message, status)
data/lib/ndb/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ndb
4
- VERSION = "0.3.2"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ndb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thadeu