use of org.apache.jena.atlas.AtlasException in project jena by apache.
the class DefaultDataBag method add.
@Override
public void add(E item) {
checkClosed();
if (finishedAdding)
throw new AtlasException("DefaultDataBag: Cannot add any more items after the writing phase is complete.");
if (!policy.isThresholdExceeded()) {
memory.add(item);
} else {
if (!spilled) {
spill();
spilled = true;
}
// Write to disk
serializer.send(item);
}
policy.increment(item);
size++;
}
use of org.apache.jena.atlas.AtlasException in project jena by apache.
the class DefaultDataBag method iterator.
@Override
public Iterator<E> iterator() {
Iterator<E> toReturn;
checkClosed();
// Close the writer
closeWriter();
// Create a new reader
if (policy.isThresholdExceeded()) {
File spillFile = getSpillFiles().get(0);
InputStream in;
try {
in = new BufferedInputStream(new FileInputStream(spillFile));
} catch (FileNotFoundException ex) {
throw new AtlasException(ex);
}
Iterator<E> deserializer = serializationFactory.createDeserializer(in);
IteratorResourceClosing<E> irc = new IteratorResourceClosing<>(deserializer, in);
registerCloseableIterator(irc);
toReturn = irc;
} else {
toReturn = memory.iterator();
}
return toReturn;
}
use of org.apache.jena.atlas.AtlasException in project jena by apache.
the class DistinctDataNet method netIterator.
/**
* Returns an iterator to all additional items that are distinct but were
* not reported to be so at the time {@link #netAdd(Object)} was invoked.
* <p/>
* If you do not exhaust the iterator, you should call {@link org.apache.jena.atlas.iterator.Iter#close(Iterator)}
* to be sure any open file handles are closed.
*/
public Iterator<E> netIterator() {
// If we havn't spilled, then we have already indicated all distinct values via .netAdd()
if (!spilled) {
return Iter.nullIterator();
}
Iterator<E> blacklist;
try {
blacklist = getInputIterator(firstSpillFile);
} catch (FileNotFoundException e) {
throw new AtlasException("Cannot find the first spill file", e);
}
// TODO: Improve performance by making the superclass .iterator() use getNetSpillFiles()
// instead of getSpillFiles() so it doesn't contain the contents of the first file
Iterator<E> rest = super.iterator();
SortedDiffIterator<E> sdi = SortedDiffIterator.create(rest, blacklist, comparator);
registerCloseableIterator(sdi);
return sdi;
}
use of org.apache.jena.atlas.AtlasException in project jena by apache.
the class SortedDataBag method iterator.
@SuppressWarnings({ "unchecked" })
private Iterator<E> iterator(int size) {
checkClosed();
int memSize = memory.size();
// like all the the other methods)
if (!finishedAdding && memSize > 1) {
E[] array = (E[]) memory.toArray();
// don't care if we aborted or not
comparator.abortableSort(array);
memory = Arrays.asList(array);
}
finishedAdding = true;
if (spilled) {
List<Iterator<E>> inputs = new ArrayList<>(size + (memSize > 0 ? 1 : 0));
if (memSize > 0) {
inputs.add(memory.iterator());
}
for (int i = 0; i < size; i++) {
File spillFile = getSpillFiles().get(i);
try {
Iterator<E> irc = getInputIterator(spillFile);
inputs.add(irc);
} catch (FileNotFoundException e) {
// Close any open streams before we throw an exception
for (Iterator<E> it : inputs) {
Iter.close(it);
}
throw new AtlasException("Cannot find one of the spill files", e);
}
}
SpillSortIterator<E> ssi = new SpillSortIterator<>(inputs, comparator);
registerCloseableIterator(ssi);
return ssi;
} else {
if (memSize > 0) {
return memory.iterator();
} else {
return Iter.nullIterator();
}
}
}
use of org.apache.jena.atlas.AtlasException in project jena by apache.
the class SortedDataBag method preMerge.
private void preMerge() {
if (getSpillFiles() == null || getSpillFiles().size() <= MAX_SPILL_FILES) {
return;
}
try {
while (getSpillFiles().size() > MAX_SPILL_FILES) {
Sink<E> sink = serializationFactory.createSerializer(getSpillStream());
Iterator<E> ssi = iterator(MAX_SPILL_FILES);
try {
while (ssi.hasNext()) {
sink.send(ssi.next());
}
} finally {
Iter.close(ssi);
sink.close();
}
List<File> toRemove = new ArrayList<>(MAX_SPILL_FILES);
for (int i = 0; i < MAX_SPILL_FILES; i++) {
File file = getSpillFiles().get(i);
file.delete();
toRemove.add(file);
}
getSpillFiles().removeAll(toRemove);
memory = new ArrayList<>();
}
} catch (IOException e) {
throw new AtlasException(e);
}
}
Aggregations