moneta 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +176 -0
- data/.travis.yml +57 -23
- data/CHANGES +12 -0
- data/Gemfile +89 -64
- data/README.md +40 -14
- data/feature_matrix.yaml +1 -0
- data/lib/action_dispatch/middleware/session/moneta_store.rb +1 -0
- data/lib/active_support/cache/moneta_store.rb +5 -5
- data/lib/moneta.rb +9 -1
- data/lib/moneta/adapters/activerecord.rb +35 -19
- data/lib/moneta/adapters/activesupportcache.rb +3 -7
- data/lib/moneta/adapters/cassandra.rb +24 -16
- data/lib/moneta/adapters/client.rb +13 -9
- data/lib/moneta/adapters/couch.rb +220 -80
- data/lib/moneta/adapters/datamapper.rb +1 -0
- data/lib/moneta/adapters/file.rb +9 -6
- data/lib/moneta/adapters/hbase.rb +1 -1
- data/lib/moneta/adapters/kyotocabinet.rb +8 -7
- data/lib/moneta/adapters/leveldb.rb +1 -1
- data/lib/moneta/adapters/lmdb.rb +3 -4
- data/lib/moneta/adapters/lruhash.rb +29 -62
- data/lib/moneta/adapters/memcached.rb +1 -0
- data/lib/moneta/adapters/memcached/dalli.rb +1 -1
- data/lib/moneta/adapters/memcached/native.rb +10 -8
- data/lib/moneta/adapters/mongo.rb +1 -0
- data/lib/moneta/adapters/mongo/base.rb +3 -3
- data/lib/moneta/adapters/mongo/moped.rb +12 -13
- data/lib/moneta/adapters/mongo/official.rb +7 -8
- data/lib/moneta/adapters/null.rb +1 -2
- data/lib/moneta/adapters/pstore.rb +3 -2
- data/lib/moneta/adapters/redis.rb +3 -3
- data/lib/moneta/adapters/restclient.rb +12 -3
- data/lib/moneta/adapters/riak.rb +2 -2
- data/lib/moneta/adapters/sequel.rb +112 -119
- data/lib/moneta/adapters/sqlite.rb +3 -3
- data/lib/moneta/adapters/tokyotyrant.rb +1 -1
- data/lib/moneta/builder.rb +0 -1
- data/lib/moneta/enumerable.rb +38 -0
- data/lib/moneta/expires.rb +12 -12
- data/lib/moneta/fallback.rb +84 -0
- data/lib/moneta/lock.rb +1 -1
- data/lib/moneta/logger.rb +2 -2
- data/lib/moneta/mixins.rb +12 -10
- data/lib/moneta/optionmerger.rb +0 -1
- data/lib/moneta/pool.rb +301 -31
- data/lib/moneta/proxy.rb +2 -2
- data/lib/moneta/server.rb +9 -12
- data/lib/moneta/shared.rb +1 -1
- data/lib/moneta/stack.rb +6 -6
- data/lib/moneta/synchronize.rb +3 -3
- data/lib/moneta/transformer.rb +19 -17
- data/lib/moneta/transformer/config.rb +6 -5
- data/lib/moneta/transformer/helper.rb +3 -3
- data/lib/moneta/transformer/helper/bson.rb +18 -15
- data/lib/moneta/utils.rb +3 -9
- data/lib/moneta/version.rb +1 -1
- data/lib/moneta/weak_each_key.rb +2 -4
- data/lib/rack/cache/moneta.rb +16 -13
- data/lib/rack/moneta_rest.rb +2 -2
- data/lib/rack/session/moneta.rb +3 -4
- data/moneta.gemspec +8 -0
- data/script/benchmarks +55 -32
- data/script/reconfigure-couchdb +13 -0
- data/script/start-hbase +1 -0
- data/script/start-services +2 -10
- data/spec/active_support/cache_moneta_store_spec.rb +3 -1
- data/spec/features/concurrent_create.rb +31 -10
- data/spec/features/concurrent_increment.rb +27 -19
- data/spec/features/increment.rb +41 -41
- data/spec/helper.rb +2 -42
- data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_spec.rb +4 -1
- data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_with_default_expires_spec.rb +4 -1
- data/spec/moneta/adapters/activesupportcache/standard_activesupportcache_spec.rb +14 -0
- data/spec/moneta/adapters/couch/adapter_couch_spec.rb +199 -2
- data/spec/moneta/adapters/couch/standard_couch_spec.rb +8 -2
- data/spec/moneta/adapters/couch/standard_couch_with_expires_spec.rb +7 -1
- data/spec/moneta/adapters/faraday_helper.rb +9 -0
- data/spec/moneta/adapters/lruhash/adapter_lruhash_spec.rb +2 -2
- data/spec/moneta/adapters/memcached/adapter_memcached_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/dalli/standard_memcached_dalli_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/native/adapter_memcached_native_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/native/adapter_memcached_native_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/native/standard_memcached_native_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/standard_memcached_spec.rb +1 -1
- data/spec/moneta/adapters/{memcached/helper.rb → memcached_helper.rb} +0 -0
- data/spec/moneta/adapters/null/null_adapter_spec.rb +1 -1
- data/spec/moneta/adapters/restclient/adapter_restclient_spec.rb +7 -5
- data/spec/moneta/adapters/restclient/helper.rb +12 -0
- data/spec/moneta/adapters/restclient/standard_restclient_spec.rb +9 -6
- data/spec/moneta/adapters/riak/standard_riak_with_expires_spec.rb +4 -0
- data/spec/moneta/adapters/tokyotyrant/adapter_tokyotyrant_spec.rb +6 -2
- data/spec/moneta/adapters/tokyotyrant/helper.rb +12 -0
- data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_spec.rb +5 -2
- data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_with_expires_spec.rb +5 -1
- data/spec/moneta/proxies/enumerable/enumerable_spec.rb +26 -0
- data/spec/moneta/proxies/fallback/fallback_spec.rb +42 -0
- data/spec/moneta/proxies/pool/pool_spec.rb +319 -6
- data/spec/restserver.rb +40 -0
- metadata +122 -7
- data/script/install-kyotocabinet +0 -17
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# Moneta: A unified interface for key/value stores
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/moneta.svg)](http://rubygems.org/gems/moneta)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/moneta.svg)](http://rubygems.org/gems/moneta)
|
4
|
+
[![Build Status](https://secure.travis-ci.org/moneta-rb/moneta.svg?branch=master)](http://travis-ci.org/moneta-rb/moneta)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/moneta-rb/moneta.svg)](https://codeclimate.com/github/moneta-rb/moneta)
|
6
|
+
[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=min4d&url=https://github.com/moneta-rb/moneta&title=Moneta&language=&tags=github&category=software)
|
4
7
|
|
5
8
|
Moneta provides a standard interface for interacting with various kinds of key/value stores. Moneta supports the well-known
|
6
9
|
NoSQL and document based stores.
|
@@ -75,6 +78,9 @@ store.close
|
|
75
78
|
* API documentation:
|
76
79
|
* Latest Gem: <http://rubydoc.info/gems/moneta/frames>
|
77
80
|
* GitHub master: <http://rubydoc.info/github/moneta-rb/moneta/master/frames>
|
81
|
+
* Changelog: <https://github.com/moneta-rb/moneta/blob/master/CHANGES>
|
82
|
+
|
83
|
+
In case you are wondering, Moneta uses [Semantic Versioning](https://semver.org/) since v1.0.0.
|
78
84
|
|
79
85
|
------
|
80
86
|
|
@@ -164,7 +170,7 @@ __NOTE:__ <a name="backend-matrix"></a>The backend matrix is much more readable
|
|
164
170
|
|
165
171
|
<tr><td>DBM</td><td>-</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td><a href="http://www.ruby-doc.org/stdlib/libdoc/dbm/rdoc/DBM.html">Berkeley DB using DBM interface or NDBM (Depends on Ruby environment)</a></td></tr>
|
166
172
|
|
167
|
-
<tr><td>GDBM</td><td
|
173
|
+
<tr><td>GDBM</td><td>ffi-gdbm on JRuby</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td><a href="http://www.ruby-doc.org/stdlib/libdoc/gdbm/rdoc/GDBM.html">GDBM</a> database</td></tr>
|
168
174
|
|
169
175
|
<tr><td>LevelDB</td><td>leveldb</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#F44">✗</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td style="text-align:center;background:#5F5">✓</td><td><a href="http://code.google.com/p/leveldb/">LevelDB</a> database</td></tr>
|
170
176
|
|
@@ -235,17 +241,37 @@ __NOTE:__ <a name="backend-matrix"></a>The backend matrix is much more readable
|
|
235
241
|
In addition it supports proxies (Similar to [Rack middlewares](http://rack.github.com/)) which
|
236
242
|
add additional features to storage backends:
|
237
243
|
|
238
|
-
* `Moneta::Proxy` and `Moneta::Wrapper` proxy base classes
|
239
|
-
* `Moneta::
|
240
|
-
|
241
|
-
|
242
|
-
* `Moneta::
|
243
|
-
|
244
|
-
* `Moneta::
|
245
|
-
|
246
|
-
|
247
|
-
* `Moneta::
|
248
|
-
|
244
|
+
* `Moneta::Proxy` and `Moneta::Wrapper` are the proxy base classes.
|
245
|
+
* `Moneta::Cache` combine two stores, one as backend and one as cache (e.g.
|
246
|
+
`Moneta::Adapters::File` + `Moneta::Adapters::LRUHash`). Add it in the
|
247
|
+
builder using `use(:Cache) {}`.
|
248
|
+
* `Moneta::Expires` to add expiration support to stores which don't support it
|
249
|
+
natively. Add it in the builder using `use :Expires`.
|
250
|
+
* `Moneta::Fallback` use a store as a fallback when exceptions occur (by default the
|
251
|
+
`:Null` adapter is used so that an error results in a no-op). Add it to the
|
252
|
+
builder using `use(:Fallback, rescue: IOError)`
|
253
|
+
* `Moneta::Lock` to make store thread safe. Add it in the builder using `use
|
254
|
+
:Lock`.
|
255
|
+
* `Moneta::Logger` to log database accesses. Add it in the builder using `use
|
256
|
+
:Logger`.
|
257
|
+
* `Moneta::Pool` to create a pool of stores as a means of making the store
|
258
|
+
thread safe. Add it in the builder using `use(:Pool, min: 2, max: 4, ttl: 60,
|
259
|
+
timeout: 5) {}`.
|
260
|
+
* `Moneta::Shared` to share a store between multiple processes. Add it in the
|
261
|
+
builder using `use(:Shared) {}`.
|
262
|
+
* `Moneta::Stack` to stack multiple stores (Read returns result from first
|
263
|
+
where the key is found, writes go to all stores). Add it in the builder using
|
264
|
+
`use(:Stack) {}`.
|
265
|
+
* `Moneta::Transformer` transforms keys and values (Marshal, YAML, JSON,
|
266
|
+
Base64, MD5, ...). Add it in the builder using `use :Transformer`.
|
267
|
+
* `Moneta::WeakIncrement` and `Moneta::WeakCreate` to add `#create` and
|
268
|
+
`#increment` support without atomicity (weak) to stores which don't support
|
269
|
+
it.
|
270
|
+
* `Moneta::WeakEachKey` to add key traversal to stores that don't support it,
|
271
|
+
with the important caveat that only those keys previously seen by this proxy
|
272
|
+
will be traversed.
|
273
|
+
|
274
|
+
Check the YARD documentation for more information and examples.
|
249
275
|
|
250
276
|
### Serializers and compressors (`Moneta::Transformer`)
|
251
277
|
|
@@ -887,7 +913,7 @@ Person.adapter :memory, Moneta.new(:Redis)
|
|
887
913
|
|
888
914
|
## Testing and Benchmarks
|
889
915
|
|
890
|
-
Testing is done using [Travis-CI](http://travis-ci.org/moneta-rb/moneta). Currently we support Ruby >=
|
916
|
+
Testing is done using [Travis-CI](http://travis-ci.org/moneta-rb/moneta). Currently we support MRI Ruby >= 2.2.2 and JRuby 9.2.x.
|
891
917
|
|
892
918
|
Benchmarks for each store are done on [Travis-CI](http://travis-ci.org/moneta-rb/moneta) for each build. Take a look there
|
893
919
|
to compare the speed of the different key value stores for different key/value sizes and size distributions.
|
data/feature_matrix.yaml
CHANGED
@@ -82,6 +82,7 @@ backends:
|
|
82
82
|
description: "[Berkeley DB using DBM interface or NDBM (Depends on Ruby environment)](http://www.ruby-doc.org/stdlib/libdoc/dbm/rdoc/DBM.html)"
|
83
83
|
- adapter: GDBM
|
84
84
|
platforms: [ MRI, JRuby ]
|
85
|
+
gems: ffi-gdbm on JRuby
|
85
86
|
features: [ increment, create, each_key, bulk_read, bulk_write ]
|
86
87
|
description: "[GDBM](http://www.ruby-doc.org/stdlib/libdoc/gdbm/rdoc/GDBM.html) database"
|
87
88
|
- adapter: LevelDB
|
@@ -79,7 +79,7 @@ module ActiveSupport
|
|
79
79
|
def write_multi(hash, options = nil)
|
80
80
|
options = merged_options(options)
|
81
81
|
|
82
|
-
instrument :write_multi, hash, options do
|
82
|
+
instrument :write_multi, hash, options do
|
83
83
|
entries = hash.each_with_object({}) do |(name, value), memo|
|
84
84
|
memo[normalize_key(name, options)] = \
|
85
85
|
Entry.new(value, options.merge(version: normalize_version(name, options)))
|
@@ -118,12 +118,12 @@ module ActiveSupport
|
|
118
118
|
|
119
119
|
def read_multi_entries(names, options)
|
120
120
|
keys = names.map { |name| normalize_key(name, options) }
|
121
|
-
entries = @store
|
122
|
-
values_at(*keys, **moneta_options(options, false))
|
123
|
-
map(&method(:make_entry))
|
121
|
+
entries = @store
|
122
|
+
.values_at(*keys, **moneta_options(options, false))
|
123
|
+
.map(&method(:make_entry))
|
124
124
|
|
125
125
|
names.zip(keys, entries).map do |name, key, entry|
|
126
|
-
next if entry
|
126
|
+
next if entry == nil
|
127
127
|
delete_entry(key, options) if entry.expired?
|
128
128
|
next if entry.expired? || entry.mismatched?(normalize_version(name, options))
|
129
129
|
|
data/lib/moneta.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
|
+
# Provides two methods for constructing stores:
|
2
|
+
#
|
3
|
+
# * {Moneta.new}
|
4
|
+
# * {Moneta.build}
|
1
5
|
module Moneta
|
2
6
|
autoload :Builder, 'moneta/builder'
|
3
7
|
autoload :Cache, 'moneta/cache'
|
4
8
|
autoload :CreateSupport, 'moneta/mixins'
|
5
9
|
autoload :Defaults, 'moneta/mixins'
|
6
10
|
autoload :EachKeySupport, 'moneta/mixins'
|
11
|
+
autoload :Enumerable, 'moneta/enumerable'
|
7
12
|
autoload :ExpiresSupport, 'moneta/mixins'
|
8
13
|
autoload :Expires, 'moneta/expires'
|
14
|
+
autoload :Fallback, 'moneta/fallback'
|
9
15
|
autoload :HashAdapter, 'moneta/mixins'
|
10
16
|
autoload :IncrementSupport, 'moneta/mixins'
|
11
17
|
autoload :Lock, 'moneta/lock'
|
@@ -26,9 +32,11 @@ module Moneta
|
|
26
32
|
autoload :WeakIncrement, 'moneta/weak'
|
27
33
|
autoload :Wrapper, 'moneta/wrapper'
|
28
34
|
|
35
|
+
# Adapters are classes which wrap databases, services etc., as described in
|
36
|
+
# {file:SPEC.md The Moneta Specification}.
|
29
37
|
module Adapters
|
30
38
|
autoload :ActiveRecord, 'moneta/adapters/activerecord'
|
31
|
-
autoload :ActiveSupportCache,'moneta/adapters/activesupportcache'
|
39
|
+
autoload :ActiveSupportCache, 'moneta/adapters/activesupportcache'
|
32
40
|
autoload :Cassandra, 'moneta/adapters/cassandra'
|
33
41
|
autoload :Client, 'moneta/adapters/client'
|
34
42
|
autoload :Cookie, 'moneta/adapters/cookie'
|
@@ -40,7 +40,8 @@ module Moneta
|
|
40
40
|
# @param [Hash] options
|
41
41
|
# @option options [Object] :backend A class object inheriting from ActiveRecord::Base to use as a table
|
42
42
|
# @option options [String] :table ('moneta') Table name
|
43
|
-
# @option options [Hash/String/Symbol] :connection ActiveRecord connection configuration (`Hash` or `String`), or
|
43
|
+
# @option options [Hash/String/Symbol] :connection ActiveRecord connection configuration (`Hash` or `String`), or
|
44
|
+
# symbol giving the name of a Rails connection (e.g. :production)
|
44
45
|
# @option options [Proc, Boolean] :create_table Proc called with a connection if table
|
45
46
|
# needs to be created. Pass false to skip the create table check all together.
|
46
47
|
# @option options [Symbol] :key_column (:k) The name of the column to use for keys
|
@@ -87,7 +88,7 @@ module Moneta
|
|
87
88
|
|
88
89
|
table_name = (options.delete(:table) || :moneta).to_sym
|
89
90
|
create_table_proc = options.delete(:create_table)
|
90
|
-
if create_table_proc
|
91
|
+
if create_table_proc == nil
|
91
92
|
create_table(table_name)
|
92
93
|
elsif create_table_proc
|
93
94
|
with_connection(&create_table_proc)
|
@@ -164,10 +165,11 @@ module Moneta
|
|
164
165
|
end
|
165
166
|
end
|
166
167
|
end
|
167
|
-
rescue ::ActiveRecord::RecordNotUnique
|
168
|
-
# This handles the "no row updated" issue, above
|
168
|
+
rescue ::ActiveRecord::RecordNotUnique, ::ActiveRecord::Deadlocked
|
169
|
+
# This handles the "no row updated" issue, above, as well as deadlocks
|
170
|
+
# which may occur on some adapters
|
169
171
|
tries ||= 0
|
170
|
-
|
172
|
+
(tries += 1) <= 3 ? retry : raise
|
171
173
|
end
|
172
174
|
|
173
175
|
# (see Proxy#create)
|
@@ -197,14 +199,32 @@ module Moneta
|
|
197
199
|
# (see Proxy#slice)
|
198
200
|
def slice(*keys, lock: false, **options)
|
199
201
|
with_connection do |conn|
|
200
|
-
|
201
|
-
|
202
|
-
|
202
|
+
conn.create_table(:slice_keys, temporary: true) do |t|
|
203
|
+
t.string :key, null: false
|
204
|
+
end
|
203
205
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
206
|
+
begin
|
207
|
+
temp_table = ::Arel::Table.new(:slice_keys)
|
208
|
+
keys.each do |key|
|
209
|
+
conn.insert ::Arel::InsertManager.new
|
210
|
+
.into(temp_table)
|
211
|
+
.insert([[temp_table[:key], key]])
|
212
|
+
end
|
213
|
+
|
214
|
+
sel = arel_sel
|
215
|
+
.join(temp_table)
|
216
|
+
.on(table[key_column].eq(temp_table[:key]))
|
217
|
+
.project(table[key_column], table[value_column])
|
218
|
+
sel = sel.lock if lock
|
219
|
+
result = conn.select_all(sel)
|
220
|
+
|
221
|
+
k = key_column.to_s
|
222
|
+
v = value_column.to_s
|
223
|
+
result.map do |row|
|
224
|
+
[row[k], decode(conn, row[v])]
|
225
|
+
end
|
226
|
+
ensure
|
227
|
+
conn.drop_table(:slice_keys)
|
208
228
|
end
|
209
229
|
end
|
210
230
|
end
|
@@ -255,7 +275,7 @@ module Moneta
|
|
255
275
|
self.class.retrieve_or_establish_connection_pool(@spec)
|
256
276
|
end
|
257
277
|
|
258
|
-
def create_table
|
278
|
+
def create_table(table_name)
|
259
279
|
with_connection do |conn|
|
260
280
|
return if conn.table_exists?(table_name)
|
261
281
|
|
@@ -287,10 +307,6 @@ module Moneta
|
|
287
307
|
arel_sel.where(table[key_column].eq(key))
|
288
308
|
end
|
289
309
|
|
290
|
-
def arel_slice(keys)
|
291
|
-
arel_sel.where(table[key_column].eq_any(keys))
|
292
|
-
end
|
293
|
-
|
294
310
|
def conn_ins(conn, key, value)
|
295
311
|
ins = ::Arel::InsertManager.new.into(table)
|
296
312
|
ins.insert([[table[key_column], key], [table[value_column], value]])
|
@@ -311,7 +327,7 @@ module Moneta
|
|
311
327
|
elsif conn.respond_to?(:escape_bytea)
|
312
328
|
conn.escape_bytea(value)
|
313
329
|
elsif defined?(::ActiveRecord::ConnectionAdapters::SQLite3Adapter) &&
|
314
|
-
|
330
|
+
conn.is_a?(::ActiveRecord::ConnectionAdapters::SQLite3Adapter)
|
315
331
|
Arel::Nodes::SqlLiteral.new("X'#{value.unpack('H*').first}'")
|
316
332
|
else
|
317
333
|
value
|
@@ -322,7 +338,7 @@ module Moneta
|
|
322
338
|
if value == nil
|
323
339
|
nil
|
324
340
|
elsif defined?(::ActiveModel::Type::Binary::Data) &&
|
325
|
-
|
341
|
+
value.is_a?(::ActiveModel::Type::Binary::Data)
|
326
342
|
value.to_s
|
327
343
|
elsif conn.respond_to?(:unescape_bytea)
|
328
344
|
conn.unescape_bytea(value)
|
@@ -25,7 +25,7 @@ module Moneta
|
|
25
25
|
# (see Proxy#key?)
|
26
26
|
def key?(key, options = {})
|
27
27
|
@backend.exist?(key).tap do |exists|
|
28
|
-
if exists &&
|
28
|
+
if exists && (expires = expires_value(options, nil)) != nil
|
29
29
|
value = @backend.read(key, options)
|
30
30
|
@backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
|
31
31
|
end
|
@@ -36,14 +36,10 @@ module Moneta
|
|
36
36
|
def load(key, options = {})
|
37
37
|
expires = expires_value(options, nil)
|
38
38
|
value = @backend.read(key, options)
|
39
|
-
if value and
|
39
|
+
if value and expires != nil
|
40
40
|
@backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
|
41
41
|
end
|
42
|
-
|
43
|
-
value && value.to_s
|
44
|
-
else
|
45
|
-
value
|
46
|
-
end
|
42
|
+
value
|
47
43
|
end
|
48
44
|
|
49
45
|
# (see Proxy#store)
|
@@ -83,10 +83,14 @@ module Moneta
|
|
83
83
|
# and then re-set it in order to update the TTL.
|
84
84
|
return false unless
|
85
85
|
row = @backend.execute(@load, options.merge(consistency: rc, arguments: [key])).first and
|
86
|
-
|
87
|
-
@backend.execute(@update_expires,
|
88
|
-
|
89
|
-
|
86
|
+
row[@expired_column] != nil
|
87
|
+
@backend.execute(@update_expires,
|
88
|
+
options.merge(consistency: wc,
|
89
|
+
arguments: [(expires || 0).to_i,
|
90
|
+
timestamp,
|
91
|
+
row[@value_column],
|
92
|
+
key,
|
93
|
+
row[@updated_column]]))
|
90
94
|
true
|
91
95
|
elsif row = @backend.execute(@key, options.merge(consistency: rc, arguments: [key])).first
|
92
96
|
row[@expired_column] != nil
|
@@ -100,9 +104,13 @@ module Moneta
|
|
100
104
|
rc, wc = consistency(options)
|
101
105
|
if row = @backend.execute(@load, options.merge(consistency: rc, arguments: [key])).first and row[@expired_column] != nil
|
102
106
|
if (expires = expires_value(options, nil)) != nil
|
103
|
-
@backend.execute(@update_expires,
|
104
|
-
|
105
|
-
|
107
|
+
@backend.execute(@update_expires,
|
108
|
+
options.merge(consistency: wc,
|
109
|
+
arguments: [(expires || 0).to_i,
|
110
|
+
timestamp,
|
111
|
+
row[@value_column],
|
112
|
+
key,
|
113
|
+
row[@updated_column]]))
|
106
114
|
end
|
107
115
|
row[@value_column]
|
108
116
|
end
|
@@ -150,7 +158,7 @@ module Moneta
|
|
150
158
|
|
151
159
|
# (see Proxy#each_key)
|
152
160
|
def each_key
|
153
|
-
rc,
|
161
|
+
rc, = consistency
|
154
162
|
return enum_for(:each_key) unless block_given?
|
155
163
|
result = @backend.execute(@each_key, consistency: rc, page_size: 100)
|
156
164
|
loop do
|
@@ -229,7 +237,7 @@ module Moneta
|
|
229
237
|
end
|
230
238
|
end
|
231
239
|
|
232
|
-
|
240
|
+
_rc, wc = consistency(options)
|
233
241
|
expires = expires_value(options)
|
234
242
|
t = timestamp
|
235
243
|
batch = @backend.batch do |batch|
|
@@ -270,16 +278,16 @@ module Moneta
|
|
270
278
|
# but use single quotes instead of double-quotes.
|
271
279
|
require 'multi_json'
|
272
280
|
option_str = options.map do |key, value|
|
273
|
-
key.to_s + ' = ' +
|
281
|
+
key.to_s + ' = ' + MultiJson.dump(value).tr(?", ?')
|
274
282
|
end.join(' AND ')
|
275
283
|
|
276
|
-
@backend.execute "CREATE KEYSPACE IF NOT EXISTS
|
277
|
-
keyspace,
|
278
|
-
option_str
|
279
|
-
|
284
|
+
@backend.execute "CREATE KEYSPACE IF NOT EXISTS %<keyspace>s WITH %<options>s" % {
|
285
|
+
keyspace: keyspace,
|
286
|
+
options: option_str
|
287
|
+
}
|
280
288
|
rescue ::Cassandra::Errors::TimeoutError
|
281
289
|
tries ||= 0
|
282
|
-
|
290
|
+
(tries += 1) <= 3 ? retry : raise
|
283
291
|
end
|
284
292
|
|
285
293
|
def prepare_statements
|
@@ -338,7 +346,7 @@ module Moneta
|
|
338
346
|
CQL
|
339
347
|
end
|
340
348
|
|
341
|
-
def consistency(options={})
|
349
|
+
def consistency(options = {})
|
342
350
|
[
|
343
351
|
options[:read_consistency] || @read_consistency,
|
344
352
|
options[:write_consistency] || @write_consistency
|
@@ -12,8 +12,12 @@ module Moneta
|
|
12
12
|
# @option options [String] :host ('127.0.0.1') Hostname
|
13
13
|
# @option options [String] :socket Unix socket file name as alternative to `:port` and `:host`
|
14
14
|
def initialize(options = {})
|
15
|
-
@socket =
|
16
|
-
|
15
|
+
@socket =
|
16
|
+
if options[:socket]
|
17
|
+
UNIXSocket.open(options[:socket])
|
18
|
+
else
|
19
|
+
TCPSocket.open(options[:host] || '127.0.0.1', options[:port] || 9000)
|
20
|
+
end
|
17
21
|
end
|
18
22
|
|
19
23
|
# (see Proxy#key?)
|
@@ -83,15 +87,15 @@ module Moneta
|
|
83
87
|
end
|
84
88
|
|
85
89
|
def read
|
86
|
-
size = @socket
|
87
|
-
recv(4).tap do |bytes|
|
88
|
-
raise EOFError
|
89
|
-
end
|
90
|
-
unpack('N')
|
91
|
-
first
|
90
|
+
size = @socket
|
91
|
+
.recv(4).tap do |bytes|
|
92
|
+
raise EOFError, 'failed to read size' unless bytes.bytesize == 4
|
93
|
+
end
|
94
|
+
.unpack('N')
|
95
|
+
.first
|
92
96
|
|
93
97
|
result = Marshal.load(@socket.recv(size).tap do |bytes|
|
94
|
-
raise EOFError
|
98
|
+
raise EOFError, 'Not enough bytes read' unless bytes.bytesize == size
|
95
99
|
end)
|
96
100
|
|
97
101
|
raise result if Exception === result
|
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'faraday'
|
2
2
|
require 'multi_json'
|
3
|
-
require 'uri'
|
4
3
|
|
5
4
|
module Moneta
|
6
5
|
module Adapters
|
@@ -16,6 +15,19 @@ module Moneta
|
|
16
15
|
class Couch
|
17
16
|
include Defaults
|
18
17
|
|
18
|
+
# @api private
|
19
|
+
class HTTPError < StandardError
|
20
|
+
attr_reader :status, :request_method, :key
|
21
|
+
|
22
|
+
def initialize(status, request_method, key)
|
23
|
+
@status = status
|
24
|
+
@request_method = request_method.to_sym
|
25
|
+
@key = key
|
26
|
+
|
27
|
+
super "HTTP Error: #{@status} (#{@request_method.to_s.upcase} #{@key})"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
19
31
|
attr_reader :backend
|
20
32
|
|
21
33
|
supports :create, :each_key
|
@@ -24,14 +36,26 @@ module Moneta
|
|
24
36
|
# @option options [String] :host ('127.0.0.1') Couch host
|
25
37
|
# @option options [String] :port (5984) Couch port
|
26
38
|
# @option options [String] :db ('moneta') Couch database
|
39
|
+
# @option options [String] :scheme ('http') HTTP scheme to use
|
27
40
|
# @option options [String] :value_field ('value') Document field to store value
|
28
41
|
# @option options [String] :type_field ('type') Document field to store value type
|
29
|
-
# @option options [
|
42
|
+
# @option options [Symbol] :adapter Adapter to use with Faraday
|
43
|
+
# @option options [Faraday::Connecton] :backend Use existing backend instance
|
44
|
+
# @option options Other options passed to {Faraday::new} (unless
|
45
|
+
# :backend option is provided).
|
30
46
|
def initialize(options = {})
|
31
|
-
@value_field = options
|
32
|
-
@type_field = options
|
33
|
-
|
34
|
-
|
47
|
+
@value_field = options.delete(:value_field) || 'value'
|
48
|
+
@type_field = options.delete(:type_field) || 'type'
|
49
|
+
@backend = options.delete(:backend) || begin
|
50
|
+
host = options.delete(:host) || '127.0.0.1'
|
51
|
+
port = options.delete(:port) || 5984
|
52
|
+
db = options.delete(:db) || 'moneta'
|
53
|
+
scheme = options.delete(:scheme) || 'http'
|
54
|
+
block = if faraday_adapter = options.delete(:adapter)
|
55
|
+
proc { |faraday| faraday.adapter(faraday_adapter) }
|
56
|
+
end
|
57
|
+
::Faraday.new("#{scheme}://#{host}:#{port}/#{db}", options, &block)
|
58
|
+
end
|
35
59
|
@rev_cache = Moneta.build do
|
36
60
|
use :Lock
|
37
61
|
adapter :LRUHash
|
@@ -40,68 +64,121 @@ module Moneta
|
|
40
64
|
end
|
41
65
|
|
42
66
|
# (see Proxy#key?)
|
67
|
+
# @option options [Boolean] :cache_rev (true) Whether to cache the rev of
|
68
|
+
# the document for faster updating
|
43
69
|
def key?(key, options = {})
|
44
|
-
|
45
|
-
|
46
|
-
response.status == 200
|
70
|
+
cache_rev = options[:cache_rev] != false
|
71
|
+
head(key, cache_rev: cache_rev)
|
47
72
|
end
|
48
73
|
|
49
74
|
# (see Proxy#load)
|
75
|
+
# @option (see #key?)
|
50
76
|
def load(key, options = {})
|
51
|
-
|
52
|
-
|
53
|
-
|
77
|
+
cache_rev = options[:cache_rev] != false
|
78
|
+
doc = get(key, cache_rev: cache_rev)
|
79
|
+
doc ? doc_to_value(doc) : nil
|
54
80
|
end
|
55
81
|
|
56
82
|
# (see Proxy#store)
|
83
|
+
# @option (see #key?)
|
84
|
+
# @option options [Boolean] :batch (false) Whether to do a
|
85
|
+
# {https://docs.couchdb.org/en/stable/api/database/common.html#api-doc-batch-writes
|
86
|
+
# batch mode write}
|
87
|
+
# @option options [Boolean] :full_commit (nil) Set to `true` or `false`
|
88
|
+
# to override the server's
|
89
|
+
# {https://docs.couchdb.org/en/stable/config/couchdb.html#couchdb/delayed_commits
|
90
|
+
# commit policy}
|
57
91
|
def store(key, value, options = {})
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
92
|
+
put(key, value_to_doc(value, rev(key)),
|
93
|
+
headers: full_commit_header(options[:full_commit]),
|
94
|
+
query: options[:batch] ? { batch: 'ok' } : {},
|
95
|
+
cache_rev: options[:cache_rev] != false,
|
96
|
+
expect: options[:batch] ? 202 : 201)
|
62
97
|
value
|
63
|
-
rescue
|
98
|
+
rescue HTTPError
|
64
99
|
tries ||= 0
|
65
100
|
(tries += 1) < 10 ? retry : raise
|
66
101
|
end
|
67
102
|
|
68
103
|
# (see Proxy#delete)
|
104
|
+
# @option options [Boolean] :batch (false) Whether to do a
|
105
|
+
# {https://docs.couchdb.org/en/stable/api/database/common.html#api-doc-batch-writes
|
106
|
+
# batch mode write}
|
107
|
+
# @option options [Boolean] :full_commit (nil) Set to `true` or `false`
|
108
|
+
# to override the server's
|
109
|
+
# {https://docs.couchdb.org/en/stable/config/couchdb.html#couchdb/delayed_commits
|
110
|
+
# commit policy}
|
69
111
|
def delete(key, options = {})
|
70
|
-
|
71
|
-
get_response
|
72
|
-
if get_response.status == 200
|
73
|
-
existing_rev = get_response['etag'][1..-2]
|
112
|
+
get_response = get(key, returns: :response)
|
113
|
+
if get_response.success?
|
74
114
|
value = body_to_value(get_response.body)
|
75
|
-
|
76
|
-
|
115
|
+
existing_rev = parse_rev(get_response)
|
116
|
+
query = { rev: existing_rev }
|
117
|
+
query[:batch] = 'ok' if options[:batch]
|
118
|
+
request(:delete, key,
|
119
|
+
headers: full_commit_header(options[:full_commit]),
|
120
|
+
query: query,
|
121
|
+
expect: options[:batch] ? 202 : 200)
|
122
|
+
delete_cached_rev(key)
|
77
123
|
value
|
78
124
|
end
|
79
|
-
rescue
|
125
|
+
rescue HTTPError
|
80
126
|
tries ||= 0
|
81
127
|
(tries += 1) < 10 ? retry : raise
|
82
128
|
end
|
83
129
|
|
84
130
|
# (see Proxy#clear)
|
131
|
+
# @option options [Boolean] :compact (false) Whether to compact the database after clearing
|
132
|
+
# @option options [Boolean] :await_compact (false) Whether to wait for compaction to complete
|
133
|
+
# before returning.
|
134
|
+
# @option options [Boolean] :full_commit (nil) Set to `true` or `false`
|
135
|
+
# to override the server's
|
136
|
+
# {https://docs.couchdb.org/en/stable/config/couchdb.html#couchdb/delayed_commits
|
137
|
+
# commit policy}
|
85
138
|
def clear(options = {})
|
86
|
-
|
87
|
-
|
139
|
+
loop do
|
140
|
+
docs = all_docs(limit: 10_000)
|
141
|
+
break if docs['rows'].empty?
|
142
|
+
deletions = docs['rows'].map do |row|
|
143
|
+
{ _id: row['id'], _rev: row['value']['rev'], _deleted: true }
|
144
|
+
end
|
145
|
+
bulk_docs(deletions, full_commit: options[:full_commit])
|
146
|
+
end
|
147
|
+
|
148
|
+
# Compact the database unless told not to
|
149
|
+
if options[:compact]
|
150
|
+
post('_compact', expect: 202)
|
151
|
+
|
152
|
+
# Performance won't be great while compaction is happening, so by default we wait for it
|
153
|
+
if options[:await_compact]
|
154
|
+
loop do
|
155
|
+
db_info = get('', expect: 200)
|
156
|
+
break unless db_info['compact_running']
|
157
|
+
|
158
|
+
# wait before checking again
|
159
|
+
sleep 1
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
88
164
|
self
|
89
165
|
end
|
90
166
|
|
91
167
|
# (see Proxy#create)
|
168
|
+
# @option (see #key?)
|
92
169
|
def create(key, value, options = {})
|
93
|
-
|
94
|
-
|
95
|
-
|
170
|
+
cache_rev = options[:cache_rev] != false
|
171
|
+
doc = value_to_doc(value, nil)
|
172
|
+
response = put(key, doc, cache_rev: cache_rev, returns: :response)
|
96
173
|
case response.status
|
97
174
|
when 201
|
98
175
|
true
|
99
176
|
when 409
|
100
177
|
false
|
101
178
|
else
|
102
|
-
raise
|
179
|
+
raise HTTPError.new(response.status, :put, @backend.create_url(key))
|
103
180
|
end
|
104
|
-
rescue
|
181
|
+
rescue HTTPError
|
105
182
|
tries ||= 0
|
106
183
|
(tries += 1) < 10 ? retry : raise
|
107
184
|
end
|
@@ -112,21 +189,14 @@ module Moneta
|
|
112
189
|
|
113
190
|
skip = 0
|
114
191
|
limit = 1000
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
result['rows'].each do |row|
|
124
|
-
key = row['id']
|
125
|
-
@rev_cache[key] = row['value']['rev']
|
126
|
-
yield key
|
127
|
-
end
|
128
|
-
else
|
129
|
-
raise "HTTP error #{response.status}"
|
192
|
+
loop do
|
193
|
+
docs = all_docs(limit: limit, skip: skip)
|
194
|
+
break if docs['rows'].empty?
|
195
|
+
skip += docs['rows'].length
|
196
|
+
docs['rows'].each do |row|
|
197
|
+
key = row['id']
|
198
|
+
@rev_cache[key] = row['value']['rev']
|
199
|
+
yield key
|
130
200
|
end
|
131
201
|
end
|
132
202
|
self
|
@@ -140,16 +210,18 @@ module Moneta
|
|
140
210
|
|
141
211
|
# (see Proxy#slice)
|
142
212
|
def slice(*keys, **options)
|
143
|
-
|
144
|
-
raise "HTTP error #{response.status}" unless response.status == 200
|
145
|
-
docs = MultiJson.load(response.body)
|
213
|
+
docs = all_docs(keys: keys, include_docs: true)
|
146
214
|
docs["rows"].map do |row|
|
147
|
-
next unless row['doc']
|
148
|
-
[row['id'], doc_to_value(
|
215
|
+
next unless doc = row['doc']
|
216
|
+
[row['id'], doc_to_value(doc)]
|
149
217
|
end.compact
|
150
218
|
end
|
151
219
|
|
152
220
|
# (see Proxy#merge!)
|
221
|
+
# @option options [Boolean] :full_commit (nil) Set to `true` or `false`
|
222
|
+
# to override the server's
|
223
|
+
# {https://docs.couchdb.org/en/stable/config/couchdb.html#couchdb/delayed_commits
|
224
|
+
# commit policy}
|
153
225
|
def merge!(pairs, options = {})
|
154
226
|
keys = pairs.map { |key, _| key }.to_a
|
155
227
|
cache_revs(*keys.reject { |key| @rev_cache[key] })
|
@@ -169,16 +241,14 @@ module Moneta
|
|
169
241
|
end
|
170
242
|
|
171
243
|
docs = pairs.map { |key, value| value_to_doc(value, @rev_cache[key], key) }.to_a
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
if row['ok'] == true
|
178
|
-
@rev_cache[row['id']] = row['rev']
|
244
|
+
results = bulk_docs(docs, full_commit: options[:full_commit], returns: :doc)
|
245
|
+
retries = results.each_with_object([]) do |row, retries|
|
246
|
+
ok, id = row.values_at('ok', 'id')
|
247
|
+
if ok
|
248
|
+
@rev_cache[id] = row['rev']
|
179
249
|
elsif row['error'] == 'conflict'
|
180
|
-
|
181
|
-
retries << pairs.find { |key
|
250
|
+
delete_cached_rev(id)
|
251
|
+
retries << pairs.find { |key,| key == id }
|
182
252
|
else
|
183
253
|
raise "Unrecognised response: #{row}"
|
184
254
|
end
|
@@ -194,6 +264,10 @@ module Moneta
|
|
194
264
|
|
195
265
|
private
|
196
266
|
|
267
|
+
def full_commit_header(full_commit)
|
268
|
+
full_commit == nil ? {} : { 'X-Couch-Full-Commit' => (!!full_commit).to_s }
|
269
|
+
end
|
270
|
+
|
197
271
|
def body_to_value(body)
|
198
272
|
doc_to_value(MultiJson.load(body))
|
199
273
|
end
|
@@ -228,61 +302,127 @@ module Moneta
|
|
228
302
|
doc
|
229
303
|
end
|
230
304
|
|
231
|
-
def value_to_body(value, rev)
|
232
|
-
MultiJson.dump(value_to_doc(value, rev))
|
233
|
-
end
|
234
|
-
|
235
305
|
def create_db
|
236
|
-
|
237
|
-
response =
|
306
|
+
loop do
|
307
|
+
response = put('', returns: :response)
|
238
308
|
case response.status
|
239
309
|
when 201
|
240
310
|
break
|
241
311
|
when 412
|
242
312
|
# Make sure the database really does exist
|
243
|
-
|
313
|
+
# See https://github.com/apache/couchdb/issues/2073
|
314
|
+
break if head('')
|
244
315
|
else
|
245
|
-
raise
|
316
|
+
raise HTTPError.new(response.status, :put, '')
|
246
317
|
end
|
247
318
|
|
248
319
|
# Wait before trying again
|
249
320
|
sleep 1
|
250
321
|
end
|
322
|
+
|
323
|
+
self
|
251
324
|
end
|
252
325
|
|
253
326
|
def cache_revs(*keys)
|
254
|
-
|
255
|
-
raise "HTTP error #{response.status}" unless response.status == 200
|
256
|
-
docs = MultiJson.load(response.body)
|
327
|
+
docs = all_docs(keys: keys)
|
257
328
|
docs['rows'].each do |row|
|
258
|
-
next
|
329
|
+
next if !row['value'] || row['value']['deleted']
|
259
330
|
@rev_cache[row['id']] = row['value']['rev']
|
260
331
|
end
|
261
332
|
end
|
262
333
|
|
263
|
-
def
|
334
|
+
def parse_rev(response)
|
335
|
+
response['etag'][1..-2]
|
336
|
+
end
|
337
|
+
|
338
|
+
def cache_response_rev(key, response)
|
264
339
|
case response.status
|
265
340
|
when 200, 201
|
266
|
-
@rev_cache[key] = response
|
341
|
+
@rev_cache[key] = parse_rev(response)
|
267
342
|
else
|
268
|
-
|
343
|
+
delete_cached_rev(key)
|
269
344
|
nil
|
270
345
|
end
|
271
346
|
end
|
272
347
|
|
273
|
-
def
|
348
|
+
def delete_cached_rev(key)
|
274
349
|
@rev_cache.delete(key)
|
275
350
|
end
|
276
351
|
|
277
352
|
def rev(key)
|
278
|
-
@rev_cache[key] ||
|
279
|
-
response = @backend.head(key)
|
280
|
-
|
353
|
+
@rev_cache[key] || begin
|
354
|
+
response = @backend.head(key)
|
355
|
+
cache_response_rev(key, response)
|
281
356
|
end
|
282
357
|
end
|
283
358
|
|
284
359
|
def encode_query(query)
|
285
|
-
|
360
|
+
query.map { |key, value| [key, MultiJson.dump(value)] }
|
361
|
+
end
|
362
|
+
|
363
|
+
def request(method, key, body = nil, returns: :nil, cache_rev: false, expect: nil, query: nil, headers: {})
|
364
|
+
url = @backend.build_url(key, query)
|
365
|
+
headers['Content-Type'] = 'application/json' if %i{put post}.include?(method)
|
366
|
+
response = @backend.run_request(method, url, body || '', headers)
|
367
|
+
|
368
|
+
if cache_rev
|
369
|
+
cache_response_rev(key, response)
|
370
|
+
end
|
371
|
+
|
372
|
+
if expect
|
373
|
+
raise HTTPError.new(response.status, method, url) unless response.status == expect
|
374
|
+
end
|
375
|
+
|
376
|
+
case returns
|
377
|
+
when :response
|
378
|
+
response
|
379
|
+
when :success
|
380
|
+
response.success?
|
381
|
+
when :doc
|
382
|
+
response.success? ? MultiJson.load(response.body) : nil
|
383
|
+
when :nil
|
384
|
+
nil
|
385
|
+
else
|
386
|
+
raise "Unknown returns param: #{returns.inspect}"
|
387
|
+
end
|
388
|
+
end
|
389
|
+
|
390
|
+
def get(key, returns: :doc, **options)
|
391
|
+
request(:get, key, returns: returns, **options)
|
392
|
+
end
|
393
|
+
|
394
|
+
def head(key, returns: :success, **options)
|
395
|
+
request(:head, key, returns: returns, **options)
|
396
|
+
end
|
397
|
+
|
398
|
+
def put(key, doc = nil, returns: :success, **options)
|
399
|
+
body = doc == nil ? '' : MultiJson.dump(doc)
|
400
|
+
request(:put, key, body, returns: returns, **options)
|
401
|
+
end
|
402
|
+
|
403
|
+
def post(key, doc = nil, returns: :success, **options)
|
404
|
+
body = doc == nil ? '' : MultiJson.dump(doc)
|
405
|
+
request(:post, key, body, returns: returns, **options)
|
406
|
+
end
|
407
|
+
|
408
|
+
def all_docs(sorted: false, **params)
|
409
|
+
keys = params.delete(:keys)
|
410
|
+
query = encode_query(params.merge(sorted: sorted))
|
411
|
+
if keys
|
412
|
+
post('_all_docs', { keys: keys },
|
413
|
+
query: query,
|
414
|
+
expect: 200,
|
415
|
+
returns: :doc)
|
416
|
+
else
|
417
|
+
get('_all_docs', query: query, expect: 200)
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
def bulk_docs(docs, returns: :success, full_commit: nil)
|
422
|
+
post('_bulk_docs', { docs: docs },
|
423
|
+
headers: full_commit_header(full_commit),
|
424
|
+
returns: returns,
|
425
|
+
expect: 201)
|
286
426
|
end
|
287
427
|
end
|
288
428
|
end
|