Search in sources :

Example 41 with FilenameFilter

use of java.io.FilenameFilter in project pinot by linkedin.

the class CreateSegmentCommand method execute.

@Override
public boolean execute() throws Exception {
    LOGGER.info("Executing command: {}", toString());
    // Load generator config if exist.
    final SegmentGeneratorConfig segmentGeneratorConfig;
    if (_generatorConfigFile != null) {
        segmentGeneratorConfig = new ObjectMapper().readValue(new File(_generatorConfigFile), SegmentGeneratorConfig.class);
    } else {
        segmentGeneratorConfig = new SegmentGeneratorConfig();
    }
    // Load config from segment generator config.
    String configDataDir = segmentGeneratorConfig.getDataDir();
    if (_dataDir == null) {
        if (configDataDir == null) {
            throw new RuntimeException("Must specify dataDir.");
        }
        _dataDir = configDataDir;
    } else {
        if (configDataDir != null && !configDataDir.equals(_dataDir)) {
            LOGGER.warn("Find dataDir conflict in command line and config file, use config in command line: {}", _dataDir);
        }
    }
    FileFormat configFormat = segmentGeneratorConfig.getFormat();
    if (_format == null) {
        if (configFormat == null) {
            throw new RuntimeException("Format cannot be null in config file.");
        }
        _format = configFormat;
    } else {
        if (configFormat != _format && configFormat != FileFormat.AVRO) {
            LOGGER.warn("Find format conflict in command line and config file, use config in command line: {}", _format);
        }
    }
    String configOutDir = segmentGeneratorConfig.getOutDir();
    if (_outDir == null) {
        if (configOutDir == null) {
            throw new RuntimeException("Must specify outDir.");
        }
        _outDir = configOutDir;
    } else {
        if (configOutDir != null && !configOutDir.equals(_outDir)) {
            LOGGER.warn("Find outDir conflict in command line and config file, use config in command line: {}", _outDir);
        }
    }
    if (segmentGeneratorConfig.isOverwrite()) {
        _overwrite = true;
    }
    String configTableName = segmentGeneratorConfig.getTableName();
    if (_tableName == null) {
        if (configTableName == null) {
            throw new RuntimeException("Must specify tableName.");
        }
        _tableName = configTableName;
    } else {
        if (configTableName != null && !configTableName.equals(_tableName)) {
            LOGGER.warn("Find tableName conflict in command line and config file, use config in command line: {}", _tableName);
        }
    }
    String configSegmentName = segmentGeneratorConfig.getSegmentName();
    if (_segmentName == null) {
        if (configSegmentName == null) {
            throw new RuntimeException("Must specify segmentName.");
        }
        _segmentName = configSegmentName;
    } else {
        if (configSegmentName != null && !configSegmentName.equals(_segmentName)) {
            LOGGER.warn("Find segmentName conflict in command line and config file, use config in command line: {}", _segmentName);
        }
    }
    // Filter out all input files.
    File dir = new File(_dataDir);
    if (!dir.exists() || !dir.isDirectory()) {
        throw new RuntimeException("Data directory " + _dataDir + " not found.");
    }
    File[] files = dir.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(_format.toString().toLowerCase());
        }
    });
    if ((files == null) || (files.length == 0)) {
        throw new RuntimeException("Data directory " + _dataDir + " does not contain " + _format.toString().toUpperCase() + " files.");
    }
    // Make sure output directory does not already exist, or can be overwritten.
    File outDir = new File(_outDir);
    if (outDir.exists()) {
        if (!_overwrite) {
            throw new IOException("Output directory " + _outDir + " already exists.");
        } else {
            FileUtils.deleteDirectory(outDir);
        }
    }
    // Set other generator configs from command line.
    segmentGeneratorConfig.setDataDir(_dataDir);
    segmentGeneratorConfig.setFormat(_format);
    segmentGeneratorConfig.setOutDir(_outDir);
    segmentGeneratorConfig.setOverwrite(_overwrite);
    segmentGeneratorConfig.setTableName(_tableName);
    segmentGeneratorConfig.setSegmentName(_segmentName);
    if (_schemaFile != null) {
        if (segmentGeneratorConfig.getSchemaFile() != null && !segmentGeneratorConfig.getSchemaFile().equals(_schemaFile)) {
            LOGGER.warn("Find schemaFile conflict in command line and config file, use config in command line: {}", _schemaFile);
        }
        segmentGeneratorConfig.setSchemaFile(_schemaFile);
    }
    if (_readerConfigFile != null) {
        if (segmentGeneratorConfig.getReaderConfigFile() != null && !segmentGeneratorConfig.getReaderConfigFile().equals(_readerConfigFile)) {
            LOGGER.warn("Find readerConfigFile conflict in command line and config file, use config in command line: {}", _readerConfigFile);
        }
        segmentGeneratorConfig.setReaderConfigFile(_readerConfigFile);
    }
    if (_enableStarTreeIndex) {
        segmentGeneratorConfig.setEnableStarTreeIndex(true);
    }
    if (_starTreeIndexSpecFile != null) {
        if (segmentGeneratorConfig.getStarTreeIndexSpecFile() != null && !segmentGeneratorConfig.getStarTreeIndexSpecFile().equals(_starTreeIndexSpecFile)) {
            LOGGER.warn("Find starTreeIndexSpecFile conflict in command line and config file, use config in command line: {}", _starTreeIndexSpecFile);
        }
        segmentGeneratorConfig.setStarTreeIndexSpecFile(_starTreeIndexSpecFile);
    }
    ExecutorService executor = Executors.newFixedThreadPool(_numThreads);
    int cnt = 0;
    for (final File file : files) {
        final int segCnt = cnt;
        executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    SegmentGeneratorConfig config = new SegmentGeneratorConfig(segmentGeneratorConfig);
                    config.setInputFilePath(file.getAbsolutePath());
                    config.setSegmentName(_segmentName + "_" + segCnt);
                    config.loadConfigFiles();
                    final SegmentIndexCreationDriverImpl driver = new SegmentIndexCreationDriverImpl();
                    driver.init(config);
                    driver.build();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
        cnt += 1;
    }
    executor.shutdown();
    return executor.awaitTermination(1, TimeUnit.HOURS);
}
Also used : IOException(java.io.IOException) FileFormat(com.linkedin.pinot.core.data.readers.FileFormat) IOException(java.io.IOException) SegmentIndexCreationDriverImpl(com.linkedin.pinot.core.segment.creator.impl.SegmentIndexCreationDriverImpl) FilenameFilter(java.io.FilenameFilter) SegmentGeneratorConfig(com.linkedin.pinot.core.indexsegment.generator.SegmentGeneratorConfig) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 42 with FilenameFilter

