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