Esempio n. 1
0
 @Override
 public synchronized void removeStatements(
     Resource subj, URI pred, Value obj, Resource... contexts) throws SailException {
   if (sail.isArchiving()) {
     CloseableIteration<? extends Statement, SailException> stmts;
     stmts = super.getStatements(subj, pred, obj, false, contexts);
     try {
       while (stmts.hasNext()) {
         Statement st = stmts.next();
         Resource s = st.getSubject();
         URI p = st.getPredicate();
         Value o = st.getObject();
         Resource ctx = st.getContext();
         removeRevision(s, p);
         if (ctx instanceof URI && !ctx.equals(trx)) {
           if (modified.add(ctx)) {
             super.addStatement(getTrx(), MODIFIED, ctx, getTrx());
           }
           BNode node = vf.createBNode();
           super.addStatement(ctx, CONTAINED, node, getTrx());
           super.addStatement(node, RDF.SUBJECT, s, getTrx());
           super.addStatement(node, RDF.PREDICATE, p, getTrx());
           super.addStatement(node, RDF.OBJECT, o, getTrx());
         }
       }
     } finally {
       stmts.close();
     }
     super.removeStatements(subj, pred, obj, contexts);
   } else {
     if (sail.getMaxArchive() > 0 && arch.size() <= sail.getMaxArchive()) {
       CloseableIteration<? extends Statement, SailException> stmts;
       stmts = super.getStatements(subj, pred, obj, false, contexts);
       try {
         int maxArchive = sail.getMaxArchive();
         while (stmts.hasNext() && arch.size() <= maxArchive) {
           Statement st = stmts.next();
           Resource ctx = st.getContext();
           if (ctx instanceof URI && !ctx.equals(trx)) {
             arch.add(st);
           }
         }
       } finally {
         stmts.close();
       }
     }
     super.removeStatements(subj, pred, obj, contexts);
     removeRevision(subj, pred);
     if (contexts != null && contexts.length > 0) {
       for (Resource ctx : contexts) {
         if (ctx != null && modified.add(ctx)) {
           addMetadata(currentTrx, MODIFIED, ctx, currentTrx);
         }
       }
     }
   }
 }
Esempio n. 2
0
  @Test
  public void testAddWhileQuerying() throws Exception {
    // Add some data to the repository
    con.begin();
    con.addStatement(painter, RDF.TYPE, RDFS.CLASS);
    con.addStatement(painting, RDF.TYPE, RDFS.CLASS);
    con.addStatement(picasso, RDF.TYPE, painter);
    con.addStatement(guernica, RDF.TYPE, painting);
    con.addStatement(picasso, paints, guernica);
    con.commit();

    ParsedTupleQuery tupleQuery =
        QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "SELECT C FROM {} rdf:type {C}", null);

    CloseableIteration<? extends BindingSet, QueryEvaluationException> iter;
    iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false);

    con.begin();

    while (iter.hasNext()) {
      BindingSet bindings = iter.next();
      Value c = bindings.getValue("C");
      if (c instanceof Resource) {
        con.addStatement((Resource) c, RDF.TYPE, RDFS.CLASS);
      }
    }

    con.commit();

    assertEquals(3, countElements(con.getStatements(null, RDF.TYPE, RDFS.CLASS, false)));

    // simulate auto-commit
    tupleQuery =
        QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "SELECT P FROM {} P {}", null);
    iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false);

    while (iter.hasNext()) {
      BindingSet bindings = iter.next();
      Value p = bindings.getValue("P");
      if (p instanceof URI) {
        con.begin();
        con.addStatement((URI) p, RDF.TYPE, RDF.PROPERTY);
        con.commit();
      }
    }

    assertEquals(2, countElements(con.getStatements(null, RDF.TYPE, RDF.PROPERTY, false)));
  }
