flockdb 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,97 @@
1
+ module Flock
2
+ class SelectOperations
3
+ EMPTY = Flock::Operation.new do |page|
4
+ [[], Flock::CursorEnd, Flock::CursorEnd]
5
+ end
6
+
7
+ [:each, :to_ary, :size, :first].each do |method|
8
+ class_eval("def #{method}(*args, &block); operation.#{method}(*args, &block) end", __FILE__, __LINE__)
9
+ end
10
+
11
+ include Enumerable
12
+
13
+ def initialize(client)
14
+ @client = client
15
+ @service = client.service
16
+ @selecticons = []
17
+ end
18
+
19
+ def select(*query)
20
+ selecticon = Selecticon.new(Flock::QueryTerm.new(query, @client.graphs))
21
+ @selecticons << selecticon
22
+ selecticon
23
+ end
24
+
25
+ def to_thrift_edges
26
+ @selecticons.map do |selecticon|
27
+ selecticon.to_thrift_edges
28
+ end
29
+ end
30
+
31
+ def to_thrift_ids
32
+ @selecticons.map do |selecticon|
33
+ selecticon.to_thrift_ids
34
+ end
35
+ end
36
+
37
+ def operation
38
+ if @selecticons.any?
39
+ @service.select2(to_thrift_ids).map do |results|
40
+ # Note: pagination will not work for now.
41
+ Flock::Operation.new do |page|
42
+ [results.ids.unpack("Q*"), Flock::CursorEnd, Flock::CursorEnd]
43
+ end
44
+ end
45
+ else
46
+ EMPTY
47
+ end
48
+ end
49
+
50
+ def edges
51
+ if @selecticons.any?
52
+ @service.select_edges(to_thrift_edges).map do |results|
53
+ # Note: pagination will not work for now.
54
+ Flock::Operation.new do |page|
55
+ [results.edges, Flock::CursorEnd, Flock::CursorEnd]
56
+ end
57
+ end
58
+ else
59
+ EMPTY
60
+ end
61
+ end
62
+
63
+ class Selecticon
64
+ def initialize(term)
65
+ @term = term
66
+ @count, @cursor = 20, Flock::CursorStart
67
+ end
68
+
69
+ def paginate(count, cursor = Flock::CursorStart)
70
+ @count, @cursor = count, cursor
71
+ end
72
+
73
+ def to_thrift_ids
74
+ query = Edges::SelectQuery.new
75
+ operation = Edges::SelectOperation.new
76
+ operation.operation_type = Edges::SelectOperationType::SimpleQuery
77
+ operation.term = @term.to_thrift
78
+ query.operations = [operation]
79
+ page = Flock::Page.new
80
+ page.cursor = @cursor
81
+ page.count = @count
82
+ query.page = page
83
+ query
84
+ end
85
+
86
+ def to_thrift_edges
87
+ query = Edges::EdgeQuery.new
88
+ query.term = @term.to_thrift
89
+ page = Flock::Page.new
90
+ page.cursor = @cursor
91
+ page.count = @count
92
+ query.page = page
93
+ query
94
+ end
95
+ end
96
+ end
97
+ end
@@ -2,15 +2,30 @@ module Flock
2
2
  class SimpleOperation < SelectOperation
3
3
  def initialize(client, query)
4
4
  super(client)
5
- @query = query
5
+ @term = query
6
+ end
7
+
8
+ def edges
9
+ Flock::Operation.new do |page|
10
+ query = to_thrift_edges
11
+ query.page = page
12
+ result = @service.select_edges(Array(query)).first
13
+ [result.edges, result.next_cursor, result.prev_cursor]
14
+ end
6
15
  end
7
16
 
8
17
  def to_thrift
9
18
  operation = Edges::SelectOperation.new
10
19
  operation.operation_type = Edges::SelectOperationType::SimpleQuery
11
- operation.term = QueryTerm.new(@query).to_thrift
20
+ operation.term = @term.to_thrift
12
21
 
13
22
  Array(operation)
14
23
  end
24
+
25
+ def to_thrift_edges
26
+ query = Edges::EdgeQuery.new
27
+ query.term = @term.to_thrift
28
+ query
29
+ end
15
30
  end
16
31
  end
@@ -10,7 +10,7 @@ module Flock
10
10
  servers = Array(servers)
11
11
  end
12
12
 
13
- super(Edges::FlockDB::Client, servers, DEFAULTS.merge(options))
13
+ super(Edges::Client, servers, DEFAULTS.merge(options))
14
14
  end
15
15
  end
16
16
  end
@@ -0,0 +1,3 @@
1
+ module Flock
2
+ VERSION = '0.7.0'
3
+ end
@@ -3,9 +3,8 @@ require 'spec_helper'
3
3
  describe Flock::ExecuteOperations do
