moneta 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (446) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.rspec +4 -0
  4. data/.travis.yml +83 -32
  5. data/.yardopts +6 -1
  6. data/CHANGES +30 -0
  7. data/CONTRIBUTORS +4 -0
  8. data/Gemfile +28 -36
  9. data/LICENSE +1 -1
  10. data/README.md +136 -67
  11. data/SPEC.md +70 -0
  12. data/feature_matrix.yaml +235 -0
  13. data/lib/active_support/cache/moneta_store.rb +102 -6
  14. data/lib/moneta.rb +9 -2
  15. data/lib/moneta/adapters/activerecord.rb +258 -98
  16. data/lib/moneta/adapters/activesupportcache.rb +133 -0
  17. data/lib/moneta/adapters/cassandra.rb +290 -44
  18. data/lib/moneta/adapters/client.rb +11 -2
  19. data/lib/moneta/adapters/couch.rb +179 -25
  20. data/lib/moneta/adapters/datamapper.rb +1 -0
  21. data/lib/moneta/adapters/daybreak.rb +21 -4
  22. data/lib/moneta/adapters/dbm.rb +7 -7
  23. data/lib/moneta/adapters/file.rb +11 -1
  24. data/lib/moneta/adapters/gdbm.rb +7 -7
  25. data/lib/moneta/adapters/kyotocabinet.rb +66 -1
  26. data/lib/moneta/adapters/leveldb.rb +27 -1
  27. data/lib/moneta/adapters/lmdb.rb +29 -1
  28. data/lib/moneta/adapters/lruhash.rb +30 -0
  29. data/lib/moneta/adapters/memcached/dalli.rb +50 -5
  30. data/lib/moneta/adapters/memcached/native.rb +11 -4
  31. data/lib/moneta/adapters/memory.rb +2 -0
  32. data/lib/moneta/adapters/mongo/base.rb +32 -7
  33. data/lib/moneta/adapters/mongo/moped.rb +59 -2
  34. data/lib/moneta/adapters/mongo/official.rb +40 -0
  35. data/lib/moneta/adapters/pstore.rb +56 -8
  36. data/lib/moneta/adapters/redis.rb +79 -19
  37. data/lib/moneta/adapters/sdbm.rb +7 -7
  38. data/lib/moneta/adapters/sequel.rb +440 -39
  39. data/lib/moneta/adapters/sqlite.rb +74 -10
  40. data/lib/moneta/adapters/tdb.rb +16 -1
  41. data/lib/moneta/adapters/tokyocabinet.rb +7 -1
  42. data/lib/moneta/adapters/tokyotyrant.rb +22 -0
  43. data/lib/moneta/cache.rb +11 -1
  44. data/lib/moneta/expires.rb +110 -8
  45. data/lib/moneta/lock.rb +21 -2
  46. data/lib/moneta/mixins.rb +279 -3
  47. data/lib/moneta/pool.rb +15 -6
  48. data/lib/moneta/proxy.rb +46 -2
  49. data/lib/moneta/server.rb +5 -1
  50. data/lib/moneta/shared.rb +27 -11
  51. data/lib/moneta/stack.rb +1 -1
  52. data/lib/moneta/synchronize.rb +2 -8
  53. data/lib/moneta/transformer.rb +152 -2
  54. data/lib/moneta/transformer/config.rb +2 -0
  55. data/lib/moneta/transformer/helper.rb +2 -2
  56. data/lib/moneta/version.rb +1 -1
  57. data/lib/moneta/weak_each_key.rb +74 -0
  58. data/lib/moneta/wrapper.rb +25 -0
  59. data/moneta.gemspec +2 -2
  60. data/script/benchmarks +174 -62
  61. data/script/parallel-tests +58 -83
  62. data/script/start-hbase +46 -0
  63. data/script/start-services +9 -2
  64. data/script/update-feature-matrix +148 -0
  65. data/spec/active_support/cache_moneta_store_spec.rb +132 -109
  66. data/spec/features/concurrent_create.rb +21 -0
  67. data/spec/features/concurrent_increment.rb +32 -0
  68. data/spec/features/create.rb +25 -0
  69. data/spec/features/create_expires.rb +19 -0
  70. data/spec/features/default_expires.rb +15 -0
  71. data/spec/features/each_key.rb +119 -0
  72. data/spec/features/expires.rb +291 -0
  73. data/spec/features/features.rb +16 -0
  74. data/spec/features/increment.rb +98 -0
  75. data/spec/features/marshallable_key.rb +43 -0
  76. data/spec/features/marshallable_value.rb +7 -0
  77. data/spec/features/multiprocess.rb +8 -0
  78. data/spec/features/not_create.rb +7 -0
  79. data/spec/features/not_each_key.rb +7 -0
  80. data/spec/features/not_increment.rb +13 -0
  81. data/spec/features/not_persist.rb +9 -0
  82. data/spec/features/null.rb +66 -0
  83. data/spec/features/persist.rb +14 -0
  84. data/spec/features/returndifferent.rb +9 -0
  85. data/spec/features/returnsame.rb +9 -0
  86. data/spec/features/store.rb +261 -0
  87. data/spec/features/store_large.rb +13 -0
  88. data/spec/features/transform_value.rb +44 -0
  89. data/spec/features/transform_value_expires.rb +41 -0
  90. data/spec/helper.rb +391 -74
  91. data/spec/moneta/adapters/activerecord/adapter_activerecord_existing_connection_spec.rb +59 -0
  92. data/spec/moneta/adapters/activerecord/adapter_activerecord_spec.rb +118 -0
  93. data/spec/moneta/adapters/activerecord/standard_activerecord_spec.rb +18 -0
  94. data/spec/moneta/adapters/activerecord/standard_activerecord_with_expires_spec.rb +24 -0
  95. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_spec.rb +51 -0
  96. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_with_default_expires_spec.rb +51 -0
  97. data/spec/moneta/adapters/cassandra/adapter_cassandra_spec.rb +17 -0
  98. data/spec/moneta/adapters/cassandra/adapter_cassandra_with_default_expires_spec.rb +18 -0
  99. data/spec/moneta/adapters/cassandra/helper.rb +18 -0
  100. data/spec/moneta/adapters/cassandra/standard_cassandra_spec.rb +18 -0
  101. data/spec/moneta/adapters/client/adapter_client_spec.rb +11 -0
  102. data/spec/moneta/adapters/client/standard_client_tcp_spec.rb +23 -0
  103. data/spec/moneta/adapters/client/standard_client_unix_spec.rb +12 -0
  104. data/spec/moneta/adapters/cookie/adapter_cookie_spec.rb +7 -0
  105. data/spec/moneta/adapters/couch/adapter_couch_spec.rb +7 -0
  106. data/spec/moneta/adapters/couch/standard_couch_spec.rb +9 -0
  107. data/spec/moneta/adapters/couch/standard_couch_with_expires_spec.rb +13 -0
  108. data/spec/moneta/adapters/datamapper/adapter_datamapper_spec.rb +43 -0
  109. data/spec/moneta/adapters/datamapper/standard_datamapper_spec.rb +21 -0
  110. data/spec/moneta/adapters/datamapper/standard_datamapper_with_expires_spec.rb +26 -0
  111. data/spec/moneta/adapters/datamapper/standard_datamapper_with_repository_spec.rb +22 -0
  112. data/spec/moneta/adapters/daybreak/adapter_daybreak_spec.rb +7 -0
  113. data/spec/moneta/adapters/daybreak/standard_daybreak_spec.rb +7 -0
  114. data/spec/moneta/adapters/daybreak/standard_daybreak_with_expires_spec.rb +11 -0
  115. data/spec/moneta/adapters/dbm/adapter_dbm_spec.rb +7 -0
  116. data/spec/moneta/adapters/dbm/standard_dbm_spec.rb +7 -0
  117. data/spec/moneta/adapters/dbm/standard_dbm_with_expires_spec.rb +12 -0
  118. data/spec/moneta/adapters/file/adapter_file_spec.rb +7 -0
  119. data/spec/moneta/adapters/file/standard_file_spec.rb +7 -0
  120. data/spec/moneta/adapters/file/standard_file_with_expires_spec.rb +11 -0
  121. data/spec/moneta/adapters/fog/adapter_fog_spec.rb +16 -0
  122. data/spec/moneta/adapters/fog/standard_fog_spec.rb +15 -0
  123. data/spec/moneta/adapters/fog/standard_fog_with_expires_spec.rb +20 -0
  124. data/spec/moneta/adapters/gdbm/adapter_gdbm_spec.rb +7 -0
  125. data/spec/moneta/adapters/gdbm/standard_gdbm_spec.rb +7 -0
  126. data/spec/moneta/adapters/gdbm/standard_gdbm_with_expires_spec.rb +11 -0
  127. data/spec/moneta/adapters/hashfile/standard_hashfile_spec.rb +7 -0
  128. data/spec/moneta/adapters/hashfile/standard_hashfile_with_expires_spec.rb +12 -0
  129. data/spec/moneta/adapters/hbase/adapter_hbase_spec.rb +7 -0
  130. data/spec/moneta/adapters/hbase/standard_hbase_spec.rb +4 -0
  131. data/spec/moneta/adapters/hbase/standard_hbase_with_expires_spec.rb +8 -0
  132. data/spec/moneta/adapters/kyotocabinet/adapter_kyotocabinet_spec.rb +7 -0
  133. data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_spec.rb +7 -0
  134. data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_with_expires_spec.rb +14 -0
  135. data/spec/moneta/adapters/leveldb/adapter_leveldb_spec.rb +7 -0
  136. data/spec/moneta/adapters/leveldb/standard_leveldb_spec.rb +7 -0
  137. data/spec/moneta/adapters/leveldb/standard_leveldb_with_expires_spec.rb +11 -0
  138. data/spec/moneta/adapters/lmdb/adapter_lmdb_spec.rb +7 -0
  139. data/spec/moneta/adapters/lmdb/adapter_lmdb_with_db_spec.rb +7 -0
  140. data/spec/moneta/adapters/lmdb/standard_lmdb_spec.rb +7 -0
  141. data/spec/moneta/adapters/lmdb/standard_lmdb_with_expires_spec.rb +11 -0
  142. data/spec/moneta/adapters/localmemcache/adapter_localmemcache_spec.rb +7 -0
  143. data/spec/moneta/adapters/localmemcache/standard_localmemcache_spec.rb +7 -0
  144. data/spec/moneta/adapters/localmemcache/standard_localmemcache_with_expires_spec.rb +11 -0
  145. data/spec/moneta/{adapter_lruhash_spec.rb → adapters/lruhash/adapter_lruhash_spec.rb} +3 -25
  146. data/spec/moneta/adapters/lruhash/standard_lruhash_spec.rb +4 -0
  147. data/spec/moneta/adapters/lruhash/standard_lruhash_with_expires_spec.rb +8 -0
  148. data/spec/moneta/adapters/memcached/adapter_memcached_spec.rb +13 -0
  149. data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_spec.rb +15 -0
  150. data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_with_default_expires_spec.rb +15 -0
  151. data/spec/moneta/adapters/memcached/dalli/standard_memcached_dalli_spec.rb +11 -0
  152. data/spec/moneta/adapters/memcached/helper.rb +20 -0
  153. data/spec/moneta/adapters/memcached/native/adapter_memcached_native_spec.rb +15 -0
  154. data/spec/moneta/adapters/memcached/native/adapter_memcached_native_with_default_expires_spec.rb +15 -0
  155. data/spec/moneta/adapters/memcached/native/standard_memcached_native_spec.rb +11 -0
  156. data/spec/moneta/adapters/memcached/standard_memcached_spec.rb +17 -0
  157. data/spec/moneta/adapters/memory/adapter_memory_spec.rb +7 -0
  158. data/spec/moneta/adapters/memory/standard_memory_spec.rb +4 -0
  159. data/spec/moneta/adapters/memory/standard_memory_with_compress_spec.rb +9 -0
  160. data/spec/moneta/adapters/memory/standard_memory_with_expires_spec.rb +8 -0
  161. data/spec/moneta/adapters/memory/standard_memory_with_json_key_serializer_spec.rb +4 -0
  162. data/spec/moneta/adapters/memory/standard_memory_with_json_serializer_spec.rb +9 -0
  163. data/spec/moneta/adapters/memory/standard_memory_with_json_value_serializer_spec.rb +9 -0
  164. data/spec/moneta/adapters/memory/standard_memory_with_prefix_spec.rb +4 -0
  165. data/spec/moneta/adapters/memory/standard_memory_with_snappy_compress_spec.rb +9 -0
  166. data/spec/moneta/adapters/mongo/adapter_mongo_moped_spec.rb +25 -0
  167. data/spec/moneta/adapters/mongo/adapter_mongo_moped_with_default_expires_spec.rb +12 -0
  168. data/spec/moneta/adapters/mongo/adapter_mongo_official_spec.rb +25 -0
  169. data/spec/moneta/adapters/mongo/adapter_mongo_official_with_default_expires_spec.rb +12 -0
  170. data/spec/moneta/adapters/mongo/adapter_mongo_spec.rb +11 -0
  171. data/spec/moneta/adapters/mongo/adapter_mongo_with_default_expires_spec.rb +12 -0
  172. data/spec/moneta/adapters/mongo/standard_mongo_moped_spec.rb +7 -0
  173. data/spec/moneta/adapters/mongo/standard_mongo_official_spec.rb +7 -0
  174. data/spec/moneta/adapters/mongo/standard_mongo_spec.rb +7 -0
  175. data/spec/moneta/adapters/null/null_adapter_spec.rb +7 -0
  176. data/spec/moneta/adapters/null/standard_null_spec.rb +4 -0
  177. data/spec/moneta/adapters/pstore/adapter_pstore_spec.rb +7 -0
  178. data/spec/moneta/adapters/pstore/standard_pstore_spec.rb +9 -0
  179. data/spec/moneta/adapters/pstore/standard_pstore_with_expires_spec.rb +13 -0
  180. data/spec/moneta/adapters/redis/adapter_redis_spec.rb +10 -0
  181. data/spec/moneta/adapters/redis/adapter_redis_with_default_expires_spec.rb +10 -0
  182. data/spec/moneta/adapters/redis/standard_redis_spec.rb +7 -0
  183. data/spec/moneta/adapters/restclient/adapter_restclient_spec.rb +11 -0
  184. data/spec/moneta/adapters/restclient/standard_restclient_spec.rb +10 -0
  185. data/spec/moneta/adapters/riak/adapter_riak_spec.rb +14 -0
  186. data/spec/moneta/adapters/riak/standard_riak_spec.rb +10 -0
  187. data/spec/moneta/adapters/riak/standard_riak_with_expires_spec.rb +10 -0
  188. data/spec/moneta/adapters/sdbm/adapter_sdbm_spec.rb +7 -0
  189. data/spec/moneta/adapters/sdbm/standard_sdbm_spec.rb +7 -0
  190. data/spec/moneta/adapters/sdbm/standard_sdbm_with_expires_spec.rb +11 -0
  191. data/spec/moneta/adapters/sequel/adapter_sequel_spec.rb +181 -0
  192. data/spec/moneta/adapters/sequel/standard_sequel_spec.rb +14 -0
  193. data/spec/moneta/adapters/sequel/standard_sequel_with_expires_spec.rb +19 -0
  194. data/spec/moneta/adapters/sqlite/adapter_sqlite_spec.rb +7 -0
  195. data/spec/moneta/adapters/sqlite/standard_sqlite_spec.rb +7 -0
  196. data/spec/moneta/adapters/sqlite/standard_sqlite_with_expires_spec.rb +15 -0
  197. data/spec/moneta/adapters/tdb/adapter_tdb_spec.rb +7 -0
  198. data/spec/moneta/adapters/tdb/standard_tdb_spec.rb +7 -0
  199. data/spec/moneta/adapters/tdb/standard_tdb_with_expires_spec.rb +11 -0
  200. data/spec/moneta/adapters/tokyocabinet/adapter_tokyocabinet_bdb_spec.rb +7 -0
  201. data/spec/moneta/adapters/tokyocabinet/adapter_tokyocabinet_hdb_spec.rb +7 -0
  202. data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_spec.rb +7 -0
  203. data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_with_expires_spec.rb +11 -0
  204. data/spec/moneta/adapters/tokyotyrant/adapter_tokyotyrant_spec.rb +7 -0
  205. data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_spec.rb +4 -0
  206. data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_with_expires_spec.rb +8 -0
  207. data/spec/moneta/adapters/yaml/adapter_yaml_spec.rb +7 -0
  208. data/spec/moneta/adapters/yaml/standard_yaml_spec.rb +9 -0
  209. data/spec/moneta/adapters/yaml/standard_yaml_with_expires_spec.rb +13 -0
  210. data/spec/moneta/builder_spec.rb +0 -1
  211. data/spec/moneta/mutex_spec.rb +6 -23
  212. data/spec/moneta/proxies/cache/cache_file_memory_spec.rb +24 -0
  213. data/spec/moneta/proxies/cache/cache_memory_null_spec.rb +12 -0
  214. data/spec/moneta/proxies/expires/expires_file_spec.rb +28 -0
  215. data/spec/moneta/proxies/expires/expires_memory_spec.rb +15 -0
  216. data/spec/moneta/proxies/expires/expires_memory_with_default_expires_spec.rb +16 -0
  217. data/spec/moneta/proxies/lock/lock_spec.rb +10 -0
  218. data/spec/moneta/{optionmerger_spec.rb → proxies/optionmerger/optionmerger_spec.rb} +2 -18
  219. data/spec/moneta/proxies/pool/pool_spec.rb +12 -0
  220. data/spec/moneta/proxies/proxy/proxy_expires_memory_spec.rb +16 -0
  221. data/spec/moneta/proxies/proxy/proxy_redis_spec.rb +13 -0
  222. data/spec/moneta/proxies/shared/shared_tcp_spec.rb +49 -0
  223. data/spec/moneta/proxies/shared/shared_unix_spec.rb +48 -0
  224. data/spec/moneta/proxies/transformer/transformer_bencode_spec.rb +19 -0
  225. data/spec/moneta/proxies/transformer/transformer_bert_spec.rb +19 -0
  226. data/spec/moneta/proxies/transformer/transformer_bson_spec.rb +23 -0
  227. data/spec/moneta/proxies/transformer/transformer_bzip2_spec.rb +19 -0
  228. data/spec/moneta/proxies/transformer/transformer_json_spec.rb +19 -0
  229. data/spec/moneta/proxies/transformer/transformer_key_inspect_spec.rb +17 -0
  230. data/spec/moneta/proxies/transformer/transformer_key_marshal_spec.rb +17 -0
  231. data/spec/moneta/proxies/transformer/transformer_key_to_s_spec.rb +17 -0
  232. data/spec/moneta/proxies/transformer/transformer_key_yaml_spec.rb +17 -0
  233. data/spec/moneta/proxies/transformer/transformer_lz4_spec.rb +19 -0
  234. data/spec/moneta/proxies/transformer/transformer_lzma_spec.rb +19 -0
  235. data/spec/moneta/proxies/transformer/transformer_lzo_spec.rb +19 -0
  236. data/spec/moneta/proxies/transformer/transformer_marshal_base64_spec.rb +20 -0
  237. data/spec/moneta/proxies/transformer/transformer_marshal_city128_spec.rb +15 -0
  238. data/spec/moneta/proxies/transformer/transformer_marshal_city32_spec.rb +15 -0
  239. data/spec/moneta/proxies/transformer/transformer_marshal_city64_spec.rb +15 -0
  240. data/spec/moneta/proxies/transformer/transformer_marshal_escape_spec.rb +15 -0
  241. data/spec/moneta/proxies/transformer/transformer_marshal_hex_spec.rb +20 -0
  242. data/spec/moneta/proxies/transformer/transformer_marshal_hmac_spec.rb +19 -0
  243. data/spec/moneta/proxies/transformer/transformer_marshal_md5_spec.rb +15 -0
  244. data/spec/moneta/proxies/transformer/transformer_marshal_md5_spread_spec.rb +15 -0
  245. data/spec/moneta/proxies/transformer/transformer_marshal_prefix_spec.rb +15 -0
  246. data/spec/moneta/proxies/transformer/transformer_marshal_qp_spec.rb +20 -0
  247. data/spec/moneta/proxies/transformer/transformer_marshal_rmd160_spec.rb +15 -0
  248. data/spec/moneta/proxies/transformer/transformer_marshal_sha1_spec.rb +15 -0
  249. data/spec/moneta/proxies/transformer/transformer_marshal_sha256_spec.rb +15 -0
  250. data/spec/moneta/proxies/transformer/transformer_marshal_sha384_spec.rb +15 -0
  251. data/spec/moneta/proxies/transformer/transformer_marshal_sha512_spec.rb +15 -0
  252. data/spec/moneta/proxies/transformer/transformer_marshal_spec.rb +19 -0
  253. data/spec/moneta/proxies/transformer/transformer_marshal_truncate_spec.rb +15 -0
  254. data/spec/moneta/proxies/transformer/transformer_marshal_urlsafe_base64_spec.rb +20 -0
  255. data/spec/moneta/proxies/transformer/transformer_marshal_uuencode_spec.rb +19 -0
  256. data/spec/moneta/proxies/transformer/transformer_msgpack_spec.rb +19 -0
  257. data/spec/moneta/proxies/transformer/transformer_ox_spec.rb +19 -0
  258. data/spec/moneta/proxies/transformer/transformer_php_spec.rb +19 -0
  259. data/spec/moneta/proxies/transformer/transformer_quicklz_spec.rb +19 -0
  260. data/spec/moneta/proxies/transformer/transformer_snappy_spec.rb +19 -0
  261. data/spec/moneta/proxies/transformer/transformer_tnet_spec.rb +19 -0
  262. data/spec/moneta/proxies/transformer/transformer_value_marshal_spec.rb +19 -0
  263. data/spec/moneta/proxies/transformer/transformer_value_yaml_spec.rb +19 -0
  264. data/spec/moneta/proxies/transformer/transformer_yaml_spec.rb +19 -0
  265. data/spec/moneta/proxies/transformer/transformer_zlib_spec.rb +19 -0
  266. data/spec/moneta/proxies/weak_create/weak_create_spec.rb +21 -0
  267. data/spec/moneta/proxies/weak_each_key/weak_each_key_spec.rb +24 -0
  268. data/spec/moneta/proxies/weak_increment/weak_increment_spec.rb +21 -0
  269. data/spec/moneta/semaphore_spec.rb +6 -23
  270. data/spec/moneta/stack_file_memory_spec.rb +5 -29
  271. data/spec/moneta/stack_memory_file_spec.rb +5 -31
  272. data/spec/quality_spec.rb +1 -1
  273. data/{spec → test}/action_dispatch/fixtures/session_autoload_test/foo.rb +0 -0
  274. data/{spec/action_dispatch/session_moneta_store_spec.rb → test/action_dispatch/session_moneta_store_test.rb} +13 -5
  275. metadata +415 -355
  276. data/script/generate-specs +0 -2696
  277. data/spec/moneta/adapter_activerecord_exisiting_connection_spec.rb +0 -36
  278. data/spec/moneta/adapter_activerecord_spec.rb +0 -65
  279. data/spec/moneta/adapter_cassandra_spec.rb +0 -33
  280. data/spec/moneta/adapter_cassandra_with_default_expires_spec.rb +0 -34
  281. data/spec/moneta/adapter_client_spec.rb +0 -35
  282. data/spec/moneta/adapter_cookie_spec.rb +0 -30
  283. data/spec/moneta/adapter_couch_spec.rb +0 -36
  284. data/spec/moneta/adapter_datamapper_spec.rb +0 -64
  285. data/spec/moneta/adapter_daybreak_spec.rb +0 -31
  286. data/spec/moneta/adapter_dbm_spec.rb +0 -31
  287. data/spec/moneta/adapter_file_spec.rb +0 -34
  288. data/spec/moneta/adapter_fog_spec.rb +0 -37
  289. data/spec/moneta/adapter_gdbm_spec.rb +0 -31
  290. data/spec/moneta/adapter_hbase_spec.rb +0 -33
  291. data/spec/moneta/adapter_kyotocabinet_spec.rb +0 -31
  292. data/spec/moneta/adapter_leveldb_spec.rb +0 -31
  293. data/spec/moneta/adapter_lmdb_spec.rb +0 -32
  294. data/spec/moneta/adapter_lmdb_with_db_spec.rb +0 -32
  295. data/spec/moneta/adapter_localmemcache_spec.rb +0 -32
  296. data/spec/moneta/adapter_memcached_dalli_spec.rb +0 -36
  297. data/spec/moneta/adapter_memcached_dalli_with_default_expires_spec.rb +0 -37
  298. data/spec/moneta/adapter_memcached_native_spec.rb +0 -36
  299. data/spec/moneta/adapter_memcached_native_with_default_expires_spec.rb +0 -37
  300. data/spec/moneta/adapter_memcached_spec.rb +0 -36
  301. data/spec/moneta/adapter_memcached_with_default_expires_spec.rb +0 -37
  302. data/spec/moneta/adapter_memory_spec.rb +0 -184
  303. data/spec/moneta/adapter_mongo_moped_spec.rb +0 -64
  304. data/spec/moneta/adapter_mongo_moped_with_default_expires_spec.rb +0 -53
  305. data/spec/moneta/adapter_mongo_official_spec.rb +0 -64
  306. data/spec/moneta/adapter_mongo_official_with_default_expires_spec.rb +0 -53
  307. data/spec/moneta/adapter_mongo_spec.rb +0 -51
  308. data/spec/moneta/adapter_mongo_with_default_expires_spec.rb +0 -53
  309. data/spec/moneta/adapter_pstore_spec.rb +0 -250
  310. data/spec/moneta/adapter_redis_spec.rb +0 -36
  311. data/spec/moneta/adapter_redis_with_default_expires_spec.rb +0 -37
  312. data/spec/moneta/adapter_restclient_spec.rb +0 -33
  313. data/spec/moneta/adapter_riak_spec.rb +0 -36
  314. data/spec/moneta/adapter_sdbm_spec.rb +0 -30
  315. data/spec/moneta/adapter_sequel_spec.rb +0 -34
  316. data/spec/moneta/adapter_sqlite_spec.rb +0 -32
  317. data/spec/moneta/adapter_tdb_spec.rb +0 -31
  318. data/spec/moneta/adapter_tokyocabinet_bdb_spec.rb +0 -31
  319. data/spec/moneta/adapter_tokyocabinet_hdb_spec.rb +0 -31
  320. data/spec/moneta/adapter_tokyotyrant_spec.rb +0 -34
  321. data/spec/moneta/adapter_yaml_spec.rb +0 -57
  322. data/spec/moneta/cache_file_memory_spec.rb +0 -50
  323. data/spec/moneta/cache_memory_null_spec.rb +0 -35
  324. data/spec/moneta/expires_file_spec.rb +0 -78
  325. data/spec/moneta/expires_memory_spec.rb +0 -189
  326. data/spec/moneta/expires_memory_with_default_expires_spec.rb +0 -190
  327. data/spec/moneta/lock_spec.rb +0 -187
  328. data/spec/moneta/null_adapter_spec.rb +0 -86
  329. data/spec/moneta/pool_spec.rb +0 -38
  330. data/spec/moneta/proxy_expires_memory_spec.rb +0 -191
  331. data/spec/moneta/proxy_redis_spec.rb +0 -40
  332. data/spec/moneta/shared_tcp_spec.rb +0 -46
  333. data/spec/moneta/shared_unix_spec.rb +0 -46
  334. data/spec/moneta/standard_activerecord_spec.rb +0 -253
  335. data/spec/moneta/standard_activerecord_with_expires_spec.rb +0 -255
  336. data/spec/moneta/standard_cassandra_spec.rb +0 -252
  337. data/spec/moneta/standard_client_tcp_spec.rb +0 -269
  338. data/spec/moneta/standard_client_unix_spec.rb +0 -254
  339. data/spec/moneta/standard_couch_spec.rb +0 -252
  340. data/spec/moneta/standard_couch_with_expires_spec.rb +0 -254
  341. data/spec/moneta/standard_datamapper_spec.rb +0 -254
  342. data/spec/moneta/standard_datamapper_with_expires_spec.rb +0 -256
  343. data/spec/moneta/standard_datamapper_with_repository_spec.rb +0 -254
  344. data/spec/moneta/standard_daybreak_spec.rb +0 -250
  345. data/spec/moneta/standard_daybreak_with_expires_spec.rb +0 -252
  346. data/spec/moneta/standard_dbm_spec.rb +0 -250
  347. data/spec/moneta/standard_dbm_with_expires_spec.rb +0 -252
  348. data/spec/moneta/standard_file_spec.rb +0 -253
  349. data/spec/moneta/standard_file_with_expires_spec.rb +0 -255
  350. data/spec/moneta/standard_fog_spec.rb +0 -256
  351. data/spec/moneta/standard_fog_with_expires_spec.rb +0 -258
  352. data/spec/moneta/standard_gdbm_spec.rb +0 -250
  353. data/spec/moneta/standard_gdbm_with_expires_spec.rb +0 -252
  354. data/spec/moneta/standard_hashfile_spec.rb +0 -253
  355. data/spec/moneta/standard_hashfile_with_expires_spec.rb +0 -255
  356. data/spec/moneta/standard_hbase_spec.rb +0 -252
  357. data/spec/moneta/standard_hbase_with_expires_spec.rb +0 -255
  358. data/spec/moneta/standard_kyotocabinet_spec.rb +0 -250
  359. data/spec/moneta/standard_kyotocabinet_with_expires_spec.rb +0 -252
  360. data/spec/moneta/standard_leveldb_spec.rb +0 -250
  361. data/spec/moneta/standard_leveldb_with_expires_spec.rb +0 -252
  362. data/spec/moneta/standard_lmdb_spec.rb +0 -251
  363. data/spec/moneta/standard_lmdb_with_expires_spec.rb +0 -253
  364. data/spec/moneta/standard_localmemcache_spec.rb +0 -251
  365. data/spec/moneta/standard_localmemcache_with_expires_spec.rb +0 -252
  366. data/spec/moneta/standard_lruhash_spec.rb +0 -187
  367. data/spec/moneta/standard_lruhash_with_expires_spec.rb +0 -189
  368. data/spec/moneta/standard_memcached_dalli_spec.rb +0 -255
  369. data/spec/moneta/standard_memcached_native_spec.rb +0 -255
  370. data/spec/moneta/standard_memcached_spec.rb +0 -255
  371. data/spec/moneta/standard_memory_spec.rb +0 -187
  372. data/spec/moneta/standard_memory_with_compress_spec.rb +0 -187
  373. data/spec/moneta/standard_memory_with_expires_spec.rb +0 -189
  374. data/spec/moneta/standard_memory_with_json_key_serializer_spec.rb +0 -86
  375. data/spec/moneta/standard_memory_with_json_serializer_spec.rb +0 -49
  376. data/spec/moneta/standard_memory_with_json_value_serializer_spec.rb +0 -90
  377. data/spec/moneta/standard_memory_with_prefix_spec.rb +0 -187
  378. data/spec/moneta/standard_memory_with_snappy_compress_spec.rb +0 -187
  379. data/spec/moneta/standard_mongo_moped_spec.rb +0 -255
  380. data/spec/moneta/standard_mongo_official_spec.rb +0 -255
  381. data/spec/moneta/standard_mongo_spec.rb +0 -255
  382. data/spec/moneta/standard_null_spec.rb +0 -120
  383. data/spec/moneta/standard_pstore_spec.rb +0 -253
  384. data/spec/moneta/standard_pstore_with_expires_spec.rb +0 -255
  385. data/spec/moneta/standard_redis_spec.rb +0 -255
  386. data/spec/moneta/standard_restclient_spec.rb +0 -252
  387. data/spec/moneta/standard_riak_spec.rb +0 -255
  388. data/spec/moneta/standard_riak_with_expires_spec.rb +0 -256
  389. data/spec/moneta/standard_sdbm_spec.rb +0 -249
  390. data/spec/moneta/standard_sdbm_with_expires_spec.rb +0 -251
  391. data/spec/moneta/standard_sequel_spec.rb +0 -253
  392. data/spec/moneta/standard_sequel_with_expires_spec.rb +0 -255
  393. data/spec/moneta/standard_sqlite_spec.rb +0 -251
  394. data/spec/moneta/standard_sqlite_with_expires_spec.rb +0 -253
  395. data/spec/moneta/standard_tdb_spec.rb +0 -250
  396. data/spec/moneta/standard_tdb_with_expires_spec.rb +0 -252
  397. data/spec/moneta/standard_tokyocabinet_spec.rb +0 -250
  398. data/spec/moneta/standard_tokyocabinet_with_expires_spec.rb +0 -252
  399. data/spec/moneta/standard_tokyotyrant_spec.rb +0 -253
  400. data/spec/moneta/standard_tokyotyrant_with_expires_spec.rb +0 -255
  401. data/spec/moneta/standard_yaml_spec.rb +0 -250
  402. data/spec/moneta/standard_yaml_with_expires_spec.rb +0 -252
  403. data/spec/moneta/transformer_bencode_spec.rb +0 -55
  404. data/spec/moneta/transformer_bert_spec.rb +0 -55
  405. data/spec/moneta/transformer_bson_spec.rb +0 -60
  406. data/spec/moneta/transformer_bzip2_spec.rb +0 -55
  407. data/spec/moneta/transformer_json_spec.rb +0 -55
  408. data/spec/moneta/transformer_key_inspect_spec.rb +0 -91
  409. data/spec/moneta/transformer_key_marshal_spec.rb +0 -191
  410. data/spec/moneta/transformer_key_to_s_spec.rb +0 -91
  411. data/spec/moneta/transformer_key_yaml_spec.rb +0 -191
  412. data/spec/moneta/transformer_lz4_spec.rb +0 -55
  413. data/spec/moneta/transformer_lzma_spec.rb +0 -55
  414. data/spec/moneta/transformer_lzo_spec.rb +0 -55
  415. data/spec/moneta/transformer_marshal_base64_spec.rb +0 -194
  416. data/spec/moneta/transformer_marshal_city128_spec.rb +0 -194
  417. data/spec/moneta/transformer_marshal_city32_spec.rb +0 -194
  418. data/spec/moneta/transformer_marshal_city64_spec.rb +0 -194
  419. data/spec/moneta/transformer_marshal_escape_spec.rb +0 -194
  420. data/spec/moneta/transformer_marshal_hex_spec.rb +0 -194
  421. data/spec/moneta/transformer_marshal_hmac_spec.rb +0 -194
  422. data/spec/moneta/transformer_marshal_md5_spec.rb +0 -194
  423. data/spec/moneta/transformer_marshal_md5_spread_spec.rb +0 -194
  424. data/spec/moneta/transformer_marshal_prefix_spec.rb +0 -194
  425. data/spec/moneta/transformer_marshal_qp_spec.rb +0 -194
  426. data/spec/moneta/transformer_marshal_rmd160_spec.rb +0 -194
  427. data/spec/moneta/transformer_marshal_sha1_spec.rb +0 -194
  428. data/spec/moneta/transformer_marshal_sha256_spec.rb +0 -194
  429. data/spec/moneta/transformer_marshal_sha384_spec.rb +0 -194
  430. data/spec/moneta/transformer_marshal_sha512_spec.rb +0 -194
  431. data/spec/moneta/transformer_marshal_spec.rb +0 -191
  432. data/spec/moneta/transformer_marshal_truncate_spec.rb +0 -194
  433. data/spec/moneta/transformer_marshal_uuencode_spec.rb +0 -194
  434. data/spec/moneta/transformer_msgpack_spec.rb +0 -55
  435. data/spec/moneta/transformer_ox_spec.rb +0 -191
  436. data/spec/moneta/transformer_php_spec.rb +0 -55
  437. data/spec/moneta/transformer_quicklz_spec.rb +0 -55
  438. data/spec/moneta/transformer_snappy_spec.rb +0 -55
  439. data/spec/moneta/transformer_tnet_spec.rb +0 -55
  440. data/spec/moneta/transformer_value_marshal_spec.rb +0 -191
  441. data/spec/moneta/transformer_value_yaml_spec.rb +0 -191
  442. data/spec/moneta/transformer_yaml_spec.rb +0 -191
  443. data/spec/moneta/transformer_zlib_spec.rb +0 -55
  444. data/spec/moneta/weak_create_spec.rb +0 -41
  445. data/spec/moneta/weak_increment_spec.rb +0 -41
  446. data/spec/monetaspecs.rb +0 -51267
