Example usage for org.apache.lucene.store Directory fileLength

List of usage examples for org.apache.lucene.store Directory fileLength

Introduction

In this page you can find the example usage for org.apache.lucene.store Directory fileLength.

Prototype

public abstract long fileLength(String name) throws IOException;

Source Link

Document

Returns the byte length of a file in the directory.

Usage

From source file:com.bah.lucene.BaseDirectoryTestSuite.java

License:Apache License

@Test
public void testEOF() throws IOException {
    Directory fsDir = new RAMDirectory();
    String name = "test.eof";
    createFile(name, fsDir, directory);/*from  ww  w .ja va 2  s .c  om*/
    long fsLength = fsDir.fileLength(name);
    long hdfsLength = directory.fileLength(name);
    assertEquals(fsLength, hdfsLength);
    testEof(name, fsDir, fsLength);
    testEof(name, directory, hdfsLength);
}

From source file:com.bah.lucene.BaseDirectoryTestSuite.java

License:Apache License

private Directory getControlDir(final Directory control, final Directory test) {
    return new Directory() {

        @Override//from ww  w  .  j av a  2 s  . co  m
        public Lock makeLock(String name) {
            return control.makeLock(name);
        }

        @Override
        public void clearLock(String name) throws IOException {
            control.clearLock(name);
        }

        @Override
        public void setLockFactory(LockFactory lockFactory) throws IOException {
            control.setLockFactory(lockFactory);
        }

        @Override
        public LockFactory getLockFactory() {
            return control.getLockFactory();
        }

        @Override
        public String getLockID() {
            return control.getLockID();
        }

        @Override
        public void copy(Directory to, String src, String dest, IOContext context) throws IOException {
            control.copy(to, src, dest, context);
        }

        @Override
        public IndexInputSlicer createSlicer(String name, IOContext context) throws IOException {
            return control.createSlicer(name, context);
        }

        @Override
        public IndexOutput createOutput(final String name, IOContext context) throws IOException {
            final IndexOutput testOutput = test.createOutput(name, context);
            final IndexOutput controlOutput = control.createOutput(name, context);
            return new IndexOutput() {

                @Override
                public void flush() throws IOException {
                    testOutput.flush();
                    controlOutput.flush();
                }

                @Override
                public void close() throws IOException {
                    testOutput.close();
                    controlOutput.close();
                }

                @Override
                public long getFilePointer() {
                    long filePointer = testOutput.getFilePointer();
                    long controlFilePointer = controlOutput.getFilePointer();
                    if (controlFilePointer != filePointer) {
                        System.err.println("Output Name [" + name + "] with filePointer [" + filePointer
                                + "] and control filePointer [" + controlFilePointer + "] does not match");
                    }
                    return filePointer;
                }

                @SuppressWarnings("deprecation")
                @Override
                public void seek(long pos) throws IOException {
                    testOutput.seek(pos);
                    controlOutput.seek(pos);
                }

                @Override
                public long length() throws IOException {
                    long length = testOutput.length();
                    long controlLength = controlOutput.length();
                    if (controlLength != length) {
                        System.err.println("Ouput Name [" + name + "] with length [" + length
                                + "] and control length [" + controlLength + "] does not match");
                    }
                    return length;
                }

                @Override
                public void writeByte(byte b) throws IOException {
                    testOutput.writeByte(b);
                    controlOutput.writeByte(b);
                }

                @Override
                public void writeBytes(byte[] b, int offset, int length) throws IOException {
                    testOutput.writeBytes(b, offset, length);
                    controlOutput.writeBytes(b, offset, length);
                }

            };
        }

        @Override
        public IndexInput openInput(final String name, IOContext context) throws IOException {
            final IndexInput testInput = test.openInput(name, context);
            final IndexInput controlInput = control.openInput(name, context);
            return new IndexInputCompare(name, testInput, controlInput);
        }

        @Override
        public String[] listAll() throws IOException {
            return test.listAll();
        }

        @Override
        public boolean fileExists(String name) throws IOException {
            return test.fileExists(name);
        }

        @Override
        public void deleteFile(String name) throws IOException {
            test.deleteFile(name);
            control.deleteFile(name);
        }

        @Override
        public long fileLength(String name) throws IOException {
            long fileLength = test.fileLength(name);
            long controlFileLength = control.fileLength(name);
            if (controlFileLength != fileLength) {
                System.err.println("Input Name [" + name + "] with length [" + fileLength
                        + "] and control length [" + controlFileLength + "] does not match");
            }
            return fileLength;
        }

        @Override
        public void sync(Collection<String> names) throws IOException {
            test.sync(names);
            test.sync(names);
        }

        @Override
        public void close() throws IOException {
            test.close();
            control.close();
        }
    };
}

From source file:com.bah.lucene.blockcache.BlockDirectoryTest.java

License:Apache License

@Test
public void testEOF() throws IOException {
    Directory fsDir = FSDirectory.open(new File(file, "normal"));
    String name = "test.eof";
    createFile(name, fsDir, directory);/*from w  ww.j  a va  2s  .c o m*/
    long fsLength = fsDir.fileLength(name);
    long hdfsLength = directory.fileLength(name);
    assertEquals(fsLength, hdfsLength);
    testEof(name, fsDir, fsLength);
    testEof(name, directory, hdfsLength);
}

From source file:com.github.rnewson.couchdb.lucene.Search.java

License:Apache License

private static long size(final Directory dir) throws IOException {
    long result = 0;
    for (final String name : dir.list()) {
        result += dir.fileLength(name);
    }/*from  w  ww.  ja v  a  2  s.c  o  m*/
    return result;
}

From source file:com.github.rnewson.couchdb.lucene.util.Utils.java

License:Apache License

