Mercurial > code > home > repos > light9
annotate bin/rdfdb @ 808:a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
Ignore-this: 8c75ec3e2bd360c6eb87f7f4d4b3dcc4
author | drewp@bigasterisk.com |
---|---|
date | Thu, 19 Jul 2012 04:23:06 +0000 |
parents | 6d8f0c088a26 |
children | b19cd005a491 |
rev | line source |
---|---|
796 | 1 #!bin/python |
2 """ | |
3 other tools POST themselves to here as subscribers to the graph. They | |
4 are providing a URL we can PUT to with graphs updates. | |
5 | |
6 we immediately PUT them back all the contents of the graph as a bunch | |
7 of adds. | |
8 | |
9 later we PUT them back with updates (add/del lists) when there are | |
10 changes. | |
11 | |
12 If we fail to reach a registered caller, we forget about it for future | |
13 calls. We can PUT empty diffs as a heartbeat to notice disappearing | |
14 callers faster. | |
15 | |
16 A caller can submit add/del changes that should be persisted and | |
17 broadcast. | |
18 | |
19 Global data undo should probably happen within this service. | |
20 | |
21 Maybe some subgraphs are for transient data (e.g. current timecode, | |
22 mouse position in curvecalc) that only some listeners want to hear about. | |
23 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
24 Deletes are graph-specific, so callers may be surprised to delete a |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
25 stmt from one graph but then find that statement is still true. |
796 | 26 |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
27 Alternate plan: would it help to insist that every patch is within |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
28 only one subgraph? I think it's ok for them to span multiple ones. |
796 | 29 |
30 Inserts can be made on any subgraphs, and each subgraph is saved in | |
31 its own file. The file might not be in a format that can express | |
32 graphs, so I'm just going to not store the subgraph URI in any file. | |
33 | |
34 I don't support wildcard deletes, and there are race conditions where a | |
35 s-p could end up with unexpected multiple objects. Every client needs | |
36 to be ready for this. | |
37 | |
38 We watch the files and push their own changes back to the clients. | |
39 | |
40 Persist our client list, to survive restarts. In another rdf file? A | |
41 random json one? memcache? Also hold the recent changes. We're not | |
42 logging everything forever, though, since the output files and a VCS | |
43 shall be used for that | |
44 | |
45 Bnodes: this rdfdb graph might be able to track bnodes correctly, and | |
46 they make for more compact n3 files. I'm not sure if it's going to be | |
47 hard to keep the client bnodes in sync though. File rereads would be | |
48 hard,if ever a bnode was used across graphs, so that probably should | |
49 not be allowed. | |
50 | |
51 Our API: | |
52 | |
53 GET / ui | |
54 GET /graph the whole graph (needed? just for ui browsing?) | |
55 PUT /patches clients submit changes | |
56 GET /patches (recent) patches from clients | |
57 POST /graphClients clientUpdate={uri} to subscribe | |
58 GET /graphClients current clients | |
59 | |
60 format: | |
61 json {"adds" : [[quads]...], | |
62 "deletes": [[quads]], | |
63 "from" : tooluri, | |
64 "created":tttt | |
65 } | |
66 maybe use some http://json-ld.org/ in there. | |
67 | |
806 | 68 proposed rule feature: |
69 rdfdb should be able to watch a pair of (sourceFile, rulesFile) and | |
70 rerun the rules when either one changes. Should the sourceFile be able | |
71 to specify its own rules file? That would be easier | |
72 configuration. How do edits work? Not allowed? Patch the source only? | |
73 Also see the source graph loaded into a different ctx, and you can | |
74 edit that one and see the results in the output context? | |
75 | |
796 | 76 Our web ui: |
77 | |
78 registered clients | |
79 | |
80 recent edits, each one says what client it came from. You can reverse | |
799
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
81 them here. We should be able to take patches that are close in time |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
82 and keep updating the same data (e.g. a stream of changes as the user |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
83 drags a slider) and collapse them into a single edit for clarity. |
796 | 84 |
806 | 85 Ways to display patches, using labels and creator/subj icons where possible: |
86 | |
87 <creator> set <subj>'s <p> to <o> | |
88 <creator> changed <subj>'s <pred> from <o1> to <o2> | |
89 <creator> added <o> to <s> <p> | |
90 | |
803 | 91 |
796 | 92 """ |
93 from twisted.internet import reactor | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
94 import twisted.internet.error |
796 | 95 import sys, optparse, logging, json, os |
96 import cyclone.web, cyclone.httpclient, cyclone.websocket | |
97 sys.path.append(".") | |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
98 from light9 import networking, showconfig, prof |
796 | 99 from rdflib import ConjunctiveGraph, URIRef, Graph |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
100 from light9.rdfdb.graphfile import GraphFile |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
101 from light9.rdfdb.patch import Patch, ALLSTMTS |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
102 from light9.rdfdb.rdflibpatch import patchQuads |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
103 from light9.rdfdb import syncedgraph |
796 | 104 |
105 from twisted.internet.inotify import INotify | |
106 logging.basicConfig(level=logging.DEBUG) | |
107 log = logging.getLogger() | |
108 | |
109 try: | |
110 import sys | |
111 sys.path.append("../homeauto/lib") | |
112 from cycloneerr import PrettyErrorHandler | |
113 except ImportError: | |
114 class PrettyErrorHandler(object): | |
115 pass | |
116 | |
117 class Client(object): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
118 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
119 one of our syncedgraph clients |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
120 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
121 def __init__(self, updateUri, label, db): |
796 | 122 self.db = db |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
123 self.label = label |
796 | 124 self.updateUri = updateUri |
125 self.sendAll() | |
126 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
127 def __repr__(self): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
128 return "<%s client at %s>" % (self.label, self.updateUri) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
129 |
796 | 130 def sendAll(self): |
131 """send the client the whole graph contents""" | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
132 log.info("sending all graphs to %s at %s" % |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
133 (self.label, self.updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
134 self.sendPatch(Patch( |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
135 addQuads=self.db.graph.quads(ALLSTMTS), |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
136 delQuads=[])) |
796 | 137 |
138 def sendPatch(self, p): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
139 return syncedgraph.sendPatch(self.updateUri, p) |
796 | 140 |
141 class Db(object): | |
142 def __init__(self): | |
143 self.clients = [] | |
144 self.graph = ConjunctiveGraph() | |
145 | |
146 notifier = INotify() | |
147 notifier.startReading() | |
148 | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
149 for inFile in [#"show/dance2012/config.n3", |
801 | 150 "show/dance2012/patch.n3", |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
151 "show/dance2012/subs/bcools", |
799
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
152 "show/dance2012/subs/bwarm", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
153 "show/dance2012/subs/house", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
154 "demo.n3", |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
155 ]: |
796 | 156 self.g = GraphFile(notifier, |
157 inFile, | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
158 URIRef("http://example.com/file/%s" % |
796 | 159 os.path.basename(inFile)), |
160 self.patch, | |
161 self.getSubgraph) | |
162 | |
163 def patch(self, p): | |
164 """ | |
165 apply this patch to the master graph then notify everyone about it | |
166 """ | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
167 log.info("patching graph -%d +%d" % (len(p.delQuads), len(p.addQuads))) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
168 |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
169 patchQuads(self.graph, p.delQuads, p.addQuads, perfect=True) |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
170 senderUpdateUri = getattr(p, 'senderUpdateUri', None) |
796 | 171 self.summarizeToLog() |
172 for c in self.clients: | |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
173 print "send to %s? %s %s" % (c, c.updateUri, senderUpdateUri) |
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
174 if c.updateUri == senderUpdateUri: |
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
175 # this client has self-applied the patch already |
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
176 continue |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
177 d = c.sendPatch(p) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
178 d.addErrback(self.clientErrored, c) |
796 | 179 sendToLiveClients(asJson=p.jsonRepr) |
180 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
181 def clientErrored(self, err, c): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
182 err.trap(twisted.internet.error.ConnectError) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
183 log.info("connection error- dropping client %r" % c) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
184 self.clients.remove(c) |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
185 self.sendClientsToAllLivePages() |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
186 |
796 | 187 def summarizeToLog(self): |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
188 log.info("contexts in graph (%s total stmts):" % len(self.graph)) |
796 | 189 for c in self.graph.contexts(): |
190 log.info(" %s: %s statements" % | |
191 (c.identifier, len(self.getSubgraph(c.identifier)))) | |
192 | |
193 def getSubgraph(self, uri): | |
194 # this is returning an empty Graph :( | |
195 #return self.graph.get_context(uri) | |
196 | |
197 g = Graph() | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
198 for s in self.graph.triples(ALLSTMTS, uri): |
796 | 199 g.add(s) |
200 return g | |
201 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
202 def addClient(self, updateUri, label): |
796 | 203 [self.clients.remove(c) |
204 for c in self.clients if c.updateUri == updateUri] | |
205 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
206 log.info("new client %s at %s" % (label, updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
207 self.clients.append(Client(updateUri, label, self)) |
796 | 208 self.sendClientsToAllLivePages() |
209 | |
210 def sendClientsToAllLivePages(self): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
211 sendToLiveClients({"clients":[ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
212 dict(updateUri=c.updateUri, label=c.label) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
213 for c in self.clients]}) |
796 | 214 |
215 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): | |
216 def get(self): | |
217 self.set_header("Content-Type", "application/xhtml+xml") | |
218 self.write(open("light9/rdfdb.xhtml").read()) | |
219 | |
220 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler): | |
221 def get(self): | |
222 pass | |
223 | |
224 class Patches(PrettyErrorHandler, cyclone.web.RequestHandler): | |
225 def __init__(self, *args, **kw): | |
226 cyclone.web.RequestHandler.__init__(self, *args, **kw) | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
227 p = syncedgraph.makePatchEndpointPutMethod(self.settings.db.patch) |
796 | 228 self.put = lambda: p(self) |
229 | |
230 def get(self): | |
231 pass | |
232 | |
233 | |
234 class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler): | |
235 def get(self): | |
236 pass | |
237 | |
238 def post(self): | |
239 upd = self.get_argument("clientUpdate") | |
240 try: | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
241 self.settings.db.addClient(upd, self.get_argument("label")) |
796 | 242 except: |
243 import traceback | |
244 traceback.print_exc() | |
245 raise | |
246 | |
247 liveClients = set() | |
248 def sendToLiveClients(d=None, asJson=None): | |
249 j = asJson or json.dumps(d) | |
250 for c in liveClients: | |
251 c.sendMessage(j) | |
252 | |
253 class Live(cyclone.websocket.WebSocketHandler): | |
254 | |
255 def connectionMade(self, *args, **kwargs): | |
256 log.info("ws opened") | |
257 liveClients.add(self) | |
258 self.settings.db.sendClientsToAllLivePages() | |
259 | |
260 def connectionLost(self, reason): | |
261 log.info("ws closed") | |
262 liveClients.remove(self) | |
263 | |
264 def messageReceived(self, message): | |
265 log.info("got message %s" % message) | |
266 self.sendMessage(message) | |
267 | |
268 if __name__ == "__main__": | |
269 logging.basicConfig() | |
270 log = logging.getLogger() | |
271 | |
272 parser = optparse.OptionParser() | |
273 parser.add_option('--show', | |
274 help='show URI, like http://light9.bigasterisk.com/show/dance2008', | |
275 default=showconfig.showUri()) | |
276 parser.add_option("-v", "--verbose", action="store_true", | |
277 help="logging.DEBUG") | |
278 (options, args) = parser.parse_args() | |
279 | |
280 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
281 | |
282 if not options.show: | |
283 raise ValueError("missing --show http://...") | |
284 | |
285 db = Db() | |
286 | |
287 port = 8051 | |
288 reactor.listenTCP(port, cyclone.web.Application(handlers=[ | |
289 (r'/', Index), | |
290 (r'/live', Live), | |
291 (r'/graph', GraphResource), | |
292 (r'/patches', Patches), | |
293 (r'/graphClients', GraphClients), | |
294 | |
295 (r"/(jquery-1\.7\.2\.min\.js)", cyclone.web.StaticFileHandler, | |
296 dict(path='lib')), | |
297 | |
298 ], db=db)) | |
299 log.info("serving on %s" % port) | |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
300 prof.run(reactor.run, profile=False) |