@@ -8,30 +8,36 @@ module Moneta
8
8
  include Defaults
9
9
  include IncrementSupport
10
10
 
11
- supports :create
11
+ supports :create, :each_key
12
12
  attr_reader :backend
13
13
 
14
14
  # @param [Hash] options
15
15
  # @option options [String] :file Database file
16
16
  # @option options [String] :table ('moneta') Table name
17
- # @option options [Fixnum] :busy_timeout (1000) Sqlite timeout if database is busy
17
+ # @option options [Integer] :busy_timeout (1000) Sqlite timeout if database is busy
18
18
  # @option options [::Sqlite3::Database] :backend Use existing backend instance
19
+ # @option options [String, Symbol] :journal_mode Set the journal mode for the connection
19
20
  def initialize(options = {})
20
- table = options[:table] || 'moneta'
21
+ @table = options[:table] || 'moneta'
21
22
  @backend = options[:backend] ||
22
23
  begin
23
24
  raise ArgumentError, 'Option :file is required' unless options[:file]
24
25
  ::SQLite3::Database.new(options[:file])
25
26
  end
26
27
  @backend.busy_timeout(options[:busy_timeout] || 1000)
27
- @backend.execute("create table if not exists #{table} (k blob not null primary key, v blob)")
28
+ @backend.execute("create table if not exists #{@table} (k blob not null primary key, v blob)")
29
+ if journal_mode = options[:journal_mode]
30
+ @backend.journal_mode = journal_mode.to_s
31
+ end
28
32
  @stmts =
