Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.qiniu.datasource.TextFileContainer Maven / Gradle / Ivy
Go to download
qiniu-suits is a efficient tools for qiniu api implemented by java8.
package com.qiniu.datasource;
import com.qiniu.convert.LineToMap;
import com.qiniu.convert.MapToString;
import com.qiniu.interfaces.ITextReader;
import com.qiniu.interfaces.ITypeConvert;
import com.qiniu.persistence.FileSaveMapper;
import com.qiniu.interfaces.IResultOutput;
import com.qiniu.util.FileUtils;
import java.io.*;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
public class TextFileContainer extends TextContainer> {
public TextFileContainer(String filePath, String parseFormat, String separator, Map> urisMap,
List antiPrefixes, String addKeyPrefix, String rmKeyPrefix, Map indexMap,
List fields, int unitLen, int threads) throws IOException {
super(filePath, parseFormat, separator, urisMap, antiPrefixes, addKeyPrefix, rmKeyPrefix, indexMap, fields, unitLen, threads);
}
@Override
protected ITypeConvert> getNewConverter() throws IOException {
return new LineToMap(parse, separator, addKeyPrefix, rmKeyPrefix, indexMap);
}
@Override
protected ITypeConvert, String> getNewStringConverter() throws IOException {
return new MapToString(saveFormat, saveSeparator, fields);
}
@Override
public String getSourceName() {
return "local";
}
@Override
protected IResultOutput getNewResultSaver(String order) throws IOException {
return order != null ? new FileSaveMapper(savePath, getSourceName(), order) : new FileSaveMapper(savePath);
}
@Override
protected ITextReader getReader(String name, String start, int unitLen) throws IOException {
File file = new File(name);
if (!file.exists()) file = new File(path, name);
if (file.isDirectory()) {
throw new IOException(name + " is a directory, but it should be a file.");
} else if (file.exists()) {
return new TextFileReader(file, start, unitLen);
} else {
throw new IOException(name + " is not exists.");
}
}
private Lock lock = new ReentrantLock();
@Override
protected List> getReaders(String path) throws IOException {
File file = new File(path);
List files = new ArrayList<>();
List directories = new ArrayList<>();
if (file.exists()) {
if (file.isDirectory()) {
directories.add(file);
while (directories.size() > 0) {
directories = directories.parallelStream().map(directory -> {
File[] listFiles = directory.listFiles();
if (listFiles == null) return null;
List fs = new ArrayList<>(listFiles.length);
List dirs = new ArrayList<>(listFiles.length);
for (File f : listFiles) {
if (f.isDirectory()) {
dirs.add(f);
} else {
String type = FileUtils.contentType(f);
if (type.startsWith("text") || type.equals("application/octet-stream")) {
fs.add(f);
}
}
}
while (!lock.tryLock());
files.addAll(fs);
lock.unlock();
return dirs;
}).filter(Objects::nonNull)
.reduce((list1, list2) -> { list1.addAll(list2); return list1; }).orElse(new ArrayList<>());
}
} else {
files.add(file);
}
} else {
throw new IOException("");
}
return files.parallelStream().map(pFile -> {
try {
return new TextFileReader(pFile, null, unitLen);
} catch (IOException e) {
e.printStackTrace();
errorLogger.error("generate lister failed by {}\t{}", pFile.getPath(), urisMap.get(pFile.getPath()), e);
return null;
}
}).filter(Objects::nonNull).peek(reader -> recordListerByUri(reader.getName())).collect(Collectors.toList());
}
}