KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > index > IndexItem


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.kaha.impl.index;
19
20 import java.io.DataInput JavaDoc;
21 import java.io.DataOutput JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import org.apache.activemq.kaha.StoreEntry;
25 import org.apache.activemq.kaha.StoreLocation;
26 import org.apache.activemq.kaha.impl.data.DataItem;
27 import org.apache.activemq.kaha.impl.data.Item;
28 /**
29  * A an Item with a relative position and location to other Items in the Store
30  *
31  * @version $Revision: 1.2 $
32  */

33  public class IndexItem implements Item, StoreEntry{
34     
35     public static final int INDEX_SIZE=51;
36     public static final int INDEXES_ONLY_SIZE=19;
37     //used by linked list
38
IndexItem next;
39     IndexItem prev;
40     
41     protected long offset=POSITION_NOT_SET;
42     private long previousItem=POSITION_NOT_SET;
43     private long nextItem=POSITION_NOT_SET;
44     private boolean active=true;
45     
46     // TODO: consider just using a DataItem for the following fields.
47
private long keyOffset=POSITION_NOT_SET;
48     private int keyFile=(int) POSITION_NOT_SET;
49     private int keySize=0;
50     
51     private long valueOffset=POSITION_NOT_SET;
52     private int valueFile=(int) POSITION_NOT_SET;
53     private int valueSize=0;
54     
55     /**
56      * Default Constructor
57      */

58     public IndexItem(){}
59
60     void reset(){
61         previousItem=POSITION_NOT_SET;
62         nextItem=POSITION_NOT_SET;
63         keyOffset=POSITION_NOT_SET;
64         keyFile=(int) POSITION_NOT_SET;
65         keySize=0;
66         valueOffset=POSITION_NOT_SET;
67         valueFile=(int) POSITION_NOT_SET;
68         valueSize=0;
69         active=true;
70     }
71
72     /**
73      * @return
74      * @see org.apache.activemq.kaha.StoreEntry#getKeyDataItem()
75      */

76     public StoreLocation getKeyDataItem(){
77         DataItem result=new DataItem();
78         result.setOffset(keyOffset);
79         result.setFile(keyFile);
80         result.setSize(keySize);
81         return result;
82     }
83
84     /**
85      * @return
86      * @see org.apache.activemq.kaha.StoreEntry#getValueDataItem()
87      */

88     public StoreLocation getValueDataItem(){
89         DataItem result=new DataItem();
90         result.setOffset(valueOffset);
91         result.setFile(valueFile);
92         result.setSize(valueSize);
93         return result;
94     }
95
96     public void setValueData(StoreLocation item){
97         valueOffset=item.getOffset();
98         valueFile=item.getFile();
99         valueSize=item.getSize();
100     }
101
102     public void setKeyData(StoreLocation item){
103         keyOffset=item.getOffset();
104         keyFile=item.getFile();
105         keySize=item.getSize();
106     }
107
108     /**
109      * @param dataOut
110      * @throws IOException
111      */

112     public void write(DataOutput JavaDoc dataOut) throws IOException JavaDoc{
113         dataOut.writeShort(MAGIC);
114         dataOut.writeBoolean(active);
115         dataOut.writeLong(previousItem);
116         dataOut.writeLong(nextItem);
117         dataOut.writeInt(keyFile);
118         dataOut.writeLong(keyOffset);
119         dataOut.writeInt(keySize);
120         dataOut.writeInt(valueFile);
121         dataOut.writeLong(valueOffset);
122         dataOut.writeInt(valueSize);
123     }
124     
125     void updateIndexes(DataOutput JavaDoc dataOut) throws IOException JavaDoc{
126         dataOut.writeShort(MAGIC);
127         dataOut.writeBoolean(active);
128         dataOut.writeLong(previousItem);
129         dataOut.writeLong(nextItem);
130     }
131
132     /**
133      * @param dataIn
134      * @throws IOException
135      */

136     public void read(DataInput JavaDoc dataIn) throws IOException JavaDoc{
137         if(dataIn.readShort()!=MAGIC){
138             throw new BadMagicException();
139         }
140         active=dataIn.readBoolean();
141         previousItem=dataIn.readLong();
142         nextItem=dataIn.readLong();
143         keyFile=dataIn.readInt();
144         keyOffset=dataIn.readLong();
145         keySize=dataIn.readInt();
146         valueFile=dataIn.readInt();
147         valueOffset=dataIn.readLong();
148         valueSize=dataIn.readInt();
149     }
150     
151     void readIndexes(DataInput JavaDoc dataIn) throws IOException JavaDoc{
152         if(dataIn.readShort()!=MAGIC){
153             throw new BadMagicException();
154         }
155         active=dataIn.readBoolean();
156         previousItem=dataIn.readLong();
157         nextItem=dataIn.readLong();
158     }
159
160     /**
161      * @param newPrevEntry
162      */

163     public void setPreviousItem(long newPrevEntry){
164         previousItem=newPrevEntry;
165     }
166
167     /**
168      * @return prev item
169      */

170     long getPreviousItem(){
171         return previousItem;
172     }
173
174     /**
175      * @param newNextEntry
176      */

177     public void setNextItem(long newNextEntry){
178         nextItem=newNextEntry;
179     }
180
181     /**
182      * @return
183      * @see org.apache.activemq.kaha.StoreEntry#getNextItem()
184      */

185     public long getNextItem(){
186         return nextItem;
187     }
188
189     /**
190      * @param newObjectOffset
191      */

192     void setKeyOffset(long newObjectOffset){
193         keyOffset=newObjectOffset;
194     }
195
196     /**
197      * @return key offset
198      */

199     long getKeyOffset(){
200         return keyOffset;
201     }
202
203     /**
204      * @return
205      * @see org.apache.activemq.kaha.StoreEntry#getKeyFile()
206      */

207     public int getKeyFile(){
208         return keyFile;
209     }
210
211     /**
212      * @param keyFile The keyFile to set.
213      */

214     void setKeyFile(int keyFile){
215         this.keyFile=keyFile;
216     }
217
218     /**
219      * @return
220      * @see org.apache.activemq.kaha.StoreEntry#getValueFile()
221      */

222     public int getValueFile(){
223         return valueFile;
224     }
225
226     /**
227      * @param valueFile The valueFile to set.
228      */

229     void setValueFile(int valueFile){
230         this.valueFile=valueFile;
231     }
232
233     /**
234      * @return
235      * @see org.apache.activemq.kaha.StoreEntry#getValueOffset()
236      */

237     public long getValueOffset(){
238         return valueOffset;
239     }
240
241     /**
242      * @param valueOffset The valueOffset to set.
243      */

244     public void setValueOffset(long valueOffset){
245         this.valueOffset=valueOffset;
246     }
247
248     /**
249      * @return Returns the active.
250      */

251     boolean isActive(){
252         return active;
253     }
254
255     /**
256      * @param active The active to set.
257      */

258     void setActive(boolean active){
259         this.active=active;
260     }
261
262     /**
263      * @return
264      * @see org.apache.activemq.kaha.StoreEntry#getOffset()
265      */

266     public long getOffset(){
267         return offset;
268     }
269
270     /**
271      * @param offset The offset to set.
272      */

273     public void setOffset(long offset){
274         this.offset=offset;
275     }
276
277     /**
278      * @return
279      * @see org.apache.activemq.kaha.StoreEntry#getKeySize()
280      */

281     public int getKeySize() {
282         return keySize;
283     }
284
285     public void setKeySize(int keySize) {
286         this.keySize = keySize;
287     }
288
289     /**
290      * @return
291      * @see org.apache.activemq.kaha.StoreEntry#getValueSize()
292      */

293     public int getValueSize() {
294         return valueSize;
295     }
296
297     public void setValueSize(int valueSize) {
298         this.valueSize = valueSize;
299     }
300
301     /**
302      * @return print of 'this'
303      */

304     public String JavaDoc toString(){
305         String JavaDoc result="offset="+offset+
306         ", key=("+keyFile+", "+keyOffset+", "+keySize+")"+
307         ", value=("+valueFile+", "+valueOffset+", "+valueSize+")"+
308         ", previousItem="+previousItem+", nextItem="+nextItem
309         ;
310         return result;
311     }
312     
313     public boolean equals(Object JavaDoc obj){
314         boolean result = obj == this;
315         if (!result && obj != null && obj instanceof IndexItem){
316             IndexItem other = (IndexItem)obj;
317             result = other.offset == this.offset;
318         }
319         return result;
320     }
321     
322     public int hashCode(){
323         return (int)offset;
324     }
325 }
326
Popular Tags