29
- [@exists = @backend.prepare("select exists(select 1 from #{table} where k = ?)"),
30
- @select = @backend.prepare("select v from #{table} where k = ?"),
31
- @replace = @backend.prepare("replace into #{table} values (?, ?)"),
32
- @delete = @backend.prepare("delete from #{table} where k = ?"),
33
- @clear = @backend.prepare("delete from #{table}"),
34
- @create = @backend.prepare("insert into #{table} values (?, ?)")]
33
+ [@exists = @backend.prepare("select exists(select 1 from #{@table} where k = ?)"),
34
+ @select = @backend.prepare("select v from #{@table} where k = ?"),
35
+ @replace = @backend.prepare("replace into #{@table} values (?, ?)"),
36
+ @delete = @backend.prepare("delete from #{@table} where k = ?"),
37
+ @clear = @backend.prepare("delete from #{@table}"),
38
+ @create = @backend.prepare("insert into #{@table} values (?, ?)"),
39
+ @keys = @backend.prepare("select k from #{@table}"),
40
+ @count = @backend.prepare("select count(*) from #{@table}")]
35
41
  end
36
42
 
37
43
  # (see Proxy#key?)
@@ -86,6 +92,64 @@ module Moneta
86
92
  @backend.close
87
93
  nil
88
94
  end
