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