Search in sources :

Example 1 with AtlasException

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++;
}
Also used : AtlasException(org.apache.jena.atlas.AtlasException)

Example 2 with AtlasException

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IteratorResourceClosing(org.apache.jena.atlas.iterator.IteratorResourceClosing) FileNotFoundException(java.io.FileNotFoundException) AtlasException(org.apache.jena.atlas.AtlasException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with AtlasException

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;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) AtlasException(org.apache.jena.atlas.AtlasException)

Example 4 with AtlasException

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();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) FileNotFoundException(java.io.FileNotFoundException) AtlasException(org.apache.jena.atlas.AtlasException) File(java.io.File)

Example 5 with AtlasException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) AtlasException(org.apache.jena.atlas.AtlasException) File(java.io.File)

Aggregations

AtlasException (org.apache.jena.atlas.AtlasException)27 IOException (java.io.IOException)5 FileNotFoundException (java.io.FileNotFoundException)4 File (java.io.File)3 InputStream (java.io.InputStream)3 RiotParseException (org.apache.jena.riot.RiotParseException)3 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 Token (org.apache.jena.riot.tokens.Token)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 Iterator (java.util.Iterator)1 InStreamUTF8 (org.apache.jena.atlas.io.InStreamUTF8)1 InputStreamBuffered (org.apache.jena.atlas.io.InputStreamBuffered)1 IteratorResourceClosing (org.apache.jena.atlas.iterator.IteratorResourceClosing)1 ProgressMonitor (org.apache.jena.atlas.lib.ProgressMonitor)1 BlockMgr (org.apache.jena.tdb.base.block.BlockMgr)1 FileSet (org.apache.jena.tdb.base.file.FileSet)1 Location (org.apache.jena.tdb.base.file.Location)1