parsley-store 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.2.0
data/// ADDED
@@ -0,0 +1,42 @@
1
+ require 'redis'
2
+ require 'biodiversity'
3
+
4
+
5
+ class ParsleyStore
6
+ #database numbers for Redis
7
+ LOCAL = 1
8
+ SLAVE = 2
9
+
10
+ def initialize(local_db = LOCAL, slave_db = SLAVE, opts)
11
+ @parser = ScientificNameParser.new
12
+ @local = Redis.new
13
+ @local.select(local_db)
14
+ @canonical_only = !!opts[:canonical_only]
15
+ # slave replication is not set up yet, so I comment it out
16
+ # @slave = Redis.new
17
+ # @slave.select(slave_db)
18
+ end
19
+
20
+ def parse(scientific_name)
21
+ stored = @local.get(scientific_name)
22
+ return JSON.parse(stored, :symbolize_names => true) if stored
23
+
24
+ begin
25
+ parsed = @parser.parse(scientific_name)
26
+ rescue
27
+ @parser = ScientificNameParser.new
28
+ parsed = @parser.parse(scientific_name)
29
+ end
30
+
31
+ if @canonical_only
32
+ @local.hset scientific_name, 'parsed', parsed[:scientificName][:parsed]
33
+ @local.hset scientific_name, 'parser_version', parsed[:scientificName][:parser_version]
34
+ @local.hset scientific_name, 'canonical', parsed[:scientificName][:canonical]
35
+ parsed[:scientificName][:canonical]
36
+ else
37
+ serialized = parsed.to_json
38
+ @local.set scientific_name, serialized
39
+ parsed
40
+ end
41
+ end
42
+ end
@@ -11,3 +11,10 @@ Feature: Cached parsind of Scientific Names
11
11
  Given a clean local database
12
12
  When I parse a name "Homo sapiens sapiens (Linn.) 1758" two times
13
13
  Then second parse should be much faster
14
+
15
+ Scenario: "Parsing only canonical names"
16
+ And a clean local database
17
+ Given configuration setting "canonical_only", "true"
18
+ When I parse a name "Homo sapiens sapiens (Linn) 1758" two times
19
+ Then I get only "canonical, parser_version, parsed" as value keys
20
+ And second parse should be much faster
@@ -22,14 +22,16 @@ Given /^a clean local database$/ do
22
22
  @conn.flushdb
23
23
  @conn.dbsize.should == 0
24
24
  @parser = ParsleyStore.new(@local_db, @slave_db)
25
+ @parse_opts = {}
25
26
  end
26
27
 
27
28
  When /^I parse a name "([^"]*)" two times$/ do |name|
28
29
  now = Time.now
29
- res = @parser.parse(name)
30
+ @name = name
31
+ res = @parser.parse(@name, @parse_opts)
30
32
  @delta1 = Time.now - now
31
33
  now = Time.now
32
- res = @parser.parse(name)
34
+ res = @parser.parse(@name, @parse_opts)
33
35
  @delta2 = Time.now - now
34
36
  end
35
37
 
@@ -37,4 +39,13 @@ Then /^second parse should be much faster$/ do
37
39
  # puts "%s/%s=%s", [@delta1, @delta2, @delta1/@delta2]
38
40
  (@delta1/@delta2).should > 10
39
41
  end
42
+
43
+ Given /^configuration setting "([^"]*)", "([^"]*)"$/ do |setting, value|
44
+ @parse_opts.merge!({setting.to_sym => get_value(value)})
45
+ end
40
46
 
47
+ Then /^I get only "([^"]*)" as value keys$/ do |keys|
48
+ keys = keys.split(",").map {|i| i.strip}
49
+ db_keys = @conn.hkeys(@name)
50
+ db_keys.sort.should == keys.sort
51
+ end
@@ -0,0 +1,11 @@
1
+ module ParsleyStoreHelpers
2
+ def get_value(val)
3
+ return nil if ['nil', 'null'].include? val
4
+ return val.to_i if val.to_i.to_s == val
5
+ return true if val == 'true'
6
+ return false if val == 'false'
7
+ end
8
+ end
9
+
10
+
11
+ World(ParsleyStoreHelpers)
@@ -11,22 +11,50 @@ class ParsleyStore
11
11
  @parser = ScientificNameParser.new
12
12
  @local = Redis.new
13
13
  @local.select(local_db)
