annotate lib/patchablegraph/patchablegraph.py @ 1505:3cdf76755812

reformat Ignore-this: e1fe14c4b973c3c1fc2714b988ecc7c8 darcs-hash:055eedef2e8db685f2c524ce4d2b608040d60b33
author drewp <drewp@bigasterisk.com>
date Mon, 03 Feb 2020 00:51:18 -0800
parents d8eba5a51c1f
children e5c0d387c248
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:
1504
d8eba5a51c1f try a text/plain response if we think it's a browser asking for the graph
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
46 print(f'acceptHeader {acceptHeader}')
d8eba5a51c1f try a text/plain response if we think it's a browser asking for the graph
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
47 if acceptHeader.startswith('text/html'):
d8eba5a51c1f try a text/plain response if we think it's a browser asking for the graph
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
48 # browser; should arrange to pick live view
d8eba5a51c1f try a text/plain response if we think it's a browser asking for the graph
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
49 req.set_header('Content-type', 'text/plain')
d8eba5a51c1f try a text/plain response if we think it's a browser asking for the graph
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
50 lines = graph.serialize(format='nquads')
d8eba5a51c1f try a text/plain response if we think it's a browser asking for the graph
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
51 lines.sort()
d8eba5a51c1f try a text/plain response if we think it's a browser asking for the graph
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
52 req.write(''.join(lines))
d8eba5a51c1f try a text/plain response if we think it's a browser asking for the graph
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
53 return
d8eba5a51c1f try a text/plain response if we think it's a browser asking for the graph
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
54
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55 req.set_header('Content-type', 'application/x-trig')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 graph.serialize(req, format='trig')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58 # forked from /my/proj/light9/light9/rdfdb/rdflibpatch.py
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59 def _graphFromQuads2(q):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 g = ConjunctiveGraph()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 #g.addN(q) # no effect on nquad output
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62 for s,p,o,c in q:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63 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
64 #g.store.add((s,p,o), c) # no effect on nquad output
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65 return g
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67 def jsonFromPatch(p):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 return json.dumps({'patch': {
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 'adds': from_rdf(_graphFromQuads2(p.addQuads)),
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70 'deletes': from_rdf(_graphFromQuads2(p.delQuads)),
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 }})
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 patchAsJson = jsonFromPatch # deprecated name
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73
1505
3cdf76755812 reformat
drewp <drewp@bigasterisk.com>
parents: 1504
diff changeset
74
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75 def patchFromJson(j):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76 body = json.loads(j)['patch']
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 a = ConjunctiveGraph()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78 a.parse(StringInputSource(json.dumps(body['adds']).encode('utf8')), format='json-ld')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 d = ConjunctiveGraph()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80 d.parse(StringInputSource(json.dumps(body['deletes']).encode('utf8')), format='json-ld')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81 return Patch(addGraph=a, delGraph=d)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83 def graphAsJson(g):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 # This is not the same as g.serialize(format='json-ld')! That
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 # version omits literal datatypes.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86 return json.dumps(from_rdf(g))
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88 _graphsInProcess = itertools.count()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89 class PatchableGraph(GraphEditApi):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91 Master graph that you modify with self.patch, and we get the
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92 updates to all current listeners.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94 def __init__(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 self._graph = ConjunctiveGraph()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96 self._observers = []
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97 scales.init(self, '/patchableGraph%s' % next(_graphsInProcess))
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
98
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
99 _serialize = scales.PmfStat('serialize')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100 def serialize(self, to, **kw):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
101 with self._serialize.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
102 return self._graph.serialize(to, **kw)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
103
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
104 _patch = scales.PmfStat('patch')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
105 _len = scales.IntStat('statementCount')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
106 def patch(self, p):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
107 with self._patch.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
108 # assuming no stmt is both in p.addQuads and p.delQuads.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
109 dels = set([q for q in p.delQuads if inGraph(q, self._graph)])
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
110 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
111 minimizedP = Patch(addQuads=adds, delQuads=dels)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112 if minimizedP.isNoop():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113 return
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114 patchQuads(self._graph,
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
115 deleteQuads=dels,
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
116 addQuads=adds,
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
117 perfect=False) # true?
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
118 for ob in self._observers:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
119 ob(patchAsJson(p))
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
120 self._len = len(self._graph)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
121
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
122 def asJsonLd(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
123 return graphAsJson(self._graph)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
124
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
125 _currentObservers = scales.IntStat('observers/current')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
126 _observersAdded = scales.IntStat('observers/added')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
127 def addObserver(self, onPatch):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
128 self._observers.append(onPatch)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
129 self._currentObservers = len(self._observers)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
130 self._observersAdded += 1
1505
3cdf76755812 reformat
drewp <drewp@bigasterisk.com>
parents: 1504
diff changeset
131
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
132 def removeObserver(self, onPatch):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
133 try:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
134 self._observers.remove(onPatch)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
135 except ValueError:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
136 pass
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
137 self._currentObservers = len(self._observers)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
138
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
139 def setToGraph(self, newGraph):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
140 self.patch(Patch.fromDiff(self._graph, newGraph))
1505
3cdf76755812 reformat
drewp <drewp@bigasterisk.com>
parents: 1504
diff changeset
141
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
142 _sendSimpleGraph = scales.PmfStat('serve/simpleGraph')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
143 _sendFullGraph = scales.PmfStat('serve/events/sendFull')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
144 _sendPatch = scales.PmfStat('serve/events/sendPatch')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
145
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
146 class CycloneGraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
147 def initialize(self, masterGraph):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
148 self.masterGraph = masterGraph
1505
3cdf76755812 reformat
drewp <drewp@bigasterisk.com>
parents: 1504
diff changeset
149
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
150 def get(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
151 with self.masterGraph._sendSimpleGraph.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
152 writeGraphResponse(self, self.masterGraph,
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
153 self.request.headers.get('accept'))
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
154
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
155
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
156 class CycloneGraphEventsHandler(cyclone.sse.SSEHandler):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
157 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
158 One session with one client.
1505
3cdf76755812 reformat
drewp <drewp@bigasterisk.com>
parents: 1504
diff changeset
159
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
160 returns current graph plus future patches to keep remote version
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
161 in sync with ours.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
162
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
163 intsead of turning off buffering all over, it may work for this
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
164 response to send 'x-accel-buffering: no', per
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
165 http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
166 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
167 def __init__(self, application, request, masterGraph):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
168 cyclone.sse.SSEHandler.__init__(self, application, request)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
169 self.masterGraph = masterGraph
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
170
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
171 def bind(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
172 with self.masterGraph._sendFullGraph.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
173 graphJson = self.masterGraph.asJsonLd()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
174 log.debug("send fullGraph event: %s", graphJson)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
175 self.sendEvent(message=graphJson, event=b'fullGraph')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
176 self.masterGraph.addObserver(self.onPatch)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
177
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
178 def onPatch(self, patchJson):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
179 with self.masterGraph._sendPatch.time():
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
180 # throttle and combine patches here- ideally we could see how
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
181 # long the latency to the client is to make a better rate choice
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
182 self.sendEvent(message=patchJson, event=b'patch')
1505
3cdf76755812 reformat
drewp <drewp@bigasterisk.com>
parents: 1504
diff changeset
183
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
184 def unbind(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
185 self.masterGraph.removeObserver(self.onPatch)