95
+
96
+ # (see Proxy#slice)
97
+ def slice(*keys, **options)
98
+ query = "select k, v from #{@table} where k in (#{(['?'] * keys.length).join(',')})"
99
+ @backend.execute(query, keys)
100
+ end
101
+
102
+ # (see Proxy#values_at)
103
+ def values_at(*keys, **options)
104
+ hash = Hash[slice(*keys, **options)]
105
+ keys.map { |key| hash[key] }
106
+ end
107
+
108
+ # (see Proxy#fetch_values)
109
+ def fetch_values(*keys, **options)
110
+ return values_at(*keys, **options) unless block_given?
111
+ hash = Hash[slice(*keys, **options)]
112
+ keys.map do |key|
113
+ if hash.key?(key)
114
+ hash[key]
115
+ else
116
+ yield key
117
+ end
118
+ end
119
+ end
120
+
121
+ # (see Proxy#merge!)
122
+ def merge!(pairs, options = {})
123
+ transaction = if block_given?; @backend.transaction end
124
+
125
+ if block_given?
126
+ existing = Hash[slice(*pairs.map { |k, _| k }.to_a)]
127
+ pairs = pairs.map do |key, new_value|
128
+ new_value = yield(key, existing[key], new_value) if existing.key?(key)
129
+ [key, new_value]
130
+ end.to_a
131
+ else
132
+ pairs = pairs.to_a
133
+ end
134
+
135
+ query = "replace into #{@table} (k, v) values" + (['(?, ?)'] * pairs.length).join(',')
136
+ @backend.query(query, pairs.flatten).close
137
+ rescue
138
+ @backend.rollback if transaction
139
+ raise
140
+ else
141
+ @backend.commit if transaction
142
+ self
143
+ end
144
+
145
+ # (see Proxy#each_key)
146
+ def each_key
147
+ return enum_for(:each_key) { @count.execute!.first.first } unless block_given?
148
+ @keys.execute!.each do |row|
149
+ yield row.first
150
+ end
151
+ self
152
+ end
89
153
  end
