sohm 0.0.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.gemfile +9 -0
- data/.travis.yml +10 -0
- data/README.md +2 -0
- data/lib/sohm/auto_id.rb +13 -0
- data/lib/sohm/command.rb +1 -1
- data/lib/sohm/index_all.rb +18 -0
- data/lib/sohm/json.rb +1 -1
- data/lib/sohm.rb +121 -207
- data/sohm.gemspec +1 -1
- data/test/association.rb +6 -12
- data/test/command.rb +6 -6
- data/test/connection.rb +5 -5
- data/test/core.rb +4 -1
- data/test/counters.rb +2 -1
- data/test/enumerable.rb +9 -3
- data/test/filtering.rb +10 -13
- data/test/hash_key.rb +3 -1
- data/test/helper.rb +5 -3
- data/test/indices.rb +6 -36
- data/test/json.rb +8 -3
- data/test/list.rb +6 -11
- data/test/model.rb +100 -179
- data/test/set.rb +6 -2
- data/test/sohm.rb +44 -0
- data/test/to_hash.rb +5 -2
- metadata +7 -6
- data/lib/sample.rb +0 -14
- data/lib/sohm/lua/delete.lua +0 -72
- data/test/thread_safety.rb +0 -67
- data/test/uniques.rb +0 -98
data/lib/sohm/lua/delete.lua
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
-- This script receives three parameters, all encoded with
|
2
|
-
-- MessagePack. The decoded values are used for deleting a model
|
3
|
-
-- instance in Redis and removing any reference to it in sets
|
4
|
-
-- (indices) and hashes (unique indices).
|
5
|
-
--
|
6
|
-
-- # model
|
7
|
-
--
|
8
|
-
-- Table with three attributes:
|
9
|
-
-- id (model instance id)
|
10
|
-
-- key (hash where the attributes will be saved)
|
11
|
-
-- name (model name)
|
12
|
-
--
|
13
|
-
-- # uniques
|
14
|
-
--
|
15
|
-
-- Fields and values to be removed from the unique indices.
|
16
|
-
--
|
17
|
-
-- # tracked
|
18
|
-
--
|
19
|
-
-- Keys that share the lifecycle of this model instance, that
|
20
|
-
-- should be removed as this object is deleted.
|
21
|
-
--
|
22
|
-
local model = cmsgpack.unpack(ARGV[1])
|
23
|
-
local uniques = cmsgpack.unpack(ARGV[2])
|
24
|
-
local tracked = cmsgpack.unpack(ARGV[3])
|
25
|
-
|
26
|
-
local function remove_indices(model)
|
27
|
-
local memo = model.key .. ":_indices"
|
28
|
-
local existing = redis.call("SMEMBERS", memo)
|
29
|
-
|
30
|
-
for _, key in ipairs(existing) do
|
31
|
-
redis.call("SREM", key, model.id)
|
32
|
-
redis.call("SREM", memo, key)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
local function remove_uniques(model, uniques)
|
37
|
-
local memo = model.key .. ":_uniques"
|
38
|
-
|
39
|
-
for field, _ in pairs(uniques) do
|
40
|
-
local key = model.name .. ":uniques:" .. field
|
41
|
-
|
42
|
-
redis.call("HDEL", key, redis.call("HGET", memo, key))
|
43
|
-
redis.call("HDEL", memo, key)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
local function remove_tracked(model, tracked)
|
48
|
-
for _, tracked_key in ipairs(tracked) do
|
49
|
-
local key = model.key .. ":" .. tracked_key
|
50
|
-
|
51
|
-
redis.call("DEL", key)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
local function delete(model)
|
56
|
-
local keys = {
|
57
|
-
model.key .. ":counters",
|
58
|
-
model.key .. ":_indices",
|
59
|
-
model.key .. ":_uniques",
|
60
|
-
model.key
|
61
|
-
}
|
62
|
-
|
63
|
-
redis.call("SREM", model.name .. ":all", model.id)
|
64
|
-
redis.call("DEL", unpack(keys))
|
65
|
-
end
|
66
|
-
|
67
|
-
remove_indices(model)
|
68
|
-
remove_uniques(model, uniques)
|
69
|
-
remove_tracked(model, tracked)
|
70
|
-
delete(model)
|
71
|
-
|
72
|
-
return model.id
|
data/test/thread_safety.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
require_relative "helper"
|
2
|
-
|
3
|
-
class Post < Ohm::Model; end
|
4
|
-
class Role < Ohm::Model; end
|
5
|
-
|
6
|
-
class User < Ohm::Model
|
7
|
-
list :posts, :Post
|
8
|
-
set :roles, :Role
|
9
|
-
end
|
10
|
-
|
11
|
-
setup do
|
12
|
-
User.create
|
13
|
-
end
|
14
|
-
|
15
|
-
test "list#replace" do |user|
|
16
|
-
Post.mutex.lock
|
17
|
-
|
18
|
-
thread = Thread.new { user.posts.replace([Post.create]) }
|
19
|
-
|
20
|
-
sleep 0.1
|
21
|
-
|
22
|
-
assert_equal true, thread.alive?
|
23
|
-
|
24
|
-
Post.mutex.unlock
|
25
|
-
|
26
|
-
sleep 0.1
|
27
|
-
|
28
|
-
assert_equal false, thread.alive?
|
29
|
-
|
30
|
-
thread.join
|
31
|
-
end
|
32
|
-
|
33
|
-
test "set#replace" do |user|
|
34
|
-
Role.mutex.lock
|
35
|
-
|
36
|
-
thread = Thread.new { user.roles.replace([Role.create]) }
|
37
|
-
|
38
|
-
sleep 0.1
|
39
|
-
|
40
|
-
assert_equal true, thread.alive?
|
41
|
-
|
42
|
-
Role.mutex.unlock
|
43
|
-
|
44
|
-
sleep 0.1
|
45
|
-
|
46
|
-
assert_equal false, thread.alive?
|
47
|
-
|
48
|
-
thread.join
|
49
|
-
end
|
50
|
-
|
51
|
-
test "collection#fetch" do
|
52
|
-
User.mutex.lock
|
53
|
-
|
54
|
-
thread = Thread.new { User.all.to_a }
|
55
|
-
|
56
|
-
sleep 0.1
|
57
|
-
|
58
|
-
assert_equal true, thread.alive?
|
59
|
-
|
60
|
-
User.mutex.unlock
|
61
|
-
|
62
|
-
sleep 0.1
|
63
|
-
|
64
|
-
assert_equal false, thread.alive?
|
65
|
-
|
66
|
-
thread.join
|
67
|
-
end
|
data/test/uniques.rb
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
require_relative "helper"
|
2
|
-
|
3
|
-
class User < Ohm::Model
|
4
|
-
attribute :email
|
5
|
-
unique :email
|
6
|
-
unique :provider
|
7
|
-
|
8
|
-
def self.[](id)
|
9
|
-
super(id.to_i)
|
10
|
-
end
|
11
|
-
|
12
|
-
def provider
|
13
|
-
email.to_s[/@(.*?).com/, 1]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
setup do
|
18
|
-
User.create(:email => "a@a.com")
|
19
|
-
end
|
20
|
-
|
21
|
-
test "findability" do |u|
|
22
|
-
assert_equal u, User.with(:email, "a@a.com")
|
23
|
-
end
|
24
|
-
|
25
|
-
test "raises when it already exists during create" do
|
26
|
-
assert_raise Ohm::UniqueIndexViolation do
|
27
|
-
User.create(:email => "a@a.com")
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
test "raises when it already exists during save" do
|
32
|
-
u = User.create(:email => "b@b.com")
|
33
|
-
u.email = "a@a.com"
|
34
|
-
|
35
|
-
assert_raise Ohm::UniqueIndexViolation do
|
36
|
-
u.save
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
test "raises if the index doesn't exist" do
|
41
|
-
User.create(:email => "b@b.com")
|
42
|
-
|
43
|
-
assert_raise Ohm::IndexNotFound do
|
44
|
-
User.with(:address, "b@b.com")
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
test "doesn't raise when saving again and again" do |u|
|
49
|
-
ex = nil
|
50
|
-
|
51
|
-
begin
|
52
|
-
User[u.id].save
|
53
|
-
rescue Exception => e
|
54
|
-
ex = e
|
55
|
-
end
|
56
|
-
|
57
|
-
assert_equal nil, ex
|
58
|
-
end
|
59
|
-
|
60
|
-
test "removes the previous index when changing" do
|
61
|
-
u = User.create(:email => "c@c.com")
|
62
|
-
u.update(:email => "d@d.com")
|
63
|
-
|
64
|
-
assert_equal nil, User.with(:email, "c@c.com")
|
65
|
-
assert_equal nil, User.redis.call("HGET", User.key[:uniques][:email], "c@c.com")
|
66
|
-
assert_equal u, User.with(:email, "d@d.com")
|
67
|
-
|
68
|
-
u.update(:email => nil)
|
69
|
-
|
70
|
-
assert_equal nil, User.with(:email, "d@d.com")
|
71
|
-
assert_equal nil, User.redis.call("HGET", User.key[:uniques][:email], "d@d.com")
|
72
|
-
end
|
73
|
-
|
74
|
-
test "removes the previous index when deleting" do |u|
|
75
|
-
u.delete
|
76
|
-
|
77
|
-
assert_equal nil, User.with(:email, "a@a.com")
|
78
|
-
assert_equal nil, User.redis.call("HGET", User.key[:uniques][:email], "a@a.com")
|
79
|
-
end
|
80
|
-
|
81
|
-
test "unique virtual attribute" do
|
82
|
-
u = User.create(:email => "foo@yahoo.com")
|
83
|
-
|
84
|
-
assert_equal u, User.with(:provider, "yahoo")
|
85
|
-
|
86
|
-
# Yahoo should be allowed because this user is the one reserved for it.
|
87
|
-
u.update(:email => "bar@yahoo.com")
|
88
|
-
|
89
|
-
# `a` is not allowed though.
|
90
|
-
assert_raise Ohm::UniqueIndexViolation do
|
91
|
-
u.update(:email => "bar@a.com")
|
92
|
-
end
|
93
|
-
|
94
|
-
# And so is yahoo if we try creating a different user.
|
95
|
-
assert_raise Ohm::UniqueIndexViolation do
|
96
|
-
User.create(:email => "baz@yahoo.com")
|
97
|
-
end
|
98
|
-
end
|