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.DataInput; 020import java.io.DataOutput; 021import java.io.IOException; 022import java.util.concurrent.CountDownLatch; 023import java.util.concurrent.atomic.AtomicReference; 024 025/** 026 * Used as a location in the data store. 027 * 028 * 029 */ 030public final class Location implements Comparable<Location> { 031 032 public static final byte USER_TYPE = 1; 033 public static final byte NOT_SET_TYPE = 0; 034 public static final int NOT_SET = -1; 035 036 private int dataFileId = NOT_SET; 037 private int offset = NOT_SET; 038 private int size = NOT_SET; 039 private byte type = NOT_SET_TYPE; 040 private CountDownLatch latch; 041 private AtomicReference<IOException> exception; 042 043 public Location() { 044 } 045 046 public Location(Location item) { 047 this.dataFileId = item.dataFileId; 048 this.offset = item.offset; 049 this.size = item.size; 050 this.type = item.type; 051 } 052 053 public Location(int dataFileId, int offset) { 054 this.dataFileId=dataFileId; 055 this.offset=offset; 056 } 057 058 boolean isValid() { 059 return dataFileId != NOT_SET; 060 } 061 062 /** 063 * @return the size of the data record including the header. 064 */ 065 public int getSize() { 066 return size; 067 } 068 069 /** 070 * @param size the size of the data record including the header. 071 */ 072 public void setSize(int size) { 073 this.size = size; 074 } 075 076 public int getOffset() { 077 return offset; 078 } 079 080 public void setOffset(int offset) { 081 this.offset = offset; 082 } 083 084 public int getDataFileId() { 085 return dataFileId; 086 } 087 088 public void setDataFileId(int file) { 089 this.dataFileId = file; 090 } 091 092 public byte getType() { 093 return type; 094 } 095 096 public void setType(byte type) { 097 this.type = type; 098 } 099 100 public String toString() { 101 return dataFileId+":"+offset; 102 } 103 104 public void writeExternal(DataOutput dos) throws IOException { 105 dos.writeInt(dataFileId); 106 dos.writeInt(offset); 107 dos.writeInt(size); 108 dos.writeByte(type); 109 } 110 111 public void readExternal(DataInput dis) throws IOException { 112 dataFileId = dis.readInt(); 113 offset = dis.readInt(); 114 size = dis.readInt(); 115 type = dis.readByte(); 116 } 117 118 public CountDownLatch getLatch() { 119 return latch; 120 } 121 122 public void setBatch(DataFileAppender.WriteBatch batch) { 123 this.latch = batch.latch; 124 this.exception = batch.exception; 125 } 126 127 public int compareTo(Location o) { 128 Location l = (Location)o; 129 if (dataFileId == l.dataFileId) { 130 int rc = offset - l.offset; 131 return rc; 132 } 133 return dataFileId - l.dataFileId; 134 } 135 136 public boolean equals(Object o) { 137 boolean result = false; 138 if (o instanceof Location) { 139 result = compareTo((Location)o) == 0; 140 } 141 return result; 142 } 143 144 public int hashCode() { 145 return dataFileId ^ offset; 146 } 147 148 public AtomicReference<IOException> getException() { 149 return exception; 150 } 151}