001package org.syncany.operations.up;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.List;
006import java.util.Queue;
007import java.util.logging.Level;
008import java.util.logging.Logger;
009
010import org.syncany.chunk.Deduper;
011import org.syncany.config.Config;
012import org.syncany.database.DatabaseVersion;
013
014/**
015 * AsyncIndexer provides a Runnable to start as a separate thread. Running
016 * this will result in the list of files provided being indexed and deduped. 
017 * The result of this indexation will be captured in DatabaseVersions, which
018 * will be stored in the provided Queue, which must be threadsafe.
019 * 
020 * @author Tim Hegeman
021 */
022public class AsyncIndexer implements Runnable {
023        private static final Logger logger = Logger.getLogger(AsyncIndexer.class.getSimpleName());
024
025        private final Indexer indexer;
026        private final List<File> files;
027        private final List<File> deletedFiles;
028        private final Queue<DatabaseVersion> databaseVersionQueue;
029
030        /** 
031         * @param config specifying all necessary options
032         * @param deduper the Deduper, already configured.
033         * @param files List of Files to be indexed.
034         * @param queue a threadsafe Queue to communicate DatabaseVersions.
035         */
036        public AsyncIndexer(Config config, Deduper deduper, List<File> files, List<File> deletedFiles, Queue<DatabaseVersion> queue) {
037                this.files = files;
038                this.databaseVersionQueue = queue;
039                this.indexer = new Indexer(config, deduper);
040                this.deletedFiles = deletedFiles;
041        }
042
043        @Override
044        public void run() {
045                try {
046                        logger.log(Level.INFO, "Starting Indexing.");
047                        indexer.index(files, deletedFiles, databaseVersionQueue);
048                }
049                catch (IOException e) {
050                        // TODO: Store this exception as a "result"?
051                        e.printStackTrace();
052                }
053                // Signal end-of-stream.
054                logger.log(Level.INFO, "Stopping indexing. Signal end of stream with empty databaseversion");
055                databaseVersionQueue.offer(new DatabaseVersion());
056        }
057
058}