forked from antirez/redis-timeseries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis-timeseries.rb
163 lines (150 loc) · 4.83 KB
/
redis-timeseries.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright (C) 2010-2012 Salvatore Sanfilippo.
# Licensed under the BSD two clause license, see the COPYING file shipped
# with this source distribution for more information.
require 'base64'
class RedisTimeSeries
def initialize(prefix,timestep,redis)
@prefix = prefix
@timestep = timestep
@redis = redis
end
def normalize_time(t)
t = t.to_i
t - (t % @timestep)
end
def getkey(t)
"ts:#{@prefix}:#{normalize_time t}"
end
def tsencode(data)
if data.index("\x00") or data.index("\x01")
"E#{Base64.encode64(data)}"
else
"R#{data}"
end
end
def tsdecode(data)
if data[0..0] == 'E'
Base64.decode64(data[1..-1])
else
data[1..-1]
end
end
def add(data,origin_time=nil)
data = tsencode(data)
origin_time = tsencode(origin_time) if origin_time
now = Time.now.to_f
value = "#{now}\x01#{data}"
value << "\x01#{origin_time}" if origin_time
value << "\x00"
@redis.append(getkey(now.to_i),value)
end
def decode_record(r)
res = {}
s = r.split("\x01")
res[:time] = s[0].to_f
res[:data] = tsdecode(s[1])
if s[2]
res[:origin_time] = tsdecode(s[2])
else
res[:origin_time] = nil
end
return res
end
def seek(time)
best_start = nil
best_time = nil
rangelen = 64
key = getkey(time.to_i)
len = @redis.strlen(key)
return 0 if len == 0
min = 0
max = len-1
while true
p = min+((max-min)/2)
# puts "Min: #{min} Max: #{max} P: #{p}"
# Seek the first complete record starting from position 'p'.
# We need to search for two consecutive \x00 chars, and enlarnge
# the range if needed as we don't know how big the record is.
while true
range_end = p+rangelen-1
range_end = len if range_end > len
r = @redis.getrange(key,p,range_end)
# puts "GETRANGE #{p} #{range_end}"
if p == 0
sep = -1
else
sep = r.index("\x00")
end
sep2 = r.index("\x00",sep+1) if sep
if sep and sep2
record = r[((sep+1)...sep2)]
record_start = p+sep+1
record_end = p+sep2-1
dr = decode_record(record)
# Take track of the best sample, that is the sample
# that is greater than our sample, but with the smallest
# increment.
if dr[:time] >= time and (!best_time or best_time>dr[:time])
best_start = record_start
best_time = dr[:time]
# puts "NEW BEST: #{best_time}"
end
# puts "Max-Min #{max-min} RS #{record_start}"
return best_start if max-min == 1
break
end
# Already at the end of the string but still no luck?
return len+1 if range_end = len
# We need to enlrange the range, it is interesting to note
# that we take the enlarged value: likely other time series
# will be the same size on average.
rangelen *= 2
end
# puts dr.inspect
return record_start if dr[:time] == time
if dr[:time] > time
max = p
else
min = p
end
end
end
def produce_result(res,key,range_begin,range_end)
r = @redis.getrange(key,range_begin,range_end)
if r
s = r.split("\x00")
s.each{|r|
record = decode_record(r)
res << record
}
end
end
def fetch_range(begin_time,end_time)
res = []
begin_key = getkey(begin_time)
end_key = getkey(end_time)
begin_off = seek(begin_time)
end_off = seek(end_time)
if begin_key == end_key
# puts "#{begin_off} #{end_off} #{begin_key}"
produce_result(res,begin_key,begin_off,end_off-1)
else
produce_result(res,begin_key,begin_off,-1)
t = normalize_time(begin_time)
while true
t += @timestep
key = getkey(t)
break if key == end_key
produce_result(res,key,0,-1)
end
produce_result(res,end_key,0,end_off-1)
end
res
end
def fetch_timestep(time)
res = []
key = getkey(time)
produce_result(res,key,0,-1)
res
end
end