90
154
  end
91
155
  end
@@ -4,7 +4,14 @@ module Moneta
4
4
  module Adapters
5
5
  # TDB backend
6
6
  # @api public
7
- class TDB < Memory
7
+ class TDB
8
+ include Defaults
9
+ include HashAdapter
10
+ include IncrementSupport
11
+ include EachKeySupport
12
+
13
+ supports :create
14
+
8
15
  # @param [Hash] options
9
16
  # @option options [String] :file Database file
10
17
  # @option options [::TDB] :backend Use existing backend instance
@@ -21,6 +28,14 @@ module Moneta
21
28
  @backend.close
22
29
  nil
23
30
  end
31
+
32
+ # (see Proxy#create)
33
+ def create(key, value, options = {})
34
+ @backend.insert!(key, value)
35
+ true
36
+ rescue ::TDB::ERR::EXISTS
37
+ false
38
+ end
24
39
  end
25
40
  end
26
41
  end
@@ -4,7 +4,13 @@ module Moneta
4
4
  module Adapters
5
5
  # TokyoCabinet backend
6
6
  # @api public
7
- class TokyoCabinet < Memory
7
+ class TokyoCabinet
8
+ include Defaults
9
+ include HashAdapter
10
+ include IncrementSupport
11
+ include CreateSupport
12
+ include EachKeySupport
13
+
8
14
  # @param [Hash] options
