ohm 1.0.1 → 1.0.2

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.
Files changed (4) hide show
  1. data/lib/ohm.rb +45 -19
  2. data/test/indices.rb +32 -0
  3. data/test/model.rb +2 -1
  4. metadata +31 -11
data/lib/ohm.rb CHANGED
@@ -1286,27 +1286,34 @@ module Ohm
1286
1286
  def __save__
1287
1287
  Transaction.new do |t|
1288
1288
  t.watch(*_unique_keys)
1289
- t.watch(key) if not new?
1289
+
1290
+ if not new?
1291
+ t.watch(key)
1292
+ t.watch(key[:_indices]) if model.indices.any?
1293
+ t.watch(key[:_uniques]) if model.uniques.any?
1294
+ end
1290
1295
 
1291
1296
  t.before do
1292
1297
  _initialize_id if new?
1293
1298
  end
1294
1299
 
1295
- existing = nil
1300
+ _uniques = nil
1296
1301
  uniques = nil
1302
+ _indices = nil
1297
1303
  indices = nil
1298
1304
 
1299
1305
  t.read do
1300
1306
  _verify_uniques
1301
- existing = db.hgetall(key)
1307
+ _uniques = db.hgetall(key[:_uniques])
1308
+ _indices = db.smembers(key[:_indices])
1302
1309
  uniques = _read_index_type(:uniques)
1303
1310
  indices = _read_index_type(:indices)
1304
1311
  end
1305
1312
 
1306
1313
  t.write do
1307
1314
  db.sadd(model.key[:all], id)
1308
- _delete_uniques(existing)
1309
- _delete_indices(existing)
1315
+ _delete_uniques(_uniques)
1316
+ _delete_indices(_indices)
1310
1317
  _save
1311
1318
  _save_indices(indices)
1312
1319
  _save_uniques(uniques)
@@ -1324,13 +1331,23 @@ module Ohm
1324
1331
  #
1325
1332
  def delete
1326
1333
  transaction do |t|
1327
- t.read do |store|
1328
- store[:existing] = db.hgetall(key)
1334
+ _uniques = nil
1335
+ _indices = nil
1336
+
1337
+ t.watch(*_unique_keys)
1338
+
1339
+ t.watch(key)
1340
+ t.watch(key[:_indices]) if model.indices.any?
1341
+ t.watch(key[:_uniques]) if model.uniques.any?
1342
+
1343
+ t.read do
1344
+ _uniques = db.hgetall(key[:_uniques])
1345
+ _indices = db.smembers(key[:_indices])
1329
1346
  end
1330
1347
 
1331
- t.write do |store|
1332
- _delete_uniques(store[:existing])
1333
- _delete_indices(store[:existing])
1348
+ t.write do
1349
+ _delete_uniques(_uniques)
1350
+ _delete_indices(_indices)
1334
1351
  model.collections.each { |e| db.del(key[e]) }
1335
1352
  db.srem(model.key[:all], id)
1336
1353
  db.del(key[:counters])
@@ -1470,28 +1487,37 @@ module Ohm
1470
1487
 
1471
1488
  def _save_uniques(uniques)
1472
1489
  uniques.each do |att, val|
1473
- db.hset(model.key[:uniques][att], val, id)
1490
+ unique = model.key[:uniques][att]
1491
+
1492
+ db.hset(unique, val, id)
1493
+ db.hset(key[:_uniques], unique, val)
1474
1494
  end
1475
1495
  end
1476
1496
 
1477
- def _delete_uniques(atts)
1478
- model.uniques.each do |att|
1479
- db.hdel(model.key[:uniques][att], atts[att.to_s])
1497
+ def _delete_uniques(_uniques)
1498
+ _uniques.each do |unique, val|
1499
+ db.hdel(unique, val)
1500
+ db.hdel(key[:_uniques], unique)
1480
1501
  end
1481
1502
  end
1482
1503
 
1483
- def _delete_indices(atts)
1484
- model.indices.each do |att|
1485
- val = atts[att.to_s]
1486
-
1487
- db.srem(model.key[:indices][att][val], id)
1504
+ def _delete_indices(_indices)
1505
+ _indices.each do |index|
1506
+ db.srem(index, id)
1507
+ db.srem(key[:_indices], index)
1488
1508
  end
1509
+ # model.indices.each do |att|
1510
+ # val = atts[att.to_s]
1511
+
1512
+ # db.srem(model.key[:indices][att][val], id)
1513
+ # end
1489
1514
  end
