spinel 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +2 -0
- data/lib/spinel/backend.rb +39 -0
- data/lib/spinel/base.rb +31 -0
- data/lib/spinel/config.rb +61 -0
- data/lib/spinel/helper.rb +36 -0
- data/lib/spinel/matcher.rb +32 -0
- data/lib/spinel/version.rb +6 -0
- data/lib/spinel.rb +20 -0
- data/spinel.gemspec +26 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a55bbe4e834685bcef3003c6a53a4722d0d4b3e7
|
4
|
+
data.tar.gz: bb615a4d0d4c79069686ecd73575f9e228c27942
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2a887be953fc67ce0688d578c71faadeeb86badddc9e0b58a92c37abae19010fe412e4f81fcb64b14171f1452c1b63cae042050b2fa9ce64a70344cd94ae94f1
|
7
|
+
data.tar.gz: 492dc54d734cf244b04d506468d394e7d0c3c1955b54f5c8a7d7542a63bc45ae6f5305e265ad222a58b5457e6259fa5ff163f21f22792b1282affbf4d546b381
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Shogo Kawaguchi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Spinel
|
2
|
+
|
3
|
+
SpinelはRedisをバックエンドに用いた軽量なサジェストシステムです。
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'spinel'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```shell
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```shell
|
22
|
+
$ gem install spinel
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Configuration
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Spinel.configure do |config|
|
31
|
+
config.redis = 'redis://127.0.0.1:6379/0'
|
32
|
+
config.min_complete = 3
|
33
|
+
config.cache_expire = 300
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it ( https://github.com/k-shogo/spinel/fork )
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module Spinel
|
2
|
+
class Backend < Base
|
3
|
+
|
4
|
+
def add(doc, opts = {})
|
5
|
+
opts = { skip_duplicate_check: false }.merge(opts)
|
6
|
+
document_validate doc
|
7
|
+
id = document_id doc
|
8
|
+
|
9
|
+
remove(id: id) unless opts[:skip_duplicate_check]
|
10
|
+
|
11
|
+
Spinel.redis.pipelined do
|
12
|
+
Spinel.redis.hset(database, id, MultiJson.encode(doc))
|
13
|
+
prefixes_for_phrase(document_to_phrase(doc)).each do |p|
|
14
|
+
Spinel.redis.zadd(base_and(p), document_score(doc), id)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def get id
|
20
|
+
if doc = Spinel.redis.hget(database, id)
|
21
|
+
MultiJson.decode(doc)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def remove(doc)
|
26
|
+
if prev_doc = Spinel.redis.hget(database, document_id(doc))
|
27
|
+
prev_doc = MultiJson.decode(prev_doc)
|
28
|
+
prev_id = document_id prev_doc
|
29
|
+
Spinel.redis.pipelined do
|
30
|
+
Spinel.redis.hdel(database, prev_id)
|
31
|
+
prefixes_for_phrase(document_to_phrase(prev_doc)).each do |p|
|
32
|
+
Spinel.redis.zrem(base_and(p), prev_id)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
data/lib/spinel/base.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Spinel
|
2
|
+
class Base
|
3
|
+
include Helper
|
4
|
+
|
5
|
+
attr_accessor :type
|
6
|
+
|
7
|
+
def initialize type = :default
|
8
|
+
@type = type
|
9
|
+
end
|
10
|
+
|
11
|
+
def base
|
12
|
+
"#{Spinel.namespace}:index:#{type}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def base_and p
|
16
|
+
"#{base}:#{p}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def database
|
20
|
+
"#{Spinel.namespace}:data:#{type}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def cachebase
|
24
|
+
"#{Spinel.namespace}:cache:#{type}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def cachekey words
|
28
|
+
"#{cachebase}:#{words.join('|')}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Spinel
|
2
|
+
module Config
|
3
|
+
DEFAULT_MIN_COMPLETE = 2
|
4
|
+
DEFAULT_CACHE_EXPIRE = 600
|
5
|
+
DEFAULT_MATCH_LIMIT = 10
|
6
|
+
DEFAULT_DOCUMENT_KEY = :body
|
7
|
+
DEFAULT_NAMESPACE = 'spinel'
|
8
|
+
|
9
|
+
attr_writer :min_complete, :cache_expire, :match_limit, :document_key, :namespace
|
10
|
+
|
11
|
+
def min_complete
|
12
|
+
@min_complete ||= DEFAULT_MIN_COMPLETE
|
13
|
+
end
|
14
|
+
|
15
|
+
def cache_expire
|
16
|
+
@cache_expire ||= DEFAULT_CACHE_EXPIRE
|
17
|
+
end
|
18
|
+
|
19
|
+
def match_limit
|
20
|
+
@match_limit ||= DEFAULT_MATCH_LIMIT
|
21
|
+
end
|
22
|
+
|
23
|
+
def document_key
|
24
|
+
(@document_key ||= DEFAULT_DOCUMENT_KEY).to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def namespace
|
28
|
+
@namespace ||= DEFAULT_NAMESPACE
|
29
|
+
end
|
30
|
+
|
31
|
+
def redis=(server)
|
32
|
+
if server.is_a?(String)
|
33
|
+
@redis = nil
|
34
|
+
@redis_url = server
|
35
|
+
else
|
36
|
+
@redis = server
|
37
|
+
end
|
38
|
+
|
39
|
+
redis
|
40
|
+
end
|
41
|
+
|
42
|
+
def redis
|
43
|
+
@redis ||= (
|
44
|
+
url = URI(@redis_url || ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0")
|
45
|
+
|
46
|
+
::Redis.new({
|
47
|
+
host: url.host,
|
48
|
+
port: url.port,
|
49
|
+
db: url.path[1..-1],
|
50
|
+
password: url.password
|
51
|
+
})
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def configure
|
56
|
+
yield self
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Spinel
|
2
|
+
module Helper
|
3
|
+
|
4
|
+
def prefixes_for_phrase(phrase)
|
5
|
+
words = phrase.split(' ')
|
6
|
+
words.map do |w|
|
7
|
+
(Spinel.min_complete-1..(w.length-1)).map{ |l| w[0..l] }
|
8
|
+
end.flatten.uniq
|
9
|
+
end
|
10
|
+
|
11
|
+
def document_to_phrase doc
|
12
|
+
[document_body(doc), *Array(document_aliases(doc))].join(' ')
|
13
|
+
end
|
14
|
+
|
15
|
+
def document_validate doc
|
16
|
+
raise ArgumentError, "documents must specify both an id and a body" unless document_id(doc) && document_body(doc)
|
17
|
+
end
|
18
|
+
|
19
|
+
def document_id doc
|
20
|
+
doc[:id] || doc["id"]
|
21
|
+
end
|
22
|
+
|
23
|
+
def document_score doc
|
24
|
+
doc[:score] || doc["score"]
|
25
|
+
end
|
26
|
+
|
27
|
+
def document_body doc
|
28
|
+
doc[Spinel.document_key.to_sym] || doc[Spinel.document_key]
|
29
|
+
end
|
30
|
+
|
31
|
+
def document_aliases doc
|
32
|
+
doc[:aliases] || doc["aliases"]
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Spinel
|
2
|
+
class Matcher < Base
|
3
|
+
|
4
|
+
def matches(term, options = {})
|
5
|
+
options = { limit: Spinel.match_limit, cache: true }.merge(options)
|
6
|
+
|
7
|
+
words = term.split(' ').reject do |w|
|
8
|
+
w.size < Spinel.min_complete
|
9
|
+
end.sort
|
10
|
+
|
11
|
+
return [] if words.empty?
|
12
|
+
|
13
|
+
tmp_cachekey = cachekey(words)
|
14
|
+
|
15
|
+
if !options[:cache] || !Spinel.redis.exists(tmp_cachekey) || Spinel.redis.exists(tmp_cachekey) == 0
|
16
|
+
interkeys = words.map { |w| base_and w }
|
17
|
+
Spinel.redis.zinterstore(tmp_cachekey, interkeys)
|
18
|
+
Spinel.redis.expire(tmp_cachekey, Spinel.cache_expire)
|
19
|
+
end
|
20
|
+
|
21
|
+
ids = Spinel.redis.zrevrange(tmp_cachekey, 0, options[:limit] - 1)
|
22
|
+
if ids.size > 0
|
23
|
+
results = Spinel.redis.hmget(database, *ids)
|
24
|
+
results = results.reject{ |r| r.nil? }
|
25
|
+
results.map { |r| MultiJson.decode(r) }
|
26
|
+
else
|
27
|
+
[]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/lib/spinel.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'multi_json'
|
3
|
+
require "spinel/version"
|
4
|
+
require "spinel/config"
|
5
|
+
require "spinel/helper"
|
6
|
+
require "spinel/base"
|
7
|
+
require "spinel/backend"
|
8
|
+
require "spinel/matcher"
|
9
|
+
|
10
|
+
module Spinel
|
11
|
+
extend Config
|
12
|
+
|
13
|
+
def self.backend type = :default
|
14
|
+
Backend.new type
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.matcher type = :default
|
18
|
+
Matcher.new type
|
19
|
+
end
|
20
|
+
end
|
data/spinel.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'spinel/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "spinel"
|
8
|
+
spec.version = Spinel::VERSION
|
9
|
+
spec.authors = ["k-shogo"]
|
10
|
+
spec.email = ["platycod0n.ramosa@gmail.com"]
|
11
|
+
spec.summary = %q{suggest related words}
|
12
|
+
spec.description = %q{suggest related words}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'redis', '>= 3.0'
|
22
|
+
spec.add_dependency 'multi_json', '~> 1.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spinel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- k-shogo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: multi_json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: suggest related words
|
70
|
+
email:
|
71
|
+
- platycod0n.ramosa@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/spinel.rb
|
82
|
+
- lib/spinel/backend.rb
|
83
|
+
- lib/spinel/base.rb
|
84
|
+
- lib/spinel/config.rb
|
85
|
+
- lib/spinel/helper.rb
|
86
|
+
- lib/spinel/matcher.rb
|
87
|
+
- lib/spinel/version.rb
|
88
|
+
- spinel.gemspec
|
89
|
+
homepage: ''
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.2.2
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: suggest related words
|
113
|
+
test_files: []
|