-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.lisp
241 lines (207 loc) · 8.92 KB
/
channel.lisp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
;;;; channel.lisp --- The channel class represents a time-series of homogeneous data.
;;;;
;;;; Copyright (C) 2011-2017 Jan Moringen
;;;;
;;;; Author: Jan Moringen <[email protected]>
(cl:in-package #:rsbag)
(defclass channel (plist-meta-data-mixin
#+sbcl sequence
print-items:print-items-mixin)
((bag :initarg :bag
:reader channel-bag
:documentation
"Stores the bag instance in which this channel is
contained.")
(name :initarg :name
:type string
:reader channel-name
:documentation
"Stores the name of the channel.")
(meta-data :reader channel-meta-data)
(transform :initarg :transform
:reader channel-transform
:initform nil
:documentation
"Stores a transformation that should be applied to
entries when they are retrieved or stored.")
(id :initarg :id
:reader channel-%id
:documentation
"Stores the id of the channel.")
(backend :initarg :backend
:reader channel-%backend
:documentation
"Stores a reference to the backend object which
implements access to the bag to which this channel
belongs."))
(:default-initargs
:bag (missing-required-initarg 'channel :bag)
:name (missing-required-initarg 'channel :name)
:id (missing-required-initarg 'channel :id)
:backend (missing-required-initarg 'channel :backend))
(:documentation
"Instances of this class represent time-series of homogeneous data
items."))
(defmethod print-items:print-items append ((object channel))
(let+ (((&structure-r/o channel- name transform) object)
(length (length object))
(transform (when transform
(rsbag.transform:transform-name transform))))
`((:name ,name "~S")
(:length ,length " (~:D)" ((:after :name)))
(:transform ,transform "~@[ ~A~]" ((:after :length))))))
(defmethod channel-timestamps/raw ((channel channel))
(let+ (((&structure-r/o channel- (id %id) (backend %backend)) channel))
(rsbag.backend:get-timestamps backend id)))
(defmethod channel-timestamps ((channel channel))
(make-instance 'channel-timestamps
:timestamps (channel-timestamps/raw channel)))
#+sbcl
(defmethod channel-entries ((channel channel))
;; Since CHANNEL is a sequence of its entries, nothing has to be
;; done.
channel)
#+sbcl
(defmethod channel-items ((channel channel))
;; Return an instance of `channel-items' which presents pairs of
;; timestamps and entries.
(make-instance 'channel-items :channel channel))
(defmethod entry :around ((channel channel)
(index t)
&key
(if-does-not-exist nil)
(transform (channel-transform channel)))
(check-type if-does-not-exist if-does-not-exist-policy)
(call-next-method channel index
:if-does-not-exist if-does-not-exist
:transform transform))
(flet ((return-entry (channel index entry if-does-not-exist transform)
(let ((raw (or entry
(ecase if-does-not-exist
(:error (error 'no-such-entry
:bag (channel-bag channel)
:channel channel
:key index))
((nil) nil)))))
(if transform
(rsbag.transform:decode transform raw)
raw))))
(declare (inline return-entry))
(defmethod entry ((channel channel)
(index integer)
&key if-does-not-exist transform)
(let+ (((&structure-r/o channel- (id %id) (backend %backend)) channel)
(raw (rsbag.backend:get-entry-at-index backend id index)))
(return-entry channel index raw if-does-not-exist transform)))
(defmethod entry ((channel channel)
(index local-time:timestamp)
&key
if-does-not-exist transform)
(let+ (((&structure-r/o channel- (id %id) (backend %backend)) channel)
(timestamp (rsbag.backend:timestamp->uint64 index))
(raw (rsbag.backend:get-entry-at-time backend id timestamp)))
(return-entry channel index raw if-does-not-exist transform))))
(defmethod (setf entry) :around ((new-value t)
(channel channel)
(index t)
&key
(if-exists :error)
(transform (channel-transform channel)))
(check-type if-exists if-exists-policy)
(when (eq if-exists :supersede)
(error "Superseding entries is not supported yet"))
(unless (member (bag-direction (channel-bag channel)) '(:output :io))
(error 'direction-error
:bag (channel-bag channel)
:expected-direction '(member :output :io)))
(call-next-method new-value channel index
:if-exists if-exists
:transform transform))
(defmethod (setf entry) ((new-value t)
(channel channel)
(index local-time:timestamp)
&key if-exists transform)
(declare (ignore if-exists))
(let+ (((&structure-r/o channel- (id %id) (backend %backend)) channel)
(timestamp (rsbag.backend:timestamp->uint64 index))
(raw (if transform
(rsbag.transform:encode transform new-value)
new-value)))
(rsbag.backend:put-entry backend id timestamp raw)
new-value))
;;; Time range protocol
(defmethod start-timestamp ((channel channel))
(unless (emptyp channel)
(elt (channel-timestamps channel) 0)))
(defmethod end-timestamp ((channel channel))
(unless (emptyp channel)
(elt (channel-timestamps channel) (1- (length channel)))))
;;; Sequence protocol
#+sbcl
(defmethod sequence:length ((sequence channel))
(let+ (((&structure-r/o channel- (id %id) (backend %backend)) sequence))
(rsbag.backend:get-num-entries backend id)))
#+sbcl
(defmethod sequence:make-sequence-like ((sequence channel)
(length integer)
&rest args
&key
initial-element
initial-contents)
(declare (ignore initial-element initial-contents))
(apply #'make-array length args))
#+sbcl
(defmethod sequence:elt ((sequence channel) (index integer))
(entry sequence index))
;;; `channel-timestamps' sequence class
#+sbcl
(defclass channel-timestamps (standard-object
sequence)
((timestamps :initarg :timestamps
:accessor %timestamps
:documentation
"Stores the sequence of associated timestamps for the
entries of the channel."))
(:default-initargs
:timestamps (missing-required-initarg 'channel-timestamps :timestamps))
(:documentation
"A sequence of timestamps for the entries of a channel."))
#+sbcl
(defmethod sequence:length ((sequence channel-timestamps))
(length (%timestamps sequence)))
#+sbcl
(defmethod sequence:elt ((sequence channel-timestamps)
(index integer))
(rsbag.backend:uint64->timestamp (elt (%timestamps sequence) index)))
;;; `channel-items' sequence class
#+sbcl
(defclass channel-items (standard-object
sequence)
((channel :initarg :channel
:reader %channel
:documentation
"Stores the channel the items of which are used.")
(timestamps :accessor %timestamps
:documentation
"Stores the sequence of associated timestamps for the
entries of the channel."))
(:default-initargs
:channel (missing-required-initarg 'channel-items :channel))
(:documentation
"Instances of this class can be used to access the timestamps and
associated entries of a channel."))
#+sbcl
(defmethod shared-initialize :after ((instance channel-items)
(slot-names t)
&key
channel)
(setf (%timestamps instance) (channel-timestamps/raw channel)))
#+sbcl
(defmethod sequence:length ((sequence channel-items))
(length (%channel sequence)))
#+sbcl
(defmethod sequence:elt ((sequence channel-items) (index integer))
(let+ (((&accessors-r/o (channel %channel) (timestamps %timestamps))
sequence))
(list (rsbag.backend:uint64->timestamp (elt timestamps index))
(elt channel index))))