<commit_before>package org.linkeddatafragments.datasource;

import com.hp.hpl.jena.graph.GraphStatisticsHandler;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.query.ReadWrite;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.tdb.TDBFactory;
import java.io.File;

/**
 * Experimental Jena TDB-backed data source of Basic Linked Data Fragments.
 * 
 * @author <NAME> <<EMAIL>>
 */
public class JenaTDBDataSource extends DataSource {
    private final Dataset tdb;
    private final String sparql = "CONSTRUCT { ?s ?p ?o } " +
                                    "WHERE { ?s ?p ?o } " +
                                    "ORDER BY ?s ?p ?o";
    
    private final Query query = QueryFactory.create(sparql, Syntax.syntaxSPARQL_11);
    
    @Override
    public TriplePatternFragment getFragment(Resource subject, Property predicate, RDFNode object, long offset, long limit) {
        checkBoundaries(offset, limit);
        
        tdb.begin(ReadWrite.READ);
        
        Model model = tdb.getDefaultModel();
        QuerySolutionMap map = new QuerySolutionMap();
        map.add("s", subject);
        map.add("p", predicate);
        map.add("o", object);
        
        query.setOffset(offset);
        query.setLimit(limit);

        QueryExecution qexec = QueryExecutionFactory.create(query, model, map);
        Model triples = ModelFactory.createDefaultModel();
        qexec.execConstruct(triples);

        // For now, fake the estimate
        long size = triples.size();
        long estimate = (size == limit) ? offset + limit + 1 : offset + size;
        // Try to get a better estimate
        GraphStatisticsHandler stats = model.getGraph().getStatisticsHandler();
        if (stats != null) {
            estimate = stats.getStatistic(subject.asNode(), predicate.asNode(), object.asNode());
        }
        
        tdb.end();<commit_msg>Corrections for query and better estimate<commit_after>package org.linkeddatafragments.datasource;

import com.hp.hpl.jena.graph.GraphStatisticsHandler;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.query.ReadWrite;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.tdb.TDBFactory;
import java.io.File;

/**
 * Experimental Jena TDB-backed data source of Basic Linked Data Fragments.
 * 
 * @author <NAME> <<EMAIL>>
 */
public class JenaTDBDataSource extends DataSource {
    private final Dataset tdb;
    private final String sparql = "CONSTRUCT { ?s ?p ?o } " +
                                    "WHERE { ?s ?p ?o } " +
                                    "ORDER BY ?s ?p ?o";
    
    private final Query query = QueryFactory.create(sparql, Syntax.syntaxSPARQL_11);
    
    @Override
    public TriplePatternFragment getFragment(Resource subject, Property predicate, RDFNode object, long offset, long limit) {
        checkBoundaries(offset, limit);
        
        tdb.begin(ReadWrite.READ);
        
        Model model = tdb.getDefaultModel();
        QuerySolutionMap map = new QuerySolutionMap();
        map.add("s", subject);
        map.add("p", predicate);
        map.add("o", object);
        
        query.setOffset(offset);
        query.setLimit(limit);

        QueryExecution qexec = QueryExecutionFactory.create(query, model, map);
        Model triples = ModelFactory.createDefaultModel();
        qexec.execConstruct(triples);

        // Try to get an estimate
        long size = triples.size();
        long estimate = -1;

        GraphStatisticsHandler stats = model.getGraph().getStatisticsHandler();
        if (stats != null) {
            estimate = stats.getStatistic(subject.asNode(), predicate.asNode(), object.asNode());
        }
        // No estimate or incorrect
        if (estimate < offset + size) {
            estimate = (size == limit) ? offset + size + 1 : offset + size;
        }
        
        tdb.end();
{'fraction_non_alphanumeric': 0.11989213856046463, 'fraction_numerical': 0.001451980916822236, 'mean_word_length': 2.712086220169361, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5042967', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1506, 'n_words': 318}