Esempio n. 3
0
  protected int verifyQueryResult(
      CloseableIteration<? extends BindingSet, QueryEvaluationException> resultIter,
      int expectedBindingCount)
      throws QueryEvaluationException {
    int resultCount = 0;

    while (resultIter.hasNext()) {
      BindingSet resultBindings = resultIter.next();
      resultCount++;

      assertEquals(
          "Wrong number of binding names for binding set",
          expectedBindingCount,
          resultBindings.getBindingNames().size());

      int bindingCount = 0;
      Iterator<Binding> bindingIter = resultBindings.iterator();
      while (bindingIter.hasNext()) {
        bindingIter.next();
        bindingCount++;
      }

      assertEquals("Wrong number of bindings in binding set", expectedBindingCount, bindingCount);
    }

    return resultCount;
  }
 public Statement next() throws QueryEvaluationException {
   try {
     return baseIteration.next();
   } catch (SailException e) {
     throw new QueryEvaluationException(e);
   }
 }
Esempio n. 5
0
 private boolean isObsolete(Resource ctx) throws SailException {
   CloseableIteration<? extends Statement, SailException> stmts;
   stmts = super.getStatements(null, null, null, true, ctx);
   try {
     while (stmts.hasNext()) {
       Statement st = stmts.next();
       URI pred = st.getPredicate();
       Value obj = st.getObject();
       String ns = pred.getNamespace();
       if (Audit.NAMESPACE.equals(ns) || PROV.equals(ns) || AUDIT_2012.equals(ns)) continue;
       if (RDF.SUBJECT.equals(pred) || RDF.PREDICATE.equals(pred) || RDF.OBJECT.equals(pred))
         continue;
       if (RDF.TYPE.equals(pred) && obj instanceof URI) {
         ns = ((URI) obj).getNamespace();
         if (Audit.NAMESPACE.equals(ns)
             || PROV.equals(ns)
             || AUDIT_2012.equals(ns)
             || RDF.NAMESPACE.equals(ns)) continue;
       }
       return false;
     }
   } finally {
     stmts.close();
   }
   return true;
 }
Esempio n. 6
0
 private void removeInforming(
     URI activity, URI entity, Resource subj, URI pred, Value obj, Resource... contexts)
     throws SailException {
   if (contexts != null && contexts.length == 0) {
     CloseableIteration<? extends Statement, SailException> stmts;
     stmts = super.getStatements(subj, pred, obj, false, contexts);
     try {
       while (stmts.hasNext()) {
         Statement st = stmts.next();
         Resource ctx = st.getContext();
         subj = st.getSubject();
         pred = st.getPredicate();
         obj = st.getObject();
         removeInformingGraph(activity, entity, subj, pred, obj, ctx);
       }
     } finally {
       stmts.close();
     }
   } else if (contexts == null) {
     removeInformingGraph(activity, entity, subj, pred, obj, null);
   } else {
     for (Resource ctx : contexts) {
       removeInformingGraph(activity, entity, subj, pred, obj, ctx);
     }
   }
 }
