Mercurial > code > home > repos > light9
annotate bin/rdfdb @ 799:fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
Ignore-this: 2ea847e25af9b784cfec6aa4335dcc70
author | drewp@bigasterisk.com |
---|---|
date | Mon, 16 Jul 2012 21:51:04 +0000 |
parents | 5c158d37f1ce |
children | caeaa88430b8 |
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 | |
68 Our web ui: | |
69 | |
70 registered clients | |
71 | |
72 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
|
73 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
|
74 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
|
75 drags a slider) and collapse them into a single edit for clarity. |
796 | 76 |
77 """ | |
78 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
|
79 import twisted.internet.error |
796 | 80 import sys, optparse, logging, json, os |
81 import cyclone.web, cyclone.httpclient, cyclone.websocket | |
82 sys.path.append(".") | |
83 from light9 import networking, showconfig | |
84 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
|
85 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
|
86 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
|
87 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
|
88 from light9.rdfdb import syncedgraph |
796 | 89 |
90 from twisted.internet.inotify import INotify | |
91 logging.basicConfig(level=logging.DEBUG) | |
92 log = logging.getLogger() | |
93 | |
94 try: | |
95 import sys | |
96 sys.path.append("../homeauto/lib") | |
97 from cycloneerr import PrettyErrorHandler | |
98 except ImportError: | |
99 class PrettyErrorHandler(object): | |
100 pass | |
101 | |
102 class Client(object): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
103 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
104 one of our syncedgraph clients |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
105 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
106 def __init__(self, updateUri, label, db): |
796 | 107 self.db = db |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
108 self.label = label |
796 | 109 self.updateUri = updateUri |
110 self.sendAll() | |
111 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
112 def __repr__(self): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
113 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
|
114 |
796 | 115 def sendAll(self): |
116 """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
|
117 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
|
118 (self.label, self.updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
119 self.sendPatch(Patch( |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
120 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
|
121 delQuads=[])) |
796 | 122 |
123 def sendPatch(self, p): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
124 return syncedgraph.sendPatch(self.updateUri, p) |
796 | 125 |
126 class Db(object): | |
127 def __init__(self): | |
128 self.clients = [] | |
129 self.graph = ConjunctiveGraph() | |
130 | |
131 notifier = INotify() | |
132 notifier.startReading() | |
133 | |
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
|
134 for inFile in [#"show/dance2012/config.n3", |
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
135 "show/dance2012/subs/bcools", |
799
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
136 "show/dance2012/subs/bwarm", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
137 "show/dance2012/subs/house", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
138 "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
|
139 ]: |
796 | 140 self.g = GraphFile(notifier, |
141 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
|
142 URIRef("http://example.com/file/%s" % |
796 | 143 os.path.basename(inFile)), |
144 self.patch, | |
145 self.getSubgraph) | |
146 | |
147 def patch(self, p): | |
148 """ | |
149 apply this patch to the master graph then notify everyone about it | |
150 """ | |
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 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
|
152 |
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
|
153 patchQuads(self.graph, p.delQuads, p.addQuads, perfect=True) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
154 |
796 | 155 self.summarizeToLog() |
156 for c in self.clients: | |
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
|
157 d = c.sendPatch(p) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
158 d.addErrback(self.clientErrored, c) |
796 | 159 sendToLiveClients(asJson=p.jsonRepr) |
160 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
161 def clientErrored(self, err, c): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
162 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
|
163 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
|
164 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
|
165 self.sendClientsToAllLivePages() |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
166 |
796 | 167 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
|
168 log.info("contexts in graph (%s total stmts):" % len(self.graph)) |
796 | 169 for c in self.graph.contexts(): |
170 log.info(" %s: %s statements" % | |
171 (c.identifier, len(self.getSubgraph(c.identifier)))) | |
172 | |
173 def getSubgraph(self, uri): | |
174 # this is returning an empty Graph :( | |
175 #return self.graph.get_context(uri) | |
176 | |
177 g = Graph() | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
178 for s in self.graph.triples(ALLSTMTS, uri): |
796 | 179 g.add(s) |
180 return g | |
181 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
182 def addClient(self, updateUri, label): |
796 | 183 [self.clients.remove(c) |
184 for c in self.clients if c.updateUri == updateUri] | |
185 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
186 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
|
187 self.clients.append(Client(updateUri, label, self)) |
796 | 188 self.sendClientsToAllLivePages() |
189 | |
190 def sendClientsToAllLivePages(self): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
191 sendToLiveClients({"clients":[ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
192 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
|
193 for c in self.clients]}) |
796 | 194 |
195 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): | |
196 def get(self): | |
197 self.set_header("Content-Type", "application/xhtml+xml") | |
198 self.write(open("light9/rdfdb.xhtml").read()) | |
199 | |
200 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler): | |
201 def get(self): | |
202 pass | |
203 | |
204 class Patches(PrettyErrorHandler, cyclone.web.RequestHandler): | |
205 def __init__(self, *args, **kw): | |
206 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
|
207 p = syncedgraph.makePatchEndpointPutMethod(self.settings.db.patch) |
796 | 208 self.put = lambda: p(self) |
209 | |
210 def get(self): | |
211 pass | |
212 | |
213 | |
214 class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler): | |
215 def get(self): | |
216 pass | |
217 | |
218 def post(self): | |
219 upd = self.get_argument("clientUpdate") | |
220 try: | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
221 self.settings.db.addClient(upd, self.get_argument("label")) |
796 | 222 except: |
223 import traceback | |
224 traceback.print_exc() | |
225 raise | |
226 | |
227 liveClients = set() | |
228 def sendToLiveClients(d=None, asJson=None): | |
229 j = asJson or json.dumps(d) | |
230 for c in liveClients: | |
231 c.sendMessage(j) | |
232 | |
233 class Live(cyclone.websocket.WebSocketHandler): | |
234 | |
235 def connectionMade(self, *args, **kwargs): | |
236 log.info("ws opened") | |
237 liveClients.add(self) | |
238 self.settings.db.sendClientsToAllLivePages() | |
239 | |
240 def connectionLost(self, reason): | |
241 log.info("ws closed") | |
242 liveClients.remove(self) | |
243 | |
244 def messageReceived(self, message): | |
245 log.info("got message %s" % message) | |
246 self.sendMessage(message) | |
247 | |
248 if __name__ == "__main__": | |
249 logging.basicConfig() | |
250 log = logging.getLogger() | |
251 | |
252 parser = optparse.OptionParser() | |
253 parser.add_option('--show', | |
254 help='show URI, like http://light9.bigasterisk.com/show/dance2008', | |
255 default=showconfig.showUri()) | |
256 parser.add_option("-v", "--verbose", action="store_true", | |
257 help="logging.DEBUG") | |
258 (options, args) = parser.parse_args() | |
259 | |
260 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
261 | |
262 if not options.show: | |
263 raise ValueError("missing --show http://...") | |
264 | |
265 db = Db() | |
266 | |
267 port = 8051 | |
268 reactor.listenTCP(port, cyclone.web.Application(handlers=[ | |
269 (r'/', Index), | |
270 (r'/live', Live), | |
271 (r'/graph', GraphResource), | |
272 (r'/patches', Patches), | |
273 (r'/graphClients', GraphClients), | |
274 | |
275 (r"/(jquery-1\.7\.2\.min\.js)", cyclone.web.StaticFileHandler, | |
276 dict(path='lib')), | |
277 | |
278 ], db=db)) | |
279 log.info("serving on %s" % port) | |
280 reactor.run() |