use of java.io.FilenameFilter in project pinot by linkedin.

the class StopProcessCommand method execute.

@Override
public boolean execute() throws Exception {
    LOGGER.info("Executing command: " + toString());
    Map<String, String> processes = new HashMap<String, String>();
    String prefix = System.getProperty("java.io.tmpdir") + File.separator;
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    if (_server) {
        File[] serverFiles = tempDir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                if (StringUtils.containsIgnoreCase(name, "pinotAdminServer")) {
                    return true;
                }
                return false;
            }
        });
        for (File serverFile : serverFiles) {
            processes.put(serverFile.getName(), serverFile.getAbsolutePath());
        }
    }
    if (_broker) {
        File[] serverFiles = tempDir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                if (StringUtils.containsIgnoreCase(name, "pinotAdminBroker")) {
                    return true;
                }
                return false;
            }
        });
        for (File serverFile : serverFiles) {
            processes.put(serverFile.getName(), serverFile.getAbsolutePath());
        }
    }
    if (_controller) {
        File[] serverFiles = tempDir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                if (StringUtils.containsIgnoreCase(name, "pinotAdminController")) {
                    return true;
                }
                return false;
            }
        });
        for (File serverFile : serverFiles) {
            processes.put(serverFile.getName(), serverFile.getAbsolutePath());
        }
    }
    if (_zooKeeper) {
        processes.put("Zookeeper", prefix + ".zooKeeper.pid");
    }
    if (_kafka) {
        processes.put("Kafka", prefix + ".kafka.pid");
    }
    boolean ret = true;
    for (Map.Entry<String, String> entry : processes.entrySet()) {
        try {
            stopProcess(entry.getValue());
        } catch (Exception e) {
            System.out.println("Failed to stop process: " + entry.getKey() + ": " + e);
            ret = false;
        }
    }
    return ret;
}
Also used : FilenameFilter(java.io.FilenameFilter) HashMap(java.util.HashMap) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) IOException(java.io.IOException)

Example 43 with FilenameFilter

use of java.io.FilenameFilter in project Openfire by igniterealtime.

the class ArchiveIndexer method getIndexSize.

/**
     * Returns the total size of the search index (in bytes).
     *
     * @return the total size of the search index (in bytes).
     */
public long getIndexSize() {
    File[] files = searchDir.listFiles(new FilenameFilter() {

        public boolean accept(File dir, String name) {
            // Ignore the index properties file since it's not part of the index.
            return !name.equals("indexprops.xml");
        }
    });
    if (files == null) {
        // Search folder does not exist so size of index is 0
        return 0;
    }
    long size = 0;
    for (File file : files) {
        size += file.length();
    }
    return size;
}
Also used : FilenameFilter(java.io.FilenameFilter) File(java.io.File)

Example 44 with FilenameFilter