Esempio n. 7
0
  @Override
  public CloseableIteration<RyaStatement, RyaDAOException> join(C conf, RyaURI... preds)
      throws RyaDAOException {
    ConcurrentHashMap<Map.Entry<RyaURI, RyaType>, Integer> ht =
        new ConcurrentHashMap<Map.Entry<RyaURI, RyaType>, Integer>();
    int count = 0;
    boolean first = true;
    for (RyaURI pred : preds) {
      count++;
      // query
      CloseableIteration<RyaStatement, RyaDAOException> results =
          ryaQueryEngine.query(new RyaStatement(null, pred, null), null);
      // add to hashtable
      while (results.hasNext()) {
        RyaStatement next = results.next();
        RyaURI subject = next.getSubject();
        RyaType object = next.getObject();
        Map.Entry<RyaURI, RyaType> entry =
            new RdfCloudTripleStoreUtils.CustomEntry<RyaURI, RyaType>(subject, object);
        if (!first) {
          if (!ht.containsKey(entry)) {
            continue; // not in join
          }
        }
        ht.put(entry, count);
      }
      // remove from hashtable values that are under count
      if (first) {
        first = false;
      } else {
        for (Map.Entry<Map.Entry<RyaURI, RyaType>, Integer> entry : ht.entrySet()) {
          if (entry.getValue() < count) {
            ht.remove(entry.getKey());
          }
        }
      }
    }
    final Enumeration<Map.Entry<RyaURI, RyaType>> keys = ht.keys();
    return new CloseableIteration<RyaStatement, RyaDAOException>() {
      @Override
      public void close() throws RyaDAOException {}

      @Override
      public boolean hasNext() throws RyaDAOException {
        return keys.hasMoreElements();
      }

      @Override
      public RyaStatement next() throws RyaDAOException {
        Map.Entry<RyaURI, RyaType> subjObj = keys.nextElement();
        return new RyaStatement(subjObj.getKey(), null, subjObj.getValue());
      }

      @Override
      public void remove() throws RyaDAOException {
        keys.nextElement();
      }
    };
  }
  @Override
  protected BindingSet getNextElement() throws QueryEvaluationException {
    if (hashTable == null) {
      setupHashTable();
    }

    while (currentScanElem == null) {
      if (scanList.size() > 0) {
        currentScanElem = scanList.remove(0);
      } else {
        if (restIter.hasNext()) {
          currentScanElem = restIter.next();
        } else {
          // no more elements available
          return null;
        }
      }

      if (currentScanElem instanceof EmptyBindingSet) {
        // the empty bindingset should be merged with all bindingset in
        // the hash table
        hashTableValues = new ArrayList<BindingSet>();
        for (BindingSet key : hashTable.keySet()) {
          hashTableValues.addAll(hashTable.get(key));
        }
      } else {
        BindingSet key = calcKey(currentScanElem, joinAttributes);

        if (hashTable.containsKey(key)) {
          hashTableValues = new ArrayList<BindingSet>(hashTable.get(key));
        } else {
          currentScanElem = null;
          hashTableValues = null;
        }
      }
    }

    BindingSet nextHashTableValue = hashTableValues.remove(0);

    QueryBindingSet result = new QueryBindingSet(currentScanElem);

    for (String name : nextHashTableValue.getBindingNames()) {
      Binding b = nextHashTableValue.getBinding(name);
      if (!result.hasBinding(name)) {
        result.addBinding(b);
      }
    }

    if (hashTableValues.size() == 0) {
      // we've exhausted the current scanlist entry
      currentScanElem = null;
      hashTableValues = null;
    }

    return result;
  }
Esempio n. 9
0
 public Statement next() {
   try {
     last = stmts.next();
     if (last == null) {
       stmts.close();
     }
     return last;
   } catch (RepositoryException e) {
     throw new ModelException(e);
   }
 }
