Permalink
Browse files

Get all the adapters (and a couple new ones) working on the new spec

  • Loading branch information...
1 parent 4542cf3 commit eab31e7bb849fd6dab940fc668f5e27a70b8fbf0 @wycats committed Jul 17, 2010
View
@@ -96,4 +96,4 @@ The base Moneta specification does not specify any atomicity guarantees. However
# Expiry
-The base Moneta specification does not specify any mechanism for time-based expiry. However, extensions to this spec may specify mechanisms (using <code>store</code> and <code>update_key</code>) to provide expiration semantics.
+The base Moneta specification does not specify any mechanism for time-based expiry. However, extensions to this spec may specify mechanisms (using <code>store</code> and <code>update_key</code>) to provide expiration semantics.
View
@@ -2,20 +2,28 @@
module Moneta
module Defaults
- def fetch(key, value = nil)
- self[key_for(key)] || begin
+ def fetch(key, value = nil, *)
+ self[key] || begin
value ||= block_given? ? yield(key) : default
- self[key_for(key)] || value
+ self[key] || value
end
end
- def store(key, value, options = {})
- self[key_for(key)] = value
+ def []=(key, value)
+ store(key, value)
end
private
def key_for(key)
key.is_a?(String) ? key : Marshal.dump(key)
end
+
+ def serialize(value)
+ Marshal.dump(value)
+ end
+
+ def deserialize(value)
+ value && Marshal.load(value)
+ end
end
end
@@ -29,7 +29,7 @@ def initialize(options = {})
ensure_directory_created(@directory)
end
- def key?(key)
+ def key?(key, *)
!self[key].nil?
end
@@ -39,39 +39,20 @@ def [](key)
end
end
- def raw_get(key)
- Marshal.load(::File.read(path(key)))
- end
-
- def []=(key, value)
- store(key, value)
- end
-
- def store(key, value, options = {})
+ def store(key, value, *)
ensure_directory_created(::File.dirname(path(key)))
::File.open(path(key), "w") do |file|
- contents = Marshal.dump(value)
- file.puts(contents)
+ file.puts(serialize(value))
end
end
- def update_key(key, options)
- store(key, self[key], options)
- end
-
- def delete!(key)
- FileUtils.rm(path(key))
- nil
- rescue Errno::ENOENT
- end
-
- def delete(key)
+ def delete(key, *)
value = self[key]
delete!(key)
value
end
- def clear
+ def clear(*)
FileUtils.rm_rf(@directory)
FileUtils.mkdir(@directory)
end
@@ -81,6 +62,16 @@ def path(key)
::File.join(@directory, key_for(key))
end
+ def raw_get(key)
+ deserialize(::File.read(path(key)))
+ end
+
+ def delete!(key)
+ FileUtils.rm(path(key))
+ nil
+ rescue Errno::ENOENT
+ end
+
def ensure_directory_created(directory_path)
if ::File.file?(directory_path)
raise StandardError, "The path you supplied #{directory_path} is a file"
@@ -90,4 +81,4 @@ def ensure_directory_created(directory_path)
end
end
end
-end
+end
@@ -1,49 +0,0 @@
-begin
- require 'bdb'
-rescue LoadError
- puts "You need bdb gem to use Bdb moneta store"
- exit
-end
-
-module Moneta
- module Adapters
- class Berkeley
- include Defaults
-
- @@dbs = []
-
- def self.close_all
- @@dbs.each {|d| d.close(0) }
- end
-
- def initialize(options={})
- file = @file = options[:file]
- @db = Bdb::Db.new
- @db.open(nil, file, nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
- @@dbs << @db
- end
-
- def key?(key)
- nil | self[key_for(key)]
- end
-
- def []=(key,value)
- @db[key_for(key)] = value
- end
-
- def [](key)
- @db[key_for(key)]
- end
-
- def delete(key)
- value = self[key]
- @db.del(nil,key_for(key),0) if value
- value
- end
-
- def clear
- @db.truncate(nil)
- end
- end
- end
-end
@@ -9,56 +9,43 @@ module Moneta
module Adapters
class Couch
include Defaults
-
+
def initialize(options = {})
@db = ::CouchRest.database!(options[:db])
end
- def key?(key)
+ def key?(key, *)
!self[key_for(key)].nil?
rescue RestClient::ResourceNotFound
false
end
- alias has_key? key?
-
def [](key)
- @db.get(key_for(key))["data"]
+ deserialize(@db.get(key_for(key))["data"])
rescue RestClient::ResourceNotFound
nil
end
- def []=(key, value)
- @db.save_doc("_id" => key_for(key), :data => value)
+ def store(key, value, *)
+ @db.save_doc("_id" => key_for(key), :data => serialize(value))
rescue RestClient::RequestFailed
self[key_for(key)]
end
- def delete(key)
+ def delete(key, *)
value = @db.get(key_for(key))
-
+
if value
@db.delete_doc({"_id" => value["_id"], "_rev" => value["_rev"]}) if value
- value["data"]
+ deserialize(value["data"])
end
rescue RestClient::ResourceNotFound
nil
end
- def update_key(key, options = {})
- val = self[key]
- self.store(key, val, options)
- rescue RestClient::ResourceNotFound
- nil
- end
-
- def clear
+ def clear(*)
@db.recreate!
end
-
- def delete_store
- @db.delete!
- end
end
end
end
@@ -1,6 +1,9 @@
begin
- gem "dm-core", "0.9.10"
+ gem "dm-core", "~> 1.0.0"
+ gem "dm-migrations", "~> 1.0.0"
+ gem "dm-sqlite-adapter", "~> 1.0.0"
require "dm-core"
+ require "dm-migrations"
rescue LoadError
puts "You need the dm-core gem in order to use the DataMapper moneta store"
exit
@@ -11,7 +14,6 @@ class MonetaHash
property :the_key, String, :key => true
property :value, Object, :lazy => false
- property :expires, Time
def self.value(key)
obj = self.get(key)
@@ -20,98 +22,55 @@ def self.value(key)
end
module Moneta
- class DataMapper
- class Expiration
- def initialize(klass, repository)
- @klass = klass
- @repository = repository
+ module Adapters
+ class DataMapper
+ include Moneta::Defaults
+
+ def initialize(options = {})
+ @repository = options.delete(:repository) || :moneta
+ ::DataMapper.setup(@repository, options[:setup])
+ MonetaHash.auto_upgrade!(@repository)
+ @hash = MonetaHash
end
- def [](key)
- if obj = get(key)
- obj.expires
- end
- end
-
- def []=(key, value)
- obj = get(key)
- obj.expires = value
- obj.save(@repository)
- end
-
- def delete(key)
- obj = get(key)
- obj.expires = nil
- obj.save(@repository)
- end
-
- private
- def get(key)
- repository(@repository) { @klass.get(key) }
- end
- end
-
- def initialize(options = {})
- @repository = options.delete(:repository) || :moneta
- ::DataMapper.setup(@repository, options[:setup])
- repository_context { MonetaHash.auto_upgrade! }
- @hash = MonetaHash
- @expiration = Expiration.new(MonetaHash, @repository)
- end
-
- module Implementation
- def key?(key)
- repository_context { !!@hash.get(key) }
- end
-
- def has_key?(key)
- repository_context { !!@hash.get(key) }
+ def key?(key, *)
+ repository_context { !!@hash.get(key_for(key)) }
end
def [](key)
- repository_context { @hash.value(key) }
+ repository_context { @hash.value(key_for(key)) }
end
- def []=(key, value)
+ def store(key, value, *)
+ string_key = key_for(key)
repository_context {
- obj = @hash.get(key)
+ obj = @hash.get(string_key)
if obj
- obj.update(key, value)
+ obj.update(string_key, value)
else
- @hash.create(:the_key => key, :value => value)
+ @hash.create(:the_key => string_key, :value => value)
end
}
end
- def fetch(key, value = nil)
- repository_context {
- value ||= block_given? ? yield(key) : default
- self[key] || value
- }
- end
+ def delete(key, *)
+ string_key = key_for(key)
- def delete(key)
repository_context {
value = self[key]
- @hash.all(:the_key => key).destroy!
+ @hash.all(:the_key => string_key).destroy!
value
}
end
- def store(key, value, options = {})
- repository_context { self[key] = value }
- end
-
- def clear
+ def clear(*)
repository_context { @hash.all.destroy! }
end
- private
+ private
def repository_context
repository(@repository) { yield }
end
end
- include Implementation
- include Expires
end
-end
+end
Oops, something went wrong.

0 comments on commit eab31e7

Please sign in to comment.