4
4
  describe "#negate" do
5
5
  it "should create a negate opperation" do
6
- service = Flock::MockService.new
7
- mock(Flock::ExecuteOperation).new(Flock::Edges::ExecuteOperationType::Negate, [1,1,3], nil)
8
- operation = Flock::ExecuteOperations.new(service, nil)
6
+ mock(Flock::ExecuteOperation).new(Flock::Edges::ExecuteOperationType::Negate, Flock::QueryTerm.new([1,1,3]), nil)
7
+ operation = Flock::ExecuteOperations.new(Flock::Client.new(Flock::MockService), nil)
9
8
  operation.negate(1,1,3)
10
9
  end
11
10
  end
@@ -26,8 +25,8 @@ describe Flock::ExecuteOperations do
26
25
  service = Flock::MockService.new
27
26
  client = Flock::Client.new(service)
28
27
  now = Time.now.to_i
29
- thing = Flock::ExecuteOperation.new(1, [1, 1, 3], 12345)
30
- mock(Flock::ExecuteOperation).new(1, [1, 1, 3], 12345) { thing}
28
+ thing = Flock::ExecuteOperation.new(1, Flock::QueryTerm.new([1, 1, 3]), 12345)
29
+ mock(Flock::ExecuteOperation).new(1, Flock::QueryTerm.new([1, 1, 3]), 12345) { thing}
31
30
  client.add(1, 1, 3, Flock::Priority::High, :position => 12345)
32
31
  end
33
32
  end
@@ -145,6 +145,22 @@ describe Flock do
145
145
  end
146
146
  end
147
147
 
148
+ describe "positions" do
149
+ it "works" do
150
+ flock.add(4,5,6,:position => 7)
151
+ flock.get(4,5,6).position.should == 7
152
+ end
153
+
154
+ it "behaves like the server, not moving position on double adds, but moving position on delete-add" do
155
+ flock.add(4,5,6,:position => 7)
156
+ flock.add(4,5,6,:position => 8)
157
+ flock.get(4,5,6).position.should == 7
158
+ flock.remove(4,5,6)
159
+ flock.add(4,5,6,:position => 8)
160
+ flock.get(4,5,6).position.should == 8
161
+ end
162
+ end
163
+
148
164
  describe 'select' do
149
165
  attr_accessor :query
150
166
 
@@ -165,6 +181,53 @@ describe Flock do
165
181
  flock.select(user, 1, nil).to_a.sort.should == [2, 3]
166
182
  end
167
183
 
184
+ it "supports selecting states" do
185
+ flock.remove(5,1,1)
186
+
187
+ flock.select(5, 1, nil, 0).to_a.sort.should == []
188
+ flock.select(5, 1, nil, :positive).to_a.sort.should == []
189
+ flock.select([5, 1, nil, :positive]).to_a.sort.should == []
190
+ flock.select(5, 1, nil, 1).to_a.sort.should == [1]
191
+ flock.select(5, 1, nil, :removed).to_a.sort.should == [1]
192
+ flock.select([5, 1, nil, :removed]).to_a.sort.should == [1]
193
+ flock.select(5, 1, nil, 2).to_a.sort.should == []
194
+ flock.select(5, 1, nil, :archived).to_a.sort.should == []
195
+ flock.select([5, 1, nil, :archived]).to_a.sort.should == []
196
+ flock.select(5, 1, nil, 0, 1, 2).to_a.sort.should == [1]
197
+ flock.select(5, 1, nil, [0, 1, 2]).to_a.sort.should == [1]
198
+ flock.select(5, 1, nil, [:positive, :removed, :archived]).to_a.sort.should == [1]
199
+ flock.select([5, 1, nil, [:positive, :removed, :archived]]).to_a.sort.should == [1]
200
+ end
201
+
202
+ it 'supports rad graphs' do
203
+ flock.select(1, :schmaph, nil).to_a.sort.should == [2, 3]
204
+ end
205
+
206
+ describe 'edges' do
207
+ it 'works' do
208
+ time = Time.now
209
+ stub(Time).now { time }
210
+
211
+ edge1 = Flock::Edges::Edge.new
212
+ edge1.source_id = 1
213
+ edge1.destination_id = 2
214
+ edge1.position = Time.now.to_i
215
+ edge1.updated_at = Time.now.to_i
216
+ edge1.count = 1
217
+ edge1.state_id = 0
218
+
219
+ edge2 = Flock::Edges::Edge.new
220
+ edge2.source_id = 1
221
+ edge2.destination_id = 3
222
+ edge2.position = Time.now.to_i
223
+ edge2.updated_at = Time.now.to_i
224
+ edge2.count = 1
225
+ edge2.state_id = 0
226
+
227
+ flock.select(1,1,nil).edges.to_a.should == [edge1, edge2]
228
+ end
229
+ end
230
+
168
231
  describe 'to_a' do
