Search in sources :

Example 1 with SortedDataBag

use of org.apache.jena.atlas.data.SortedDataBag in project jena by apache.

the class TestSortedDataBag method testSorting.

private void testSorting(int numBindings, int threshold) {
    List<Binding> unsorted = randomBindings(numBindings);
    List<SortCondition> conditions = new ArrayList<>();
    conditions.add(new SortCondition(new ExprVar("8"), Query.ORDER_ASCENDING));
    conditions.add(new SortCondition(new ExprVar("1"), Query.ORDER_ASCENDING));
    conditions.add(new SortCondition(new ExprVar("0"), Query.ORDER_DESCENDING));
    BindingComparator comparator = new BindingComparator(conditions);
    List<Binding> sorted = new ArrayList<>();
    SortedDataBag<Binding> db = new SortedDataBag<>(new ThresholdPolicyCount<Binding>(threshold), SerializationFactoryFinder.bindingSerializationFactory(), comparator);
    try {
        db.addAll(unsorted);
        Iterator<Binding> iter = db.iterator();
        while (iter.hasNext()) {
            sorted.add(iter.next());
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    Collections.sort(unsorted, comparator);
    assertEquals(unsorted, sorted);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ExprVar(org.apache.jena.sparql.expr.ExprVar) SortCondition(org.apache.jena.query.SortCondition) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) SortedDataBag(org.apache.jena.atlas.data.SortedDataBag) ArrayList(java.util.ArrayList)

Example 2 with SortedDataBag

use of org.apache.jena.atlas.data.SortedDataBag in project jena by apache.

the class TestSortedDataBag method testTemporaryFilesAreCleanedUpAfterCompletion.

@Test
public void testTemporaryFilesAreCleanedUpAfterCompletion() {
    List<Binding> unsorted = randomBindings(500);
    List<SortCondition> conditions = new ArrayList<>();
    conditions.add(new SortCondition(new ExprVar("8"), Query.ORDER_ASCENDING));
    BindingComparator comparator = new BindingComparator(conditions);
    SortedDataBag<Binding> db = new SortedDataBag<>(new ThresholdPolicyCount<Binding>(10), SerializationFactoryFinder.bindingSerializationFactory(), comparator);
    List<File> spillFiles = new ArrayList<>();
    try {
        db.addAll(unsorted);
        spillFiles.addAll(db.getSpillFiles());
        int count = 0;
        for (File file : spillFiles) {
            if (file.exists()) {
                count++;
            }
        }
        // 500 bindings divided into 50 chunks (49 in files, and 1 in memory)
        assertEquals(49, count);
        Iterator<Binding> iter = db.iterator();
        while (iter.hasNext()) {
            iter.next();
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    int count = 0;
    for (File file : spillFiles) {
        if (file.exists()) {
            count++;
        }
    }
    assertEquals(0, count);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ExprVar(org.apache.jena.sparql.expr.ExprVar) SortCondition(org.apache.jena.query.SortCondition) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) ArrayList(java.util.ArrayList) SortedDataBag(org.apache.jena.atlas.data.SortedDataBag) File(java.io.File) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 SortedDataBag (org.apache.jena.atlas.data.SortedDataBag)2 SortCondition (org.apache.jena.query.SortCondition)2 Binding (org.apache.jena.sparql.engine.binding.Binding)2 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)2 ExprVar (org.apache.jena.sparql.expr.ExprVar)2 File (java.io.File)1 Test (org.junit.Test)1