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     */
017    package org.apache.activemq.store.kahadb.disk.journal;
018    
019    import java.io.File;
020    import java.io.IOException;
021    import java.io.RandomAccessFile;
022    
023    import org.apache.activemq.store.kahadb.disk.util.LinkedNode;
024    import org.apache.activemq.store.kahadb.disk.util.SequenceSet;
025    import org.apache.activemq.util.IOHelper;
026    
027    /**
028     * DataFile
029     *
030     *
031     */
032    public class DataFile extends LinkedNode<DataFile> implements Comparable<DataFile> {
033    
034        protected final File file;
035        protected final Integer dataFileId;
036        protected volatile int length;
037        protected final SequenceSet corruptedBlocks = new SequenceSet();
038    
039        DataFile(File file, int number, int preferedSize) {
040            this.file = file;
041            this.dataFileId = Integer.valueOf(number);
042            length = (int)(file.exists() ? file.length() : 0);
043        }
044    
045        public File getFile() {
046            return file;
047        }
048    
049        public Integer getDataFileId() {
050            return dataFileId;
051        }
052    
053        public synchronized int getLength() {
054            return length;
055        }
056    
057        public void setLength(int length) {
058            this.length = length;
059        }
060    
061        public synchronized void incrementLength(int size) {
062            length += size;
063        }
064    
065        @Override
066            public synchronized String toString() {
067            return file.getName() + " number = " + dataFileId + " , length = " + length;
068        }
069    
070        public synchronized RandomAccessFile openRandomAccessFile() throws IOException {
071            return new RandomAccessFile(file.getCanonicalPath(), "rw");
072        }
073    
074        public synchronized void closeRandomAccessFile(RandomAccessFile file) throws IOException {
075            file.close();
076        }
077    
078        public synchronized boolean delete() throws IOException {
079            return file.delete();
080        }
081    
082        public synchronized void move(File targetDirectory) throws IOException{
083            IOHelper.moveFile(file,targetDirectory);
084        }
085    
086        public SequenceSet getCorruptedBlocks() {
087            return corruptedBlocks;
088        }
089    
090        @Override
091            public int compareTo(DataFile df) {
092            return dataFileId - df.dataFileId;
093        }
094    
095        @Override
096        public boolean equals(Object o) {
097            boolean result = false;
098            if (o instanceof DataFile) {
099                result = compareTo((DataFile)o) == 0;
100            }
101            return result;
102        }
103    
104        @Override
105        public int hashCode() {
106            return dataFileId;
107        }
108    }