-
Notifications
You must be signed in to change notification settings - Fork 49
/
category_indexing.rb
128 lines (110 loc) · 2.65 KB
/
category_indexing.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module Picky
#
#
class Category
include Helpers::Indexing
attr_reader :exact,
:partial
# Prepares and caches this category.
#
# This one should be used by users.
#
def index scheduler = Scheduler.new
timed_indexing scheduler do
prepare scheduler
scheduler.finish
cache scheduler
scheduler.finish
end
end
# Indexes, creates the "prepared_..." file.
#
def prepare scheduler = Scheduler.new
categories = Categories.new
categories << self
with_data_snapshot do
scheduler.schedule do
indexer.prepare categories, scheduler
nil # Note: Needed so procrastinate is happy.
end
end
end
# Generates all caches for this category.
#
def cache scheduler = Scheduler.new
scheduler.schedule do
empty
retrieve
dump
nil # Note: Needed so procrastinate is happy.
end
end
# Empty all the indexes.
#
def empty
exact.empty
partial.empty
end
# Take a data snapshot if the source offers it.
#
def with_data_snapshot
if source.respond_to? :with_snapshot
source.with_snapshot(@index) do
yield
end
else
yield
end
end
# Retrieves the prepared index data into the indexes and
# generates the necessary derived indexes.
#
def retrieve
prepared.retrieve { |id, token| add_tokenized_token id, token, :<< }
end
# Return an appropriate source.
#
# If we have no explicit source, we'll check the index for one.
#
def source
(@source = extract_source(@source, nil_ok: true)) || @index.source
end
# Return the key format.
#
# If no key_format is defined on the category
# and the source has no key format, ask
# the index for one.
#
# Default is to_i.
#
def key_format
@key_format ||= @index.key_format || :to_i
end
# Where the data is taken from.
#
def from
@from || name
end
# The indexer is lazily generated and cached.
#
def indexer
@indexer ||= source.respond_to?(:each) ? Indexers::Parallel.new(self) : Indexers::Serial.new(self)
end
# Returns an appropriate tokenizer.
# If one isn't set on this category, will try the index,
# and finally the default index tokenizer.
#
def tokenizer
@tokenizer || @index.tokenizer
end
# Clears the caches.
#
# THINK about the semantics of clear.
# Is a delete even needed or is it clear+dump?
#
def clear
exact.clear
partial.clear
end
end
end