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
@@ -0,0 +1,133 @@
1
+ module Moneta
2
+ module Adapters
3
+ # ActiveSupport::Cache::Store adapter
4
+ # @api public
5
+ class ActiveSupportCache
6
+ include Defaults
7
+ include ExpiresSupport
8
+
9
+ supports :increment
10
+
11
+ # @param [Hash] options
12
+ # @option options [Numeric] :expires default expiration in seconds
13
+ def initialize(options = {})
14
+ self.default_expires = options.delete(:expires)
15
+ @backend =
16
+ if options[:backend]
17
+ options[:backend]
18
+ elsif defined?(Rails)
19
+ Rails.cache
20
+ else
21
+ raise ArgumentError, 'Option :backend is required'
22
+ end
23
+ end
24
+
25
+ # (see Proxy#key?)
26
+ def key?(key, options = {})
27
+ @backend.exist?(key).tap do |exists|
28
+ if exists && !(expires = expires_value(options, nil)).nil?
29
+ value = @backend.read(key, options)
30
+ @backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
31
+ end
32
+ end
33
+ end
34
+
35
+ # (see Proxy#load)
36
+ def load(key, options = {})
37
+ expires = expires_value(options, nil)
38
+ value = @backend.read(key, options)
39
+ if value and !expires.nil?
40
+ @backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
41
+ end
42
+ if options[:raw]
43
+ value && value.to_s
44
+ else
45
+ value
46
+ end
47
+ end
48
+
49
+ # (see Proxy#store)
50
+ def store(key, value, options = {})
51
+ expires = expires_value(options)
52
+ @backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
53
+ value
54
+ end
55
+
56
+ # (see Proxy#increment)
57
+ def increment(key, amount = 1, options = {})
58
+ expires = expires_value(options)
59
+ options.delete(:raw)
60
+ existing = Integer(@backend.fetch(key, options.merge(raw: true)) { 0 })
61
+ if amount > 0
62
+ @backend.increment(key, amount, options.merge(expires_in: expires ? expires.seconds : nil))
63
+ elsif amount < 0
64
+ @backend.decrement(key, -amount, options.merge(expires_in: expires ? expires.seconds : nil))
65
+ else
66
+ existing
67
+ end
68
+ end
69
+
70
+ # (see Proxy#delete)
71
+ def delete(key, options = {})
72
+ value = @backend.read(key, options)
73
+ if value != nil
74
+ @backend.delete(key, options)
75
+ options[:raw] ? value.to_s : value
76
+ end
77
+ end
78
+
79
+ # (see Proxy#clear)
80
+ def clear(options = {})
81
+ @backend.clear
82
+ self
83
+ end
84
+
85
+ # (see Proxy#slice)
86
+ def slice(*keys, **options)
87
+ hash = @backend.read_multi(*keys)
88
+ if (expires = expires_value(options, nil)) != nil
89
+ hash.each do |key, value|
90
+ @backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
91
+ end
92
+ end
93
+ if options[:raw]
94
+ hash.each do |key, value|
95
+ hash[key] = value.to_s if value != nil
96
+ end
97
+ end
98
+ hash
99
+ end
100
+
101
+ # (see Proxy#values_at)
102
+ def values_at(*keys, **options)
103
+ hash = slice(*keys, **options)
104
+ keys.map { |key| hash[key] }
105
+ end
106
+
107
+ # (see Proxy#merge!)
108
+ def merge!(pairs, options = {})
109
+ if block_given?
110
+ existing = slice(*pairs.map { |k, _| k }, **options)
111
+ pairs = pairs.map do |key, new_value|
112
+ if existing.key?(key)
113
+ new_value = yield(key, existing[key], new_value)
114
+ end
115
+
116
+ [key, new_value]
117
+ end
118
+ end
119
+
120
+ hash = Hash === pairs ? pairs : Hash[pairs.to_a]
121
+ expires = expires_value(options)
122
+ @backend.write_multi(hash, options.merge(expires_in: expires ? expires.seconds : nil))
123
+ self
124
+ end
125
+
126
+ private
127
+
128
+ def expires_value(options, default = @default_expires)
129
+ super.tap { options.delete(:expires) unless options.frozen? }
130
+ end
131
+ end
132
+ end
133
+ end
@@ -4,61 +4,92 @@ module Moneta
4
4
  module Adapters