use of java.io.FilenameFilter in project jna by java-native-access.

the class Native method removeTemporaryFiles.

/** Remove all marked temporary files in the given directory. */
static void removeTemporaryFiles() throws IOException {
    File dir = getTempDir();
    FilenameFilter filter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".x") && name.startsWith(JNA_TMPLIB_PREFIX);
        }
    };
    File[] files = dir.listFiles(filter);
    for (int i = 0; files != null && i < files.length; i++) {
        File marker = files[i];
        String name = marker.getName();
        name = name.substring(0, name.length() - 2);
        File target = new File(marker.getParentFile(), name);
        if (!target.exists() || target.delete()) {
            marker.delete();
        }
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) File(java.io.File)

Example 45 with FilenameFilter

use of java.io.FilenameFilter in project Openfire by igniterealtime.

the class AuditorImpl method createAuditFile.

/* if this new logic still causes problems one may want to 
	* use log4j or change the file format from YYYYmmdd-nnn to YYYYmmdd-HHMM */
/**
	* Sets <b>xmlWriter</b> so this class can use it to write audit logs<br>
	* The audit filename <b>currentAuditFile</b> will be `jive.audit-YYYYmmdd-nnn.log´<br>
	* `nnn´ will be reset to `000´ when a new log file is created the next day <br>
	* `nnn´ will be increased for log files which belong to the same day<br>
	* <b>WARNING:</b> If log files of the current day are deleted and the server is restarted then
	* the value of `nnn´ may be random (it's calculated by `Math.max(files.length, filesIndex);´
	* with `filesIndex=0´ and  `files.length=nr(existing jive.audit-YYYYmmdd-???.log files)´ - 
	* if there are 10 audit files (033-043) then nnn will be 10 instead of 44).<br>
	* If  `nnn=999´ then all audit data will be written to this file till the next day.<br>
	* @param auditDate
	* @throws IOException
	*/
private void createAuditFile(Date auditDate) throws IOException {
    final String filePrefix = "jive.audit-" + dateFormat.format(auditDate) + "-";
    if (currentDateLimit == null || auditDate.after(currentDateLimit)) {
        // Set limit date after which we need to rollover the audit file (based on the date)
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(auditDate);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        currentDateLimit = calendar.getTime();
        filesIndex = 0;
    }
    // Get list of existing audit files
    FilenameFilter filter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith(filePrefix) && name.endsWith(".log");
        }
    };
    File[] files = baseFolder.listFiles(filter);
    // if some daily files were already deleted then files.length will be smaller than filesIndex
    // see also WARNING above
    filesIndex = Math.max(files.length, filesIndex);
    if (filesIndex >= maxTotalFilesDay) {
        // don't close this file, continue auditing to it
        return;
    }
    File tmpAuditFile = new File(logDir, filePrefix + StringUtils.zeroPadString(Integer.toString(filesIndex), 3) + ".log");
    if ((filesIndex == maxTotalFilesDay - 1) && !tmpAuditFile.exists()) {
        Log.warn("Creating last audit file for this date: " + dateFormat.format(auditDate));
    }
    while ((filesIndex < (maxTotalFilesDay - 1)) && (tmpAuditFile.exists())) {
        Log.debug("Audit file '" + tmpAuditFile.getName() + "' does already exist.");
        filesIndex++;
        tmpAuditFile = new File(logDir, filePrefix + StringUtils.zeroPadString(Integer.toString(filesIndex), 3) + ".log");
    }
    currentAuditFile = tmpAuditFile;
    close();
    // always append to an existing file (after restart)
    writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(currentAuditFile, true), StandardCharsets.UTF_8));
    writer.write("<jive xmlns=\"http://www.jivesoftware.org\">");
    xmlWriter = new org.jivesoftware.util.XMLWriter(writer);
}
Also used : FilenameFilter(java.io.FilenameFilter) Calendar(java.util.Calendar) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Aggregations

FilenameFilter (java.io.FilenameFilter)402 File (java.io.File)398 IOException (java.io.IOException)100 ArrayList (java.util.ArrayList)72 Test (org.junit.Test)60 URL (java.net.URL)22 List (java.util.List)22 RandomAccessFile (java.io.RandomAccessFile)19 HashSet (java.util.HashSet)17 FileOutputStream (java.io.FileOutputStream)14 JarFile (java.util.jar.JarFile)14 FileNotFoundException (java.io.FileNotFoundException)13 MalformedURLException (java.net.MalformedURLException)13 HashMap (java.util.HashMap)13 FileFilter (java.io.FileFilter)11 FileWriter (java.io.FileWriter)11 ZipFile (java.util.zip.ZipFile)11 TestClient (org.syncany.tests.util.TestClient)11 FileReader (java.io.FileReader)10 Pattern (java.util.regex.Pattern)10