1490
1515
 
1491
1516
  def _save_indices(indices)
1492
1517
  indices.each do |att, val|
1493
1518
  model.toindices(att, val).each do |index|
1494
1519
  db.sadd(index, id)
1520
+ db.sadd(key[:_indices], index)
1495
1521
  end
1496
1522
  end
1497
1523
  end
@@ -95,3 +95,35 @@ test "allow indexing by an arbitrary attribute" do
95
95
  assert [@user1, @user2] == gmail.sort_by { |u| u.id }
96
96
  assert [@user3] == User.find(:email_provider => "yahoo.com").to_a
97
97
  end
98
+
99
+ scope do
100
+ # Just to give more context around this bug, basically it happens
101
+ # when you define a virtual unique or index.
102
+ #
103
+ # Previously it was unable to cleanup the indices mainly because
104
+ # it relied on the attributes being set.
105
+ class Node < Ohm::Model
106
+ index :available
107
+ attribute :capacity
108
+
109
+ unique :available
110
+
111
+ def available
112
+ capacity.to_i <= 90
113
+ end
114
+ end
115
+
116
+ test "index bug" do
117
+ n = Node.create
118
+ n.update(capacity: 91)
119
+
120
+ assert_equal 0, Node.find(available: true).size
121
+ end
122
+
123
+ test "uniques bug" do
124
+ n = Node.create
125
+ n.update(capacity: 91)
126
+
127
+ assert_equal nil, Node.with(:available, true)
128
+ end
129
+ end
@@ -347,7 +347,8 @@ test "be no leftover keys" do
347
347
  assert_equal [], Ohm.redis.keys("*")
348
348
 
349
349
  Foo.create(:name => "Bar")
350
- expected = %w[Foo:1 Foo:all Foo:id Foo:indices:name:Bar]
350
+ expected = %w[Foo:1 Foo:1:_indices Foo:all Foo:id Foo:indices:name:Bar]
351
+
351
352
  assert expected.sort == Ohm.redis.keys("*").sort
352
353
 
353
354
  Foo[1].delete
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-05-03 00:00:00.000000000 Z
14
+ date: 2012-05-25 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: redis
18
- requirement: &2155998060 !ruby/object:Gem::Requirement
18
+ requirement: !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ~>
@@ -23,10 +23,15 @@ dependencies:
23
23
  version: '2.2'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *2155998060
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: '2.2'
27
32
  - !ruby/object:Gem::Dependency
28
33
  name: nest
29
- requirement: &2155997480 !ruby/object:Gem::Requirement
34
+ requirement: !ruby/object:Gem::Requirement
30
35
  none: false
31
36
  requirements:
32
37
  - - ~>
@@ -34,10 +39,15 @@ dependencies:
34
39
  version: '1.0'
35
40
  type: :runtime
36
41
  prerelease: false
37
- version_requirements: *2155997480
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
38
48
  - !ruby/object:Gem::Dependency
39
49
  name: scrivener
40
- requirement: &2155996820 !ruby/object:Gem::Requirement
50
+ requirement: !ruby/object:Gem::Requirement
41
51
  none: false
42
52
  requirements:
43
53
  - - ~>
@@ -45,10 +55,15 @@ dependencies:
45
55
  version: 0.0.3
46
56
  type: :runtime
47
57
  prerelease: false
48
- version_requirements: *2155996820
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ version: 0.0.3
49
64
  - !ruby/object:Gem::Dependency
50
65
  name: cutest
51
- requirement: &2155996160 !ruby/object:Gem::Requirement
66
+ requirement: !ruby/object:Gem::Requirement
52
67
  none: false
53
68
  requirements:
54
69
  - - ~>
@@ -56,7 +71,12 @@ dependencies:
56
71
  version: '0.1'
57
72
  type: :development
58
73
  prerelease: false
59
- version_requirements: *2155996160
74
+ version_requirements: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ version: '0.1'
60
80
  description: Ohm is a library that allows to store an object in Redis, a persistent
61
81
  key-value database. It includes an extensible list of validations and has very good
62
82
  performance.
@@ -115,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
135
  version: '0'
116
136
  requirements: []
117
137
  rubyforge_project: ohm
118
- rubygems_version: 1.8.11
138
+ rubygems_version: 1.8.23
119
139
  signing_key:
120
140
  specification_version: 3
121
141
  summary: Object-hash mapping library for Redis.