Search in sources :

Example 1 with BindingComparator

use of org.apache.jena.sparql.engine.binding.BindingComparator in project jena by apache.

the class TestDistinctDataBag method testTemporaryFilesAreCleanedUpAfterCompletion.

@Test
public void testTemporaryFilesAreCleanedUpAfterCompletion() {
    List<Binding> undistinct = new ArrayList<>();
    random = new Random();
    Var[] vars = new Var[] { Var.alloc("1"), Var.alloc("2"), Var.alloc("3"), Var.alloc("4"), Var.alloc("5"), Var.alloc("6"), Var.alloc("7"), Var.alloc("8"), Var.alloc("9"), Var.alloc("0") };
    for (int i = 0; i < 500; i++) {
        undistinct.add(randomBinding(vars));
    }
    DistinctDataBag<Binding> db = new DistinctDataBag<>(new ThresholdPolicyCount<Binding>(10), SerializationFactoryFinder.bindingSerializationFactory(), new BindingComparator(new ArrayList<SortCondition>()));
    List<File> spillFiles = new ArrayList<>();
    try {
        db.addAll(undistinct);
        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) BuilderBinding(org.apache.jena.sparql.sse.builders.BuilderBinding) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) DistinctDataBag(org.apache.jena.atlas.data.DistinctDataBag) Random(java.util.Random) File(java.io.File) Test(org.junit.Test)

Example 2 with BindingComparator

use of org.apache.jena.sparql.engine.binding.BindingComparator in project jena by apache.

the class TestDistinctDataNet method testDistinct2.

@Test
public void testDistinct2() {
    List<Binding> undistinct = new ArrayList<>();
    undistinct.add(b12);
    undistinct.add(b19);
    undistinct.add(b02);
    undistinct.add(b12);
    undistinct.add(b19);
    undistinct.add(b12);
    undistinct.add(b02);
    undistinct.add(x10);
    List<Binding> control = Iter.toList(Iter.distinct(undistinct.iterator()));
    List<Binding> distinct = new ArrayList<>();
    DistinctDataNet<Binding> db = new DistinctDataNet<>(new ThresholdPolicyCount<Binding>(2), SerializationFactoryFinder.bindingSerializationFactory(), new BindingComparator(new ArrayList<SortCondition>()));
    try {
        for (Binding b : undistinct) {
            if (db.netAdd(b)) {
                distinct.add(b);
            }
        }
        Iterator<Binding> iter = db.netIterator();
        while (iter.hasNext()) {
            distinct.add(iter.next());
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    assertEquals(control.size(), distinct.size());
    assertTrue(ResultSetCompare.equalsByTest(control, distinct, NodeUtils.sameTerm));
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) BuilderBinding(org.apache.jena.sparql.sse.builders.BuilderBinding) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) DistinctDataNet(org.apache.jena.atlas.data.DistinctDataNet) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with BindingComparator

use of org.apache.jena.sparql.engine.binding.BindingComparator in project jena by apache.

the class TestDistinctDataNet method testDistinct.

@Test
public void testDistinct() {
    List<Binding> undistinct = new ArrayList<>();
    undistinct.add(b12);
    undistinct.add(b19);
    undistinct.add(b02);
    undistinct.add(b12);
    undistinct.add(b19);
    undistinct.add(b12);
    undistinct.add(b02);
    undistinct.add(x10);
    List<Binding> control = Iter.toList(Iter.distinct(undistinct.iterator()));
    List<Binding> distinct = new ArrayList<>();
    DistinctDataNet<Binding> db = new DistinctDataNet<>(new ThresholdPolicyCount<Binding>(2), SerializationFactoryFinder.bindingSerializationFactory(), new BindingComparator(new ArrayList<SortCondition>()));
    try {
        db.addAll(undistinct);
        Iterator<Binding> iter = db.iterator();
        while (iter.hasNext()) {
            distinct.add(iter.next());
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    assertEquals(control.size(), distinct.size());
    assertTrue(ResultSetCompare.equalsByTest(control, distinct, NodeUtils.sameTerm));
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) BuilderBinding(org.apache.jena.sparql.sse.builders.BuilderBinding) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) DistinctDataNet(org.apache.jena.atlas.data.DistinctDataNet) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with BindingComparator

use of org.apache.jena.sparql.engine.binding.BindingComparator in project jena by apache.

the class QueryIterDistinct method loadDataBag.

/** Load the data bag with. Filter incoming by the already seen in-memory elements */
private void loadDataBag() {
    ThresholdPolicy<Binding> policy = ThresholdPolicyFactory.policyFromContext(super.getExecContext().getContext());
    Comparator<Binding> comparator = new BindingComparator(new ArrayList<SortCondition>(), super.getExecContext());
    this.db = BagFactory.newDistinctBag(policy, SerializationFactoryFinder.bindingSerializationFactory(), comparator);
    for (; ; ) {
        Binding b = getInputNextUnseen();
        if (b == null)
            break;
        db.add(b);
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) SortCondition(org.apache.jena.query.SortCondition) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator)

Example 5 with BindingComparator

use of org.apache.jena.sparql.engine.binding.BindingComparator in project jena by apache.

the class TestQueryIterSort method setup.

@Before
public void setup() {
    random = new Random();
    Var[] vars = new Var[] { Var.alloc("1"), Var.alloc("2"), Var.alloc("3"), Var.alloc("4"), Var.alloc("5"), Var.alloc("6"), Var.alloc("7"), Var.alloc("8"), Var.alloc("9"), Var.alloc("0") };
    unsorted = new ArrayList<>();
    for (int i = 0; i < 500; i++) {
        unsorted.add(randomBinding(vars));
    }
    List<SortCondition> conditions = new ArrayList<>();
    conditions.add(new SortCondition(new ExprVar("8"), Query.ORDER_ASCENDING));
    comparator = new BindingComparator(conditions);
    iterator = new CallbackIterator(unsorted.iterator(), 25, null);
    iterator.setCallback(new Callback() {

        @Override
        public void call() {
            throw new QueryCancelledException();
        }
    });
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) SortCondition(org.apache.jena.query.SortCondition) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) Random(java.util.Random) QueryCancelledException(org.apache.jena.query.QueryCancelledException) Before(org.junit.Before)

Aggregations

BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)9 ArrayList (java.util.ArrayList)8 Binding (org.apache.jena.sparql.engine.binding.Binding)8 Test (org.junit.Test)6 BuilderBinding (org.apache.jena.sparql.sse.builders.BuilderBinding)5 SortCondition (org.apache.jena.query.SortCondition)4 File (java.io.File)3 Random (java.util.Random)3 DistinctDataNet (org.apache.jena.atlas.data.DistinctDataNet)3 Var (org.apache.jena.sparql.core.Var)3 ExprVar (org.apache.jena.sparql.expr.ExprVar)3 DistinctDataBag (org.apache.jena.atlas.data.DistinctDataBag)2 SortedDataBag (org.apache.jena.atlas.data.SortedDataBag)2 QueryCancelledException (org.apache.jena.query.QueryCancelledException)1 Before (org.junit.Before)1