lunar 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +17 -0
  5. data/Rakefile +53 -0
  6. data/VERSION +1 -0
  7. data/examples/ohm.rb +23 -0
  8. data/lib/lunar.rb +12 -0
  9. data/lib/lunar/doc.rb +13 -0
  10. data/lib/lunar/index.rb +70 -0
  11. data/lib/lunar/scoring.rb +11 -0
  12. data/test/helper.rb +13 -0
  13. data/test/test_lunar.rb +4 -0
  14. data/test/test_lunar_document.rb +20 -0
  15. data/test/test_lunar_index.rb +174 -0
  16. data/test/test_lunar_scoring.rb +26 -0
  17. data/vendor/nest/nest.rb +7 -0
  18. data/vendor/redis/.gitignore +9 -0
  19. data/vendor/redis/LICENSE +20 -0
  20. data/vendor/redis/README.markdown +120 -0
  21. data/vendor/redis/Rakefile +75 -0
  22. data/vendor/redis/benchmarking/logging.rb +62 -0
  23. data/vendor/redis/benchmarking/pipeline.rb +44 -0
  24. data/vendor/redis/benchmarking/speed.rb +21 -0
  25. data/vendor/redis/benchmarking/suite.rb +24 -0
  26. data/vendor/redis/benchmarking/worker.rb +71 -0
  27. data/vendor/redis/bin/distredis +33 -0
  28. data/vendor/redis/examples/basic.rb +15 -0
  29. data/vendor/redis/examples/dist_redis.rb +43 -0
  30. data/vendor/redis/examples/incr-decr.rb +17 -0
  31. data/vendor/redis/examples/list.rb +26 -0
  32. data/vendor/redis/examples/pubsub.rb +25 -0
  33. data/vendor/redis/examples/sets.rb +36 -0
  34. data/vendor/redis/lib/edis.rb +3 -0
  35. data/vendor/redis/lib/redis.rb +496 -0
  36. data/vendor/redis/lib/redis/client.rb +265 -0
  37. data/vendor/redis/lib/redis/dist_redis.rb +118 -0
  38. data/vendor/redis/lib/redis/distributed.rb +460 -0
  39. data/vendor/redis/lib/redis/hash_ring.rb +131 -0
  40. data/vendor/redis/lib/redis/pipeline.rb +13 -0
  41. data/vendor/redis/lib/redis/raketasks.rb +1 -0
  42. data/vendor/redis/lib/redis/subscribe.rb +79 -0
  43. data/vendor/redis/profile.rb +22 -0
  44. data/vendor/redis/tasks/redis.tasks.rb +140 -0
  45. data/vendor/redis/test/db/.gitignore +1 -0
  46. data/vendor/redis/test/distributed_test.rb +1131 -0
  47. data/vendor/redis/test/redis_test.rb +1134 -0
  48. data/vendor/redis/test/test.conf +8 -0
  49. data/vendor/redis/test/test_helper.rb +113 -0
  50. metadata +127 -0
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Cyril David
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = lunar
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Cyril David. See LICENSE for details.
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "lunar"
8
+ gem.summary = %Q{a minimalistic full text search implementation in redis}
9
+ gem.description = %Q{uses sorted sets and sets, sorting by score}
10
+ gem.email = "cyx.ucron@gmail.com"
11
+ gem.homepage = "http://github.com/cyx/lunar"
12
+ gem.authors = ["Cyril David"]
13
+ gem.add_development_dependency "contest"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "lunar #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,23 @@
1
+ class Item < Ohm::Model
2
+ attribute :name
3
+ attribute :description
4
+
5
+ def index
6
+ Lunar::Index.create(
7
+ self.class.name,
8
+ self.id,
9
+ :name => name,
10
+ :description => description
11
+ )
12
+
13
+ index = Lunar::Index.new('Item')
14
+ index.attrs = { :name => name, :description => description }
15
+ index.create
16
+
17
+ Luner::Index.create 'Item' do |i|
18
+ i.key id
19
+ i.attr :name, name
20
+ i.attr :description, description
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ require 'base64'
2
+ require File.join(File.dirname(__FILE__), '..', 'vendor', 'nest', 'nest')
3
+
4
+ module Lunar
5
+ autoload :Scoring, 'lunar/scoring'
6
+ autoload :Doc, 'lunar/doc'
7
+ autoload :Index, 'lunar/index'
8
+
9
+ def self.redis(connection = defined?(Ohm) ? Ohm.redis : nil)
10
+ @connection ||= connection
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Lunar
2
+ class Doc
3
+ def initialize(attrs)
4
+ @attrs = attrs
5
+ end
6
+
7
+ def attr_scores
8
+ @attrs.inject(Hash.new([])) { |hash, (k, v)|
9
+ (hash[k.to_sym] = Scoring.new(v).scores) && hash
10
+ }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,70 @@
1
+ module Lunar
2
+ class Index
3
+ attr :ns
4
+
5
+ def self.create(prefix)
6
+ new(prefix).tap { |i| yield i }.index
7
+ end
8
+
9
+ def self.delete(prefix, key)
10
+ new(prefix).tap { |i| i.key key }.delete
11
+ end
12
+
13
+ def initialize(prefix, redis = nil)
14
+ @ns = Nest.new(:Lunar)[prefix]
15
+ @attrs = {}
16
+ @redis = (redis || Lunar.redis)
17
+ end
18
+
19
+ def key(key = nil)
20
+ @key = key if key
21
+ @key
22
+ end
23
+
24
+ def attrs=(attrs)
25
+ attrs.each { |k, v| attr k, v }
26
+ end
27
+
28
+ def attr(field, value = nil)
29
+ @attrs[field.to_sym] = value if value
30
+ @attrs[field.to_sym]
31
+ end
32
+
33
+ def index
34
+ @attrs.each do |field, value|
35
+ scoring = Scoring.new(value)
36
+ words = []
37
+ scoring.scores.each do |word, score|
38
+ words << word
39
+ @redis.zadd @ns[field][encode(word)], score, key
40
+ @redis.sadd @ns[key][field], word
41
+ end
42
+
43
+ unused_words = @redis.smembers(@ns[key][field]) - words
44
+ unused_words.each do |word|
45
+ @redis.zrem @ns[field][encode(word)], key
46
+ @redis.srem @ns[key][field], word
47
+ end
48
+ end
49
+
50
+ return self
51
+ end
52
+
53
+ def delete
54
+ keys = @redis.keys @ns[key]['*']
55
+ keys.each do |k|
56
+ field = k.gsub("#{ @ns[key] }:", '')
57
+ words = @redis.smembers(k)
58
+ words.each do |w|
59
+ @redis.zrem @ns[field][encode(w)], key
60
+ end
61
+ @redis.del k
62
+ end
63
+ end
64
+
65
+ private
66
+ def encode(str)
67
+ Base64.encode64(str).strip
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,11 @@
1
+ module Lunar
2
+ class Scoring
3
+ def initialize(words)
4
+ @words = words.split(/\s+/).reject { |w| w.to_s.strip.empty? }
5
+ end
6
+
7
+ def scores
8
+ @words.inject(Hash.new(0)) { |a, w| a[w.downcase] += 1; a }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'contest'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'lunar'
8
+
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../vendor/redis/lib')
10
+ require 'redis'
11
+
12
+ class Test::Unit::TestCase
13
+ end
@@ -0,0 +1,4 @@
1
+ require 'helper'
2
+
3
+ class TestLunar < Test::Unit::TestCase
4
+ end
@@ -0,0 +1,20 @@
1
+ require "helper"
2
+
3
+ class LunarDocTest < Test::Unit::TestCase
4
+ describe "given title => iPhone, description => apple Cellphone Mobile" do
5
+ setup do
6
+ @doc = Lunar::Doc.new(:title => 'iPhone',
7
+ :description => 'apple Cellphone Mobile')
8
+ end
9
+
10
+ should "return attr_scores with title iphone 1" do
11
+ assert_equal 1, @doc.attr_scores[:title]["iphone"]
12
+ end
13
+
14
+ should "return attr_scores with description apple cellphone mobile 1" do
15
+ assert_equal 1, @doc.attr_scores[:description]["apple"]
16
+ assert_equal 1, @doc.attr_scores[:description]["cellphone"]
17
+ assert_equal 1, @doc.attr_scores[:description]["mobile"]
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,174 @@
1
+ require "helper"
2
+
3
+ class LunarIndexTest < Test::Unit::TestCase
4
+ context "given Item" do
5
+ setup do
6
+ @index = Lunar::Index.new('Item')
7
+ end
8
+
9
+ should "have a Lunar:Item ns" do
10
+ assert_equal 'Lunar:Item', @index.ns
11
+ end
12
+
13
+ context "when setting attr :name, 'iphone'" do
14
+ should "have attr(:name) iphone" do
15
+ @index.attr :name, 'iphone'
16
+
17
+ assert_equal 'iphone', @index.attr(:name)
18
+ end
19
+ end
20
+
21
+ context "when setting attrs name iphone desc cellular mobile" do
22
+ setup do
23
+ @index.attrs = { :name => 'iphone', :desc => 'cellular mobile' }
24
+ end
25
+
26
+ should "be able to retrieve them via symbol or string" do
27
+ assert_equal 'iphone', @index.attr(:name)
28
+ assert_equal 'cellular mobile', @index.attr('desc')
29
+ end
30
+ end
31
+ end
32
+
33
+ context "on create" do
34
+ setup do
35
+ Lunar.redis(Redis.new(:host => '127.0.0.1', :port => '6380'))
36
+ Lunar.redis.flushdb
37
+
38
+ @index = Lunar::Index.create 'Item' do |i|
39
+ i.key 1001
40
+ i.attr :name, 'iphone 3G'
41
+ i.attr :description, 'the cellular mobile is the shiznit'
42
+ end
43
+ end
44
+
45
+ should "store Lunar:Item:name:iphone 1001 1" do
46
+ assert_equal "1",
47
+ zscore("Lunar:Item:name:#{ encode('iphone') }", 1001)
48
+ end
49
+
50
+ should "store Lunar:Item:name:3g 1001 1" do
51
+ assert_equal "1",
52
+ zscore("Lunar:Item:name:#{ encode('3g') }", 1001)
53
+ end
54
+
55
+ should "store Lunar:Item:description:cellular 1001 1" do
56
+ assert_equal "1",
57
+ zscore("Lunar:Item:description:#{ encode('cellular') }", 1001)
58
+ end
59
+
60
+ should "store Lunar:Item:description:the 1001 2" do
61
+ assert_equal "2",
62
+ zscore("Lunar:Item:description:#{ encode('the') }", 1001)
63
+ end
64
+
65
+ should "store Lunar:Item:1001:name <iphone 3g> set" do
66
+ set = Lunar.redis.smembers("Lunar:Item:1001:name")
67
+
68
+ assert set.include?('iphone')
69
+ assert set.include?('3g')
70
+ end
71
+
72
+ should "store Lunar:Item:1001:description of all words" do
73
+ set = Lunar.redis.smembers("Lunar:Item:1001:description")
74
+
75
+ assert set.include?('the')
76
+ assert set.include?('cellular')
77
+ assert set.include?('mobile')
78
+ assert set.include?('is')
79
+ assert set.include?('shiznit')
80
+ end
81
+ end
82
+
83
+ context "when creating an index that already exists" do
84
+ setup do
85
+ Lunar.redis(Redis.new(:host => '127.0.0.1', :port => '6380'))
86
+ Lunar.redis.flushdb
87
+
88
+ @index = Lunar::Index.create 'Item' do |i|
89
+ i.key 1001
90
+ i.attr :name, 'iphone 3G'
91
+ i.attr :description, 'the cellular mobile is the shiznit'
92
+ end
93
+
94
+ @index = Lunar::Index.create 'Item' do |i|
95
+ i.key 1001
96
+ i.attr :name, 'iphone 3GS'
97
+ i.attr :description, 'kickass'
98
+ end
99
+ end
100
+
101
+ should "update the references for Item:name" do
102
+ set = Lunar.redis.smembers("Lunar:Item:1001:name")
103
+
104
+ assert ! set.include?('3g'), "should not include 3G"
105
+ assert set.include?('3gs'), "should include 3GS"
106
+ end
107
+
108
+ should "also remove the scores for the non-existent references for name" do
109
+ assert_nil zscore("Lunar:Item:name:#{ encode('3g') }", 1001)
110
+ end
111
+
112
+ should "update the references for Item:description" do
113
+ set = Lunar.redis.smembers("Lunar:Item:1001:description")
114
+
115
+ assert ! set.include?('the')
116
+ assert ! set.include?('cellular')
117
+ assert ! set.include?('mobile')
118
+ assert ! set.include?('is')
119
+ assert ! set.include?('shiznit')
120
+
121
+ assert set.include?('kickass')
122
+ end
123
+
124
+ should "also remove the scores for the non-existent references for desc" do
125
+ assert_nil zscore("Lunar:Item:description:#{ encode('the') }", 1001)
126
+ assert_nil zscore("Lunar:Item:description:#{ encode('cellular') }", 1001)
127
+ assert_nil zscore("Lunar:Item:description:#{ encode('mobile') }", 1001)
128
+ assert_nil zscore("Lunar:Item:description:#{ encode('is') }", 1001)
129
+ assert_nil zscore("Lunar:Item:description:#{ encode('shiznit') }", 1001)
130
+
131
+ assert_equal "1",
132
+ zscore("Lunar:Item:description:#{ encode('kickass') }", 1001)
133
+ end
134
+ end
135
+
136
+ context "on delete" do
137
+ setup do
138
+ Lunar.redis(Redis.new(:host => '127.0.0.1', :port => '6380'))
139
+ Lunar.redis.flushdb
140
+
141
+ @index = Lunar::Index.create 'Item' do |i|
142
+ i.key 1001
143
+ i.attr :name, 'iphone 3G'
144
+ i.attr :description, 'the cellular mobile is the shiznit'
145
+ end
146
+
147
+ Lunar::Index.delete('Item', 1001)
148
+ end
149
+
150
+ should "remove all references of the document words from the scores" do
151
+ assert_nil zscore("Lunar:Item:name:#{ encode('iphone') }", 1001)
152
+ assert_nil zscore("Lunar:Item:name:#{ encode('3g') }", 1001)
153
+
154
+ assert_nil zscore("Lunar:Item:description:#{ encode('the') }", 1001)
155
+ assert_nil zscore("Lunar:Item:description:#{ encode('cellular') }", 1001)
156
+ assert_nil zscore("Lunar:Item:description:#{ encode('mobile') }", 1001)
157
+ assert_nil zscore("Lunar:Item:description:#{ encode('is') }", 1001)
158
+ assert_nil zscore("Lunar:Item:description:#{ encode('shiznit') }", 1001)
159
+ end
160
+
161
+ should "remove all Lunar:Item:* references" do
162
+ assert ! Lunar.redis.exists("Lunar:Item:1001:name")
163
+ assert ! Lunar.redis.exists("Lunar:Item:1001:description")
164
+ end
165
+ end
166
+ protected
167
+ def encode(str)
168
+ Base64.encode64(str).strip
169
+ end
170
+
171
+ def zscore(key, value)
172
+ Lunar.redis.zscore(key, value)
173
+ end
174
+ end