Skip to content

Commit

Permalink
First cut at Perpetuity in-memory support as a gem.
Browse files Browse the repository at this point in the history
This is based on perpetuity-mongodb and Christopher Garvis's
pull request (jgaskins/perpetuity#20)
for an in-memory data store.
  • Loading branch information
booch committed Jan 11, 2014
0 parents commit f421c46
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in the perpetuity-memory.gemspec file.
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2013 Jamie Gaskins

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
@@ -0,0 +1,36 @@
# Perpetuity::Memory

This is the in-memory adapter for Perpetuity.

## Installation

Add this line to your application's Gemfile:

gem 'perpetuity-memory'

And then execute:

$ bundle

Or install it yourself as:

$ gem install perpetuity-memory

## Usage

Put this at the top of your application (or in a Rails initializer):

```ruby
require 'perpetuity/memory' # Unnecessary if using Rails
Perpetuity.data_source :memory
```

For information on using Perpetuity to persist your Ruby objects, see the [main Perpetuity repo](https://github.com/jgaskins/perpetuity).

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
require "bundler/gem_tasks"
131 changes: 131 additions & 0 deletions lib/perpetuity/memory.rb
@@ -0,0 +1,131 @@
require 'perpetuity'
require 'perpetuity/exceptions/duplicate_key_error'
require 'perpetuity/attribute'
require 'set'
require 'securerandom'

module Perpetuity
class Memory
def initialize options = {}
@cache = Hash.new
@indexes = Hash.new { |hash, key| hash[key] = active_indexes(key) }
end

def insert klass, attributes, _
if attributes.is_a? Array
return attributes.map{|attr| insert(klass, attr, _)}
end

unless attributes.has_key? :id
attributes[:id] = SecureRandom.uuid
end

# make keys indifferent
attributes.default_proc = proc do |h, k|
case k
when String then sym = k.to_sym; h[sym] if h.key?(sym)
when Symbol then str = k.to_s; h[str] if h.key?(str)
end
end

collection(klass) << attributes
attributes[:id]
end

def count klass, criteria=nil, &block
if block_given?
collection(klass).select(&block).size
elsif criteria
collection(klass).select(&criteria).size
else
collection(klass).size
end
end

def delete_all klass
collection(klass).clear
end

def first klass
collection(klass).first
end

def find klass, id
collection(klass).find{|o| o[:id] = id}
end

def retrieve klass, criteria, options = {}
collection(klass).find_all(&criteria)
end

def all klass
collection(klass)
end

def delete object, klass=nil
klass ||= object.class
id = object.class == String || !object.respond_to?(:id) ? object : object.id

collection(klass).each_with_index do |record, index|
if record[:id] === id
collection(klass).delete_at index
end
end
end

def update klass, id, new_data
collection(klass).each_with_index do |record, index|
if record[:id] == id
collection(klass)[index] = record.merge(new_data)
end
end
end

def increment klass, id, attribute, count=1
find(klass, id)[attribute] += count
end

def can_serialize? value
true
end

def serialize object, mapper
Marshal.dump(object)
end

def unserialize data, mapper
Marshal.load(data)
end


def index klass, attribute, options={}
@indexes[klass] ||= Set.new
end

def indexes klass
@indexes[klass]
end

def active_indexes klass
Set.new
end

def activate_index! klass
true
end

def remove_index index
end

def query &block
block
end

protected

def collection klass
@cache[klass] = Array.new unless @cache.key? klass
@cache[klass]
end
end
end
5 changes: 5 additions & 0 deletions lib/perpetuity/memory/version.rb
@@ -0,0 +1,5 @@
module Perpetuity
module Memory
VERSION = "0.1.0"
end
end
25 changes: 25 additions & 0 deletions perpetuity-memory.gemspec
@@ -0,0 +1,25 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'perpetuity/memory/version'

Gem::Specification.new do |spec|
spec.name = "perpetuity-memory"
spec.version = Perpetuity::Memory::VERSION
spec.authors = ["Jamie Gaskins", "Christopher Garvis", "Craig Buchek"]
spec.email = ["craig@boochtek.com"]
spec.description = %q{In-memory adapter for Perpetuity}
spec.summary = %q{In-memory adapter for Perpetuity}
spec.homepage = "https://github.com/boochtek/perpetuity-memory"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rake"
spec.add_runtime_dependency "perpetuity", "~> 1.0.0.beta3"
end
82 changes: 82 additions & 0 deletions spec/perpetuity/memory_spec.rb
@@ -0,0 +1,82 @@
require 'perpetuity/memory'
require 'date'

module Perpetuity
describe Memory do
let(:klass) { String }

it 'inserts' do
expect { subject.insert klass, { name: 'foo' }, [] }.to change { subject.count klass }.by 1
end

it 'inserts multiple objects' do
expect { subject.insert klass, [{name: 'foo'}, {name: 'bar'}], [] }
.to change { subject.count klass }.by 2
end

it 'removes all objects' do
subject.insert klass, {}, []
subject.delete_all klass
subject.count(klass).should == 0
end

it 'counts the number of objects' do
subject.delete_all klass
3.times do
subject.insert klass, {}, []
end
subject.count(klass).should == 3
end

it 'counts the objects matching a query' do
subject.delete_all klass
1.times { subject.insert klass, { name: 'bar' }, [] }
3.times { subject.insert klass, { name: 'foo' }, [] }
subject.count(klass) { |o| o[:name] == 'foo' }.should == 3
end

it 'gets the first object' do
value = {value: 1}
subject.insert klass, value, []
subject.first(klass)[:value].should == value['value']
end

it 'gets all objects' do
values = [{value: 1}, {value: 2}]
subject.insert klass, values, []
subject.all(klass).should == values
end

it 'retrieves by ID if the ID is a string' do
time = Time.now.utc
id = subject.insert Object, {inserted: time}, []

object = subject.retrieve(Object, subject.query{|o| o[:id] == id.to_s }).first
retrieved_time = object["inserted"]
retrieved_time.to_f.should be_within(0.001).of time.to_f
end

describe 'serialization' do
end

describe 'indexing' do
it 'does nothing' do
subject.index(klass, 'anything')
end
end

describe 'atomic operations' do
after(:all) { subject.delete_all klass }

it 'increments the value of an attribute' do
id = subject.insert klass, { count: 1 }, []
subject.increment klass, id, :count
subject.increment klass, id, :count, 10
query = subject.query { |o| o[:id] == id }
subject.retrieve(klass, query).first['count'].should be == 12
subject.increment klass, id, :count, -1
subject.retrieve(klass, query).first['count'].should be == 11
end
end
end
end

0 comments on commit f421c46

Please sign in to comment.