Esempio n. 10
0
  protected void testValueRoundTrip(Resource subj, URI pred, Value obj) throws Exception {
    con.begin();
    con.addStatement(subj, pred, obj);
    con.commit();

    CloseableIteration<? extends Statement, SailException> stIter =
        con.getStatements(null, null, null, false);

    try {
      assertTrue(stIter.hasNext());

      Statement st = stIter.next();
      assertEquals(subj, st.getSubject());
      assertEquals(pred, st.getPredicate());
      assertEquals(obj, st.getObject());
      assertTrue(!stIter.hasNext());
    } finally {
      stIter.close();
    }

    ParsedTupleQuery tupleQuery =
        QueryParserUtil.parseTupleQuery(
            QueryLanguage.SERQL,
            "SELECT S, P, O FROM {S} P {O} WHERE P = <" + pred.stringValue() + ">",
            null);

    CloseableIteration<? extends BindingSet, QueryEvaluationException> iter;
    iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false);

    try {
      assertTrue(iter.hasNext());

      BindingSet bindings = iter.next();
      assertEquals(subj, bindings.getValue("S"));
      assertEquals(pred, bindings.getValue("P"));
      assertEquals(obj, bindings.getValue("O"));
      assertTrue(!iter.hasNext());
    } finally {
      iter.close();
    }
  }
  private void setupHashTable() throws QueryEvaluationException {

    hashTable = new HashMap<BindingSet, ArrayList<BindingSet>>();

    List<BindingSet> leftArgResults = new ArrayList<BindingSet>();
    List<BindingSet> rightArgResults = new ArrayList<BindingSet>();

    while (leftIter.hasNext() && rightIter.hasNext()) {
      BindingSet b = leftIter.next();
      leftArgResults.add(b);
      b = rightIter.next();
      rightArgResults.add(b);
    }

    List<BindingSet> smallestResult = null;

    if (leftIter.hasNext()) { // leftArg is the greater relation
      smallestResult = rightArgResults;
      scanList = leftArgResults;
      restIter = leftIter;
    } else { // rightArg is the greater relation (or they are equal)
      smallestResult = leftArgResults;
      scanList = rightArgResults;
      restIter = rightIter;
    }

    // create the hash table for our join
    for (BindingSet b : smallestResult) {
      BindingSet hashKey = calcKey(b, joinAttributes);

      ArrayList<BindingSet> hashValue = null;
      if (hashTable.containsKey(hashKey)) {
        hashValue = hashTable.get(hashKey);
      } else {
        hashValue = new ArrayList<BindingSet>();
      }
      hashValue.add(b);
      hashTable.put(hashKey, hashValue);
    }
  }
 protected static Value _getObject(Resource subject, URI predicate, SailConnection c)
     throws SailException {
   CloseableIteration<? extends Statement, SailException> i =
       c.getStatements(subject, predicate, null, true);
   try {
     if (i.hasNext()) {
       return i.next().getObject();
     } else {
       return null;
     }
   } finally {
     i.close();
   }
 }
Esempio n. 13
0
  @Override
  public T getNextElement() {
    while (rightIter != null || result.hasNext()) {
      if (rightIter == null) {
        rightIter = result.next();
      }
      if (rightIter.hasNext()) {
        return rightIter.next();
      } else {
        rightIter.close();
        rightIter = null;
      }
    }

    return null;
  }
Esempio n. 14
0
  @Test
  public void testGetNamespaces() throws Exception {
    con.begin();
    con.setNamespace("rdf", RDF.NAMESPACE);
    con.commit();

    CloseableIteration<? extends Namespace, SailException> namespaces = con.getNamespaces();
    try {
      assertTrue(namespaces.hasNext());
      Namespace rdf = namespaces.next();
      assertEquals("rdf", rdf.getPrefix());
      assertEquals(RDF.NAMESPACE, rdf.getName());
      assertTrue(!namespaces.hasNext());
    } finally {
      namespaces.close();
    }
  }
