dynarex-blog 0.6.19 → 0.7.0
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.
- data/lib/dynarex-blog.rb +99 -85
- metadata +2 -2
data/lib/dynarex-blog.rb
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
# file: dynarex-blog.rb
|
4
4
|
|
5
|
-
require 'rexml/document'
|
6
5
|
require 'polyrex'
|
7
6
|
require 'dynarex'
|
8
7
|
require 'hashcache'
|
9
8
|
|
10
9
|
class DynarexBlog
|
11
|
-
|
12
|
-
|
10
|
+
|
11
|
+
|
12
|
+
ENTITIES = 'entities.xml'
|
13
13
|
attr_accessor :id
|
14
14
|
|
15
15
|
def initialize(file_path='./')
|
@@ -17,48 +17,53 @@ class DynarexBlog
|
|
17
17
|
if File.exists? (@file_path + 'index.xml') then open(@file_path) else fresh_start() end
|
18
18
|
@current_lookup = '_entry_lookup.xml'
|
19
19
|
@hc_lookup = HashCache.new(size: 15)
|
20
|
-
|
20
|
+
|
21
|
+
@hc_lookup.read(@current_lookup) { Rexle.new File.open(@file_path + @current_lookup,'r').read }
|
21
22
|
|
22
23
|
@hc_result = HashCache.new(size: 10)
|
23
24
|
@hc_entry_file = HashCache.new(size: 10)
|
24
25
|
@hc_lookup_a = HashCache.new(size: 10)
|
25
26
|
|
26
|
-
|
27
|
-
@tags =
|
27
|
+
xpath = "records/section[summary/name='tags']/records/entity/summary"
|
28
|
+
@tags = @entities.xpath(xpath){|e| [e.text('name'), e.text('count')]}
|
28
29
|
super()
|
29
30
|
end
|
30
31
|
|
31
32
|
def create_entry(record={})
|
32
|
-
|
33
33
|
@hc_result.reset
|
34
34
|
@id += 1
|
35
35
|
# create a new record
|
36
36
|
@index.create record, @id
|
37
|
-
|
37
|
+
|
38
38
|
# if there is more than 10 records shift the 1st record.
|
39
|
-
if @index.
|
39
|
+
if @index.flat_records.length > 10 then
|
40
40
|
# '+++ deleting the 1st index record +++'
|
41
|
-
@index.delete
|
41
|
+
@index.to_doc.delete('records/*[1]')
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
|
+
@index.save
|
44
45
|
|
45
46
|
create_record(record, @id.to_s, name='_entry', type='main')
|
47
|
+
|
46
48
|
if record[:tags] then
|
47
49
|
record[:tags].split(/\s/).each do |tag|
|
48
50
|
create_record(record, @id.to_s, name=tag, type='tags')
|
49
51
|
end
|
50
52
|
end
|
51
|
-
|
53
|
+
|
52
54
|
end
|
53
55
|
|
54
56
|
def entry(id)
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
|
58
|
+
doc_lookup = @hc_lookup.read(@current_lookup) { Rexle.new File.open(@file_path + @current_lookup,'r').read }
|
59
|
+
file = doc_lookup.text("records/entry[id='#{id}']/file")
|
60
|
+
doc_entries = Rexle.new(@hc_entry_file.read(file) { File.open(@file_path + file,'r').read })
|
61
|
+
|
62
|
+
doc_entries.element("records/entry[@id='#{id}']")
|
59
63
|
end
|
60
64
|
|
61
65
|
def update(id, h)
|
66
|
+
|
62
67
|
lookup = Dynarex.new @file_path + @current_lookup
|
63
68
|
lookup_id = lookup.records[id][:id]
|
64
69
|
file = lookup.records[id][:body][:file]
|
@@ -75,25 +80,25 @@ class DynarexBlog
|
|
75
80
|
|
76
81
|
if prev_tags and prev_tags.length > 0 then
|
77
82
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
83
|
+
b = prev_tags.split(/\s/)
|
84
|
+
old_list = a - b # tags to be deleted
|
85
|
+
new_list = b - a # tags to be inserted
|
86
|
+
|
87
|
+
old_list.each {|x| delete_entry(tag_name, id) }
|
88
|
+
common = a.to_set.intersection(b).to_a # tags to be updated
|
89
|
+
common.each {|name| update_entry(name, id, h) }
|
85
90
|
else
|
86
|
-
|
91
|
+
new_list = a
|
87
92
|
end
|
88
93
|
|
89
94
|
new_list.each {|tag| create_record(h, id, name=tag, type='tags') }
|
90
|
-
|
91
95
|
dynarex.update(id, h)
|
92
96
|
dynarex.save
|
93
97
|
end
|
94
98
|
|
95
99
|
lookup.update(lookup_id, uri: h[:title].gsub(/\s/,'-')).save
|
96
|
-
|
100
|
+
|
101
|
+
@hc_lookup.write(@current_lookup) { Rexle.new File.open(@file_path + @current_lookup,'r').read }
|
97
102
|
|
98
103
|
refresh_index if index_include? id
|
99
104
|
|
@@ -120,8 +125,18 @@ class DynarexBlog
|
|
120
125
|
end
|
121
126
|
|
122
127
|
# start of reindexing
|
123
|
-
|
124
|
-
|
128
|
+
|
129
|
+
if index_include? id then
|
130
|
+
|
131
|
+
count = @index.xpath('count(records/entry)')
|
132
|
+
|
133
|
+
if count < 10 then
|
134
|
+
@index.delete(id)
|
135
|
+
@index.save
|
136
|
+
else
|
137
|
+
refresh_index
|
138
|
+
end
|
139
|
+
end
|
125
140
|
end
|
126
141
|
|
127
142
|
def fresh_start()
|
@@ -131,11 +146,13 @@ class DynarexBlog
|
|
131
146
|
@index = new_blog_file 'index.xml'
|
132
147
|
|
133
148
|
@entities = Polyrex.new('entities/section[name]/entity[name,count,entry_count]')
|
134
|
-
@entities.create.section({name: 'main'}, id='main')
|
135
|
-
@entities.id('main').create.entity(name: '_entry', count: '1')
|
136
|
-
@entities.create.section({name: 'tags'}, id='tags')
|
137
|
-
@entities.save @file_path + 'entities.xml'
|
138
149
|
|
150
|
+
@entities.create.section({name: 'main'}, id='main') do |create|
|
151
|
+
create.entity(name: '_entry', count: '1')
|
152
|
+
end
|
153
|
+
|
154
|
+
@entities.create.section({name: 'tags'}, id='tags')
|
155
|
+
@entities.save @file_path + ENTITIES
|
139
156
|
@lookup = new_lookup_file '_entry_lookup.xml'
|
140
157
|
end
|
141
158
|
|
@@ -147,24 +164,26 @@ class DynarexBlog
|
|
147
164
|
@index = Dynarex.new @file_path + 'index.xml'
|
148
165
|
@id = @index.records ? @index.records.to_a[-1][-1][:id].to_i : 0
|
149
166
|
}
|
150
|
-
threads << Thread.new{@entities = Polyrex.new @file_path +
|
167
|
+
threads << Thread.new{@entities = Polyrex.new @file_path + ENTITIES}
|
151
168
|
threads.each {|thread| thread.join}
|
152
169
|
|
153
170
|
end
|
154
171
|
|
155
|
-
def page(number=
|
172
|
+
def page(number='1')
|
156
173
|
lookup = @current_lookup
|
157
174
|
|
158
175
|
result = nil
|
159
176
|
|
160
177
|
result = @hc_result.read(lookup + number.to_s) do
|
161
178
|
|
162
|
-
if (number == 1) and (lookup == '_entry_lookup.xml') and (@index.records.length
|
179
|
+
if (number.to_s == '1') and (lookup == '_entry_lookup.xml') and (@index.records.length <= 10) then
|
180
|
+
|
163
181
|
doc = @hc_lookup.refresh(lookup)
|
164
|
-
r =
|
182
|
+
r = @index.to_doc
|
165
183
|
else
|
166
|
-
|
167
|
-
|
184
|
+
|
185
|
+
doc = @hc_lookup.read(lookup) { Rexle.new File.open(@file_path + lookup,'r').read }
|
186
|
+
r = select_page(doc, number.to_i)
|
168
187
|
@current_lookup = '_entry_lookup.xml'
|
169
188
|
|
170
189
|
# refresh to maintain _entry_lookup in the cache
|
@@ -172,7 +191,9 @@ class DynarexBlog
|
|
172
191
|
@hc_lookup_a.refresh(@current_lookup)
|
173
192
|
end
|
174
193
|
|
175
|
-
total_records =
|
194
|
+
total_records = doc.xpath('count(records/entry)')
|
195
|
+
return nil if total_records.nil?
|
196
|
+
|
176
197
|
total_pages, remainder = %w(/ %).map {|x| total_records.send x, 10}
|
177
198
|
total_pages += 1 if remainder > 0
|
178
199
|
|
@@ -183,7 +204,7 @@ class DynarexBlog
|
|
183
204
|
}
|
184
205
|
|
185
206
|
summary.each do |name, text|
|
186
|
-
r.
|
207
|
+
r.element('summary').add Rexle::Element.new(name.to_s).add_text(text.to_s)
|
187
208
|
end
|
188
209
|
|
189
210
|
r
|
@@ -248,7 +269,7 @@ class DynarexBlog
|
|
248
269
|
|
249
270
|
@hc_lookup.delete(lookup_filename)
|
250
271
|
end
|
251
|
-
|
272
|
+
|
252
273
|
def reset_cache_entry(lookup_filename, file)
|
253
274
|
|
254
275
|
@hc_entry_file.delete(file)
|
@@ -260,30 +281,26 @@ class DynarexBlog
|
|
260
281
|
|
261
282
|
def select_page(doc, number)
|
262
283
|
|
263
|
-
#doc = Document.new File.open(@file_path + lookup,'r').read
|
264
|
-
|
265
284
|
x1 = (number - 1) * 10
|
266
285
|
x2 = x1 + 9
|
267
286
|
|
268
|
-
lookup_a =
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
xpath_ids = "entry[%s]" % a.map{|x| x.text('id').to_s}.map{|x| "@id='%s'" % x}.join(' or ')
|
287
|
+
lookup_a = doc.xpath('records/entry') do |entry|
|
288
|
+
%w(file id).map{|x| entry.text(x)}
|
289
|
+
end
|
273
290
|
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
291
|
+
threads = lookup_a.reverse[x1..x2].group_by(&:first).map do |filename,raw_ids|
|
292
|
+
Thread.new do
|
293
|
+
xpath = raw_ids.map{|x| "@id='%s'" % x[-1]}.join(' or ')
|
294
|
+
Thread.current[:records] = Rexle.new(File.open(filename,'r').read)\
|
295
|
+
.xpath("records/entry[#{xpath}]")
|
279
296
|
end
|
280
297
|
end
|
281
298
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
299
|
+
records = threads.map{|x| x.join; x[:records]}.flatten(1)
|
300
|
+
|
301
|
+
result = Rexle.new(Dynarex.new('entries/entry(title,body,tags,user)').to_xml)
|
302
|
+
records_node = result.element('records')
|
303
|
+
records.each{|record| records_node.add_element record}
|
287
304
|
|
288
305
|
result
|
289
306
|
|
@@ -302,18 +319,18 @@ class DynarexBlog
|
|
302
319
|
lookup
|
303
320
|
end
|
304
321
|
|
305
|
-
def create_record(record, id, name, type)
|
322
|
+
def create_record(record={}, id, name, type)
|
306
323
|
|
307
|
-
|
324
|
+
xpath = "records/section[summary/name='#{type}']/records/entity/summary[name='#{name}']/count"
|
325
|
+
entry_count = @entities.element xpath
|
308
326
|
lookup_file = "%s_lookup.xml" % name
|
309
327
|
|
310
328
|
if entry_count.nil?
|
311
|
-
@entities.id('tags').create.entity(name: name, count: '1', entry_count: '1')
|
312
|
-
@entities.save @file_path + 'entities.xml'
|
313
|
-
@entities = Polyrex.new @file_path + 'entities.xml'
|
314
|
-
entry_count = @entities.xpath "records/section[summary/name='#{type}']/records/entity/summary[name='#{name}']/count"
|
315
329
|
|
330
|
+
@entities.id('tags').create.entity(name: name, count: '1', entry_count: '1')
|
331
|
+
entry_count = @entities.element "records/section[summary/name='#{type}']/records/entity/summary[name='#{name}']/count"
|
316
332
|
dynarex = Dynarex.new('entries/entry(title,body,tags,user)')
|
333
|
+
|
317
334
|
dynarex.summary[:format_mask].gsub!(/\s/,'; ')
|
318
335
|
dynarex_path = @file_path + name + '1.xml'
|
319
336
|
dynarex.save dynarex_path
|
@@ -325,48 +342,45 @@ class DynarexBlog
|
|
325
342
|
entry_file = "%s%s.xml" % [name, entry_count.text]
|
326
343
|
dynarex_path = "%s%s" % [@file_path, entry_file]
|
327
344
|
dynarex = Dynarex.new dynarex_path
|
328
|
-
|
345
|
+
|
329
346
|
dynarex.create record, id
|
330
347
|
dynarex.save dynarex_path
|
331
|
-
|
332
348
|
delete_cache_entry(lookup_file, entry_file)
|
349
|
+
|
333
350
|
# add the record to lookup
|
334
351
|
|
335
352
|
lookup = Dynarex.new @file_path + lookup_file
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
353
|
+
h = {id: id, file: entry_file, year: Time.now.strftime("%Y"), month: Time.now.strftime("%m"), uri: record[:title].gsub(/\s/,'-')}
|
354
|
+
|
355
|
+
lookup.create h
|
356
|
+
lookup.save
|
357
|
+
@hc_lookup.write(lookup_file) { Rexle.new File.open(@file_path + lookup_file,'r').read }
|
358
|
+
|
340
359
|
# if there is 15 items create a new entries file
|
341
|
-
if dynarex.
|
360
|
+
if dynarex.flat_records.length >= 15 then
|
342
361
|
|
343
362
|
entry_count.text = (entry_count.text.to_i + 1).to_s
|
344
|
-
@entities.save @file_path +
|
345
|
-
|
363
|
+
@entities.save @file_path + ENTITIES
|
346
364
|
dynarex = new_blog_file "%s%s.xml" % [name, entry_count.text]
|
347
|
-
|
348
365
|
end
|
366
|
+
|
349
367
|
end
|
350
368
|
|
351
369
|
def refresh_index()
|
352
|
-
doc_index = Document.new File.open(@file_path + 'index.xml','r').read
|
353
370
|
|
354
|
-
|
355
|
-
node_records.parent.delete node_records
|
371
|
+
@index.delete('records')
|
356
372
|
|
357
373
|
lookup = '_entry_lookup.xml'
|
358
|
-
doc =
|
374
|
+
doc = Rexle.new File.open(@file_path + lookup,'r').read
|
359
375
|
@hc_lookup_a.delete(lookup + '1')
|
360
|
-
page = select_page(doc, 1)
|
361
|
-
doc_index.root.add XPath.first(page.root, 'records')
|
362
376
|
|
363
|
-
|
364
|
-
@index
|
377
|
+
page = select_page(doc, 1)
|
378
|
+
@index.add page.element('records')
|
379
|
+
@index.save
|
365
380
|
end
|
366
381
|
|
367
382
|
def index_include?(id)
|
368
|
-
|
369
|
-
XPath.first(doc.root, "records/entry[@id='#{id}']")
|
383
|
+
@index.element("records/entry[@id='#{id}']")
|
370
384
|
end
|
371
385
|
|
372
|
-
end
|
386
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynarex-blog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors: []
|
7
7
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2011-01-11 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|