use of java.util.Stack in project storm by apache.
the class DirectoryCleaner method deleteOldestWhileTooLarge.
/**
* If totalSize of files exceeds the either the per-worker quota or global quota,
* Logviewer deletes oldest inactive log files in a worker directory or in all worker dirs.
* We use the parameter for_per_dir to switch between the two deletion modes.
* @param dirs the list of directories to be scanned for deletion
* @param quota the per-dir quota or the total quota for the all directories
* @param for_per_dir if true, deletion happens for a single dir; otherwise, for all directories globally
* @param active_dirs only for global deletion, we want to skip the active logs in active_dirs
* @return number of files deleted
*/
public int deleteOldestWhileTooLarge(List<File> dirs, long quota, boolean for_per_dir, Set<String> active_dirs) throws IOException {
// max number of files to delete for every round
final int PQ_SIZE = 1024;
// max rounds of scanning the dirs
final int MAX_ROUNDS = 512;
long totalSize = 0;
int deletedFiles = 0;
for (File dir : dirs) {
try (DirectoryStream<Path> stream = getStreamForDirectory(dir)) {
for (Path path : stream) {
File file = path.toFile();
totalSize += file.length();
}
}
}
long toDeleteSize = totalSize - quota;
if (toDeleteSize <= 0) {
return deletedFiles;
}
Comparator<File> comparator = new Comparator<File>() {
public int compare(File f1, File f2) {
if (f1.lastModified() > f2.lastModified()) {
return -1;
} else {
return 1;
}
}
};
// the oldest pq_size files in this directory will be placed in PQ, with the newest at the root
PriorityQueue<File> pq = new PriorityQueue<File>(PQ_SIZE, comparator);
int round = 0;
while (toDeleteSize > 0) {
LOG.debug("To delete size is {}, start a new round of deletion, round: {}", toDeleteSize, round);
for (File dir : dirs) {
try (DirectoryStream<Path> stream = getStreamForDirectory(dir)) {
for (Path path : stream) {
File file = path.toFile();
if (for_per_dir) {
if (ACTIVE_LOG_PATTERN.matcher(file.getName()).matches()) {
// skip active log files
continue;
}
} else {
// for global cleanup
if (active_dirs.contains(dir.getCanonicalPath())) {
// for an active worker's dir, make sure for the last "/"
if (ACTIVE_LOG_PATTERN.matcher(file.getName()).matches()) {
// skip active log files
continue;
}
} else {
if (META_LOG_PATTERN.matcher(file.getName()).matches()) {
// skip yaml and pid files
continue;
}
}
}
if (pq.size() < PQ_SIZE) {
pq.offer(file);
} else {
if (file.lastModified() < pq.peek().lastModified()) {
pq.poll();
pq.offer(file);
}
}
}
}
}
// need to reverse the order of elements in PQ to delete files from oldest to newest
Stack<File> stack = new Stack<File>();
while (!pq.isEmpty()) {
File file = pq.poll();
stack.push(file);
}
while (!stack.isEmpty() && toDeleteSize > 0) {
File file = stack.pop();
toDeleteSize -= file.length();
LOG.info("Delete file: {}, size: {}, lastModified: {}", file.getCanonicalPath(), file.length(), file.lastModified());
file.delete();
deletedFiles++;
}
pq.clear();
round++;
if (round >= MAX_ROUNDS) {
if (for_per_dir) {
LOG.warn("Reach the MAX_ROUNDS: {} during per-dir deletion, you may have too many files in " + "a single directory : {}, will delete the rest files in next interval.", MAX_ROUNDS, dirs.get(0).getCanonicalPath());
} else {
LOG.warn("Reach the MAX_ROUNDS: {} during global deletion, you may have too many files, " + "will delete the rest files in next interval.", MAX_ROUNDS);
}
break;
}
}
return deletedFiles;
}
use of java.util.Stack in project hive by apache.
the class PTFOperator method setupChain.
private PTFInvocation setupChain() {
Stack<PartitionedTableFunctionDef> fnDefs = new Stack<PartitionedTableFunctionDef>();
PTFInputDef iDef = conf.getFuncDef();
while (iDef instanceof PartitionedTableFunctionDef) {
fnDefs.push((PartitionedTableFunctionDef) iDef);
iDef = ((PartitionedTableFunctionDef) iDef).getInput();
}
PTFInvocation curr = null, first = null;
while (!fnDefs.isEmpty()) {
PartitionedTableFunctionDef currFn = fnDefs.pop();
curr = new PTFInvocation(curr, currFn.getTFunction());
if (first == null) {
first = curr;
}
}
return first;
}
use of java.util.Stack in project hive by apache.
the class ParseUtils method sameTree.
public static boolean sameTree(ASTNode node, ASTNode otherNode) {
if (node == null && otherNode == null) {
return true;
}
if ((node == null && otherNode != null) || (node != null && otherNode == null)) {
return false;
}
Stack<Tree> stack = new Stack<Tree>();
stack.push(node);
Stack<Tree> otherStack = new Stack<Tree>();
otherStack.push(otherNode);
while (!stack.empty() && !otherStack.empty()) {
Tree p = stack.pop();
Tree otherP = otherStack.pop();
if (p.isNil() != otherP.isNil()) {
return false;
}
if (!p.isNil()) {
if (!p.toString().equals(otherP.toString())) {
return false;
}
}
if (p.getChildCount() != otherP.getChildCount()) {
return false;
}
for (int i = p.getChildCount() - 1; i >= 0; i--) {
Tree t = p.getChild(i);
stack.push(t);
Tree otherT = otherP.getChild(i);
otherStack.push(otherT);
}
}
return stack.empty() && otherStack.empty();
}
use of java.util.Stack in project tomcat by apache.
the class JspC method scanFiles.
/**
* Locate all jsp files in the webapp. Used if no explicit
* jsps are specified.
* @param base Base path
*/
public void scanFiles(File base) {
Stack<String> dirs = new Stack<>();
dirs.push(base.toString());
// Make sure default extensions are always included
if ((getExtensions() == null) || (getExtensions().size() < 2)) {
addExtension("jsp");
addExtension("jspx");
}
while (!dirs.isEmpty()) {
String s = dirs.pop();
File f = new File(s);
if (f.exists() && f.isDirectory()) {
String[] files = f.list();
String ext;
for (int i = 0; (files != null) && i < files.length; i++) {
File f2 = new File(s, files[i]);
if (f2.isDirectory()) {
dirs.push(f2.getPath());
} else {
String path = f2.getPath();
String uri = path.substring(uriRoot.length());
ext = files[i].substring(files[i].lastIndexOf('.') + 1);
if (getExtensions().contains(ext) || jspConfig.isJspPage(uri)) {
pages.add(path);
}
}
}
}
}
}
use of java.util.Stack in project CtCI-6th-Edition by careercup.
the class QuestionB method isPalindrome.
public static boolean isPalindrome(LinkedListNode head) {
LinkedListNode fast = head;
LinkedListNode slow = head;
Stack<Integer> stack = new Stack<Integer>();
while (fast != null && fast.next != null) {
stack.push(slow.data);
slow = slow.next;
fast = fast.next.next;
}
/* Has odd number of elements, so skip the middle */
if (fast != null) {
slow = slow.next;
}
while (slow != null) {
int top = stack.pop().intValue();
if (top != slow.data) {
return false;
}
slow = slow.next;
}
return true;
}
Aggregations