5
5
  # Cassandra backend
6
6
  # @api public
7
- # @author Potapov Sergey (aka Blake)
8
7
  class Cassandra
9
8
  include Defaults
10
9
  include ExpiresSupport
11
10
 
12
11
  attr_reader :backend
13
12
 
13
+ supports :each_key
14
+
14
15
  # @param [Hash] options
15
16
  # @option options [String] :keyspace ('moneta') Cassandra keyspace
16
- # @option options [String] :column_family ('moneta') Cassandra column family
17
+ # @option options [String] :table ('moneta') Cassandra table
17
18
  # @option options [String] :host ('127.0.0.1') Server host name
18
19
  # @option options [Integer] :port (9160) Server port
19
20
  # @option options [Integer] :expires Default expiration time
20
- # @option options [::Cassandra] :backend Use existing backend instance
21
+ # @option options [String] :key_column ('key') Name of the key column
22
+ # @option options [String] :value_column ('value') Name of the value
23
+ # column
24
+ # @option options [String] :updated_column ('updated_at') Name of the
25
+ # column used to track last update
26
+ # @option options [String] :expired_column ('expired') Name of the column
27
+ # used to track expiry
28
+ # @option options [Symbol] :read_consistency (:all) Default read
29
+ # consistency
30
+ # @option options [Symbol] :write_consistency (:all) Default write
31
+ # consistency
32
+ # @option options [Proc, Boolean, Hash] :create_keyspace Provide a proc
33
+ # for creating the keyspace, or a Hash of options to use when creating
34
+ # it, or set to false to disable. The Proc will only be called if the
35
+ # keyspace does not already exist.
36
+ # @option options [::Cassandra::Cluster] :cluster Existing cluster to use
37
+ # @option options [::Cassandra::Session] :backend Existing session to use
21
38
  # @option options Other options passed to `Cassandra#new`
22
39
  def initialize(options = {})
23
40
  self.default_expires = options.delete(:expires)
24
- @cf = (options.delete(:column_family) || 'moneta').to_sym
25
- if options[:backend]
26
- @backend = options[:backend]
27
- else
28
- keyspace = options.delete(:keyspace) || 'moneta'
29
- options[:retries] ||= 3
30
- options[:connect_timeout] ||= 10
31
- options[:timeout] ||= 10
32
- @backend = ::Cassandra.new('system',
33
- "#{options[:host] || '127.0.0.1'}:#{options[:port] || 9160}",
34
- options)
35
- unless @backend.keyspaces.include?(keyspace)
36
- cf_def = ::Cassandra::ColumnFamily.new(keyspace: keyspace, name: @cf.to_s)
37
- ks_def = ::Cassandra::Keyspace.new(name: keyspace,
38
- strategy_class: 'SimpleStrategy',
39
- strategy_options: { 'replication_factor' => '1' },
40
- replication_factor: 1,
41
- cf_defs: [cf_def])
42
- # Wait for keyspace to be created (issue #24)
43
- 10.times do
44
- begin
45
- @backend.add_keyspace(ks_def)
46
- rescue Exception => ex
47
- warn "Moneta::Adapters::Cassandra - #{ex.message}"
48
- end
49
- break if @backend.keyspaces.include?(keyspace)
50
- sleep 0.1
51
- end
41
+ keyspace = options.delete(:keyspace) || 'moneta'
42
+
43
+ @table = (options.delete(:column_family) || 'moneta').to_sym
44
+ @key_column = options.delete(:key_column) || 'key'
45
+ @value_column = options.delete(:value_column) || 'value'
46
+ @updated_column = options.delete(:updated_column) || 'updated_at'
47
+ @expired_column = options.delete(:expired_column) || 'expired'
48
+ @read_consistency = options.delete(:read_consistency) || :all
49
+ @write_consistency = options.delete(:write_consistency) || :all
50
+ @create_keyspace = options.delete(:create_keyspace)
51
+
52
+ unless @backend = options.delete(:backend)
53
+ cluster = options.delete(:cluster) || (@own_cluster = ::Cassandra.cluster(options))
54
+ begin
55
+ @backend = cluster.connect(keyspace)
56
+ rescue ::Cassandra::Errors::InvalidError
57
+ @backend = cluster.connect
58
+ create_keyspace(keyspace)
59
+ @backend.execute("USE " + keyspace)
60
+ @backend
52
61
  end