169
232
  it 'works' do
170
233
  flock.select(1,1,nil).to_a.sort.should == [2, 3]
@@ -214,5 +277,56 @@ describe Flock do
214
277
  end
215
278
  end
216
279
  end
280
+
281
+ describe 'multi' do
282
+ it 'works' do
283
+ results = flock.multi do |m|
284
+ m.select(1,1,nil)
285
+ m.select(nil,1,1)
286
+ end
287
+ first, second = results
288
+ first.to_a.should == [2, 3]
289
+ second.to_a.should == [2, 4]
290
+ end
291
+
292
+ it 'works with empty multis' do
293
+ results = flock.multi { |m| }
294
+ results.to_a.should == []
295
+ end
296
+
297
+ describe 'edges' do
298
+ it 'works' do
299
+ time = Time.now
300
+ stub(Time).now { time }
301
+
302
+ prototype = Flock::Edges::Edge.new
303
+ prototype.position = Time.now.to_i
304
+ prototype.updated_at = Time.now.to_i
305
+ prototype.count = 1
306
+ prototype.state_id = 0
307
+
308
+ results = flock.multi do |m|
309
+ m.select(1,1,nil)
310
+ m.select(nil,1,1)
311
+ end.edges
312
+ first, second = results
313
+
314
+ a = prototype.dup
315
+ a.source_id, a.destination_id = 1, 2
316
+
317
+ b = prototype.dup
318
+ b.source_id, b.destination_id = 1, 3
319
+
320
+ first.to_a.should == [a, b]
321
+
322
+ c = prototype.dup
323
+ c.destination_id, c.source_id = 2, 1
324
+
325
+ d = prototype.dup
326
+ d.destination_id, d.source_id = 4, 1
327
+ second.to_a.should == [c, d]
328
+ end
329
+ end
330
+ end
217
331
  end
218
332
  end
@@ -2,8 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe Flock::QueryTerm do
4
4
  describe '#new' do
5
- it 'requires a query of length 4' do
6
- lambda { Flock::QueryTerm.new([1, 1, 1, 1, 1]) }.should raise_error(ArgumentError)
5
+ it 'requires a query of length >= 3' do
6
+ lambda { Flock::QueryTerm.new([1, 1]) }.should raise_error(ArgumentError)
7
7
  end
8
8
 
9
9
  it 'has a source, graph, destination, and states' do
@@ -62,4 +62,16 @@ describe Flock::QueryTerm do
62
62
  @term.to_thrift.state_ids.should == [Flock::Edges::EdgeState::Positive, Flock::Edges::EdgeState::Negative]
63
63
  end
64
64
  end
65
+
66
+ describe 'when given symbols' do
67
+ it 'works for graphs' do
68
+ expected = Flock::QueryTerm.new([1, 1, 2, [Flock::Edges::EdgeState::Positive, Flock::Edges::EdgeState::Negative]])
69
+ qt = Flock::QueryTerm.new([1, :follows, 2, [Flock::Edges::EdgeState::Positive, Flock::Edges::EdgeState::Negative]], :follows => 1)
70
+ end
71
+
72
+ it 'works for states' do
73
+ expected = Flock::QueryTerm.new([1, 1, 2, [Flock::Edges::EdgeState::Positive, Flock::Edges::EdgeState::Negative]])
74
+ qt = Flock::QueryTerm.new([1, :follows, 2, :positive, :negative], :follows => 1)
75
+ end
76
+ end
65
77
  end
@@ -3,9 +3,8 @@ require 'spec_helper'
3
3
  describe Flock::ExecuteOperations do
4
4
  describe "#negate" do
5
5
  it "should create a negate operation" do
6
- service = Flock::MockService.new
7
- mock(Flock::ExecuteOperation).new(Flock::Edges::ExecuteOperationType::Negate, [1,1,3], nil)
8
- operation = Flock::ExecuteOperations.new(Flock::Client.new(service), nil)
6
+ mock(Flock::ExecuteOperation).new(Flock::Edges::ExecuteOperationType::Negate, Flock::QueryTerm.new([1,1,3]), nil)
7
+ operation = Flock::ExecuteOperations.new(Flock::Client.new(Flock::MockService), nil)
9
8
  operation.negate(1,1,3)
10
9
  end
11
10
  end
@@ -17,5 +17,5 @@ Spec::Runner.configure do |config|
17
17
  end
18
18
 
19
19
  def new_flock_client
