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;
021
022import org.apache.activemq.store.kahadb.disk.util.LinkedNode;
023import org.apache.activemq.store.kahadb.disk.util.SequenceSet;
024import org.apache.activemq.util.IOHelper;
025import org.apache.activemq.util.RecoverableRandomAccessFile;
026
027/**
028 * DataFile
029 */
030public class DataFile extends LinkedNode<DataFile> implements Comparable<DataFile> {
031
032    public final static byte STANDARD_LOG_FILE = 0x0;
033
034    protected final File file;
035    protected final Integer dataFileId;
036    protected volatile int length;
037    protected int typeCode = STANDARD_LOG_FILE;
038    protected final SequenceSet corruptedBlocks = new SequenceSet();
039
040    DataFile(File file, int number) {
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 int getTypeCode() {
055        return typeCode;
056    }
057
058    public void setTypeCode(int typeCode) {
059        this.typeCode = typeCode;
060    }
061
062    public synchronized int getLength() {
063        return length;
064    }
065
066    public void setLength(int length) {
067        this.length = length;
068    }
069
070    public synchronized void incrementLength(int size) {
071        length += size;
072    }
073
074    @Override
075    public synchronized String toString() {
076        return file.getName() + " number = " + dataFileId + " , length = " + length;
077    }
078
079    public synchronized RecoverableRandomAccessFile openRandomAccessFile() throws IOException {
080        return new RecoverableRandomAccessFile(file.getCanonicalPath(), "rw");
081    }
082
083    public synchronized void closeRandomAccessFile(RecoverableRandomAccessFile file) throws IOException {
084        file.close();
085    }
086
087    public synchronized boolean delete() throws IOException {
088        return file.delete();
089    }
090
091    public synchronized void move(File targetDirectory) throws IOException{
092        IOHelper.moveFile(file, targetDirectory);
093    }
094
095    public SequenceSet getCorruptedBlocks() {
096        return corruptedBlocks;
097    }
098
099    @Override
100    public int compareTo(DataFile df) {
101        return dataFileId - df.dataFileId;
102    }
103
104    @Override
105    public boolean equals(Object o) {
106        boolean result = false;
107        if (o instanceof DataFile) {
108            result = compareTo((DataFile)o) == 0;
109        }
110        return result;
111    }
112
113    @Override
114    public int hashCode() {
115        return dataFileId;
116    }
117}