53
- @backend.keyspace = keyspace
54
62
  end
63
+
64
+ @backend.execute <<-CQL
65
+ CREATE TABLE IF NOT EXISTS #{@table} (
66
+ #{@key_column} blob,
67
+ #{@value_column} blob,
68
+ #{@updated_column} timeuuid,
69
+ #{@expired_column} boolean,
70
+ PRIMARY KEY (#{@key_column}, #{@updated_column})
71
+ )
72
+ CQL
73
+
74
+ prepare_statements
55
75
  end
56
76
 
57
77
  # (see Proxy#key?)
58
78
  def key?(key, options = {})
59
- if @backend.exists?(@cf, key)
60
- load(key, options) if options.include?(:expires)
79
+ rc, wc = consistency(options)
80
+ if (expires = expires_value(options, nil)) != nil
81
+ # Because Cassandra expires each value in a column, rather than the
82
+ # whole column, when we want to update the expiry we load the value
83
+ # and then re-set it in order to update the TTL.
84
+ return false unless
85
+ row = @backend.execute(@load, options.merge(consistency: rc, arguments: [key])).first and
86
+ row[@expired_column] != nil
87
+ @backend.execute(@update_expires, options.merge(consistency: wc, arguments: [
88
+ (expires || 0).to_i, timestamp, row[@value_column], key, row[@updated_column]
89
+ ]))
61
90
  true
91
+ elsif row = @backend.execute(@key, options.merge(consistency: rc, arguments: [key])).first
92
+ row[@expired_column] != nil
62
93
  else
63
94
  false
64
95
  end
@@ -66,38 +97,253 @@ module Moneta
66
97
 
67
98
  # (see Proxy#load)
68
99
  def load(key, options = {})
69
- if value = @backend.get(@cf, key)
70
- expires = expires_value(options, nil)
71
- @backend.insert(@cf, key, {'value' => value['value'] }, ttl: expires || nil) if expires != nil
72
- value['value']
100
+ rc, wc = consistency(options)
101
+ if row = @backend.execute(@load, options.merge(consistency: rc, arguments: [key])).first and row[@expired_column] != nil
102
+ if (expires = expires_value(options, nil)) != nil
103
+ @backend.execute(@update_expires, options.merge(consistency: wc, arguments: [
104
+ (expires || 0).to_i, timestamp, row[@value_column], key, row[@updated_column]
105
+ ]))
106
+ end
107
+ row[@value_column]
73
108
  end
74
109
  end
75
110
 
76
111
  # (see Proxy#store)
77
112
  def store(key, value, options = {})
78
- @backend.insert(@cf, key, {'value' => value}, ttl: expires_value(options) || nil)
113
+ _, wc = consistency(options)
114
+ expires = expires_value(options)
115
+ t = timestamp
116
+ batch = @backend.batch do |batch|
117
+ batch.add(@store_delete, arguments: [t, key])
118
+ batch.add(@store, arguments: [key, value, (expires || 0).to_i, t + 1])
119
+ end
120
+ @backend.execute(batch, options.merge(consistency: wc))
79
121
  value
80
122
  end
81
123
 
82
124
  # (see Proxy#delete)
83
125
  def delete(key, options = {})
84
- if value = load(key, options)
85
- @backend.remove(@cf, key)
86
- value
126
+ rc, wc = consistency(options)
127
+ result = @backend.execute(@delete_value, options.merge(consistency: rc, arguments: [key]))
128
+ if row = result.first and row[@expired_column] != nil
129
+ @backend.execute(@delete, options.merge(consistency: wc, arguments: [timestamp, key, row[@updated_column]]))
130
+ row[@value_column]
87
131
  end
