DistinctDataBag extends SortedDataBag<E> { @Override public Iterator<E> iterator() { if (!spilled) { checkClosed(); finishedAdding = true; if (memory.size() > 0) { return memory.iterator(); } else { return Iter.nullIterator(); } } else { return new DistinctReducedIterator<>(super.iterator()); } } DistinctDataBag(ThresholdPolicy<E> policy, SerializationFactory<E> serializerFactory, Comparator<E> comparator); @Override boolean isSorted(); @Override boolean isDistinct(); @Override Iterator<E> iterator();  }
@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.sameNode)); }
@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++; } } 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); }
@Test public void testOptionalVariables() { BindingMap binding1 = BindingFactory.create(); binding1.add(Var.alloc("1"), NodeFactory.createLiteral("A")); BindingMap binding2 = BindingFactory.create(); binding2.add(Var.alloc("1"), NodeFactory.createLiteral("A")); binding2.add(Var.alloc("2"), NodeFactory.createLiteral("B")); List<Binding> undistinct = Arrays.asList(binding1, binding2, binding1); 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.sameNode)); }