Changeset - 2fc0e726a3c3
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 8 years ago 2017-06-10 02:06:30
drewp@bigasterisk.com
rdfdb alternate graph formats for timing tests
Ignore-this: 4aea5721454500c088927b10ca17aae
1 file changed with 8 insertions and 0 deletions:
0 comments (0 inline, 0 general)
bin/rdfdb
Show inline comments
 
@@ -341,96 +341,104 @@ class Db(object):
 
        if c in self.clients:
 
            self.clients.remove(c)
 
        self.sendClientsToAllLivePages()
 

	
 
    def summarizeToLog(self):
 
        log.info("contexts in graph (%s total stmts):" % len(self.graph))
 
        for c in self.graph.contexts():
 
            log.info("  %s: %s statements" %
 
                     (c.identifier, len(self.getSubgraph(c.identifier))))
 

	
 
    def getSubgraph(self, uri):
 
        """
 
        this is meant to return a live view of the given subgraph, but
 
        if i'm still working around an rdflib bug, it might return a
 
        copy
 

	
 
        and it's returning triples, but I think quads would be better
 
        """
 
        # this is returning an empty Graph :(
 
        #return self.graph.get_context(uri)
 

	
 
        g = Graph()
 
        for s in self.graph.triples(ALLSTMTS, uri):
 
            g.add(s)
 
        return g
 

	
 
    def addClient(self, newClient):
 
        [self.clients.remove(c)
 
         for c in self.clients if c.updateUri == newClient.updateUri]
 

	
 
        log.info("new client %r" % newClient)
 
        sendGraphToClient(self.graph, newClient)
 
        self.clients.append(newClient)
 
        self.sendClientsToAllLivePages()
 

	
 
    def sendClientsToAllLivePages(self):
 
        sendToLiveClients({"clients":[
 
            dict(updateUri=c.updateUri, label=repr(c))
 
            for c in self.clients]})
 

	
 
class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler):
 
    def get(self):
 
        accept = self.request.headers.get('accept', '')
 
        format = 'n3'
 
        if accept == 'text/plain':
 
            format = 'nt'
 
        elif accept == 'application/n-quads':
 
            format = 'nquads'
 
        elif accept == 'pickle':
 
            # don't use this; it's just for speed comparison
 
            import cPickle as pickle
 
            pickle.dump(self.settings.db.graph, self, protocol=2)
 
            return
 
        elif accept == 'msgpack':
 
            self.write(repr(self.settings.db.graph.__getstate__))
 
            return
 
        self.write(self.settings.db.graph.serialize(format=format))
 

	
 
class Patches(PrettyErrorHandler, cyclone.web.RequestHandler):
 
    def __init__(self, *args, **kw):
 
        cyclone.web.RequestHandler.__init__(self, *args, **kw)
 
        p = makePatchEndpointPutMethod(self.settings.db.patch)
 
        self.put = lambda: p(self)
 

	
 
    def get(self):
 
        pass
 

	
 
class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler):
 
    def get(self):
 
        pass
 

	
 
    def post(self):
 
        upd = self.get_argument("clientUpdate")
 
        try:
 
            self.settings.db.addClient(Client(upd, self.get_argument("label")))
 
        except:
 
            import traceback
 
            traceback.print_exc()
 
            raise
 
            
 
class Prefixes(PrettyErrorHandler, cyclone.web.RequestHandler):
 
    def post(self):
 
        suggestion = json.loads(self.request.body)
 
        addlPrefixes = self.settings.db.watchedFiles.addlPrefixes
 
        addlPrefixes.setdefault(URIRef(suggestion['ctx']), {}).update(suggestion['prefixes'])
 
    
 
_wsClientSerial = 0
 
class WebsocketClient(cyclone.websocket.WebSocketHandler):
 

	
 
    def connectionMade(self, *args, **kwargs):
 
        global _wsClientSerial
 
        connectionId = 'connection-%s' % _wsClientSerial
 
        _wsClientSerial += 1
 

	
 
        self.wsClient = WsClient(connectionId, self.sendMessage)
 
        log.info("new ws client %r", self.wsClient)
 
        self.settings.db.addClient(self.wsClient)
 

	
 
    def connectionLost(self, reason):
 
        log.info("bye ws client %r", self.wsClient)
 
        self.settings.db.clientErrored(
 
            Failure(WebsocketDisconnect(reason)), self.wsClient)
 

	
 
    def messageReceived(self, message):
0 comments (0 inline, 0 general)