9
15
  # @option options [String] :file Database file
10
16
  # @option options [Symbol] :type (:hdb) Database type (:bdb and :hdb possible)
@@ -92,6 +92,28 @@ module Moneta
92
92
  nil
93
93
  end
94
94
 
95
+ # (see Proxy#slice)
96
+ def slice(*keys, **options)
97
+ hash =
98
+ if @native
99
+ @backend.mget(*keys)
100
+ else
101
+ hash = Hash[keys.map { |key| [key] }]
102
+ raise unless @backend.mget(hash) >= 0
103
+ hash
104
+ end
105
+
106
+ hash.each do |key, value|
107
+ hash[key] = unpack(value)
108
+ end
109
+ end
110
+
111
+ # (see Proxy#values_at)
112
+ def values_at(*keys, **options)
113
+ hash = slice(*keys, **options)
114
+ keys.map { |key| hash[key] }
115
+ end
116
+
95
117
  private
96
118
 
97
119
  def pack(value)
@@ -101,9 +101,19 @@ module Moneta
101
101
  @adapter.close
102
102
  end
103
103
 
104
+ # (see Proxy#each_key)
105
+ def each_key(&block)
106
+ raise NotImplementedError, 'adapter doesn\'t support #each_key' \
107
+ unless supports? :each_key
108
+
109
+ return enum_for(:each_key) unless block_given?
110
+ @adapter.each_key(&block)
111
+ self
112
+ end
113
+
104
114
  # (see Proxy#features)
105
115
  def features
106
- @features ||= ((@cache.features + [:create, :increment]) & @adapter.features).freeze
116
+ @features ||= ((@cache.features + [:create, :increment, :each_key]) & @adapter.features).freeze
107
117
  end
108
118
  end
109
119
  end
@@ -44,7 +44,7 @@ module Moneta
44
44
  def delete(key, options = {})
45
45
  return super if options.include?(:raw)
46
46
  value, expires = super
47
- value if !expires || Time.now.to_i <= expires
47
+ value if !expires || Time.now <= Time.at(expires)
48
48
  end
49
49
 
50
50
  # (see Proxy#store)
@@ -54,34 +54,136 @@ module Moneta
54
54
  @adapter.create(key, new_entry(value, expires), Utils.without(options, :expires))
55
55
  end
56
56
 
