@Override
  protected BigdataSail reopenSail(final BigdataSail sail) {

    final Properties properties = sail.database.getProperties();

    if (sail.isOpen()) {

      try {

        sail.shutDown();

      } catch (Exception ex) {

        throw new RuntimeException(ex);
      }
    }

    return getSail(properties);
  }
  public void testInlineValuesLT() throws Exception {

    final BigdataSail sail = getSail();
    sail.initialize();
    final BigdataSailRepository repo = new BigdataSailRepository(sail);
    final BigdataSailRepositoryConnection cxn =
        (BigdataSailRepositoryConnection) repo.getConnection();
    cxn.setAutoCommit(false);

    try {

      final ValueFactory vf = sail.getValueFactory();

      URI A = vf.createURI("_:A");
      URI B = vf.createURI("_:B");
      URI X = vf.createURI("_:X");
      URI AGE = vf.createURI("_:AGE");
      Literal _25 = vf.createLiteral(25);
      Literal _45 = vf.createLiteral(45);

      cxn.add(A, RDF.TYPE, X);
      cxn.add(B, RDF.TYPE, X);
      cxn.add(A, AGE, _25);
      cxn.add(B, AGE, _45);

      /*
       * Note: The either flush() or commit() is required to flush the
       * statement buffers to the database before executing any operations
       * that go around the sail.
       */
      cxn.flush(); // commit();

      if (log.isInfoEnabled()) {
        log.info("\n" + sail.getDatabase().dumpStore());
      }

      {
        String query =
            "select ?s ?age "
                + "WHERE { "
                + "  ?s <"
                + RDF.TYPE
                + "> <"
                + X
                + "> . "
                + "  ?s <"
                + AGE
                + "> ?age . "
                + "  FILTER( ?age < 35 ) . "
                + "}";

        final TupleQuery tupleQuery = cxn.prepareTupleQuery(QueryLanguage.SPARQL, query);
        TupleQueryResult result = tupleQuery.evaluate();

        Collection<BindingSet> solution = new LinkedList<BindingSet>();
        solution.add(
            createBindingSet(new Binding[] {new BindingImpl("s", A), new BindingImpl("age", _25)}));

        compare(result, solution);
      }

    } finally {
      cxn.close();
      sail.__tearDownUnitTest();
    }
  }
  public void test_query() throws Exception {

    final BigdataSail sail = getSail();

    try {

      sail.initialize();

      if (!((BigdataSail) sail).database.getStatementIdentifiers()) {

        log.warn("Statement identifiers are not enabled");

        return;
      }

      /*
       * Load data into the sail.
       */
      {
        final DataLoader dataLoader = sail.database.getDataLoader();

        dataLoader.loadData(
            "/com/bigdata/rdf/sail/provenance01.ttlx",
            "" /*baseURL*/,
            ServiceProviderHook.TURTLE_RDR);
      }

      /*
       * Serialize as RDF/XML.
       *
       * Note: This is just for debugging.
       */
      if (log.isInfoEnabled()) {

        final BigdataStatementIterator itr = sail.database.getStatements(null, null, null);
        final String rdfXml;
        try {

          final Writer w = new StringWriter();

          //                final RDFXMLWriter rdfWriter = new RDFXMLWriter(w);
          final RDFWriterFactory writerFactory =
              RDFWriterRegistry.getInstance().get(RDFFormat.RDFXML);

          assertNotNull(writerFactory);

          final RDFWriter rdfWriter = writerFactory.getWriter(w);

          rdfWriter.startRDF();

          while (itr.hasNext()) {

            final BigdataStatementImpl stmt = (BigdataStatementImpl) itr.next();

            // only write the explicit statements.
            if (!stmt.isExplicit()) continue;

            rdfWriter.handleStatement(stmt);
          }

          rdfWriter.endRDF();

          rdfXml = w.toString();

        } finally {

          itr.close();
        }

        // write the rdf/xml
        log.info(rdfXml);
      }

      final SailConnection conn = sail.getConnection();

      try {

        final URI y = new URIImpl("http://www.foo.org/y");

        final URI B = new URIImpl("http://www.foo.org/B");

        final URI dcCreator = new URIImpl("http://purl.org/dc/terms/creator");

        final Literal bryan = new LiteralImpl("bryan");

        final Literal mike = new LiteralImpl("mike");

        /*
         * This is a hand-coded query.
         *
         * Note: When statement identifiers are enabled, the only way to
         * bind the context position is to already have a statement on hand -
         * there is no index which can be used to look up a statement by its
         * context and the context is always a blank node.
         */

        //            final TupleExpr tupleExpr =
        //                new Projection(
        //                new Join(//
        //                    new StatementPattern(//
        //                            new Var("X", y),//
        //                            new Var("1", RDF.TYPE),//
        //                            new Var("2", B),//
        //                            new Var("SID")),// unbound.
        //                    new StatementPattern(//
        //                            new Var("SID"),//
        //                            new Var("3", dcCreator),//
        //                            new Var("Y"))),
        //                new ProjectionElemList(new ProjectionElem[] { new ProjectionElem( "Y"
        // )}));

        //            final String q = "select ?Y where { ?SID <"+dcCreator+"> ?Y . graph ?SID {
        // <"+y+"> <"+RDF.TYPE+"> <"+B+"> . } }";
        final String q =
            "select ?Y where { <<<"
                + y
                + "> <"
                + RDF.TYPE
                + "> <"
                + B
                + ">>> <"
                + dcCreator
                + "> ?Y . }";

        /*
         * Create a data set consisting of the contexts to be queried.
         *
         * Note: a [null] DataSet will cause context to be ignored when the
         * query is processed.
         */
        //            final DatasetImpl dataSet = null; //new DatasetImpl();
        //
        //            final BindingSet bindingSet = new QueryBindingSet();
        //
        //            final CloseableIteration<? extends BindingSet, QueryEvaluationException> itr =
        // conn
        //                    .evaluate(tupleExpr, dataSet, bindingSet, true/* includeInferred */);

        final TupleQuery tq =
            new BigdataSailRepository(sail)
                .getReadOnlyConnection()
                .prepareTupleQuery(QueryLanguage.SPARQL, q);

        final TupleQueryResult itr = tq.evaluate();

        if (log.isInfoEnabled()) log.info("Verifying query.");

        /*
         * These are the expected results for the query (the bindings for Y).
         */

        final Set<Value> expected = new HashSet<Value>();

        expected.add(bryan);

        expected.add(mike);

        /*
         * Verify that the query results is the correct solutions.
         */

        final int nresults = expected.size();

        try {

          int i = 0;

          while (itr.hasNext()) {

            final BindingSet solution = itr.next();

            if (log.isInfoEnabled()) log.info("solution[" + i + "] : " + solution);

            final Value actual = solution.getValue("Y");

            assertTrue("Not expecting Y=" + actual, expected.remove(actual));

            i++;
          }

          assertEquals("#results", nresults, i);

        } finally {

          itr.close();
        }

      } finally {

        conn.close();
      }

    } finally {

      sail.__tearDownUnitTest();
    }
  }