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