57
+ # (see Defaults#values_at)
58
+ def values_at(*keys, **options)
59
+ return super if options.include?(:raw)
60
+ new_expires = expires_at(options, nil)
61
+ options = Utils.without(options, :expires)
62
+ with_updates(options) do |updates|
63
+ keys.zip(@adapter.values_at(*keys, **options)).map do |key, entry|
64
+ entry = invalidate_entry(key, entry, new_expires) do |new_entry|
65
+ updates[key] = new_entry
66
+ end
67
+ next if entry.nil?
68
+ value, _ = entry
69
+ value
70
+ end
71
+ end
72
+ end
73
+
74
+ # (see Defaults#fetch_values)
75
+ def fetch_values(*keys, **options)
76
+ return super if options.include?(:raw)
77
+ new_expires = expires_at(options, nil)
78
+ options = Utils.without(options, :expires)
79
+ substituted = {}
80
+ block = if block_given?
81
+ lambda do |key|
82
+ substituted[key] = true
83
+ yield key
84
+ end
85
+ end
86
+
87
+ with_updates(options) do |updates|
88
+ keys.zip(@adapter.fetch_values(*keys, **options, &block)).map do |key, entry|
89
+ next entry if substituted[key]
90
+ entry = invalidate_entry(key, entry, new_expires) do |new_entry|
91
+ updates[key] = new_entry
92
+ end
93
+ if entry.nil?
94
+ value = if block_given?
95
+ yield key
96
+ end
97
+ else
98
+ value, _ = entry
99
+ end
100
+ value
101
+ end
102
+ end
103
+ end
104
+
105
+ # (see Defaults#slice)
106
+ def slice(*keys, **options)
107
+ return super if options.include?(:raw)
108
+ new_expires = expires_at(options, nil)
109
+ options = Utils.without(options, :expires)
110
+
111
+ with_updates(options) do |updates|
112
+ @adapter.slice(*keys, **options).map do |key, entry|
113
+ entry = invalidate_entry(key, entry, new_expires) do |new_entry|
114
+ updates[key] = new_entry
115
+ end
116
+ next if entry.nil?
117
+ value, _ = entry
118
+ [key, value]
119
+ end.reject(&:nil?)
120
+ end
121
+ end
122
+
123
+ # (see Defaults#merge!)
124
+ def merge!(pairs, options={})
125
+ expires = expires_at(options)
126
+ options = Utils.without(options, :expires)
127
+
128
+ block = if block_given?
129
+ lambda do |key, old_entry, entry|
130
+ old_entry = invalidate_entry(key, old_entry)
131
+ if old_entry.nil?
132
+ entry # behave as if no replace is happening
133
+ else
134
+ old_value, _ = old_entry
135
+ new_value, _ = entry
136
+ new_entry(yield(key, old_value, new_value), expires)
137
+ end
138
+ end
139
+ end
140
+
141
+ entry_pairs = pairs.map do |key, value|
142
+ [key, new_entry(value, expires)]
143
+ end
144
+ @adapter.merge!(entry_pairs, options, &block)
145
+ self
146
+ end
147
+
57
148
  private
58
149
 
59
150
  def load_entry(key, options)
60
151
  new_expires = expires_at(options, nil)
61
152
  options = Utils.without(options, :expires)
62
153
  entry = @adapter.load(key, options)
154
+ invalidate_entry(key, entry, new_expires) do |new_entry|
155
+ @adapter.store(key, new_entry, options)
156
+ end
157
+ end
158
+
159
+ def invalidate_entry(key, entry, new_expires = nil)
63
160
  if entry != nil
64
161
  value, expires = entry
65
- if expires && Time.now.to_i > expires
162
+ if expires && Time.now > Time.at(expires)
66
163
  delete(key)
67
- nil
164
+ entry = nil
68
165
  elsif new_expires != nil
69
- @adapter.store(key, new_entry(value, new_expires), options)
70
- entry
71
- else
72
- entry
166
+ yield new_entry(value, new_expires) if block_given?
73
167
  end
74
168
  end
169
+ entry
75
170
  end
76
171
 
77
172
  def new_entry(value, expires)
78
173
  if expires
79
- [value, expires.to_i]
174
+ [value, expires.to_r]
80
175
  elsif Array === value || value == nil
81
176
  [value]
82
177
  else
83
178
  value
84
179
  end
85
180
  end
181
+
182
+ def with_updates(options)
183
+ updates = {}
184
+ yield(updates).tap do
185
+ @adapter.merge!(updates, options) unless updates.empty?
186
+ end
187
+ end
86
188
  end
87
189
  end
@@ -1,4 +1,4 @@
1
- require 'thread'
1
+ require 'set'
2
2
 
3
3
  module Moneta
4
4
  # Locks the underlying stores with a Mutex
@@ -14,8 +14,27 @@ module Moneta
14
14
 
15
15
  protected
16
16
 
17
- def wrap(*args, &block)
17
+ def wrap(name, *args, &block)
18
+ if locked?
19
+ block.call
20
+ else
21
+ lock!(&block)
22
+ end
23
+ end
24
+
25
+ def locks
26
+ Thread.current['Moneta::Lock'] ||= Set.new
27
+ end
28
+
29
+ def lock!(&block)
30
+ locks << @lock
18
31
  @lock.synchronize(&block)
32
+ ensure
33
+ locks.delete @lock
34
+ end
35
+
36
+ def locked?
37
+ locks.include? @lock
19
38
  end
20
39
  end
21
40
  end
@@ -74,7 +74,26 @@ module Moneta
74
74
  # end
75
75
  # end
76
76
  def supports(*features)
77
- @features = (self.features + features).uniq.freeze
77
+ @features = (self.features | features).freeze
78
+ end
79
+
80
+ # Declares that this adapter does not support the given feature, and adds
81
+ # a stub method that raises a NotImplementedError. Useful when inheriting
82
+ # from another adapter.
83
+ #
84
+ # @example
85
+ # class MyAdapter < OtherAdapterWithCreate
86
+ # include Moneta::Defaults
87
+ # not_supports :create
88
+ # end
89
+ def not_supports(*features)
90
+ features.each do |feature|
91
+ define_method(feature) do
92
+ raise ::NotImplementedError, "#{feature} not supported"
93
+ end
94
+ end
95
+
96
+ @features = (self.features - features).freeze
78
97
  end
79
98
  end
80
99
 
@@ -188,6 +207,24 @@ module Moneta
188
207
  store(key, value)
189
208
  end
190
209
 
210
+ # Calls block once for each key in store, passing the key as a parameter. If
211
+ # no block is given, an enumerator is returned instead.
212
+ #
213
+ # @note Not every Moneta store implements this method,
214
+ # a NotImplementedError is raised if it is not supported.
215
+ #
216
+ # @overload each_key
217
+ # @return [Enumerator] An all-the-keys enumerator
218
+ #
219
+ # @overload each_key
220
+ # @yieldparam key [Object] Each key is yielded to the supplied block
221
+ # @return [self]
222
+ #
223
+ # @api public
224
+ def each_key
225
+ raise NotImplementedError, 'each_key is not supported'
226
+ end
227
+
191
228
  # Atomically sets a key to value if it's not set.
192
229
  #
193
230
  # @note Not every Moneta store implements this method,
@@ -204,6 +241,119 @@ module Moneta
204
241
  raise NotImplementedError, 'create is not supported'
205
242
  end
206
243
 
