All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.code.fqueue.log.FileRunner Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
/*
 *  Copyright 2011 sunli [[email protected]][weibo.com@sunli1223]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.google.code.fqueue.log;

import me.aifaq.commons.lang.SunUtil;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author sunli
 * @date 2011-5-18
 * @version $Id: FileRunner.java 74 2012-03-21 13:51:26Z [email protected] $
 */
public class FileRunner implements Runnable {
	private static final Logger logger = Logger.getLogger(FileRunner.class.getName());
    // 删除队列
    private static final Queue deleteQueue = new ConcurrentLinkedQueue();
    // 新创建队列
    private static final Queue createQueue = new ConcurrentLinkedQueue();

    private final String baseDir;
    private final long fileLimitLength;

    public static void addDeleteFile(String path) {
        deleteQueue.add(path);
    }

    public static void addCreateFile(String path) {
        createQueue.add(path);
    }

    public FileRunner(String baseDir, long fileLimitLength) {
        this.baseDir = baseDir;
        this.fileLimitLength = fileLimitLength;
    }

    @Override
    public void run() {
        String filePath, fileNum;
        while (true) {
            filePath = deleteQueue.poll();
            fileNum = createQueue.poll();
            if (filePath == null && fileNum == null) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                	logger.log(Level.WARNING, e.getMessage(), e);
                }
                continue;
            }
            if (filePath != null) {
                File delFile = new File(filePath);
                // 删除失败
                if (!delFile.delete()) {
                	logger.log(Level.WARNING, "failed to delete {}", filePath);
                }
                // 删除成功
                else if (logger.isLoggable(Level.INFO)) {
                	logger.info("success to delete " + filePath);
                }
            }

            if (fileNum != null) {
                filePath = baseDir + fileNum + ".idb";
                try {
                    create(filePath);
                } catch (IOException e) {
					logger.log(Level.SEVERE, "预创建数据文件失败:" + filePath, e);
                }
            }
        }

    }
    
    private boolean create(String path) throws IOException {
    	return create(path, this.fileLimitLength);
    }

    public static boolean create(String path, long fileLimitLength) throws IOException {
        File file = new File(path);
        if (file.exists() == false) {
            if (file.createNewFile() == false) {
                return false;
            }
            RandomAccessFile raFile = new RandomAccessFile(file, "rwd");
            FileChannel fc = raFile.getChannel();
            try {
	            MappedByteBuffer mappedByteBuffer = fc.map(MapMode.READ_WRITE, 0, fileLimitLength);
	            mappedByteBuffer.put(LogEntity.MAGIC.getBytes());
	            mappedByteBuffer.putInt(1);// 8 version
	            mappedByteBuffer.putInt(-1);// 12next fileindex
	            mappedByteBuffer.putInt(-2);// 16
	            mappedByteBuffer.force();
				SunUtil.cleanPrivileged(mappedByteBuffer, "cleaner");
	            
	            if (logger.isLoggable(Level.INFO)) {
	            	logger.info("success to create " + file.getAbsolutePath());
	            }
            } catch(Exception e) {
            	throw new IOException("Error to create " + file.getAbsolutePath(), e);
            } finally {
            	fc.close();
            	raFile.close();
            }
            return true;
        } else {
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy