forked from brightroll/rq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashdir.rb
116 lines (102 loc) · 3.19 KB
/
hashdir.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
module RQ
class HashDir
# For now, the system is not configurable in terms of pattern match or depth
def self.make(path)
FileUtils.mkdir_p(path)
return true
end
def self.exist(que_base_path, state, msg_id)
parts = self.msg_id_parts(msg_id)
# parts = [ "YYYYmmDD", "HH", "MM" ]
# If we got bad data, return false
return false unless parts
File.exist?("#{que_base_path}/#{state}/#{parts[0]}/#{parts[1]}/#{parts[2]}/#{msg_id}")
end
# Do a DFT traverse in reverse order so most
# recent is first
def self.entries(path, limit = nil)
self.entries_int(path, 0, [], limit)
end
def self.num_entries(path)
self.num_entries_int(path, 0)
end
def self.inject(prev_msg_path, new_base_path, msg_id)
parts = self.msg_id_parts(msg_id)
FileUtils.mkdir_p("#{new_base_path}/#{parts[0]}/#{parts[1]}/#{parts[2]}")
newname = "#{new_base_path}/#{parts[0]}/#{parts[1]}/#{parts[2]}/#{msg_id}"
File.rename(prev_msg_path, newname)
end
def self.path_for(que_base_path, state, msg_id)
parts = self.msg_id_parts(msg_id)
"#{que_base_path}/#{state}/#{parts[0]}/#{parts[1]}/#{parts[2]}/#{msg_id}"
end
private
def self.num_entries_int(path, level)
sum = 0
if level == 0
# YYYYMMDD
ents1 = Dir.glob("#{path}/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
ents1.sort.reverse.each do |e|
sum += self.num_entries_int(e, 1)
end
elsif level == 1
# HH
ents1 = Dir.glob("#{path}/[0-9][0-9]")
ents1.sort.reverse.each do |e|
sum += self.num_entries_int(e, 2)
end
elsif level == 2
# MM
ents1 = Dir.glob("#{path}/[0-9][0-9]")
ents1.sort.reverse.each do |e|
sum += self.num_entries_int(e, 3)
end
elsif level == 3
# MESG-ID
ents1 = Dir.glob("#{path}/[0-9][0-9]*")
sum += ents1.length
end
sum
end
def self.entries_int(path, level, accum, limit = nil)
if level == 0
# YYYYMMDD
ents1 = Dir.glob("#{path}/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
ents1.sort.reverse.each do |e|
self.entries_int(e, 1, accum, limit)
break if limit && accum.length == limit
end
elsif level == 1
# HH
ents1 = Dir.glob("#{path}/[0-9][0-9]")
ents1.sort.reverse.each do |e|
self.entries_int(e, 2, accum, limit)
break if limit && accum.length == limit
end
elsif level == 2
# MM
ents1 = Dir.glob("#{path}/[0-9][0-9]")
ents1.sort.reverse.each do |e|
self.entries_int(e, 3, accum, limit)
break if limit && accum.length == limit
end
elsif level == 3
# MESG-ID
ents1 = Dir.glob("#{path}/[0-9][0-9]*")
ents1.sort.reverse.each do |e|
accum << e.split('/').last
break if limit && accum.length == limit
end
end
accum
end
def self.msg_id_parts(msg_id)
# Ex. msg_id 20100625.0127.35.122.7509656
if msg_id =~ /(\d\d\d\d\d\d\d\d)\.(\d\d)(\d\d)/
[$1, $2, $3]
else
nil
end
end
end
end