ohm-pop 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +60 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/ohm/lua/pop.lua +72 -0
- data/lib/ohm/pop.rb +7 -0
- data/lib/ohm/pop/version.rb +5 -0
- data/lib/ohm/set.rb +61 -0
- data/ohm-pop.gemspec +27 -0
- data/wercker.yml +12 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3f60a3216c61e3a119eb8eec9b225c5ce48c4135
|
4
|
+
data.tar.gz: a419caccadbae56bac9cbad76bf530e6cf318297
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b5a941ad823a6a475f24a4239e180c1f84764e4308224788313730854fd37568a0ffd342a28bc8117bdedbb1cc6f8abdc32a2ade97ab5db13cac0a8282e9dcef
|
7
|
+
data.tar.gz: 0cf9c450640712c3834e194156972ea841a194b13c74fcacce3fb822cd5858f0f70d70c1e396ccc7846d14e068a1b6324e27c894b8d605b632b8e3f7585bfa22
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 dany1468
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
[![wercker
|
2
|
+
status](https://app.wercker.com/status/2a15e93654b2ff7d6ee34782791addeb/m/master
|
3
|
+
"wercker
|
4
|
+
status")](https://app.wercker.com/project/bykey/2a15e93654b2ff7d6ee34782791addeb)
|
5
|
+
|
6
|
+
# Ohm::Pop
|
7
|
+
|
8
|
+
A pop implementation for Ohm
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'ohm-pop'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install ohm-pop
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'ohm'
|
30
|
+
require 'ohm/pop'
|
31
|
+
|
32
|
+
class Item < Ohm::Model
|
33
|
+
attribute :name
|
34
|
+
attribute :priority
|
35
|
+
|
36
|
+
index :name
|
37
|
+
index :priority
|
38
|
+
end
|
39
|
+
|
40
|
+
Item.create(name: 'item1', priority: 1)
|
41
|
+
Item.create(name: 'item2', priority: 2)
|
42
|
+
Item.create(name: 'item3', priority: 3)
|
43
|
+
|
44
|
+
Item.all.size
|
45
|
+
# => 3
|
46
|
+
|
47
|
+
Item.all.pop(by: :priority)
|
48
|
+
# => #<Item:xxxx @attributes={:name=>"item1", :priority=>"1"}, @_memo={}, @id="1">
|
49
|
+
|
50
|
+
Item.all.size
|
51
|
+
# => 2
|
52
|
+
```
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it ( https://github.com/[my-github-username]/ohm-pop/fork )
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ohm/pop"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/ohm/lua/pop.lua
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
local queries = cmsgpack.unpack(ARGV[1])
|
2
|
+
local ids = cmsgpack.unpack(ARGV[2])
|
3
|
+
local model_name = cmsgpack.unpack(ARGV[3])
|
4
|
+
local uniques = cmsgpack.unpack(ARGV[4])
|
5
|
+
local tracked = cmsgpack.unpack(ARGV[5])
|
6
|
+
|
7
|
+
local function remove_indices(model_key, id)
|
8
|
+
local memo = model_key .. ":_indices"
|
9
|
+
local existing = redis.call("SMEMBERS", memo)
|
10
|
+
|
11
|
+
for _, key in ipairs(existing) do
|
12
|
+
redis.call("SREM", key, id)
|
13
|
+
redis.call("SREM", memo, key)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
local function remove_uniques(model_key, model_name, uniques)
|
18
|
+
local memo = model_key .. ":_uniques"
|
19
|
+
|
20
|
+
for _, unique_key in pairs(uniques) do
|
21
|
+
local key = model_name .. ":uniques:" .. unique_key
|
22
|
+
|
23
|
+
redis.call("HDEL", key, redis.call("HGET", memo, key))
|
24
|
+
redis.call("HDEL", memo, key)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
local function remove_tracked(model_key, tracked)
|
29
|
+
for _, tracked_key in ipairs(tracked) do
|
30
|
+
local key = model_key .. ":" .. tracked_key
|
31
|
+
|
32
|
+
redis.call("DEL", key)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
local function delete(model_key, model_name, id)
|
37
|
+
local keys = {
|
38
|
+
model_key .. ":counters",
|
39
|
+
model_key .. ":_indices",
|
40
|
+
model_key .. ":_uniques",
|
41
|
+
model_key
|
42
|
+
}
|
43
|
+
|
44
|
+
redis.call("SREM", model_name .. ":all", id)
|
45
|
+
redis.call("DEL", unpack(keys))
|
46
|
+
end
|
47
|
+
|
48
|
+
local results = {}
|
49
|
+
for _, query in pairs(queries) do
|
50
|
+
results = redis.call(unpack(query))
|
51
|
+
end
|
52
|
+
|
53
|
+
if next(ids) then
|
54
|
+
redis.call("DEL", unpack(ids))
|
55
|
+
end
|
56
|
+
|
57
|
+
if results[1] ~= nil then
|
58
|
+
local model = redis.call("HGETALL", model_name .. ":" .. results[1])
|
59
|
+
table.insert(model, 'id')
|
60
|
+
table.insert(model, results[1])
|
61
|
+
|
62
|
+
local model_key = model_name .. ":" .. results[1]
|
63
|
+
|
64
|
+
remove_indices(model_key, results[1])
|
65
|
+
remove_uniques(model_key, model_name, uniques)
|
66
|
+
remove_tracked(model_key, tracked)
|
67
|
+
delete(model_key, model_name, results[1])
|
68
|
+
|
69
|
+
return model
|
70
|
+
end
|
71
|
+
|
72
|
+
return nil
|
data/lib/ohm/pop.rb
ADDED
data/lib/ohm/set.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module Ohm
|
2
|
+
class Set
|
3
|
+
LUA_POP = File.expand_path("../lua/pop.lua", __FILE__)
|
4
|
+
|
5
|
+
def pop(options = {})
|
6
|
+
if options[:by]
|
7
|
+
options.merge!(:by => to_key(options.delete(:by)))
|
8
|
+
end
|
9
|
+
|
10
|
+
if options.has_key?(:limit)
|
11
|
+
raise ArgumentError, 'limit options is not supported. always limit: [0, 1]'
|
12
|
+
end
|
13
|
+
|
14
|
+
if options.has_key?(:get)
|
15
|
+
raise ArgumentError, 'get options is not supported.'
|
16
|
+
end
|
17
|
+
|
18
|
+
if options.has_key?(:store)
|
19
|
+
raise ArgumentError, 'store options is not supported.'
|
20
|
+
end
|
21
|
+
|
22
|
+
options.merge!(limit: [0, 1])
|
23
|
+
|
24
|
+
ids = []
|
25
|
+
ops = []
|
26
|
+
|
27
|
+
expr = ["SORT", key, *Utils.sort_options(options)]
|
28
|
+
ops.push(Stal.compile(expr, ids, ops))
|
29
|
+
|
30
|
+
response = script(
|
31
|
+
LUA_POP,
|
32
|
+
0,
|
33
|
+
ops.to_msgpack,
|
34
|
+
ids.to_msgpack,
|
35
|
+
@model.name.to_msgpack,
|
36
|
+
@model.uniques.to_msgpack,
|
37
|
+
@model.tracked.to_msgpack
|
38
|
+
)
|
39
|
+
|
40
|
+
return nil unless response
|
41
|
+
|
42
|
+
@model.new(Utils.dict(response))
|
43
|
+
end
|
44
|
+
|
45
|
+
# NOTE This method is the same as Ohm::Model#script
|
46
|
+
def script(file, *args)
|
47
|
+
cache = LUA_CACHE[redis.url]
|
48
|
+
|
49
|
+
if cache.key?(file)
|
50
|
+
sha = cache[file]
|
51
|
+
else
|
52
|
+
src = File.read(file)
|
53
|
+
sha = redis.call("SCRIPT", "LOAD", src)
|
54
|
+
|
55
|
+
cache[file] = sha
|
56
|
+
end
|
57
|
+
|
58
|
+
redis.call("EVALSHA", sha, *args)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/ohm-pop.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ohm/pop/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ohm-pop"
|
8
|
+
spec.version = Ohm::Pop::VERSION
|
9
|
+
spec.authors = ["dany1468"]
|
10
|
+
spec.email = ["dany1468@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A pop implementation for Ohm.}
|
13
|
+
spec.description = %q{A pop implementation for Ohm.}
|
14
|
+
spec.homepage = "https://github.com/dany1468/ohm-pop"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "ohm", "= 2.2"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
data/wercker.yml
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ohm-pop
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dany1468
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ohm
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A pop implementation for Ohm.
|
70
|
+
email:
|
71
|
+
- dany1468@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/ohm/lua/pop.lua
|
85
|
+
- lib/ohm/pop.rb
|
86
|
+
- lib/ohm/pop/version.rb
|
87
|
+
- lib/ohm/set.rb
|
88
|
+
- ohm-pop.gemspec
|
89
|
+
- wercker.yml
|
90
|
+
homepage: https://github.com/dany1468/ohm-pop
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: A pop implementation for Ohm.
|
114
|
+
test_files: []
|