use of org.apache.jena.sparql.engine.binding.BindingComparator in project jena by apache.
the class TestDistinctDataBag 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<>();
DistinctDataBag<Binding> db = new DistinctDataBag<>(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));
}
use of org.apache.jena.sparql.engine.binding.BindingComparator in project jena by apache.
the class TestDistinctDataNet 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));
}
DistinctDataNet<Binding> db = new DistinctDataNet<>(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);
}
use of org.apache.jena.sparql.engine.binding.BindingComparator 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);
}
use of org.apache.jena.sparql.engine.binding.BindingComparator 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);
}
Aggregations