001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.store.kahadb.disk.journal;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.RandomAccessFile;
022
023import org.apache.activemq.store.kahadb.disk.util.LinkedNode;
024import org.apache.activemq.store.kahadb.disk.util.SequenceSet;
025import org.apache.activemq.util.IOHelper;
026import org.apache.activemq.util.RecoverableRandomAccessFile;
027
028/**
029 * DataFile
030 *
031 *
032 */
033public class DataFile extends LinkedNode<DataFile> implements Comparable<DataFile> {
034
035    protected final File file;
036    protected final Integer dataFileId;
037    protected volatile int length;
038    protected final SequenceSet corruptedBlocks = new SequenceSet();
039
040    DataFile(File file, int number, int preferedSize) {
041        this.file = file;
042        this.dataFileId = Integer.valueOf(number);
043        length = (int)(file.exists() ? file.length() : 0);
044    }
045
046    public File getFile() {
047        return file;
048    }
049
050    public Integer getDataFileId() {
051        return dataFileId;
052    }
053
054    public synchronized int getLength() {
055        return length;
056    }
057
058    public void setLength(int length) {
059        this.length = length;
060    }
061
062    public synchronized void incrementLength(int size) {
063        length += size;
064    }
065
066    @Override
067        public synchronized String toString() {
068        return file.getName() + " number = " + dataFileId + " , length = " + length;
069    }
070
071    public synchronized RecoverableRandomAccessFile openRandomAccessFile() throws IOException {
072        return new RecoverableRandomAccessFile(file.getCanonicalPath(), "rw");
073    }
074
075    public synchronized void closeRandomAccessFile(RecoverableRandomAccessFile file) throws IOException {
076        file.close();
077    }
078
079    public synchronized boolean delete() throws IOException {
080        return file.delete();
081    }
082
083    public synchronized void move(File targetDirectory) throws IOException{
084        IOHelper.moveFile(file, targetDirectory);
085    }
086
087    public SequenceSet getCorruptedBlocks() {
088        return corruptedBlocks;
089    }
090
091    @Override
092        public int compareTo(DataFile df) {
093        return dataFileId - df.dataFileId;
094    }
095
096    @Override
097    public boolean equals(Object o) {
098        boolean result = false;
099        if (o instanceof DataFile) {
100            result = compareTo((DataFile)o) == 0;
101        }
102        return result;
103    }
104
105    @Override
106    public int hashCode() {
107        return dataFileId;
108    }
109}