14
- # slave replication is not set up yet, so I comment it out
15
- # @slave = Redis.new
16
- # @slave.select(slave_db)
17
14
  end
18
15
 
19
- def parse(scientific_name)
20
- stored = @local.get(scientific_name)
21
- return JSON.parse(stored, :symbolize_names => true) if stored
16
+ def parse(scientific_name, opts = {})
17
+ @canonical_only = !!opts[:canonical_only]
18
+ @scientific_name = scientific_name
19
+
20
+ parsed_data = get_redis_data
21
+ return parsed_data if parsed_data
22
+
23
+ cache_parsed_data(parse_scientific_name)
24
+ end
25
+
26
+ private
27
+
28
+ def get_redis_data
29
+ if @canonical_only
30
+ stored = @local.hget(@scientific_name, 'canonical')
31
+ return stored if stored
32
+ else
33
+ stored = @local.get(@scientific_name)
34
+ return JSON.parse(stored, :symbolize_names => true) if stored
35
+ end
36
+ end
37
+
38
+ def parse_scientific_name
22
39
  begin
23
- parsed = @parser.parse(scientific_name)
40
+ @parser.parse(@scientific_name)
24
41
  rescue
25
42
  @parser = ScientificNameParser.new
26
- parsed = @parser.parse(scientific_name)
43
+ @parser.parse(@scientific_name)
44
+ end
45
+ end
46
+
47
+ def cache_parsed_data(parsed_data)
48
+ if @canonical_only
49
+ @local.hset @scientific_name, 'parsed', parsed_data[:scientificName][:parsed]
50
+ @local.hset @scientific_name, 'parser_version', parsed_data[:scientificName][:parser_version]
51
+ @local.hset @scientific_name, 'canonical', parsed_data[:scientificName][:canonical]
52
+ parsed_data[:scientificName][:canonical]
53
+ else
54
+ serialized = parsed_data.to_json
55
+ @local.set @scientific_name, serialized
56
+ parsed_data
27
57
  end
28
- serialized = parsed.to_json
29
- @local.set scientific_name, serialized
30
- parsed
31
58
  end
59
+
32
60
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsley-store
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 23
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 1
8
- - 5
9
- version: 0.1.5
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Dmitry Mozzherin
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-10-06 00:00:00 -04:00
18
+ date: 2010-11-05 00:00:00 -04:00
18
19
  default_executable: parsley-store
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,6 +26,7 @@ dependencies:
25
26
  requirements:
26
27
  - - ">="
27
28
  - !ruby/object:Gem::Version
29
+ hash: 13
28
30
  segments:
29
31
  - 1
30
32
  - 2
@@ -40,6 +42,7 @@ dependencies:
40
42
  requirements:
41
43
  - - ">="
42
44
  - !ruby/object:Gem::Version
45
+ hash: 3
43
46
  segments:
44
47
  - 0
45
48
  version: "0"
@@ -53,6 +56,7 @@ dependencies:
53
56
  requirements:
54
57
  - - ">="
55
58
  - !ruby/object:Gem::Version
59
+ hash: 3
56
60
  segments:
57
61
  - 0
58
62
  version: "0"
@@ -66,6 +70,7 @@ dependencies:
66
70
  requirements:
67
71
  - - ">="
68
72
  - !ruby/object:Gem::Version
73
+ hash: 3
69
74
  segments:
70
75
  - 0
71
76
  version: "0"
@@ -87,10 +92,12 @@ files:
87
92
  - README.rdoc
88
93
  - Rakefile
89
94
  - VERSION
95
+ - \
90
96
  - bin/parsley-store
91
97
  - features/parsley-store.feature
92
98
  - features/step_definitions/parsley-store_steps.rb
93
99
  - features/support/env.rb
100
+ - features/support/helper.rb
94
101
  - lib/parsley-store.rb
95
102
  - spec/parsley-store_spec.rb
96
103
  - spec/spec.opts
@@ -109,6 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
116
  requirements:
110
117
  - - ">="
111
118
  - !ruby/object:Gem::Version
119
+ hash: 3
112
120
  segments:
113
121
  - 0
114
122
  version: "0"
@@ -117,6 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
125
  requirements:
118
126
  - - ">="
119
127
  - !ruby/object:Gem::Version
128
+ hash: 3
120
129
  segments:
121
130
  - 0
122
131
  version: "0"