Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » kaha » impl » async » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.kaha.impl.async;
   18   
   19   import java.io.File;
   20   import java.io.IOException;
   21   import java.io.RandomAccessFile;
   22   
   23   import org.apache.activemq.util.IOHelper;
   24   import org.apache.activemq.util.LinkedNode;
   25   
   26   /**
   27    * DataFile
   28    * 
   29    * @version $Revision: 1.1.1.1 $
   30    */
   31   public class DataFile extends LinkedNode implements Comparable<DataFile> {
   32   
   33       protected final File file;
   34       protected final Integer dataFileId;
   35       protected final int preferedSize;
   36   
   37       protected int length;
   38       protected int referenceCount;
   39   
   40       DataFile(File file, int number, int preferedSize) {
   41           this.file = file;
   42           this.preferedSize = preferedSize;
   43           this.dataFileId = Integer.valueOf(number);
   44           length = (int)(file.exists() ? file.length() : 0);
   45       }
   46       
   47       File getFile() {
   48           return file;
   49       }
   50   
   51       public Integer getDataFileId() {
   52           return dataFileId;
   53       }
   54   
   55       public synchronized int getLength() {
   56           return length;
   57       }
   58   
   59       public void setLength(int length) {
   60           this.length = length;
   61       }
   62   
   63       public synchronized void incrementLength(int size) {
   64           length += size;
   65       }
   66   
   67       public synchronized int increment() {
   68           return ++referenceCount;
   69       }
   70   
   71       public synchronized int decrement() {
   72           return --referenceCount;
   73       }
   74       
   75       public synchronized int getReferenceCount(){
   76       	return referenceCount;
   77       }
   78   
   79       public synchronized boolean isUnused() {
   80           return referenceCount <= 0;
   81       }
   82   
   83       public synchronized String toString() {
   84           String result = file.getName() + " number = " + dataFileId + " , length = " + length + " refCount = " + referenceCount;
   85           return result;
   86       }
   87   
   88       public synchronized RandomAccessFile openRandomAccessFile(boolean appender) throws IOException {
   89           RandomAccessFile rc = new RandomAccessFile(file, "rw");
   90           // When we start to write files size them up so that the OS has a chance
   91           // to allocate the file contiguously.
   92           if (appender) {
   93               if (length < preferedSize) {
   94                   try {
   95                       // this can throw if we run out of disk space
   96                       rc.setLength(preferedSize);
   97                   } catch (IOException ioe) {            
   98                       try {
   99                           rc.close();
  100                       } catch(Exception ignored) {
  101                       }
  102                       throw ioe;
  103                   }
  104               }
  105           }
  106           return rc;
  107       }
  108   
  109       public synchronized void closeRandomAccessFile(RandomAccessFile file) throws IOException {
  110           // On close set the file size to the real size.
  111           if (length != file.length()) {
  112               file.setLength(getLength());
  113           }
  114           file.close();
  115       }
  116   
  117       public synchronized boolean delete() throws IOException {
  118           return file.delete();
  119       }
  120       
  121       public synchronized void move(File targetDirectory) throws IOException{
  122           IOHelper.moveFile(file,targetDirectory);
  123       }
  124   
  125       public int compareTo(DataFile df) {
  126           return dataFileId - df.dataFileId;
  127       }
  128   
  129       @Override
  130       public boolean equals(Object o) {
  131           boolean result = false;
  132           if (o instanceof DataFile) {
  133               result = compareTo((DataFile)o) == 0;
  134           }
  135           return result;
  136       }
  137   
  138       @Override
  139       public int hashCode() {
  140           return dataFileId;
  141       }
  142   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » kaha » impl » async » [javadoc | source]