Changeset - 3d8ad77176ec
[Not reviewed]
default
0 3 0
drewp@bigasterisk.com - 12 years ago 2013-06-12 02:35:30
drewp@bigasterisk.com
rdfdb handles file removals
Ignore-this: 3f3db320b7418979f4061cb86c226573
3 files changed with 26 insertions and 5 deletions:
0 comments (0 inline, 0 general)
bin/rdfdb
Show inline comments
 
@@ -243,14 +243,19 @@ class WatchedFiles(object):
 
        gf.reread()
 

	
 
    def aboutToPatch(self, ctx):
 
        """
 
        warn us that a patch is about to come to this context. it's more
 
        straightforward to create the new file now
 

	
 
        this is meant to make the file before we add triples, so we
 
        wouldn't see the blank file and lose those triples. But it
 
        didn't work, so there are other measures that make us not lose
 
        the triples from a new file. Calling this before patching the
 
        graph is still a reasonable thing to do, though.
 
        """
 
    
 
        g = self.getSubgraph(ctx)
 

	
 
        if ctx not in self.graphFiles:
 
            outFile = self.fileForUri(ctx)
 
            log.info("starting new file %r", outFile)
 
            self.graphFiles[ctx] = GraphFile(self.notifier, outFile, ctx,
 
@@ -308,13 +313,13 @@ class Db(object):
 
        back to the sender with that updateUri
 
        """
 
        ctx = p.getContext()
 
        log.info("patching graph %s -%d +%d" % (
 
            ctx, len(p.delQuads), len(p.addQuads)))
 

	
 
        if hasattr(self, 'watchedFiles'): # not during startup
 
        if hasattr(self, 'watchedFiles'): # not available during startup
 
            self.watchedFiles.aboutToPatch(ctx)
 
        
 
        patchQuads(self.graph, p.delQuads, p.addQuads, perfect=True)
 
        senderUpdateUri = getattr(p, 'senderUpdateUri', None)
 

	
 
        for c in self.clients:
light9/rdfdb/graphfile.py
Show inline comments
 
@@ -12,12 +12,16 @@ iolog = logging.getLogger('io')
 
class GraphFile(object):
 
    """
 
    one rdf file that we read from, write to, and notice external changes to
 
    """
 
    def __init__(self, notifier, path, uri, patch, getSubgraph):
 
        """
 
        uri is the context for the triples in this file. We assume
 
        sometimes that we're the only ones with triples in this
 
        context.
 
        
 
        this does not include an initial reread() call
 
        """
 
        self.path, self.uri = path, uri
 
        self.patch, self.getSubgraph = patch, getSubgraph
 

	
 
        self.lastWriteTimestamp = 0 # mtime from the last time _we_ wrote
 
@@ -29,12 +33,14 @@ class GraphFile(object):
 
            except OSError:
 
                pass
 
            f = open(path, "w")
 
            f.write("#new\n")
 
            f.close()
 
            iolog.info("%s created", path)
 
            # this was supposed to cut out some extra reads but it
 
            # didn't work:
 
            self.lastWriteTimestamp = os.path.getmtime(path)
 

	
 

	
 
        self.flushDelay = 2 # seconds until we have to call flush() when dirty
 
        self.writeCall = None # or DelayedCall
 

	
 
@@ -52,14 +58,15 @@ class GraphFile(object):
 
        notifier.watch(FilePath(path), callbacks=[self.notify])
 
      
 
    def notify(self, notifier, filepath, mask):
 
        maskNames = humanReadableMask(mask)
 
        if maskNames[0] == 'delete_self':
 
            if not filepath.exists():
 
                log.warn("%s delete_self event: need to dump the stmts from "
 
                         "this file", filepath)
 
                log.info("%s delete_self", filepath)
 
                self.fileGone()
 
                return
 
            else:
 
                log.warn("%s delete_self event but file is here. ignoring",
 
                         filepath)
 
            return
 

	
 
        # we could filter these out in the watch() call, but I want
 
@@ -79,12 +86,21 @@ class GraphFile(object):
 
        log.info("%s needs reread because of %s event", filepath, maskNames)
 
        try:
 
            self.reread()
 
        except Exception:
 
            traceback.print_exc()
 

	
 
    def fileGone(self):
 
        """
 
        our file is gone; remove the statements from that context
 
        """
 
        myQuads = [(s,p,o,self.uri) for s,p,o in self.getSubgraph(self.uri)]
 
        log.debug("dropping all statements from context %s", self.uri)
 
        if myQuads:
 
            self.patch(Patch(delQuads=myQuads), dueToFileChange=True)
 
            
 
    def reread(self):
 
        """update the graph with any diffs from this file
 

	
 
        n3 parser fails on "1.e+0" even though rdflib was emitting that itself
 
        """
 
        old = self.getSubgraph(self.uri)
light9/rdfdb/rdflibpatch.py
Show inline comments
 
@@ -78,13 +78,13 @@ def serializeQuad(g):
 
                                     c.n3())
 
    return out
 

	
 
def inContext(graph, newContext):
 
    """
 
    make a ConjunctiveGraph where all the triples in the given graph
 
    are in newContext
 
    are now in newContext
 
    """
 
    return graphFromQuads([(s,p,o,newContext) for s,p,o in graph])
 

	
 
def contextsForStatement(graph, triple):
 
    return [q[3] for q in graph.quads(triple)]
 

	
0 comments (0 inline, 0 general)