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 protected RecoverableRandomAccessFile appendRandomAccessFile; 040 041 DataFile(File file, int number) { 042 this.file = file; 043 this.dataFileId = Integer.valueOf(number); 044 length = (int)(file.exists() ? file.length() : 0); 045 } 046 047 public File getFile() { 048 return file; 049 } 050 051 public Integer getDataFileId() { 052 return dataFileId; 053 } 054 055 public int getTypeCode() { 056 return typeCode; 057 } 058 059 public void setTypeCode(int typeCode) { 060 this.typeCode = typeCode; 061 } 062 063 public synchronized int getLength() { 064 return length; 065 } 066 067 public void setLength(int length) { 068 this.length = length; 069 } 070 071 public synchronized void incrementLength(int size) { 072 length += size; 073 } 074 075 @Override 076 public synchronized String toString() { 077 return file.getName() + " number = " + dataFileId + " , length = " + length; 078 } 079 080 public synchronized RecoverableRandomAccessFile appendRandomAccessFile() throws IOException { 081 if (appendRandomAccessFile == null) { 082 appendRandomAccessFile = new RecoverableRandomAccessFile(file.getCanonicalPath(), "rw"); 083 } 084 return appendRandomAccessFile; 085 } 086 087 public synchronized RecoverableRandomAccessFile openRandomAccessFile() throws IOException { 088 return new RecoverableRandomAccessFile(file.getCanonicalPath(), "rw"); 089 } 090 091 public synchronized void closeRandomAccessFile(RecoverableRandomAccessFile file) throws IOException { 092 file.close(); 093 if (file == appendRandomAccessFile) { 094 appendRandomAccessFile = null; 095 } 096 } 097 098 public synchronized boolean delete() throws IOException { 099 return file.delete(); 100 } 101 102 public synchronized void move(File targetDirectory) throws IOException{ 103 IOHelper.moveFile(file, targetDirectory); 104 } 105 106 public SequenceSet getCorruptedBlocks() { 107 return corruptedBlocks; 108 } 109 110 @Override 111 public int compareTo(DataFile df) { 112 return dataFileId - df.dataFileId; 113 } 114 115 @Override 116 public boolean equals(Object o) { 117 boolean result = false; 118 if (o instanceof DataFile) { 119 result = compareTo((DataFile)o) == 0; 120 } 121 return result; 122 } 123 124 @Override 125 public int hashCode() { 126 return dataFileId; 127 } 128}