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 public synchronized void decrementLength(int size) { 073 length -= size; 074 } 075 076 @Override 077 public synchronized String toString() { 078 return file.getName() + " number = " + dataFileId + " , length = " + length; 079 } 080 081 public synchronized RecoverableRandomAccessFile openRandomAccessFile() throws IOException { 082 return new RecoverableRandomAccessFile(file.getCanonicalPath(), "rw"); 083 } 084 085 public synchronized void closeRandomAccessFile(RecoverableRandomAccessFile file) throws IOException { 086 file.close(); 087 } 088 089 public synchronized boolean delete() throws IOException { 090 return file.delete(); 091 } 092 093 public synchronized void move(File targetDirectory) throws IOException{ 094 IOHelper.moveFile(file, targetDirectory); 095 } 096 097 public SequenceSet getCorruptedBlocks() { 098 return corruptedBlocks; 099 } 100 101 @Override 102 public int compareTo(DataFile df) { 103 return dataFileId - df.dataFileId; 104 } 105 106 @Override 107 public boolean equals(Object o) { 108 boolean result = false; 109 if (o instanceof DataFile) { 110 result = compareTo((DataFile)o) == 0; 111 } 112 return result; 113 } 114 115 @Override 116 public int hashCode() { 117 return dataFileId; 118 } 119 120}