88
132
  end
89
133
 
90
134
  # (see Proxy#clear)
91
135
  def clear(options = {})
92
- @backend.clear_column_family!(@cf)
136
+ @backend.execute(@clear)
93
137
  self
94
138
  end
95
139
 
96
140
  # (see Proxy#close)
97
141
  def close
98
- @backend.disconnect!
142
+ @backend.close_async
143
+ @backend = nil
144
+ if @own_cluster
145
+ @own_cluster.close_async
146
+ @own_cluster = nil
147
+ end
99
148
  nil
100
149
  end
150
+
151
+ # (see Proxy#each_key)
152
+ def each_key
153
+ rc, _ = consistency
154
+ return enum_for(:each_key) unless block_given?
155
+ result = @backend.execute(@each_key, consistency: rc, page_size: 100)
156
+ loop do
157
+ result.each do |row|
158
+ next if row[@expired_column] == nil
159
+ yield row[@key_column]
160
+ end
161
+
162
+ break if result.last_page?
163
+ result = result.next_page
164
+ end
165
+ self
166
+ end
167
+
168
+ # (see Proxy#slice)
169
+ def slice(*keys, **options)
170
+ rc, wc = consistency(options)
171
+ result = @backend.execute(@slice, options.merge(consistency: rc, arguments: [keys]))
172
+ expires = expires_value(options, nil)
173
+ updated = [] if expires != nil
174
+ pairs = result.map do |row|
175
+ next if row[@expired_column] == nil
176
+ if expires != nil
177
+ updated << [row[@key_column], row[@value_column], row[@updated_column]]
178
+ end
179
+ [row[@key_column], row[@value_column]]
180
+ end.compact
181
+
182
+ if expires != nil && !updated.empty?
183
+ ttl = (expires || 0).to_i
184
+ t = timestamp
185
+ batch = @backend.batch do |batch|
186
+ updated.each do |key, value, updated|
187
+ batch.add(@update_expires, arguments: [ttl, t, value, key, updated])
188
+ end
189
+ end
190
+
191
+ @backend.execute(batch, options.merge(consistency: wc))
192
+ end
193
+
194
+ pairs
195
+ end
196
+
197
+ # (see Proxy#values_at)
198
+ def values_at(*keys, **options)
199
+ hash = Hash[slice(*keys, **options)]
200
+ keys.map { |key| hash[key] }
201
+ end
202
+
203
+ # (see Proxy#fetch_values)
204
+ def fetch_values(*keys, **options)
205
+ return values_at(*keys, **options) unless block_given?
206
+ hash = Hash[slice(*keys, **options)]
207
+ keys.map do |key|
208
+ if hash.key?(key)
209
+ hash[key]
210
+ else
211
+ yield key
212
+ end
213
+ end
214
+ end
215
+
216
+ # (see Proxy#merge!)
217
+ def merge!(pairs, options = {})
218
+ keys = pairs.map { |k, _| k }.to_a
219
+ return self if keys.empty?
220
+
221
+ if block_given?
222
+ existing = Hash[slice(*keys, **options)]
223
+ pairs = pairs.map do |key, new_value|
224
+ if existing.key?(key)
225
+ [key, yield(key, existing[key], new_value)]
226
+ else
227
+ [key, new_value]
228
+ end
229
+ end
230
+ end
231
+
232
+ rc, wc = consistency(options)
233
+ expires = expires_value(options)
234
+ t = timestamp
235
+ batch = @backend.batch do |batch|
236
+ batch.add(@merge_delete, arguments: [t, keys])
237
+ pairs.each do |key, value|
238
+ batch.add(@store, arguments: [key, value, (expires || 0).to_i, t + 1])
239
+ end
240
+ end
241
+ @backend.execute(batch, options.merge(consistency: wc))
242
+
243
+ self
244
+ end
245
+
246
+ private
247
+
248
+ def timestamp
249
+ (Time.now.to_r * 1_000_000).to_i
250
+ end
251
+
252
+ def create_keyspace(keyspace)
253
+ options = {
254
+ replication: {
255
+ class: 'SimpleStrategy',
256
+ replication_factor: 1
257
+ }
258
+ }
259
+
260
+ case @create_keyspace
261
+ when Proc
262
+ return @create_keyspace.call(keyspace)
263
+ when false
264
+ return
265
+ when Hash
266
+ options.merge!(@create_keyspace)
267
+ end
268
+
269
+ # This is a bit hacky, but works. Options in Cassandra look like JSON,
270
+ # but use single quotes instead of double-quotes.
271
+ require 'multi_json'
272
+ option_str = options.map do |key, value|
273
+ key.to_s + ' = ' + MultiJson.dump(value).gsub('"', "'")
274
+ end.join(' AND ')
275
+
276
+ @backend.execute "CREATE KEYSPACE IF NOT EXISTS %s WITH %s" % [
277
+ keyspace,
278
+ option_str
279
+ ]
280
+ rescue ::Cassandra::Errors::TimeoutError
281
+ tries ||= 0
282
+ if (tries += 1) <= 3; retry else raise end
283
+ end
284
+
285
+ def prepare_statements
286
+ @key = @backend.prepare(<<-CQL)
287
+ SELECT #{@updated_column}, #{@expired_column}
288
+ FROM #{@table} WHERE #{@key_column} = ?
289
+ LIMIT 1
290
+ CQL
291
+ @store_delete = @backend.prepare(<<-CQL)
292
+ DELETE FROM #{@table}
293
+ USING TIMESTAMP ?
294
+ WHERE #{@key_column} = ?
295
+ CQL
296
+ @store = @backend.prepare(<<-CQL)
297
+ INSERT INTO #{@table} (#{@key_column}, #{@value_column}, #{@updated_column}, #{@expired_column})
298
+ VALUES (?, ?, now(), false)
299
+ USING TTL ? AND TIMESTAMP ?
300
+ CQL
301
+ @load = @backend.prepare(<<-CQL)
302
+ SELECT #{@value_column}, #{@updated_column}, #{@expired_column}
303
+ FROM #{@table}
304
+ WHERE #{@key_column} = ?
305
+ LIMIT 1
306
+ CQL
307
+ @update_expires = @backend.prepare(<<-CQL)
308
+ UPDATE #{@table}
309
+ USING TTL ? AND TIMESTAMP ?
310
+ SET #{@value_column} = ?, #{@expired_column} = false
311
+ WHERE #{@key_column} = ? AND #{@updated_column} = ?
312
+ CQL
313
+ @clear = @backend.prepare("TRUNCATE #{@table}")
314
+ @delete_value = @backend.prepare(<<-CQL)
315
+ SELECT #{@value_column}, #{@updated_column}, #{@expired_column}
316
+ FROM #{@table}
317
+ WHERE #{@key_column} = ?
318
+ LIMIT 1
319
+ CQL
320
+ @delete = @backend.prepare(<<-CQL, idempotent: true)
321
+ DELETE FROM #{@table}
322
+ USING TIMESTAMP ?
323
+ WHERE #{@key_column} = ? AND #{@updated_column} = ?
324
+ CQL
325
+ @each_key = @backend.prepare(<<-CQL)
326
+ SELECT #{@key_column}, #{@expired_column}
327
+ FROM #{@table}
328
+ CQL
329
+ @slice = @backend.prepare(<<-CQL)
330
+ SELECT #{@key_column}, #{@value_column}, #{@updated_column}, #{@expired_column}
331
+ FROM #{@table}
332
+ WHERE #{@key_column} IN ?
333
+ CQL
334
+ @merge_delete = @backend.prepare(<<-CQL)
335
+ DELETE FROM #{@table}
336
+ USING TIMESTAMP ?
337
+ WHERE #{@key_column} IN ?
338
+ CQL
339
+ end
340
+
341
+ def consistency(options={})
342
+ [
343
+ options[:read_consistency] || @read_consistency,
344
+ options[:write_consistency] || @write_consistency
345
+ ]
346
+ end
101
347
  end
102
348
  end
103
349
  end