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