annotate lib/patchablegraph/patchablegraph.py @ 1463:2452dd87aa9c

release 0.9.0 Ignore-this: cd9ac6c22e35bce9d647edd85ab75acd darcs-hash:ee94646423f37369ac319ecce7a49cb108ecada4
author drewp <drewp@bigasterisk.com>
date Sun, 24 Nov 2019 00:02:42 -0800
parents 43791ec0beb2
children d8eba5a51c1f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
1 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2 Design:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 1. Services each have (named) graphs, which they patch as things
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5 change. PatchableGraph is an object for holding this graph.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6 2. You can http GET that graph, or ...
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7 3. You can http GET/SSE that graph and hear about modifications to it
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 4. The client that got the graph holds and maintains a copy. The
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 client may merge together multiple graphs.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10 5. Client queries its graph with low-level APIs or client-side sparql.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 6. When the graph changes, the client knows and can update itself at
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 low or high granularity.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15 See also:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 * http://iswc2007.semanticweb.org/papers/533.pdf RDFSync: efficient remote synchronization of RDF
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 models
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18 * https://www.w3.org/2009/12/rdf-ws/papers/ws07 Supporting Change Propagation in RDF
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 * https://www.w3.org/DesignIssues/lncs04/Diff.pdf Delta: an ontology for the distribution of
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20 differences between RDF graphs
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23 import json, logging, itertools
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25 from greplin import scales
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 from rdfdb.grapheditapi import GraphEditApi
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 from rdflib import ConjunctiveGraph
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28 from rdflib.parser import StringInputSource
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29 from rdflib_jsonld.serializer import from_rdf
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 import cyclone.sse
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 from cycloneerr import PrettyErrorHandler
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 from rdfdb.patch import Patch
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 from rdfdb.rdflibpatch import patchQuads, inGraph
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 log = logging.getLogger('patchablegraph')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 def writeGraphResponse(req, graph, acceptHeader):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39 if acceptHeader == 'application/nquads':
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 req.set_header('Content-type', 'application/nquads')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 graph.serialize(req, format='nquads')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 elif acceptHeader == 'application/ld+json':
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
43 req.set_header('Content-type', 'application/ld+json')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
44 graph.serialize(req, format='json-ld', indent=2)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
45 else:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
46 req.set_header('Content-type', 'application/x-trig')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
47 graph.serialize(req, format='trig')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
48
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
49 # forked from /my/proj/light9/light9/rdfdb/rdflibpatch.py
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
50 def _graphFromQuads2(q):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51 g = ConjunctiveGraph()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52 #g.addN(q) # no effect on nquad output
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53 for s,p,o,c in q:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
54 g.get_context(c).add((s,p,o)) # kind of works with broken rdflib nquad serializer code
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55 #g.store.add((s,p,o), c) # no effect on nquad output
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 return g
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58 def jsonFromPatch(p):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59 return json.dumps({'patch': {
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 'adds': from_rdf(_graphFromQuads2(p.addQuads)),
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 'deletes': from_rdf(_graphFromQuads2(p.delQuads)),
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62 }})
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63 patchAsJson = jsonFromPatch # deprecated name
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
64
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66 def patchFromJson(j):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67 body = json.loads(j)['patch']
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 a = ConjunctiveGraph()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 a.parse(StringInputSource(json.dumps(body['adds']).encode('utf8')), format='json-ld')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70 d = ConjunctiveGraph()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 d.parse(StringInputSource(json.dumps(body['deletes']).encode('utf8')), format='json-ld')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 return Patch(addGraph=a, delGraph=d)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74 def graphAsJson(g):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75 # This is not the same as g.serialize(format='json-ld')! That
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76 # version omits literal datatypes.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 return json.dumps(from_rdf(g))
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 _graphsInProcess = itertools.count()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80 class PatchableGraph(GraphEditApi):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82 Master graph that you modify with self.patch, and we get the
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83 updates to all current listeners.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 def __init__(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86 self._graph = ConjunctiveGraph()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87 self._observers = []
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88 scales.init(self, '/patchableGraph%s' % next(_graphsInProcess))
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90 _serialize = scales.PmfStat('serialize')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91 def serialize(self, to, **kw):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92 with self._serialize.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 return self._graph.serialize(to, **kw)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 _patch = scales.PmfStat('patch')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96 _len = scales.IntStat('statementCount')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97 def patch(self, p):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
98 with self._patch.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
99 # assuming no stmt is both in p.addQuads and p.delQuads.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100 dels = set([q for q in p.delQuads if inGraph(q, self._graph)])
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
101 adds = set([q for q in p.addQuads if not inGraph(q, self._graph)])
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
102 minimizedP = Patch(addQuads=adds, delQuads=dels)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
103 if minimizedP.isNoop():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
104 return
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
105 patchQuads(self._graph,
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
106 deleteQuads=dels,
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
107 addQuads=adds,
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
108 perfect=False) # true?
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
109 for ob in self._observers:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
110 ob(patchAsJson(p))
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
111 self._len = len(self._graph)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113 def asJsonLd(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114 return graphAsJson(self._graph)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
115
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
116 _currentObservers = scales.IntStat('observers/current')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
117 _observersAdded = scales.IntStat('observers/added')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
118 def addObserver(self, onPatch):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
119 self._observers.append(onPatch)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
120 self._currentObservers = len(self._observers)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
121 self._observersAdded += 1
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
122
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
123 def removeObserver(self, onPatch):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
124 try:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
125 self._observers.remove(onPatch)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
126 except ValueError:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
127 pass
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
128 self._currentObservers = len(self._observers)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
129
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
130 def setToGraph(self, newGraph):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
131 self.patch(Patch.fromDiff(self._graph, newGraph))
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
132
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
133 _sendSimpleGraph = scales.PmfStat('serve/simpleGraph')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
134 _sendFullGraph = scales.PmfStat('serve/events/sendFull')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
135 _sendPatch = scales.PmfStat('serve/events/sendPatch')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
136
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
137 class CycloneGraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
138 def initialize(self, masterGraph):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
139 self.masterGraph = masterGraph
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
140
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
141 def get(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
142 with self.masterGraph._sendSimpleGraph.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
143 writeGraphResponse(self, self.masterGraph,
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
144 self.request.headers.get('accept'))
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
145
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
146
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
147 class CycloneGraphEventsHandler(cyclone.sse.SSEHandler):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
148 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
149 One session with one client.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
150
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
151 returns current graph plus future patches to keep remote version
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
152 in sync with ours.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
153
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
154 intsead of turning off buffering all over, it may work for this
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
155 response to send 'x-accel-buffering: no', per
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
156 http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
157 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
158 def __init__(self, application, request, masterGraph):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
159 cyclone.sse.SSEHandler.__init__(self, application, request)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
160 self.masterGraph = masterGraph
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
161
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
162 def bind(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
163 with self.masterGraph._sendFullGraph.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
164 graphJson = self.masterGraph.asJsonLd()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
165 log.debug("send fullGraph event: %s", graphJson)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
166 self.sendEvent(message=graphJson, event=b'fullGraph')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
167 self.masterGraph.addObserver(self.onPatch)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
168
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
169 def onPatch(self, patchJson):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
170 with self.masterGraph._sendPatch.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
171 # throttle and combine patches here- ideally we could see how
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
172 # long the latency to the client is to make a better rate choice
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
173 self.sendEvent(message=patchJson, event=b'patch')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
174
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
175 def unbind(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
176 self.masterGraph.removeObserver(self.onPatch)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
177