activetokyocabinet 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -34,7 +34,7 @@ module ActiveRecord
34
34
  end
35
35
  end
36
36
 
37
- rcols['id'] = rkey
37
+ rcols['id'] = rkey.to_i
38
38
  rows << rcols
39
39
  end
40
40
  end
@@ -107,8 +107,8 @@ def tcordertype(type)
107
107
  case type.upcase
108
108
  when 'ASC', 'STRASC'
109
109
  :QOSTRASC
110
- when 'DESC', 'STRASC'
111
- :QOSTRASC
110
+ when 'DESC', 'STRDESC'
111
+ :QOSTRDESC
112
112
  when 'NUMASC'
113
113
  :QONUMASC
114
114
  when 'NUMDESC'
@@ -288,8 +288,8 @@ def tcordertype(type)
288
288
  case type.upcase
289
289
  when 'ASC', 'STRASC'
290
290
  :QOSTRASC
291
- when 'DESC', 'STRASC'
292
- :QOSTRASC
291
+ when 'DESC', 'STRDESC'
292
+ :QOSTRDESC
293
293
  when 'NUMASC'
294
294
  :QONUMASC
295
295
  when 'NUMDESC'
@@ -1,20 +1,20 @@
1
- module ActiveTokyoCabinet
2
- module TDB
3
- def self.included(mod)
4
- {:string => :to_s, :int => :to_i, :float => :to_s}.each do |type, conv|
5
- mod.instance_eval %{
6
- def #{type}(name)
7
- unless @columns
8
- primary_key = ActiveRecord::ConnectionAdapters::Column.new('id', nil)
9
- primary_key.primary = true
10
- @columns = [primary_key]
11
- end
12
-
13
- @columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, nil)
14
- class_eval "def \#{name}; v = self[:\#{name}]; v && v.#{conv}; end"
15
- end
16
- }
17
- end
18
- end
19
- end
20
- end
1
+ module ActiveTokyoCabinet
2
+ module TDB
3
+ def self.included(mod)
4
+ {:string => :to_s, :int => :to_i, :float => :to_f}.each do |type, conv|
5
+ mod.instance_eval %{
6
+ def #{type}(name)
7
+ unless @columns
8
+ primary_key = ActiveRecord::ConnectionAdapters::Column.new('id', nil)
9
+ primary_key.primary = true
10
+ @columns = [primary_key]
11
+ end
12
+
13
+ @columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, nil)
14
+ class_eval "def \#{name}; v = self[:\#{name}]; (v.nil? || v.empty?) ? nil : v.#{conv}; end"
15
+ end
16
+ }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,260 @@
1
+ $wd = File.dirname(File.expand_path(__FILE__))
2
+ ENV['RUBYLIB'] = "#{$wd}/../lib"
3
+
4
+ require 'spec_helper'
5
+ include SpecHelper
6
+ include ActiveTokyoCabinetSpec
7
+
8
+ describe 'tokyocabinet:' do
9
+ before do
10
+ TokyoCabinetSpec.establish_connection
11
+ TokyoCabinetSpec.create_tables
12
+ TokyoCabinetSpec.setup_employee
13
+ TokyoCabinetSpec.setup_department
14
+ end
15
+
16
+ it "employees length > 0" do
17
+ employees = Employee.find(:all)
18
+ employees.length.should == employee_data.length
19
+ end
20
+
21
+ it "employees has a one data (ename = 'SMITH')" do
22
+ employees = Employee.find(:all, :conditions => ['ename = ?', 'SMITH'])
23
+ employees.length.should == 1
24
+ data = employee_data.find {|i| i[EMP_ENAME] == 'SMITH' }
25
+ validate_employee(data, employees[0])
26
+ end
27
+
28
+ it "employees has a one data (empno = 7521)" do
29
+ employees = Employee.find(:all, :conditions => ['empno = ?', 7521])
30
+ employees.length.should == 1
31
+ data = employee_data.find {|i| i[EMP_EMPNO] == 7521 }
32
+ validate_employee(data, employees[0])
33
+ end
34
+
35
+ it "employees has no data (ename = 'SMITH' and job = 'SALESMAN')" do
36
+ employees = Employee.find(:all, :conditions => ['ename = ? and job = ?', 'SMITH', 'SALESMAN'])
37
+ employees.should be_empty
38
+ end
39
+
40
+ it "employees a one data (ename = 'TURNER' and job = 'SALESMAN')" do
41
+ employees = Employee.find(:all, :conditions => ['ename = ? and job = ?', 'TURNER', 'SALESMAN'])
42
+ employees.length.should == 1
43
+ data = employee_data.find {|i| i[EMP_ENAME] == 'TURNER' and i[EMP_JOB] == 'SALESMAN' }
44
+ validate_employee(data, employees[0])
45
+ end
46
+
47
+ it "employees a one data ({:ename => 'TURNER', :job => 'SALESMAN'})" do
48
+ employees = Employee.find(:all, :conditions => {:ename => 'TURNER', :job => 'SALESMAN'})
49
+ employees.length.should == 1
50
+ data = employee_data.find {|i| i[EMP_ENAME] == 'TURNER' and i[EMP_JOB] == 'SALESMAN' }
51
+ validate_employee(data, employees[0])
52
+ end
53
+
54
+ it "employees has any data" do
55
+ employee_data.each_with_index do |data, i|
56
+ employee_id = i + 1
57
+ employee = Employee.find(employee_id)
58
+
59
+ employee.should_not be_nil
60
+ employee.id.should == employee_id
61
+ validate_employee(data, employee)
62
+ end
63
+ end
64
+
65
+ it "employees has any data (job = 'SALESMAN')" do
66
+ employees = Employee.find(:all, :conditions => ['job = ?', 'SALESMAN'])
67
+
68
+ employees.each do |employee|
69
+ data = employee_data[employee.id - 1]
70
+
71
+ data.should_not be_nil
72
+ data[EMP_JOB].should == 'SALESMAN'
73
+ validate_employee(data, employee)
74
+ end
75
+ end
76
+
77
+ it "employees has any data (id=1,2,3)" do
78
+ employees = Employee.find([1, 2, 3])
79
+ employees.length.should == 3
80
+
81
+ employee_data[0..2].each_with_index do |data, i|
82
+ empno, ename, job, mgr, hiredate, sal, comm, deptno = data
83
+ employee_id = i + 1
84
+ employee = employees.find {|i| i.id == employee_id }
85
+
86
+ employee.should_not be_nil
87
+ employee.id.should == employee_id
88
+ validate_employee(data, employee)
89
+ end
90
+ end
91
+
92
+ it "employees has any data (order by ename desc limit 3)" do
93
+ employees = Employee.find(:all, :order => 'ename desc', :limit => 3)
94
+ employees.length.should == 3
95
+
96
+ employee_data.sort_by {|i| i[EMP_ENAME] || '' }.reverse[0..2].each do |data|
97
+ employee = employees.find {|i| i.id == data.id }
98
+ employee.should_not be_nil
99
+ validate_employee(data, employee)
100
+ end
101
+ end
102
+
103
+ it "employees has any data (order by ename desc limit 4 offset 5)" do
104
+ employees = Employee.find(:all, :order => 'ename desc', :limit => 4, :offset => 5)
105
+ employees.length.should == 4
106
+
107
+ employee_data.sort_by {|i| i[EMP_ENAME] || '' }.reverse[5..8].each do |data|
108
+ employee = employees.find {|i| i.id == data.id }
109
+ employee.should_not be_nil
110
+ validate_employee(data, employee)
111
+ end
112
+ end
113
+
114
+ it "employees has any data ([])" do
115
+ employee_data.each_with_index do |data, i|
116
+ empno, ename, job, mgr, hiredate, sal, comm, deptno = data
117
+ employee_id = i + 1
118
+ employee = Employee.find(employee_id)
119
+
120
+ employee.should_not be_nil
121
+ employee[:id].should == employee_id
122
+ employee[:empno].should == empno.to_s
123
+ employee[:ename].should == ename.to_s
124
+ employee[:job].should == job.to_s
125
+ employee[:mgr].should == mgr.to_s
126
+ employee[:hiredate].should == hiredate.to_s
127
+ employee[:sal].should == sal.to_s
128
+ employee[:comm].should == comm.to_s
129
+ employee[:deptno].should == deptno.to_s
130
+ end
131
+ end
132
+
133
+ # -------------------------------------------------------------------
134
+
135
+ it "departments length > 0" do
136
+ departments = Department.find(:all)
137
+ departments.length.should == department_data.length
138
+ end
139
+
140
+ it "departments has a one data (dname = 'SALES')" do
141
+ departments = Department.find(:all, :conditions => ['dname = ?', 'SALES'])
142
+ departments.length.should == 1
143
+ data = department_data.find {|i| i[DEPT_DNAME] == 'SALES' }
144
+ validate_department(data, departments[0])
145
+ end
146
+
147
+ it "departments has a one data (deptno = 20)" do
148
+ departments = Department.find(:all, :conditions => ['deptno = ?', 20])
149
+ departments.length.should == 1
150
+ data = department_data.find {|i| i[DEPT_DEPTNO] == 20 }
151
+ validate_department(data, departments[0])
152
+ end
153
+
154
+ it "departments has no data (deptno = 20 and loc = 'BOSTON')" do
155
+ departments = Department.find(:all, :conditions => ['deptno = ? and loc = ?', 20, 'BOSTON'])
156
+ departments.should be_empty
157
+ end
158
+
159
+ it "departments has a one data (deptno = 40 and loc = 'BOSTON')" do
160
+ departments = Department.find(:all, :conditions => ['deptno = ? and loc = ?', 40, 'BOSTON'])
161
+ departments.length.should == 1
162
+ data = department_data.find {|i| i[DEPT_DEPTNO] == 40 and i[DEPT_LOC] == 'BOSTON' }
163
+ validate_department(data, departments[0])
164
+ end
165
+
166
+ it "departments has a one data ({:deptno => 40. :loc => 'BOSTON'})" do
167
+ departments = Department.find(:all, :conditions => {:deptno => 40, :loc => 'BOSTON'})
168
+ departments.length.should == 1
169
+ data = department_data.find {|i| i[DEPT_DEPTNO] == 40 and i[DEPT_LOC] == 'BOSTON' }
170
+ validate_department(data, departments[0])
171
+ end
172
+
173
+ it "departments has any data" do
174
+ department_data.each_with_index do |data, i|
175
+ department_id = i + 1
176
+ department = Department.find(department_id)
177
+
178
+ department.should_not be_nil
179
+ department.id.should == department_id
180
+ validate_department(data, department)
181
+ end
182
+ end
183
+
184
+ it "departments has any data ([])" do
185
+ department_data.each_with_index do |data, i|
186
+ deptno, dname, loc = data
187
+ department_id = i + 1
188
+ department = Department.find(department_id)
189
+
190
+ department.should_not be_nil
191
+ department[:id].should == department_id
192
+ department[:deptno].should == deptno.to_s
193
+ department[:dname].should == dname.to_s
194
+ department[:loc].should == loc.to_s
195
+ end
196
+ end
197
+
198
+ it "department has any data (loc in ('NEW YORK', 'CHICAGO'))" do
199
+ departments = Department.find(:all, :conditions => ['loc in (?)', ['NEW YORK', 'CHICAGO']])
200
+
201
+ departments.each do |department|
202
+ data = department_data[department.id - 1]
203
+
204
+ data.should_not be_nil
205
+ validate_department(data, department)
206
+ end
207
+ end
208
+
209
+ it "department has any data ({:loc => ['NEW YORK', 'CHICAGO']})" do
210
+ departments = Department.find(:all, :conditions => {:loc => ['NEW YORK', 'CHICAGO']})
211
+
212
+ departments.each do |department|
213
+ data = department_data[department.id - 1]
214
+
215
+ data.should_not be_nil
216
+ ['NEW YORK', 'CHICAGO'].should include(data[DEPT_LOC])
217
+ validate_department(data, department)
218
+ end
219
+ end
220
+
221
+ it "departments has any data (id=1,2,3)" do
222
+ departments = Department.find([1, 2, 3])
223
+ departments.length.should == 3
224
+
225
+ department_data[0..2].each_with_index do |data, i|
226
+ deptno, dname, loc = data
227
+ department_id = i + 1
228
+ department = departments.find {|i| i.id == department_id }
229
+
230
+ department.should_not be_nil
231
+ validate_department(data, department)
232
+ end
233
+ end
234
+
235
+ it "departments has any data (order by deptno numdesc limit 65535 offset 1)" do
236
+ departments = Department.find(:all, :order => 'deptno numdesc', :limit => 65535, :offset => 1)
237
+ departments.length.should == department_data.length - 1
238
+
239
+ department_data.sort_by {|i| i[DEPT_DEPTNO] || 0 }.reverse[1..-1].each do |data|
240
+ department = departments.find {|i| i.id == data.id }
241
+ department.should_not be_nil
242
+ validate_department(data, department)
243
+ end
244
+ end
245
+
246
+ it "departments has any data (order by deptno numasc)" do
247
+ departments = Department.find(:all, :order => 'deptno numasc')
248
+ departments.length.should == department_data.length
249
+
250
+ department_data.sort_by {|i| i[DEPT_DEPTNO] || 0 }.each do |data|
251
+ department = departments.find {|i| i.id == data.id }
252
+ department.should_not be_nil
253
+ validate_department(data, department)
254
+ end
255
+ end
256
+
257
+ after do
258
+ TokyoCabinetSpec.clean
259
+ end
260
+ end
@@ -0,0 +1,7 @@
1
+ class Department < ActiveRecord::Base
2
+ include ActiveTokyoCabinet::TDB
3
+
4
+ int :deptno
5
+ string :dname
6
+ string :loc
7
+ end
@@ -0,0 +1,12 @@
1
+ class Employee < ActiveRecord::Base
2
+ include ActiveTokyoCabinet::TDB
3
+
4
+ int :empno
5
+ string :ename
6
+ string :job
7
+ int :mgr
8
+ string :hiredate
9
+ float :sal
10
+ float :comm
11
+ int :deptno
12
+ end
@@ -0,0 +1,196 @@
1
+ require 'rubygems'
2
+ require 'fileutils'
3
+ require 'logger'
4
+ require 'pp'
5
+ require 'tokyocabinet'
6
+ require 'tokyotyrant'
7
+ require 'active_record'
8
+ require 'active_tokyocabinet/tdb'
9
+ require "#{$wd}/models/employee"
10
+ require "#{$wd}/models/department"
11
+
12
+ ActiveRecord::Base.logger = Logger.new($stderr)
13
+ ActiveRecord::Base.logger.level = Logger::INFO
14
+
15
+ module SpecHelper
16
+ EMP_EMPNO = 0
17
+ EMP_ENAME = 1
18
+ EMP_JOB = 2
19
+ EMP_MGR = 3
20
+ EMP_HIREDATE = 4
21
+ EMP_SAL = 5
22
+ EMP_COMM = 6
23
+ EMP_DEPTNO = 7
24
+
25
+ DEPT_DEPTNO = 0
26
+ DEPT_DNAME = 1
27
+ DEPT_LOC = 2
28
+
29
+ def show_sql
30
+ begin
31
+ ActiveRecord::Base.logger.level = Logger::DEBUG
32
+ yield
33
+ ensure
34
+ ActiveRecord::Base.logger.level = Logger::INFO
35
+ end
36
+ end
37
+
38
+ def desc(klass, out = $stdout)
39
+ if klass.kind_of?(Array)
40
+ rows = klass
41
+ else
42
+ rows = klass.find(:all)
43
+ end
44
+
45
+ if rows.empty?
46
+ out.puts "Empty set"
47
+ return
48
+ end
49
+
50
+ if klass.kind_of?(Array)
51
+ klass = rows.first.class
52
+ end
53
+
54
+ cols = {}
55
+
56
+ klass.instance_variable_get(:@columns).each do |col|
57
+ cols[col.name] = col.name.to_s.length
58
+ end
59
+
60
+ rows.each do |row|
61
+ row.attributes.each do |col, val|
62
+ val = val.to_s
63
+ cols[col] = val.length if val.length > cols[col]
64
+ end
65
+ end
66
+
67
+ id_len = cols.delete('id')
68
+ cols = cols.map {|k, v| [k, v] }
69
+ cols.unshift ['id', id_len]
70
+
71
+ line = cols.map {|col, len| '+' + '-' * (len + 2) }.join + '+'
72
+ head = cols.map {|col, len| "| %-*s " % [len, col] }.join + '|'
73
+
74
+ body = rows.map do |row|
75
+ cols.map {|col, len| "| %-*s " % [len, row[col]] }.join + '|'
76
+ end
77
+
78
+ out.puts <<EOS
79
+ #{line}
80
+ #{head}
81
+ #{line}
82
+ #{body.join "\n"}
83
+ #{line}
84
+ EOS
85
+ end
86
+
87
+ def employee_data
88
+ data = [
89
+ [7369, 'SMITH' , 'CLERK' , 7902, '17-DEC-1980', 800.0, nil, 20],
90
+ [7499, 'ALLEN' , 'SALESMAN' , 7698, '20-FEB-1981', 1600.0, 300.0, 30],
91
+ [7521, 'WARD' , 'SALESMAN' , 7698, '22-FEB-1981', 1250.0, 500.0, 30],
92
+ [7566, 'JONES' , 'MANAGER' , 7839, '2-APR-1981' , 2975.0, nil, 20],
93
+ [7654, 'MARTIN', 'SALESMAN' , 7698, '28-SEP-1981', 1250.0, 1400.0, 30],
94
+ [7698, 'BLAKE' , 'MANAGER' , 7839, '1-MAY-1981' , 2850.0, nil, 30],
95
+ [7782, 'CLARK' , 'MANAGER' , 7839, '9-JUN-1981' , 2450.0, nil, 10],
96
+ [7788, 'SCOTT' , 'ANALYST' , 7566, '09-DEC-1982', 3000.0, nil, 20],
97
+ [7839, 'KING' , 'PRESIDENT', nil, '17-NOV-1981', 5000.0, nil, 10],
98
+ [7844, 'TURNER', 'SALESMAN' , 7698, '8-SEP-1981' , 1500.0, 0.0, 30],
99
+ [7876, 'ADAMS' , 'CLERK' , 7788, '12-JAN-1983', 1100.0, nil, 20],
100
+ [7900, 'JAMES' , 'CLERK' , 7698, '3-DEC-1981' , 950.0, nil, 30],
101
+ [7902, 'FORD' , 'ANALYST' , 7566, '3-DEC-1981' , 3000.0, nil, 20],
102
+ [7934, 'MILLER', 'CLERK' , 7782, '23-JAN-1982', 1300.0, nil, 10],
103
+ [ nil, nil , nil , nil, nil , nil, nil, nil],
104
+ ]
105
+
106
+ data.each_with_index do |i, n|
107
+ i.instance_eval "def id; #{n + 1}; end"
108
+ end
109
+
110
+ return data
111
+ end
112
+
113
+ def department_data
114
+ data = [
115
+ [ 10, 'ACCOUNTING', 'NEW YORK'],
116
+ [ 20, 'RESEARCH' , 'DALLAS' ],
117
+ [ 30, 'SALES' , 'CHICAGO' ],
118
+ [ 40, 'OPERATIONS', 'BOSTON' ],
119
+ [nil, nil , nil ],
120
+ ]
121
+
122
+ data.each_with_index do |i, n|
123
+ i.instance_eval "def id; #{n + 1}; end"
124
+ end
125
+
126
+ return data
127
+ end
128
+
129
+ def validate_employee(expected, employee)
130
+ employee.empno.should == expected[EMP_EMPNO]
131
+ employee.ename.should == expected[EMP_ENAME]
132
+ employee.job.should == expected[EMP_JOB]
133
+ employee.mgr.should == expected[EMP_MGR]
134
+ employee.hiredate.should == expected[EMP_HIREDATE]
135
+ employee.sal.should == expected[EMP_SAL]
136
+ employee.comm.should == expected[EMP_COMM]
137
+ employee.deptno.should == expected[EMP_DEPTNO]
138
+ end
139
+
140
+ def validate_department(expected, department)
141
+ department.deptno.should == expected[DEPT_DEPTNO]
142
+ department.dname.should == expected[DEPT_DNAME]
143
+ department.loc.should == expected[DEPT_LOC]
144
+ end
145
+ end # SpecHelper
146
+
147
+ module ActiveTokyoCabinetSpec
148
+ module Base
149
+ def create_row(klass, vals)
150
+ cols = klass.instance_variable_get(:@columns).map {|col| col.name.to_sym }
151
+ cols.delete(:id)
152
+ row = {}
153
+ cols.zip(vals).each {|col, val| row[col] = val }
154
+ klass.create!(row)
155
+ end
156
+
157
+ def setup_employee
158
+ establish_connection
159
+ employee_data.each {|row| create_row(Employee, row) }
160
+ end
161
+
162
+ def setup_department
163
+ establish_connection
164
+ department_data.each {|row| create_row(Department, row) }
165
+ end
166
+ end # module Base
167
+ end # module ActiveTokyoCabinetSpec
168
+
169
+ module ActiveTokyoCabinetSpec
170
+ module TokyoCabinetSpec
171
+ extend ActiveTokyoCabinetSpec::Base
172
+
173
+ class << self
174
+ def establish_connection
175
+ ActiveRecord::Base.establish_connection(
176
+ :adapter => 'tokyocabinet',
177
+ :database => $wd
178
+ )
179
+ end
180
+
181
+ def tctmgr_create(path)
182
+ `tctmgr create #{path}`
183
+ end
184
+
185
+ def create_tables
186
+ tctmgr_create "#{$wd}/employees.tct"
187
+ tctmgr_create "#{$wd}/departments.tct"
188
+ end
189
+
190
+ def clean
191
+ FileUtils.rm_f "#{$wd}/employees.tct"
192
+ FileUtils.rm_f "#{$wd}/departments.tct"
193
+ end
194
+ end # class << self
195
+ end # module TokyoCabinetSpec
196
+ end # module ActiveTokyoCabinetSpec
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activetokyocabinet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - winebarrel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-08 00:00:00 +09:00
12
+ date: 2010-01-10 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -37,6 +37,10 @@ files:
37
37
  - lib/active_tokyocabinet/tdb.rb
38
38
  - lib/active_tokyocabinet/sqlparser.y
39
39
  - lib/active_tokyocabinet/sqlparser.tab.rb
40
+ - spec/activetokyocabinet_spec.rb
41
+ - spec/models/department.rb
42
+ - spec/models/employee.rb
43
+ - spec/spec_helper.rb
40
44
  - README
41
45
  has_rdoc: true
42
46
  homepage: http://activetokyocabi.rubyforge.org/