20
- Flock.new($mock_service)
20
+ Flock.new($mock_service, :graphs => { :schmaph => 1 })
21
21
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flockdb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
5
- prerelease: false
4
+ hash: 3
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 1
10
- version: 0.6.1
8
+ - 7
9
+ - 0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Freels
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-02-28 00:00:00 -08:00
20
+ date: 2011-06-16 00:00:00 -07:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -53,9 +53,57 @@ dependencies:
53
53
  type: :runtime
54
54
  version_requirements: *id002
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
56
+ name: bundler
57
57
  prerelease: false
58
58
  requirement: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 1
66
+ - 0
67
+ - 10
68
+ version: 1.0.10
69
+ type: :development
70
+ version_requirements: *id003
71
+ - !ruby/object:Gem::Dependency
72
+ name: rake
73
+ prerelease: false
74
+ requirement: &id004 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - "="
78
+ - !ruby/object:Gem::Version
79
+ hash: 49
80
+ segments:
81
+ - 0
82
+ - 8
83
+ - 7
84
+ version: 0.8.7
85
+ type: :development
86
+ version_requirements: *id004
87
+ - !ruby/object:Gem::Dependency
88
+ name: rspec
89
+ prerelease: false
90
+ requirement: &id005 !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ~>
94
+ - !ruby/object:Gem::Version
95
+ hash: 27
96
+ segments:
97
+ - 1
98
+ - 3
99
+ - 0
100
+ version: 1.3.0
101
+ type: :development
102
+ version_requirements: *id005
103
+ - !ruby/object:Gem::Dependency
104
+ name: diff-lcs
105
+ prerelease: false
106
+ requirement: &id006 !ruby/object:Gem::Requirement
59
107
  none: false
60
108
  requirements:
61
109
  - - ">="
@@ -65,11 +113,11 @@ dependencies:
65
113
  - 0
66
114
  version: "0"
67
115
  type: :development
68
- version_requirements: *id003
116
+ version_requirements: *id006
69
117
  - !ruby/object:Gem::Dependency
70
118
  name: rr
71
119
  prerelease: false
72
- requirement: &id004 !ruby/object:Gem::Requirement
120
+ requirement: &id007 !ruby/object:Gem::Requirement
73
121
  none: false
74
122
  requirements:
75
123
  - - ">="
@@ -79,7 +127,7 @@ dependencies:
79
127
  - 0
80
128
  version: "0"
81
129
  type: :development
82
- version_requirements: *id004
130
+ version_requirements: *id007
83
131
  description: Get your flock on in Ruby.
84
132
  email: freels@twitter.com
85
133
  executables: []
@@ -87,14 +135,15 @@ executables: []
87
135
  extensions: []
88
136
 
89
137
  extra_rdoc_files:
90
- - LICENSE
91
138
  - README.md
139
+ - LICENSE
92
140
  files:
93
141
  - .gitignore
142
+ - Gemfile
143
+ - Gemfile.lock
94
144
  - LICENSE
95
145
  - README.md
96
146
  - Rakefile
97
- - VERSION
98
147
  - flockdb.gemspec
99
148
  - lib/flock.rb
100
149
  - lib/flock/client.rb
@@ -109,9 +158,11 @@ files:
109
158
  - lib/flock/operations/execute_operations.rb
110
159
  - lib/flock/operations/query_term.rb
111
160
  - lib/flock/operations/select_operation.rb
161
+ - lib/flock/operations/select_operations.rb
112
162
  - lib/flock/operations/simple_operation.rb
113
163
  - lib/flock/service.rb
114
164
  - lib/flock/thrift.rb
165
+ - lib/flock/version.rb
115
166
  - lib/flockdb.rb
116
167
  - spec/execute_operations_spec.rb
117
168
  - spec/flock_spec.rb
@@ -121,7 +172,7 @@ files:
121
172
  - spec/spec.opts
122
173
  - spec/spec_helper.rb
123
174
  has_rdoc: true
124
- homepage: http://github.com/twitter/flockdb-client
175
+ homepage: http://github.com/twitter/flock-client
125
176
  licenses: []
126
177
 
127
178
  post_install_message:
@@ -149,15 +200,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
200
  version: "0"
150
201
  requirements: []
151
202
 
152
- rubyforge_project:
153
- rubygems_version: 1.3.7
203
+ rubyforge_project: flock-client
204
+ rubygems_version: 1.6.2
154
205
  signing_key:
155
206
  specification_version: 3
156
- summary: Ruby Flock client
207
+ summary: Get your flock on in Ruby
157
208
  test_files:
158
209
  - spec/execute_operations_spec.rb
159
210
  - spec/flock_spec.rb
160
211
  - spec/mock_service_spec.rb
161
212
  - spec/query_term_spec.rb
162
213
  - spec/simple_operation_spec.rb
214
+ - spec/spec.opts
163
215
  - spec/spec_helper.rb