picky 3.1.2 → 3.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/picky/backends/file/basic.rb +46 -0
- data/lib/picky/backends/helpers/file.rb +43 -0
- data/lib/picky/backends/memory/basic.rb +3 -26
- data/lib/picky/loader.rb +1 -0
- data/lib/picky/query/allocation.rb +2 -7
- data/lib/picky/query/allocations.rb +2 -9
- data/lib/picky/query/combination.rb +0 -6
- data/lib/picky/query/combinations.rb +4 -15
- data/lib/picky/query/indexes.rb +21 -4
- data/lib/picky/search.rb +4 -2
- data/spec/lib/application_spec.rb +1 -0
- data/spec/lib/backends/memory/basic_spec.rb +1 -1
- data/spec/lib/query/allocation_spec.rb +2 -10
- data/spec/lib/query/allocations_spec.rb +5 -31
- data/spec/lib/query/combination_spec.rb +0 -12
- data/spec/lib/query/combinations_spec.rb +4 -17
- metadata +3 -2
@@ -13,6 +13,8 @@ module Picky
|
|
13
13
|
#
|
14
14
|
class Basic
|
15
15
|
|
16
|
+
include Helpers::File
|
17
|
+
|
16
18
|
attr_reader :cache_path, # This index file's location.
|
17
19
|
:mapping_file # The index file's mapping file (loaded into memory for quick access).
|
18
20
|
|
@@ -34,6 +36,50 @@ module Picky
|
|
34
36
|
:index
|
35
37
|
end
|
36
38
|
|
39
|
+
# Will copy the index file to a location that
|
40
|
+
# is in a directory named "backup" right under
|
41
|
+
# the directory the index file is in.
|
42
|
+
#
|
43
|
+
def backup
|
44
|
+
mapping_file.backup
|
45
|
+
|
46
|
+
prepare_backup backup_directory(cache_path)
|
47
|
+
FileUtils.cp cache_path, target, verbose: true
|
48
|
+
end
|
49
|
+
|
50
|
+
# Copies the file from its backup location back
|
51
|
+
# to the original location.
|
52
|
+
#
|
53
|
+
def restore
|
54
|
+
mapping_file.restore
|
55
|
+
|
56
|
+
FileUtils.cp backup_file_path_of(cache_path), cache_path, verbose: true
|
57
|
+
end
|
58
|
+
|
59
|
+
# Deletes the file.
|
60
|
+
#
|
61
|
+
def delete
|
62
|
+
mapping_file.delete
|
63
|
+
|
64
|
+
`rm -Rf #{cache_path}`
|
65
|
+
end
|
66
|
+
|
67
|
+
# Is this cache file suspiciously small?
|
68
|
+
# (less than 8 Bytes of size)
|
69
|
+
#
|
70
|
+
def cache_small?
|
71
|
+
size_of(cache_path) < 8
|
72
|
+
end
|
73
|
+
|
74
|
+
# Is the cache ok? (existing and larger than
|
75
|
+
# zero Bytes in size)
|
76
|
+
#
|
77
|
+
# A small cache is still ok.
|
78
|
+
#
|
79
|
+
def cache_ok?
|
80
|
+
size_of(cache_path) > 0
|
81
|
+
end
|
82
|
+
|
37
83
|
end
|
38
84
|
|
39
85
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Picky
|
2
|
+
|
3
|
+
module Backends
|
4
|
+
|
5
|
+
module Helpers
|
6
|
+
|
7
|
+
# Common file helpers.
|
8
|
+
#
|
9
|
+
module File
|
10
|
+
|
11
|
+
# The backup directory of this file.
|
12
|
+
# Equal to the file's dirname plus /backup
|
13
|
+
#
|
14
|
+
def backup_directory path
|
15
|
+
::File.join ::File.dirname(path), 'backup'
|
16
|
+
end
|
17
|
+
|
18
|
+
# Prepares the backup directory for the file.
|
19
|
+
#
|
20
|
+
def prepare_backup target
|
21
|
+
FileUtils.mkdir target unless Dir.exists?(target)
|
22
|
+
end
|
23
|
+
|
24
|
+
# The backup filename.
|
25
|
+
#
|
26
|
+
def backup_file_path_of path
|
27
|
+
dir, name = ::File.split path
|
28
|
+
::File.join dir, 'backup', name
|
29
|
+
end
|
30
|
+
|
31
|
+
# Extracts the size of the file in Bytes.
|
32
|
+
#
|
33
|
+
def size_of path
|
34
|
+
`ls -l #{path} | awk '{print $5}'`.to_i
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -13,6 +13,8 @@ module Picky
|
|
13
13
|
#
|
14
14
|
class Basic
|
15
15
|
|
16
|
+
include Helpers::File
|
17
|
+
|
16
18
|
# This file's location.
|
17
19
|
#
|
18
20
|
attr_reader :cache_path
|
@@ -39,20 +41,6 @@ module Picky
|
|
39
41
|
FileUtils.cp cache_path, target, verbose: true
|
40
42
|
end
|
41
43
|
|
42
|
-
# The backup directory of this file.
|
43
|
-
# Equal to the file's dirname plus /backup
|
44
|
-
#
|
45
|
-
|
46
|
-
def backup_directory
|
47
|
-
::File.join ::File.dirname(cache_path), 'backup'
|
48
|
-
end
|
49
|
-
|
50
|
-
# Prepares the backup directory for the file.
|
51
|
-
#
|
52
|
-
def prepare_backup target
|
53
|
-
FileUtils.mkdir target unless Dir.exists?(target)
|
54
|
-
end
|
55
|
-
|
56
44
|
# Copies the file from its backup location back
|
57
45
|
# to the original location.
|
58
46
|
#
|
@@ -60,13 +48,6 @@ module Picky
|
|
60
48
|
FileUtils.cp backup_file_path_of(cache_path), cache_path, verbose: true
|
61
49
|
end
|
62
50
|
|
63
|
-
# The backup filename.
|
64
|
-
#
|
65
|
-
def backup_file_path_of path
|
66
|
-
dir, name = ::File.split path
|
67
|
-
::File.join dir, 'backup', name
|
68
|
-
end
|
69
|
-
|
70
51
|
# Deletes the file.
|
71
52
|
#
|
72
53
|
def delete
|
@@ -82,6 +63,7 @@ module Picky
|
|
82
63
|
def cache_small?
|
83
64
|
size_of(cache_path) < 8
|
84
65
|
end
|
66
|
+
|
85
67
|
# Is the cache ok? (existing and larger than
|
86
68
|
# zero Bytes in size)
|
87
69
|
#
|
@@ -90,11 +72,6 @@ module Picky
|
|
90
72
|
def cache_ok?
|
91
73
|
size_of(cache_path) > 0
|
92
74
|
end
|
93
|
-
# Extracts the size of the file in Bytes.
|
94
|
-
#
|
95
|
-
def size_of path
|
96
|
-
`ls -l #{path} | awk '{print $5}'`.to_i
|
97
|
-
end
|
98
75
|
|
99
76
|
#
|
100
77
|
#
|
data/lib/picky/loader.rb
CHANGED
@@ -52,13 +52,8 @@ module Picky
|
|
52
52
|
|
53
53
|
#
|
54
54
|
#
|
55
|
-
def
|
56
|
-
@combinations.
|
57
|
-
end
|
58
|
-
#
|
59
|
-
#
|
60
|
-
def remove identifiers = [] # categories
|
61
|
-
@combinations.remove identifiers
|
55
|
+
def remove categories = []
|
56
|
+
@combinations.remove categories
|
62
57
|
end
|
63
58
|
|
64
59
|
# Sort highest score first.
|
@@ -38,19 +38,12 @@ module Picky
|
|
38
38
|
@allocations = @allocations.shift amount
|
39
39
|
end
|
40
40
|
|
41
|
-
# Keeps combinations.
|
42
|
-
#
|
43
|
-
# Only those passed in remain.
|
44
|
-
#
|
45
|
-
def keep identifiers = []
|
46
|
-
@allocations.each { |allocation| allocation.keep identifiers } unless identifiers.empty?
|
47
|
-
end
|
48
41
|
# Removes combinations.
|
49
42
|
#
|
50
43
|
# Only those passed in are removed.
|
51
44
|
#
|
52
|
-
def remove
|
53
|
-
@allocations.each { |allocation| allocation.remove
|
45
|
+
def remove categories = []
|
46
|
+
@allocations.each { |allocation| allocation.remove categories } unless categories.empty?
|
54
47
|
end
|
55
48
|
|
56
49
|
# Returns the top amount ids.
|
@@ -37,26 +37,15 @@ module Picky
|
|
37
37
|
weights.score_for @combinations
|
38
38
|
end
|
39
39
|
|
40
|
-
# Filters the tokens and
|
41
|
-
# that are passed in,
|
40
|
+
# Filters the tokens and categories such that categories
|
41
|
+
# that are passed in, are removed.
|
42
42
|
#
|
43
43
|
# Note: This method is not totally independent of the calculate_ids one.
|
44
44
|
# Since identifiers are only nullified, we need to not include the
|
45
45
|
# ids that have an associated identifier that is nil.
|
46
46
|
#
|
47
|
-
def
|
48
|
-
@combinations.reject! { |combination|
|
49
|
-
end
|
50
|
-
|
51
|
-
# Filters the tokens and identifiers such that identifiers
|
52
|
-
# that are passed in, are removed, including their tokens.
|
53
|
-
#
|
54
|
-
# Note: This method is not totally independent of the calculate_ids one.
|
55
|
-
# Since identifiers are only nullified, we need to not include the
|
56
|
-
# ids that have an associated identifier that is nil.
|
57
|
-
#
|
58
|
-
def remove identifiers = []
|
59
|
-
@combinations.reject! { |combination| combination.in?(identifiers) }
|
47
|
+
def remove categories = []
|
48
|
+
@combinations.reject! { |combination| categories.include?(combination.category) }
|
60
49
|
end
|
61
50
|
|
62
51
|
#
|
data/lib/picky/query/indexes.rb
CHANGED
@@ -13,7 +13,8 @@ module Picky
|
|
13
13
|
#
|
14
14
|
class Indexes
|
15
15
|
|
16
|
-
attr_reader :indexes
|
16
|
+
attr_reader :indexes,
|
17
|
+
:ignored_categories
|
17
18
|
|
18
19
|
# Creates a new Query::Indexes.
|
19
20
|
#
|
@@ -24,10 +25,11 @@ module Picky
|
|
24
25
|
IndexesCheck.check_backend_types indexes
|
25
26
|
|
26
27
|
@indexes = indexes
|
27
|
-
|
28
|
+
|
28
29
|
map_categories
|
29
30
|
end
|
30
31
|
def map_categories
|
32
|
+
@mapper = Query::QualifierCategoryMapper.new
|
31
33
|
@indexes.each do |index|
|
32
34
|
index.each_category do |category|
|
33
35
|
@mapper.add category
|
@@ -35,6 +37,21 @@ module Picky
|
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
40
|
+
# Ignore the categories with these qualifiers.
|
41
|
+
#
|
42
|
+
# Example:
|
43
|
+
# search = Search.new(index1, index2, index3) do
|
44
|
+
# ignore :name, :first_name
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# Cleans up / optimizes after being called.
|
48
|
+
#
|
49
|
+
def ignore *qualifiers
|
50
|
+
@ignored_categories ||= []
|
51
|
+
@ignored_categories += qualifiers.map { |qualifier| @mapper.map qualifier }.compact
|
52
|
+
@ignored_categories.uniq!
|
53
|
+
end
|
54
|
+
|
38
55
|
# Returns a number of prepared (sorted, reduced etc.) allocations for the given tokens.
|
39
56
|
#
|
40
57
|
def prepared_allocations_for tokens, weights = {}
|
@@ -57,9 +74,9 @@ module Picky
|
|
57
74
|
#
|
58
75
|
# allocations.reduce_to some_amount
|
59
76
|
|
60
|
-
# Remove
|
77
|
+
# Remove categories from allocations.
|
61
78
|
#
|
62
|
-
|
79
|
+
allocations.remove ignored_categories if ignored_categories
|
63
80
|
|
64
81
|
allocations
|
65
82
|
end
|
data/lib/picky/search.rb
CHANGED
@@ -21,11 +21,13 @@ module Picky
|
|
21
21
|
attr_accessor :tokenizer,
|
22
22
|
:weights
|
23
23
|
|
24
|
+
delegate :ignore,
|
25
|
+
:to => :indexes
|
26
|
+
|
24
27
|
# Takes:
|
25
28
|
# * A number of indexes
|
26
29
|
#
|
27
|
-
# TODO Add
|
28
|
-
# TODO categories_to_remove ?
|
30
|
+
# TODO Add reduce_allocations_to_amount (rename).
|
29
31
|
#
|
30
32
|
# It is also possible to define the tokenizer and weights like so.
|
31
33
|
# Example:
|
@@ -12,7 +12,7 @@ describe Picky::Backends::Memory::Basic do
|
|
12
12
|
|
13
13
|
describe 'backup_directory' do
|
14
14
|
it "returns the cache path's backup path" do
|
15
|
-
file.backup_directory.should == 'some/cache/path/to/backup'
|
15
|
+
file.backup_directory('some/cache/path/to/file').should == 'some/cache/path/to/backup'
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -43,17 +43,9 @@ describe Picky::Query::Allocation do
|
|
43
43
|
|
44
44
|
describe 'remove' do
|
45
45
|
it 'should delegate to the combinations' do
|
46
|
-
@combinations.should_receive(:remove).once.with [:
|
46
|
+
@combinations.should_receive(:remove).once.with [:some_categories]
|
47
47
|
|
48
|
-
@allocation.remove [:
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe 'keep' do
|
53
|
-
it 'should delegate to the combinations' do
|
54
|
-
@combinations.should_receive(:keep).once.with [:some_identifiers]
|
55
|
-
|
56
|
-
@allocation.keep [:some_identifiers]
|
48
|
+
@allocation.remove [:some_categories]
|
57
49
|
end
|
58
50
|
end
|
59
51
|
|
@@ -31,39 +31,13 @@ describe Picky::Query::Allocations do
|
|
31
31
|
@allocations.remove
|
32
32
|
end
|
33
33
|
end
|
34
|
-
context '
|
34
|
+
context 'categories not empty' do
|
35
35
|
it 'should remove each' do
|
36
|
-
@allocation1.should_receive(:remove).once.with :
|
37
|
-
@allocation2.should_receive(:remove).once.with :
|
38
|
-
@allocation3.should_receive(:remove).once.with :
|
36
|
+
@allocation1.should_receive(:remove).once.with :some_category
|
37
|
+
@allocation2.should_receive(:remove).once.with :some_category
|
38
|
+
@allocation3.should_receive(:remove).once.with :some_category
|
39
39
|
|
40
|
-
@allocations.remove :
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
describe 'keep' do
|
46
|
-
before(:each) do
|
47
|
-
@allocation1 = stub :allocation1
|
48
|
-
@allocation2 = stub :allocation2
|
49
|
-
@allocation3 = stub :allocation3
|
50
|
-
@allocations = described_class.new [@allocation1, @allocation2, @allocation3]
|
51
|
-
end
|
52
|
-
context 'identifiers empty' do
|
53
|
-
it 'should do nothing' do
|
54
|
-
@allocation1.should_receive(:keep).never
|
55
|
-
@allocation2.should_receive(:keep).never
|
56
|
-
|
57
|
-
@allocations.keep
|
58
|
-
end
|
59
|
-
end
|
60
|
-
context 'identifiers not empty' do
|
61
|
-
it 'should filter each' do
|
62
|
-
@allocation1.should_receive(:keep).once.with :some_identifier
|
63
|
-
@allocation2.should_receive(:keep).once.with :some_identifier
|
64
|
-
@allocation3.should_receive(:keep).once.with :some_identifier
|
65
|
-
|
66
|
-
@allocations.keep :some_identifier
|
40
|
+
@allocations.remove :some_category
|
67
41
|
end
|
68
42
|
end
|
69
43
|
end
|
@@ -26,18 +26,6 @@ describe Picky::Query::Combination do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
describe 'in?' do
|
30
|
-
before(:each) do
|
31
|
-
@combination.stub! :identifier => :some_identifier
|
32
|
-
end
|
33
|
-
it 'should check if the given identifiers include the identifier' do
|
34
|
-
@combination.in?([:some_other_identifier, :some_identifier, :some_other_identifier]).should == true
|
35
|
-
end
|
36
|
-
it 'should check if the given identifiers include the identifier' do
|
37
|
-
@combination.in?([:some_other_identifier, :some_other_identifier]).should == false
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
29
|
describe 'to_result' do
|
42
30
|
context 'functional with qualifier' do
|
43
31
|
before(:each) do
|
@@ -80,27 +80,14 @@ describe Picky::Query::Combinations do
|
|
80
80
|
|
81
81
|
describe 'remove' do
|
82
82
|
before(:each) do
|
83
|
-
@combination1 = stub :combination1, :
|
84
|
-
@combination2 = stub :combination2, :
|
85
|
-
@combination3 = stub :combination3, :
|
83
|
+
@combination1 = stub :combination1, :category => :other
|
84
|
+
@combination2 = stub :combination2, :category => :to_remove
|
85
|
+
@combination3 = stub :combination3, :category => :to_remove
|
86
86
|
|
87
87
|
@combinations = described_class.new [@combination1, @combination2, @combination3]
|
88
88
|
end
|
89
89
|
it 'should remove the combinations' do
|
90
|
-
@combinations.remove([:
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
describe 'keep' do
|
95
|
-
before(:each) do
|
96
|
-
@combination1 = stub :combination1, :in? => false
|
97
|
-
@combination2 = stub :combination2, :in? => true
|
98
|
-
@combination3 = stub :combination3, :in? => true
|
99
|
-
|
100
|
-
@combinations = described_class.new [@combination1, @combination2, @combination3]
|
101
|
-
end
|
102
|
-
it 'should filter the combinations' do
|
103
|
-
@combinations.keep([:any]).should == [@combination2, @combination3]
|
90
|
+
@combinations.remove([:to_remove]).should == [@combination1]
|
104
91
|
end
|
105
92
|
end
|
106
93
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: picky
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 3.1.
|
5
|
+
version: 3.1.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Florian Hanke
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
requirements:
|
33
33
|
- - "="
|
34
34
|
- !ruby/object:Gem::Version
|
35
|
-
version: 3.1.
|
35
|
+
version: 3.1.3
|
36
36
|
type: :development
|
37
37
|
version_requirements: *id002
|
38
38
|
description: Fast Ruby semantic text search engine with comfortable single field interface.
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- lib/picky/backends/file/basic.rb
|
56
56
|
- lib/picky/backends/file/json.rb
|
57
57
|
- lib/picky/backends/file.rb
|
58
|
+
- lib/picky/backends/helpers/file.rb
|
58
59
|
- lib/picky/backends/memory/basic.rb
|
59
60
|
- lib/picky/backends/memory/json.rb
|
60
61
|
- lib/picky/backends/memory/marshal.rb
|