public static long directorySize(final Directory dir) throws IOException {
    long result = 0;
    for (final String name : dir.listAll()) {
        result += dir.fileLength(name);
    }/*from w  ww. j a v a  2s .  co m*/
    return result;
}

From source file:com.liferay.portal.search.lucene.dump.DumpIndexDeletionPolicyTest.java

License:Open Source License

private void _assertDirectory(Directory sourceDirectory, Directory targetDirectory) throws Exception {

    String[] sourceFileNames = sourceDirectory.listAll();

    Arrays.sort(sourceFileNames);

    String[] targetFileNames = targetDirectory.listAll();

    Arrays.sort(targetFileNames);

    if (sourceFileNames.length != targetFileNames.length) {
        fail(Arrays.toString(sourceFileNames) + " does not have the same length as "
                + Arrays.toString(targetFileNames));
    }/*from  www  . jav a2 s .c  om*/

    for (String fileName : sourceFileNames) {
        long sourceLength = sourceDirectory.fileLength(fileName);
        long targetLength = targetDirectory.fileLength(fileName);

        if (sourceLength != targetLength) {
            fail(fileName + " has different source and target lengths");
        }

        _assertContent(fileName, sourceDirectory.openInput(fileName), targetDirectory.openInput(fileName));
    }
}

From source file:com.liferay.portal.search.lucene.dump.IndexCommitMetaInfo.java

License:Open Source License

public IndexCommitMetaInfo(IndexCommit indexCommit) throws IOException {
    if (indexCommit == null) {
        _empty = true;/*  www.j av  a 2s.  c  om*/

        return;
    }

    List<String> fileNames = new ArrayList<String>(indexCommit.getFileNames());

    _segments = new ArrayList<Segment>(fileNames.size());

    Directory directory = indexCommit.getDirectory();

    for (String fileName : fileNames) {
        Segment segment = new Segment(fileName, directory.fileLength(fileName));

        _segments.add(segment);
    }

    _generation = indexCommit.getGeneration();
}

From source file:com.nearinfinity.blur.mapreduce.BlurReducer.java

License:Apache License

protected long getTotalBytes(Directory directory) throws IOException {
    long total = 0;
    for (String file : directory.listAll()) {
        total += directory.fileLength(file);
    }//  w ww.  j ava 2 s . c o m
    return total;
}

From source file:com.tuplejump.stargate.lucene.BasicIndexer.java

License:Apache License

public static long calcTotalFileSize(String path, Directory directory) throws Exception {
    long totalFileSize = 0L;
    String[] files = directory.listAll();
    if (files == null)
        return totalFileSize;
    for (int i = 0; i < files.length; i++) {
        totalFileSize += directory.fileLength(files[i]);
    }// w ww.  ja  va  2s . c om
    return totalFileSize;
}

From source file:ensemble.compiletime.search.BuildEnsembleSearchIndex.java

License:Open Source License

public static void buildSearchIndex(List<Sample> allSamples, String javaDocBaseUrl,
        String javafxDocumentationHome, File indexDir) {
    try {//w  ww . j  ava2s .c  om
        List<Document> docs = new ArrayList<>();
        List<Callable<List<Document>>> tasks = new ArrayList<>();
        // create callables to collect data
        System.out.println("Creating Documents for Samples...");
        docs.addAll(indexSamples(allSamples));
        System.out.println("Creating tasks for getting all documentation...");
        tasks.addAll(indexJavaDocAllClasses(javaDocBaseUrl));
        tasks.addAll(indexAllDocumentation(javafxDocumentationHome));
        // execute all the tasks in 32 threads, collecting all the documents to write
        System.out.println("Executing tasks getting all documentation...");
        try {
            ThreadPoolExecutor executor = new ThreadPoolExecutor(32, 32, 30, TimeUnit.SECONDS,
                    new LinkedBlockingQueue());
            executor.setThreadFactory(new ThreadFactory() {
                int index = 0;

                @Override
                public Thread newThread(Runnable r) {
                    Thread thread = new Thread(r, "Thread-" + (++index));
                    thread.setDaemon(true);
                    return thread;
                }
            });
            List<Future<List<Document>>> results = executor.invokeAll(tasks);
            for (Future<List<Document>> future : results) {
                docs.addAll(future.get());
            }
        } catch (ExecutionException | InterruptedException ex) {
            Logger.getLogger(BuildEnsembleSearchIndex.class.getName()).log(Level.SEVERE, null, ex);
        }
        // create index
        System.out.println("Indexing to directory '" + indexDir + "'...");
        Directory dir = FSDirectory.open(indexDir);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer);
        iwc.setOpenMode(OpenMode.CREATE);
        try (IndexWriter writer = new IndexWriter(dir, iwc)) {
            // write all docs
            System.out.println("Writing [" + docs.size() + "] documents to index....");
            writer.addDocuments(docs);
            // optimize the writen index
            System.out.println("Optimizing search index....");
            writer.optimize();
            System.out.println("NUMBER OF INDEXED DOCUMENTS = [" + writer.numDocs() + "]");
        }
        // write file listing all the search index files, so we know what
        // is in the jar file at runtime
        try (FileWriter listAllOut = new FileWriter(new File(indexDir, "listAll.txt"))) {
            for (String fileName : dir.listAll()) {
                if (!"listAll.txt".equals(fileName)) { // don't include the "listAll.txt" file
                    Long length = dir.fileLength(fileName);
                    listAllOut.write(fileName);
                    listAllOut.write(':');
                    listAllOut.write(length.toString());
                    listAllOut.write('\n');
                }
            }
            listAllOut.flush();
        }
        System.out.println("Finished writing search index to directory '" + indexDir);
    } catch (IOException ex) {
        Logger.getLogger(BuildEnsembleSearchIndex.class.getName()).log(Level.SEVERE, null, ex);
    }
}