244
+ # Returns an array containing the values associated with the given keys, in
245
+ # the same order as the supplied keys. If a key is not present in the
246
+ # key-value-store, nil is returned in its place.
247
+ #
248
+ # @note Some adapters may implement this method atomically, but the default
249
+ # implementation simply makes repeated calls to {#load}.
250
+ #
251
+ # @param keys [<Object>] The keys for the values to fetch
252
+ # @param options [Hash]
253
+ # @option options (see Proxy#load)
254
+ # @return [Array<Object, nil>] Array containing the values requested, with
255
+ # nil for missing values
256
+ # @api public
257
+ def values_at(*keys, **options)
258
+ keys.map { |key| load(key, options) }
259
+ end
260
+
261
+ # Behaves identically to {#values_at} except that it accepts an optional
262
+ # block. When supplied, the block will be called successively with each
263
+ # supplied key that is not present in the store. The return value of the
264
+ # block call will be used in place of nil in returned the array of values.
265
+ #
266
+ # @note Some adapters may implement this method atomically. The default
267
+ # implmentation uses {#values_at}.
268
+ #
269
+ # @overload fetch_values(*keys, **options)
270
+ # @param (see #values_at)
271
+ # @option options (see #values_at)
272
+ # @return (see #values_at)
273
+ # @overload fetch_values(*keys, **options)
274
+ # @param (see #values_at)
275
+ # @option options (see #values_at)
276
+ # @yieldparam key [Object] Each key that is not found in the store
277
+ # @yieldreturn [Object, nil] The value to substitute for the missing one
278
+ # @return [Array<Object, nil>] Array containing the values requested, or
279
+ # where keys are missing, the return values from the corresponding block
280
+ # calls
281
+ # @api public
282
+ def fetch_values(*keys, **options)
283
+ values = values_at(*keys, **options)
284
+ return values unless block_given?
285
+ keys.zip(values).map do |key, value|
286
+ if value == nil
287
+ yield key
288
+ else
289
+ value
290
+ end
291
+ end
292
+ end
293
+
294
+ # Returns a collection of key-value pairs corresponding to those supplied
295
+ # keys which are present in the key-value store, and their associated
296
+ # values. Only those keys present in the store will have pairs in the
297
+ # return value. The return value can be any enumerable object that yields
298
+ # pairs, so it could be a hash, but needn't be.
299
+ #
300
+ # @note The keys in the return value may be the same objects that were
301
+ # supplied (i.e. {Object#equal?}), or may simply be equal (i.e.
302
+ # {Object#==}).
303
+ #
304
+ # @note Some adapters may implement this method atomically. The default
305
+ # implmentation uses {#values_at}.
306
+ #
307
+ # @param (see #values_at)
308
+ # @option options (see #values_at)
309
+ # @return [<(Object, Object)>] A collection of key-value pairs
310
+ # @api public
311
+ def slice(*keys, **options)
312
+ keys.zip(values_at(*keys, **options)).reject do |_, value|
313
+ value == nil
314
+ end
315
+ end
316
+
317
+ # Stores the pairs in the key-value store, and returns itself. When a block
318
+ # is provided, it will be called before overwriting any existing values with
319
+ # the key, old value and supplied value, and the return value of the block
320
+ # will be used in place of the supplied value.
321
+ #
322
+ # @note Some adapters may implement this method atomically, or in two passes
323
+ # when a block is provided. The default implmentation uses {#key?},
324
+ # {#load} and {#store}.
325
+ #
326
+ # @overload merge!(pairs, options={})
327
+ # @param [<(Object, Object)>] pairs A collection of key-value pairs to store
328
+ # @param [Hash] options
329
+ # @option options (see Proxy#store)
330
+ # @return [self]
331
+ # @overload merge!(pairs, options={})
332
+ # @param [<(Object, Object)>] pairs A collection of key-value pairs to store
333
+ # @param [Hash] options
334
+ # @option options (see Proxy#store)
335
+ # @yieldparam key [Object] A key that whose value is being overwritten
336
+ # @yieldparam old_value [Object] The existing value which is being overwritten
337
+ # @yieldparam new_value [Object] The value supplied in the method call
338
+ # @yieldreturn [Object] The value to use for overwriting
339
+ # @return [self]
340
+ # @api public
341
+ def merge!(pairs, options={})
342
+ pairs.each do |key, value|
343
+ if block_given?
344
+ existing = load(key, options)
345
+ value = yield(key, existing, value) unless existing == nil
346
+ end
347
+ store(key, value, options)
348
+ end
349
+ self
350
+ end
351
+
352
+ # (see #merge!)
353
+ def update(pairs, options = {}, &block)
354
+ merge!(pairs, options, &block)
355
+ end
356
+
207
357
  # Returns features list
208
358
  #
209
359
  # @return [Array<Symbol>] list of features
@@ -219,6 +369,40 @@ module Moneta
219
369
  end
220
370
  end
221
371
 
372
+ # This contains overrides of methods in Defaults where additional nil
373
+ # checks are required, because nil values are possible in the store.
374
+ # @api private
375
+ module NilValues
376
+ def fetch_values(*keys, **options)
377
+ values = values_at(*keys, **options)
378
+ return values unless block_given?
379
+ keys.zip(values).map do |key, value|
380
+ if value == nil && !key?(key)
381
+ yield key
382
+ else
383
+ value
384
+ end
385
+ end
386
+ end
387
+
388
+ def slice(*keys, **options)
389
+ keys.zip(values_at(*keys, **options)).reject do |key, value|
390
+ value == nil && !key?(key)
391
+ end
392
+ end
393
+
394
+ def merge!(pairs, options={})
395
+ pairs.each do |key, value|
396
+ if block_given? && key?(key, options)
397
+ existing = load(key, options)
398
+ value = yield(key, existing, value)
399
+ end
400
+ store(key, value, options)
401
+ end
402
+ self
403
+ end
404
+ end
405
+
222
406
  # @api private
223
407
  module IncrementSupport
224
408
  # (see Defaults#increment)
@@ -233,6 +417,32 @@ module Moneta
233
417
  end
234
418
  end
235
419
 
420
+ # This provides an each_key implementation that works in most cases.
421
+ # @api private
422
+ module EachKeySupport
423
+ def each_key
424
+ return enum_for(:each_key) unless block_given?
425
+
426
+ if @backend.respond_to?(:each_key)
427
+ @backend.each_key { |key| yield key }
428
+ elsif @backend.respond_to?(:keys)
429
+ if keys = @backend.keys
430
+ keys.each { |key| yield key }
431
+ end
432
+ elsif @backend.respond_to?(:each)
433
+ @backend.each { |key, _| yield key }
434
+ else
435
+ raise ::NotImplementedError, "No enumerable found on backend"
436
+ end
437
+
438
+ self
439
+ end
440
+
441
+ def self.included(base)
442
+ base.supports(:each_key) if base.respond_to?(:supports)
443
+ end
444
+ end
445
+
236
446
  # Implements simple create using key? and store.
237
447
  #
238
448
  # This is sufficient for non-shared stores or if atomicity is not required.
@@ -282,6 +492,72 @@ module Moneta
282
492
  @backend.clear
283
493
  self
284
494
  end
495
+
496
+ # (see Defaults#values_at)
497
+ def values_at(*keys, **options)
498
+ return super unless @backend.respond_to? :values_at
499
+ @backend.values_at(*keys)
500
+ end
501
+
502
+ # (see Defaults#fetch_values)
503
+ def fetch_values(*keys, **options, &defaults)
504
+ return super unless @backend.respond_to? :fetch_values
505
+ defaults ||= {} # prevents KeyError
506
+ @backend.fetch_values(*keys, &defaults)
507
+ end
508
+
509
+ # (see Defaults#slice)
510
+ def slice(*keys, **options)
511
+ return super unless @backend.respond_to? :slice
512
+ @backend.slice(*keys)
513
+ end
514
+
515
+ # (see Defaults#merge!)
516
+ def merge!(pairs, options={}, &block)
517
+ return super unless method = [:merge!, :update].find do |method|
518
+ @backend.respond_to? method
519
+ end
520
+
521
+ hash = Hash === pairs ? pairs : Hash[pairs.to_a]
522
+ case method
523
+ when :merge!
524
+ @backend.merge!(hash, &block)
525
+ when :update
526
+ @backend.update(hash, &block)
527
+ end
528
+
529
+ self
530
+ end
531
+ end
532
+
533
+ # This is for adapters that conform to the DBM interface
534
+ # @api private
535
+ module DBMAdapter
536
+ include HashAdapter
537
+
538
+ # (see Proxy#close)
539
+ def close
540
+ @backend.close
541
+ nil
542
+ end
543
+
544
+ # (see Proxy#merge!)
545
+ def merge!(pairs, options = {})
546
+ hash =
547
+ if block_given?
548
+ keys = pairs.map { |k, _| k }
549
+ old_pairs = Hash[slice(*keys)]
550
+ Hash[pairs.map do |key, new_value|
551
+ new_value = yield(key, old_pairs[key], new_value) if old_pairs.key?(key)
552
+ [key, new_value]
553
+ end.to_a]
554
+ else
555
+ Hash === pairs ? pairs : Hash[pairs.to_a]
556
+ end
557
+
558
+ @backend.update(hash)
559
+ self
560
+ end
285
561
  end
286
562
 
287
563
  # This mixin handles the calculation of expiration times.
@@ -326,9 +602,9 @@ module Moneta
326
602
  when 0, false
327
603
  false
328
604
  when nil
329
- default ? default.to_i : nil
605
+ default ? default.to_r : nil
330
606
  when Numeric
331
- value = value.to_i
607
+ value = value.to_r
332
608
  raise ArgumentError, ":expires must be a positive value, got #{value}" if value < 0
333
609
  value
334
610
  else