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.kahadb.journal;
018
019 import java.io.File;
020 import java.io.IOException;
021 import java.io.RandomAccessFile;
022
023 import org.apache.kahadb.util.IOHelper;
024 import org.apache.kahadb.util.LinkedNode;
025 import org.apache.kahadb.util.SequenceSet;
026
027 /**
028 * DataFile
029 *
030 * @version $Revision: 805550 $
031 */
032 public class DataFile extends LinkedNode<DataFile> implements Comparable<DataFile> {
033
034 protected final File file;
035 protected final Integer dataFileId;
036 protected 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 public synchronized String toString() {
066 return file.getName() + " number = " + dataFileId + " , length = " + length;
067 }
068
069 public synchronized RandomAccessFile openRandomAccessFile() throws IOException {
070 return new RandomAccessFile(file, "rw");
071 }
072
073 public synchronized void closeRandomAccessFile(RandomAccessFile file) throws IOException {
074 file.close();
075 }
076
077 public synchronized boolean delete() throws IOException {
078 return file.delete();
079 }
080
081 public synchronized void move(File targetDirectory) throws IOException{
082 IOHelper.moveFile(file,targetDirectory);
083 }
084
085 public SequenceSet getCorruptedBlocks() {
086 return corruptedBlocks;
087 }
088
089 public int compareTo(DataFile df) {
090 return dataFileId - df.dataFileId;
091 }
092
093 @Override
094 public boolean equals(Object o) {
095 boolean result = false;
096 if (o instanceof DataFile) {
097 result = compareTo((DataFile)o) == 0;
098 }
099 return result;
100 }
101
102 @Override
103 public int hashCode() {
104 return dataFileId;
105 }
106 }