def add(self, triple, context, quoted=False): Store.add(self, triple, context, quoted) if context is not None: self.__all_contexts.add(context) enctriple = self.__encodeTriple(triple) sid, pid, oid = enctriple self.__addTripleContext(enctriple, context, quoted) if sid in self.__subjectIndex: self.__subjectIndex[sid].add(enctriple) else: self.__subjectIndex[sid] = set([enctriple]) if pid in self.__predicateIndex: self.__predicateIndex[pid].add(enctriple) else: self.__predicateIndex[pid] = set([enctriple]) if oid in self.__objectIndex: self.__objectIndex[oid].add(enctriple) else: self.__objectIndex[oid] = set([enctriple])
def add(self, triple, context, quoted = False): # logging.debug(f'BaseStore add {triple}') self.__length += 1 Store.add(self, triple, context, quoted) if context is not None: self.__all_contexts.add(context) sub, pre, obj = triple self.__addTripleContext(triple, context, quoted) if sub in self.__subjectIndex: self.__subjectIndex[sub].add(triple) else: self.__subjectIndex[sub] = set([triple]) if pre in self.__predicateIndex: self.__predicateIndex[pre].add(triple) else: self.__predicateIndex[pre] = set([triple]) if obj in self.__objectIndex: self.__objectIndex[obj].add(triple) else: self.__objectIndex[obj] = set([triple])
def add(self, triple, context, quoted=False): # oldlen = len(self) Store.add(self, triple, context, quoted) context = getattr(context, 'identifier', context) if context is None: context = DEFAULT if context is not DEFAULT and context not in self.__all_contexts: self.__all_contexts.add(context) enctriple = self.__encodeTriple(triple) sid, pid, oid = enctriple self.__addTripleContext(enctriple, context, quoted) if sid in self.__subjectIndex: self.__subjectIndex[sid].add(enctriple) else: self.__subjectIndex[sid] = self.family.OO.Set((enctriple,)) if pid in self.__predicateIndex: self.__predicateIndex[pid].add(enctriple) else: self.__predicateIndex[pid] = self.family.OO.Set((enctriple,)) if oid in self.__objectIndex: self.__objectIndex[oid].add(enctriple) else: self.__objectIndex[oid] = self.family.OO.Set((enctriple,))
def add(self, triple, context=None, quoted=False): """ Add a triple to the store. Apply the modifications on the cache, trigger an exception if data has already been modified on the server. :param triple: Triple (subject, predicate, object) to add. :param context: :param quoted: The quoted argument is interpreted by formula-aware stores to indicate this statement is quoted/hypothetical. It should be an error to not specify a context and have the quoted argument be True. It should also be an error for the quoted argument to be True when the store is not formula-aware. :returns: """ LOG.debug("-- ProxyStore.add(triple=%s, context=%s, quoted=%s) --", triple, context, quoted) assert self._identifier is not None, "The store must be open." # TODO LATER : Wrong, assert is made to test bugs assert self._format is not None, "The store must be open." assert quoted == False, "The store -proxyStore- is not formula-aware" Store.add(self, triple, context, quoted) # Instruction suivant extraite du plugin Sleepycat # Store.add(self, (subject, predicate, object), context, quoted) self._graph.add(triple)
def add(self, triple, context, quoted=False): # oldlen = len(self) Store.add(self, triple, context, quoted) context = getattr(context, 'identifier', context) if context is None: context = DEFAULT if context is not DEFAULT and context not in self.__all_contexts: self.__all_contexts.add(context) enctriple = self.__encodeTriple(triple) sid, pid, oid = enctriple self.__addTripleContext(enctriple, context, quoted) if sid in self.__subjectIndex: self.__subjectIndex[sid].add(enctriple) else: self.__subjectIndex[sid] = self.family.OO.Set((enctriple, )) if pid in self.__predicateIndex: self.__predicateIndex[pid].add(enctriple) else: self.__predicateIndex[pid] = self.family.OO.Set((enctriple, )) if oid in self.__objectIndex: self.__objectIndex[oid].add(enctriple) else: self.__objectIndex[oid] = self.family.OO.Set((enctriple, ))
def add(self, triple, context, quoted=False): """\ Add a triple to the store of triples. """ # add dictionary entries for spo[s][p][p] = 1 and pos[p][o][s] # = 1, creating the nested dictionaries where they do not yet # exits. Store.add(self, triple, context, quoted=quoted) if context is not None: self.__all_contexts.add(context) subject, predicate, object_ = triple spo = self.__spo try: po = spo[subject] except LookupError: po = spo[subject] = {} try: o = po[predicate] except LookupError: o = po[predicate] = {} try: _ = o[object_] # This cannot be reached if (s, p, o) was not inserted before. triple_exists = True except KeyError: o[object_] = 1 triple_exists = False self.__add_triple_context(triple, triple_exists, context, quoted) if triple_exists: # No need to insert twice this triple. return pos = self.__pos try: os = pos[predicate] except LookupError: os = pos[predicate] = {} try: s = os[object_] except LookupError: s = os[object_] = {} s[subject] = 1 osp = self.__osp try: sp = osp[object_] except LookupError: sp = osp[object_] = {} try: p = sp[subject] except LookupError: p = sp[subject] = {} p[predicate] = 1
def add(self, triple, context=None, quoted=False): """ Add a triple and start indexing. :param tuple(rdflib.Identifier) triple: Tuple of three identifiers. :param context: Context identifier. ``None`` inserts in the default graph. :type context: rdflib.Identifier or None :param bool quoted: Not used. """ context = self._normalize_context(context) if context is None: context = RDFLIB_DEFAULT_GRAPH_URI Store.add(self, triple, context) #logger.info('Adding triple: {}'.format(triple)) pk_trp = self._pickle(triple) pk_s, pk_p, pk_o = [self._pickle(t) for t in triple] #logger.debug('Adding quad: {} {}'.format(triple, context)) pk_c = self._pickle(context) # Add new individual terms or gather keys for existing ones. keys = [None, None, None, None] with self.cur('th:t') as icur: for i, pk_t in enumerate((pk_s, pk_p, pk_o, pk_c)): thash = self._hash(pk_t) if icur.set_key(thash): keys[i] = bytes(icur.value()) else: # Put new term. with self.cur('t:st') as dcur: keys[i] = self._append(dcur, (pk_t, ))[0] # Index. icur.put(thash, keys[i]) # Add context in context DB. ck = keys[3] with self.cur('c:') as cur: if not cur.set_key(ck): cur.put(ck, b'') # Add triple:context association. spok = b''.join(keys[:3]) with self.cur('spo:c') as dcur: if not dcur.set_key_dup(spok, ck): dcur.put(spok, ck) # Index spo:c association. with self.cur('c:spo') as icur: icur.put(ck, spok) self._index_triple('add', spok)
def add(self, triple, context, quoted=False): assert self.__open, "The Store must be open." assert context != self, "Can not add triple directly to store" Store.add(self, triple, context, quoted) if self.inbatch: self.tripleBuffer.append(self.__serialise(triple)) if len(self.tripleBuffer) >= self.bufferMaxSize: self.__flushBuffer() else: result = self.session.run( "CALL n10s.rdf.import.inline($rdf,'N-Triples')", rdf=self.__serialise(triple)).single() if (result["terminationStatus"]) == "KO": raise Exception("Could not persist triple in Neo4j: ", result["extraInfo"])
def add(self, xxx_todo_changeme, context, quoted=False): if quoted or self.locked: raise RuntimeError('nope') Store.add(self, xxx_todo_changeme, context, quoted) s1, p1, o1 = xxx_todo_changeme incoming = s1, p1, o1, context for pat in patterns: i = self.indexes[pat] for incoming_idx, l in enumerate(pat): if l == 'f': i = i[None] else: i = i[incoming[incoming_idx]] i.append((xxx_todo_changeme, context)) self.quads.append((xxx_todo_changeme, context))
def add(self, triple, context, quoted=False): """\ Add a triple to the store of triples. """ # add dictionary entries for spo[s][p][p] = 1 and pos[p][o][s] # = 1, creating the nested dictionaries where they do not yet # exits. Store.add(self, triple, context, quoted=quoted) if context is not None: self.__all_contexts.add(context) subject, predicate, object_ = triple self.__add_triple_context(triple, context, quoted) spo = self.__spo try: po = spo[subject] except LookupError: po = spo[subject] = {} try: o = po[predicate] except LookupError: o = po[predicate] = {} o[object_] = 1 pos = self.__pos try: os = pos[predicate] except LookupError: os = pos[predicate] = {} try: s = os[object_] except LookupError: s = os[object_] = {} s[subject] = 1 osp = self.__osp try: sp = osp[object_] except LookupError: sp = osp[object_] = {} try: p = sp[subject] except LookupError: p = sp[subject] = {} p[predicate] = 1
def add(self, triple, context, quoted=False, txn=None): """\ Add a triple to the store of triples. """ (subject, predicate, object) = triple assert self.__open, "The Store must be open." assert context != self, "Can not add triple directly to store" Store.add(self, (subject, predicate, object), context, quoted) _to_string = self._to_string s = _to_string(subject, txn=txn) p = _to_string(predicate, txn=txn) o = _to_string(object, txn=txn) c = _to_string(context, txn=txn) cspo, cpos, cosp = self.__indicies value = cspo.get(bb("%s^%s^%s^%s^" % (c, s, p, o)), txn=txn) if value is None: self.__contexts.put(bb(c), "", txn=txn) contexts_value = cspo.get(bb("%s^%s^%s^%s^" % ("", s, p, o)), txn=txn) or "".encode("latin-1") contexts = set(contexts_value.split("^".encode("latin-1"))) contexts.add(bb(c)) contexts_value = "^".encode("latin-1").join(contexts) assert contexts_value is not None cspo.put(bb("%s^%s^%s^%s^" % (c, s, p, o)), "", txn=txn) cpos.put(bb("%s^%s^%s^%s^" % (c, p, o, s)), "", txn=txn) cosp.put(bb("%s^%s^%s^%s^" % (c, o, s, p)), "", txn=txn) if not quoted: cspo.put(bb("%s^%s^%s^%s^" % ("", s, p, o)), contexts_value, txn=txn) cpos.put(bb("%s^%s^%s^%s^" % ("", p, o, s)), contexts_value, txn=txn) cosp.put(bb("%s^%s^%s^%s^" % ("", o, s, p)), contexts_value, txn=txn) self.__needs_sync = True
def addN(self, quads): c1, c2, c3 = 0, 0, 0 for qgroup in grouper(quads, 10000): encquads = list() for q in qgroup: ctx = _fix_ctx(q[3]) Store.add(self, q[:3], q[3], False) encquads.append(((self._obj2id(q[0]), self._obj2id(q[1]), self._obj2id(q[2])), self._obj2id(ctx), ctx)) for enctriple, cid, context in encquads: if context is not DEFAULT: self._all_contexts.add(context) self._addTripleContext(enctriple, cid, False) c1 += self._addN_helper(encquads, self._subjectIndex, 0) c2 += self._addN_helper(encquads, self._predicateIndex, 1) c3 += self._addN_helper(encquads, self._objectIndex, 2) assert c1 == c2 assert c2 == c3 return c1
def add(self, triple, context, quoted=False, txn=None): """\ Add a triple to the store of triples. """ (subject, predicate, object) = triple assert self.__open, "The Store must be open." assert context != self, "Can not add triple directly to store" Store.add(self, (subject, predicate, object), context, quoted) _to_string = self._to_string s = _to_string(subject, txn=txn) p = _to_string(predicate, txn=txn) o = _to_string(object, txn=txn) c = _to_string(context, txn=txn) cspo, cpos, cosp = self.__indicies value = cspo.get(bb("%s^%s^%s^%s^" % (c, s, p, o)), txn=txn) if value is None: self.__contexts.put(bb(c), "", txn=txn) contexts_value = cspo.get( bb("%s^%s^%s^%s^" % ("", s, p, o)), txn=txn) or b("") contexts = set(contexts_value.split(b("^"))) contexts.add(bb(c)) contexts_value = b("^").join(contexts) assert contexts_value is not None cspo.put(bb("%s^%s^%s^%s^" % (c, s, p, o)), "", txn=txn) cpos.put(bb("%s^%s^%s^%s^" % (c, p, o, s)), "", txn=txn) cosp.put(bb("%s^%s^%s^%s^" % (c, o, s, p)), "", txn=txn) if not quoted: cspo.put(bb( "%s^%s^%s^%s^" % ("", s, p, o)), contexts_value, txn=txn) cpos.put(bb( "%s^%s^%s^%s^" % ("", p, o, s)), contexts_value, txn=txn) cosp.put(bb( "%s^%s^%s^%s^" % ("", o, s, p)), contexts_value, txn=txn) self.__needs_sync = True
def add(self, triple, context, quoted=False): """\ Add a triple to the store of triples. """ (subject, predicate, object) = triple assert self.__open, "The Store must be open." assert context != self, "Can not add triple directly to store" # Add the triple to the Store, triggering TripleAdded events Store.add(self, (subject, predicate, object), context, quoted) _to_string = self._to_string s = _to_string(subject) p = _to_string(predicate) o = _to_string(object) c = _to_string(context) cspo, cpos, cosp = self.__indices value = cspo.get(bb("%s^%s^%s^%s^" % (c, s, p, o))) if value is None: self.__contexts.set(bb(c), "") contexts_value = cspo.get(bb( "%s^%s^%s^%s^" % ("", s, p, o))) or b("") contexts = set(contexts_value.split(b("^"))) contexts.add(bb(c)) contexts_value = b("^").join(contexts) assert contexts_value != None cspo.set(bb("%s^%s^%s^%s^" % (c, s, p, o)), "") cpos.set(bb("%s^%s^%s^%s^" % (c, p, o, s)), "") cosp.set(bb("%s^%s^%s^%s^" % (c, o, s, p)), "") if not quoted: cspo.set(bb("%s^%s^%s^%s^" % ("", s, p, o)), contexts_value) cpos.set(bb("%s^%s^%s^%s^" % ("", p, o, s)), contexts_value) cosp.set(bb("%s^%s^%s^%s^" % ("", o, s, p)), contexts_value) self.__needs_sync = True
def add(self, xxx_todo_changeme, context, quoted=False): """\ Add a triple to the store of triples. """ (subject, predicate, object) = xxx_todo_changeme assert self.__open, "The Store must be open." assert context != self, "Can not add triple directly to store" # Add the triple to the Store, triggering TripleAdded events Store.add(self, (subject, predicate, object), context, quoted) _to_string = self._to_string s = _to_string(subject) p = _to_string(predicate) o = _to_string(object) c = _to_string(context) cspo, cpos, cosp = self.__indices value = cspo.get(bb("%s^%s^%s^%s^" % (c, s, p, o))) if value is None: self.__contexts.set(bb(c), "") contexts_value = cspo.get(bb( "%s^%s^%s^%s^" % ("", s, p, o))) or b("") contexts = set(contexts_value.split(b("^"))) contexts.add(bb(c)) contexts_value = b("^").join(contexts) assert contexts_value != None cspo.set(bb("%s^%s^%s^%s^" % (c, s, p, o)), "") cpos.set(bb("%s^%s^%s^%s^" % (c, p, o, s)), "") cosp.set(bb("%s^%s^%s^%s^" % (c, o, s, p)), "") if not quoted: cspo.set(bb("%s^%s^%s^%s^" % ("", s, p, o)), contexts_value) cpos.set(bb("%s^%s^%s^%s^" % ("", p, o, s)), contexts_value) cosp.set(bb("%s^%s^%s^%s^" % ("", o, s, p)), contexts_value) self.__needs_sync = True
def add(self, triple, context, quoted=False): """\ Add a triple to the store. """ Store.add(self, triple, context, quoted) for triple, cg in self.triples(triple, context): #triple is already in the store. return subject, predicate, object = triple f = self.forward r = self.reverse # assign keys for new identifiers if not r.has_key(subject): si = randid() while f.has_key(si): si = randid() f[si] = subject r[subject] = si else: si = r[subject] if not r.has_key(predicate): pi = randid() while f.has_key(pi): pi = randid() f[pi] = predicate r[predicate] = pi else: pi = r[predicate] if not r.has_key(object): oi = randid() while f.has_key(oi): oi = randid() f[oi] = object r[object] = oi else: oi = r[object] if not r.has_key(context): ci = randid() while f.has_key(ci): ci = randid() f[ci] = context r[context] = ci else: ci = r[context] # add dictionary entries for cspo[c][s][p][o] = 1, # cpos[c][p][o][s] = 1, and cosp[c][o][s][p] = 1, creating the # nested {} where they do not yet exits. self._setNestedIndex(self.cspo, ci, si, pi, oi) self._setNestedIndex(self.cpos, ci, pi, oi, si) self._setNestedIndex(self.cosp, ci, oi, si, pi) if not quoted: self._setNestedIndex(self.spo, si, pi, oi, ci) self._setNestedIndex(self.pos, pi, oi, si, ci) self._setNestedIndex(self.osp, oi, si, pi, ci)
def add(self, triple, context, quoted=False): """\ Add a triple to the store. """ Store.add(self, triple, context, quoted) for triple, cg in self.triples(triple, context): # triple is already in the store. return subject, predicate, object = triple f = self.forward r = self.reverse # assign keys for new identifiers if subject not in r: si = randid() while si in f: si = randid() f[si] = subject r[subject] = si else: si = r[subject] if predicate not in r: pi = randid() while pi in f: pi = randid() f[pi] = predicate r[predicate] = pi else: pi = r[predicate] if object not in r: oi = randid() while oi in f: oi = randid() f[oi] = object r[object] = oi else: oi = r[object] if context not in r: ci = randid() while ci in f: ci = randid() f[ci] = context r[context] = ci else: ci = r[context] # add dictionary entries for cspo[c][s][p][o] = 1, # cpos[c][p][o][s] = 1, and cosp[c][o][s][p] = 1, creating the # nested {} where they do not yet exits. self._setNestedIndex(self.cspo, ci, si, pi, oi) self._setNestedIndex(self.cpos, ci, pi, oi, si) self._setNestedIndex(self.cosp, ci, oi, si, pi) if not quoted: self._setNestedIndex(self.spo, si, pi, oi, ci) self._setNestedIndex(self.pos, pi, oi, si, ci) self._setNestedIndex(self.osp, oi, si, pi, ci)
self.__prefix.close() self.__i2k.close() self.__k2i.close() self.db_env.close() def add(self, (subject, predicate, object), context, quoted=False, txn=None): """\ Add a triple to the store of triples. """ assert self.__open, "The Store must be open." assert context != self, "Can not add triple directly to store" Store.add(self, (subject, predicate, object), context, quoted) _to_string = self._to_string s = _to_string(subject, txn=txn) p = _to_string(predicate, txn=txn) o = _to_string(object, txn=txn) c = _to_string(context, txn=txn) cspo, cpos, cosp = self.__indicies value = cspo.get(bb("%s^%s^%s^%s^" % (c, s, p, o)), txn=txn) if value is None: self.__contexts.put(bb(c), "", txn=txn) contexts_value = cspo.get(bb("%s^%s^%s^%s^" % ("", s, p, o)),
def destroy(self, configuration=''): import os path = configuration or self.homeDir if os.path.exists(path): for f in os.listdir(path): os.unlink(path+'/'+f) os.rmdir(path) def add(self, (subject, predicate, object), context, quoted=False): """\ Add a triple to the store of triples. """ assert self.__open, "The Store must be open." assert context != self, "Can not add triple directly to store" # Add the triple to the Store, triggering TripleAdded events Store.add(self, (subject, predicate, object), context, quoted) _to_string = self._to_string s = _to_string(subject) p = _to_string(predicate) o = _to_string(object) c = _to_string(context) cspo, cpos, cosp = self.__indices value = cspo.get(bb("%s^%s^%s^%s^" % (c, s, p, o))) if value is None: self.__contexts.set(bb(c), "") contexts_value = cspo.get(bb("%s^%s^%s^%s^" % ("", s, p, o))) or b("")
def add(self, xxx_todo_changeme, context, quoted=False): Store.add(self, xxx_todo_changeme, context, quoted) s1, p1, o1 = xxx_todo_changeme self.quads.append(((s1, p1, o1), context)) self.quoted_info.append(quoted)