Exemple #1
0
def __update_fragment_cache(fid, gp):
    """
    Recreate fragment <fid> cached data and all its data-contexts from the corresponding stream (Redis)
    :param fid:
    :return:
    """
    fragments_cache.remove_context(fragments_cache.get_context('/' + fid))

    gp_graph = graph_from_gp(gp)
    roots = filter(lambda x: gp_graph.in_degree(x) == 0, gp_graph.nodes())

    fragment_triples = load_stream_triples(fid, calendar.timegm(dt.utcnow().timetuple()))
    visited_contexts = set([])
    for c, s, p, o in fragment_triples:
        if c not in visited_contexts:
            fragments_cache.remove_context(fragments_cache.get_context(str((fid, c))))
            visited_contexts.add(c)
        fragments_cache.get_context(str((fid, c))).add((s, p, o))
        fragments_cache.get_context('/' + fid).add((s, p, o))
        if c[0] in roots:
            fragments_cache.get_context('/' + fid).add((s, RDF.type, STOA.Root))
    visited_contexts.clear()
    with r.pipeline() as pipe:
        pipe.delete('{}:{}:stream'.format(fragments_key, fid))
        pipe.execute()
 def __load_contexts():
     contexts = fragment_contexts(self.sink.fragment_id)
     triple_patterns = {context: eval(context)[1] for context in contexts}
     # Yield triples for each known triple pattern context
     for context in contexts:
         for (s, p, o) in fragments_cache.get_context(context):
             yield triple_patterns[context], s, p, o
def __cache_plan_context(fid, graph):
    """
    Use <graph> to extract the triple patterns of the current fragment <fid> and replace them as the expected context
    (triple patterns context) in the cache graph
    """
    try:
        fid_context = fragments_cache.get_context(fid)
        fragments_cache.remove_context(fid_context)
        tps = graph.subjects(RDF.type, AGORA.TriplePattern)
        for tp in tps:
            for (s, p, o) in graph.triples((tp, None, None)):
                fid_context.add((s, p, o))
                for t in graph.triples((o, None, None)):
                    fid_context.add(t)
    except Exception, e:
        log.error(e.message)
def __update_fragment_cache(fid, gp):
    """
    Recreate fragment <fid> cached data and all its data-contexts from the corresponding stream (Redis)
    :param fid:
    :return:
    """
    plan_tps = fragments_cache.get_context(fid).subjects(RDF.type, AGORA.TriplePattern)
    fragments_cache.remove_context(fragments_cache.get_context('/' + fid))
    for tp in plan_tps:
        fragments_cache.remove_context(
            fragments_cache.get_context(str((fid, __extract_tp_from_plan(fragments_cache, tp)))))

    gp_graph = graph_from_gp(gp)
    roots = filter(lambda x: gp_graph.in_degree(x) == 0, gp_graph.nodes())

    fragment_triples = load_stream_triples(fid, calendar.timegm(dt.now().timetuple()))
    for c, s, p, o in fragment_triples:
        fragments_cache.get_context(str((fid, c))).add((s, p, o))
        fragments_cache.get_context('/' + fid).add((s, p, o))
        if c[0] in roots:
            fragments_cache.get_context('/' + fid).add((s, RDF.type, STOA.Root))
    with r.pipeline() as pipe:
        pipe.delete('{}:{}:stream'.format(fragments_key, fid))
        pipe.execute()
def fragment_graph(fid):
    return fragments_cache.get_context('/' + fid)