Esempio n. 15
0
 private void removeAllRevisions(Resource subj) throws SailException {
   CloseableIteration<? extends Statement, SailException> stmts;
   Resource s = getContainerURI(subj);
   stmts = super.getStatements(s, REVISION, null, true);
   try {
     while (stmts.hasNext()) {
       Statement st = stmts.next();
       Value ctx = st.getObject();
       if (ctx instanceof URI && modified.add((URI) ctx)) {
         addMetadata(getTrx(), MODIFIED, ctx, getTrx());
       }
       super.removeStatements(s, REVISION, ctx);
     }
   } finally {
     stmts.close();
   }
 }
  private void cacheStatements(final Resource subj, final URI pred, final Value obj)
      throws SailException {
    boolean includeInferred = false;

    CloseableIteration<? extends Statement, SailException> iter =
        baseSailConnection.getStatements(subj, pred, obj, includeInferred);

    while (iter.hasNext()) {
      Statement st = iter.next();
      cacheConnection.addStatement(
          st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
    }

    iter.close();

    cacheConnection.commit();
  }
Esempio n. 17
0
  protected boolean containsGraph(String graph) {
    connect();

    try {
      for (CloseableIteration<? extends Resource, SailException> res = connection.getContextIDs();
          res.hasNext(); ) {
        Resource r = res.next();
        if (r.stringValue().equals(graph.toString())) return true;
      }
    } catch (SailException e) {
      e.printStackTrace();
    }

    disconnect();

    return false;
  }
Esempio n. 18
0
  /**
   * LDP-Style to serialize a resource.
   *
   * @param writer the writer to serialize to
   * @param subject the resource to serialize
   * @param iteration the Iteration containing the data
   * @throws RDFHandlerException
   * @throws RepositoryException
   */
  public static void exportIteration(
      RDFWriter writer, URI subject, CloseableIteration<Statement, RepositoryException> iteration)
      throws RDFHandlerException, RepositoryException {
    writer.startRDF();

    writer.handleNamespace(LDP.PREFIX, LDP.NAMESPACE);
    writer.handleNamespace(RDF.PREFIX, RDF.NAMESPACE);
    writer.handleNamespace(XSD.PREFIX, XSD.NAMESPACE);
    writer.handleNamespace(DCTERMS.PREFIX, DCTERMS.NAMESPACE);

    writer.handleNamespace("parent", subject.getNamespace());
    writer.handleNamespace("child", subject.stringValue().replaceFirst("/*$", "/"));
    writer.handleNamespace("this", subject.stringValue().replaceFirst("/*$", "#"));

    while (iteration.hasNext()) {
      writer.handleStatement(iteration.next());
    }

    writer.endRDF();
  }
Esempio n. 19
0
 @Override
 public CloseableIteration<RyaURI, RyaDAOException> join(
     C conf, Map.Entry<RyaURI, RyaType>... predObjs) throws RyaDAOException {
   ConcurrentHashMap<RyaURI, Integer> ht = new ConcurrentHashMap<RyaURI, Integer>();
   int count = 0;
   boolean first = true;
   for (Map.Entry<RyaURI, RyaType> predObj : predObjs) {
     count++;
     RyaURI pred = predObj.getKey();
     RyaType obj = predObj.getValue();
     // query
     CloseableIteration<RyaStatement, RyaDAOException> results =
         ryaQueryEngine.query(new RyaStatement(null, pred, obj), null);
     // add to hashtable
     while (results.hasNext()) {
       RyaURI subject = results.next().getSubject();
       if (!first) {
         if (!ht.containsKey(subject)) {
           continue; // not in join
         }
       }
       ht.put(subject, count);
     }
     // remove from hashtable values that are under count
     if (first) {
       first = false;
     } else {
       for (Map.Entry<RyaURI, Integer> entry : ht.entrySet()) {
         if (entry.getValue() < count) {
           ht.remove(entry.getKey());
         }
       }
     }
   }
   return new EnumerationWrapper<RyaURI, RyaDAOException>(ht.keys());
 }
    private void evaluateIntoStatements(ParsedGraphQuery query, Collection<Statement> statements)
        throws SailException, RDFHandlerException, QueryEvaluationException {
      CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter =
          getWrappedConnection()
              .evaluate(query.getTupleExpr(), null, EmptyBindingSet.getInstance(), true);

      try {
        ValueFactory vf = getValueFactory();

        while (bindingsIter.hasNext()) {
          BindingSet bindings = bindingsIter.next();

          Value subj = bindings.getValue("subject");
          Value pred = bindings.getValue("predicate");
          Value obj = bindings.getValue("object");

          if (subj instanceof Resource && pred instanceof IRI && obj != null) {
            statements.add(vf.createStatement((Resource) subj, (IRI) pred, obj));
          }
        }
      } finally {
        bindingsIter.close();
      }
    }