eventsource.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. /*
  11. Package brawler contains an eventsource for Brawler which forwards
  12. internal EliasDB events to Brawler engines.
  13. */
  14. package brawler
  15. import (
  16. "fmt"
  17. "io"
  18. "devt.de/krotik/common/defs/brawler"
  19. "devt.de/krotik/eliasdb/graph"
  20. "devt.de/krotik/eliasdb/graph/data"
  21. )
  22. /*
  23. AddEventPublisher adds an EventPublisher to a given Manager using an EventBridge.
  24. */
  25. func AddEventPublisher(gm *graph.Manager, publisher brawler.EventPublisher, errOut io.Writer) {
  26. gm.SetGraphRule(&EventBridge{publisher, errOut})
  27. }
  28. /*
  29. EventMapping is a mapping between EliasDB event types to Brawler event kinds.
  30. */
  31. var EventMapping = map[int]string{
  32. /*
  33. EventNodeCreated is thrown when a node gets created.
  34. Parameters: partition of created node, created node
  35. */
  36. graph.EventNodeCreated: "db.node.created",
  37. /*
  38. EventNodeUpdated is thrown when a node gets updated.
  39. Parameters: partition of updated node, updated node, old node
  40. */
  41. graph.EventNodeUpdated: "db.node.updated",
  42. /*
  43. EventNodeDeleted is thrown when a node gets deleted.
  44. Parameters: partition of deleted node, deleted node
  45. */
  46. graph.EventNodeDeleted: "db.node.deleted",
  47. /*
  48. EventEdgeCreated is thrown when an edge gets created.
  49. Parameters: partition of created edge, created edge
  50. */
  51. graph.EventEdgeCreated: "db.edge.created",
  52. /*
  53. EventEdgeUpdated is thrown when an edge gets updated.
  54. Parameters: partition of updated edge, updated edge, old edge
  55. */
  56. graph.EventEdgeUpdated: "db.edge.updated",
  57. /*
  58. EventEdgeDeleted is thrown when an edge gets deleted.
  59. Parameters: partition of deleted edge, deleted edge
  60. */
  61. graph.EventEdgeDeleted: "db.edge.deleted",
  62. }
  63. // Event bridge between EliasDB and Brawler
  64. // ===========================================
  65. /*
  66. EventBridge is a rule for a graph manager to forward all graph events to
  67. Brawler.
  68. */
  69. type EventBridge struct {
  70. publisher brawler.EventPublisher
  71. errOut io.Writer
  72. }
  73. /*
  74. Name returns the name of the rule.
  75. */
  76. func (r *EventBridge) Name() string {
  77. return "brawler.eventbridge"
  78. }
  79. /*
  80. Handles returns a list of events which are handled by this rule.
  81. */
  82. func (r *EventBridge) Handles() []int {
  83. return []int{graph.EventNodeCreated, graph.EventNodeUpdated, graph.EventNodeDeleted,
  84. graph.EventEdgeCreated, graph.EventEdgeUpdated, graph.EventEdgeDeleted}
  85. }
  86. /*
  87. Handle handles an event.
  88. */
  89. func (r *EventBridge) Handle(gm *graph.Manager, trans graph.Trans, event int, ed ...interface{}) error {
  90. if name, ok := EventMapping[event]; ok {
  91. // Build up state
  92. state := map[interface{}]interface{}{
  93. "part": fmt.Sprint(ed[0]),
  94. }
  95. switch event {
  96. case graph.EventNodeCreated:
  97. state["node"] = ed[1].(data.Node)
  98. case graph.EventNodeUpdated:
  99. state["node"] = ed[1].(data.Node)
  100. state["old_node"] = ed[2].(data.Node)
  101. case graph.EventNodeDeleted:
  102. state["node"] = ed[1].(data.Node)
  103. case graph.EventEdgeCreated:
  104. state["edge"] = ed[1].(data.Edge)
  105. case graph.EventEdgeUpdated:
  106. state["edge"] = ed[1].(data.Edge)
  107. state["old_edge"] = ed[2].(data.Edge)
  108. case graph.EventEdgeDeleted:
  109. state["edge"] = ed[1].(data.Edge)
  110. }
  111. // Try to inject the event
  112. err := r.publisher.AddEvent(name, []string{name}, state)
  113. if err != nil && r.errOut != nil {
  114. r.errOut.Write([]byte(err.Error()))
  115. }
  116. }
  117. return nil
  118. }