Skip to content

Commit 7b5c28f

Browse files
author
Colin James
committed
Factor out event reification
In order to provide event records to subscribers, we must convert the accumulated events of the form (table, objref, time) into event records. The process of doing this is simple for objects in the database. The only difference is that deletion events do not provide a snapshot (as the object has been deleted). To avoid repeating ourselves, we define an "events_of" function that accumulates event records. The function takes an argument that specifies whether an attempt to provide a snapshot should be performed. The reification of events associated with messages - which are not stored in the database - is untouched. This relies on a callback instated elsewhere. Signed-off-by: Colin James <[email protected]>
1 parent 196f4e9 commit 7b5c28f

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

ocaml/xapi/xapi_event.ml

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -651,39 +651,32 @@ let from_inner __context session subs from from_t timer batching =
651651
; snapshot
652652
}
653653
in
654-
let events =
655-
List.fold_left
656-
(fun acc x ->
657-
let ev = event_of `del x in
658-
if Subscription.event_matches subs ev then ev :: acc else acc
659-
)
660-
[] deletes
661-
in
662-
let events =
663-
List.fold_left
664-
(fun acc (table, objref, mtime) ->
654+
let events_of ~kind ?(with_snapshot = true) entries acc =
655+
let rec go events ((table, obj, _time) as entry) =
656+
let snapshot =
665657
let serialiser = Eventgen.find_get_record table in
666-
try
667-
let xml = serialiser ~__context ~self:objref () in
668-
let ev = event_of `_mod ?snapshot:xml (table, objref, mtime) in
669-
if Subscription.event_matches subs ev then ev :: acc else acc
670-
with _ -> acc
671-
)
672-
events mods
658+
if with_snapshot then
659+
serialiser ~__context ~self:obj ()
660+
else
661+
None
662+
in
663+
let event = event_of kind ?snapshot entry in
664+
if Subscription.event_matches subs event then
665+
event :: events
666+
else
667+
events
668+
in
669+
List.fold_left go acc entries
673670
in
674671
let events =
675-
List.fold_left
676-
(fun acc (table, objref, ctime) ->
677-
let serialiser = Eventgen.find_get_record table in
678-
try
679-
let xml = serialiser ~__context ~self:objref () in
680-
let ev = event_of `add ?snapshot:xml (table, objref, ctime) in
681-
if Subscription.event_matches subs ev then ev :: acc else acc
682-
with _ -> acc
683-
)
684-
events creates
672+
[] (* Accumulate the events for objects stored in the database. *)
673+
|> events_of ~kind:`del ~with_snapshot:false deletes
674+
|> events_of ~kind:`_mod mods
675+
|> events_of ~kind:`add creates
685676
in
686677
let events =
678+
(* Messages require a special casing as their contents are not
679+
stored in the database. *)
687680
List.fold_left
688681
(fun acc mev ->
689682
let event =

0 commit comments

Comments
 (0)