KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21 import java.io.RandomAccessFile JavaDoc;
22
23 import org.apache.activemq.kaha.impl.DataManager;
24 import org.apache.activemq.util.DataByteArrayOutputStream;
25 /**
26  * Optimized Store writer
27  *
28  * @version $Revision: 1.1.1.1 $
29  */

30 class StoreIndexWriter{
31     
32     protected final DataByteArrayOutputStream dataOut = new DataByteArrayOutputStream();
33     protected final RandomAccessFile JavaDoc file;
34     protected final String JavaDoc name;
35     protected final DataManager redoLog;
36
37     /**
38      * Construct a Store index writer
39      *
40      * @param file
41      */

42     StoreIndexWriter(RandomAccessFile JavaDoc file){
43         this(file, null, null);
44     }
45
46     public StoreIndexWriter(RandomAccessFile JavaDoc file, String JavaDoc indexName, DataManager redoLog) {
47         this.file=file;
48         this.name = indexName;
49         this.redoLog = redoLog;
50     }
51
52     void storeItem(IndexItem indexItem) throws IOException JavaDoc{
53         
54         if( redoLog!=null ) {
55             RedoStoreIndexItem redo = new RedoStoreIndexItem(name, indexItem.getOffset(), indexItem);
56             redoLog.storeRedoItem(redo);
57         }
58         
59         dataOut.reset();
60         indexItem.write(dataOut);
61         file.seek(indexItem.getOffset());
62         file.write(dataOut.getData(),0,IndexItem.INDEX_SIZE);
63     }
64     
65     void updateIndexes(IndexItem indexItem) throws IOException JavaDoc{
66         if( redoLog!=null ) {
67             RedoStoreIndexItem redo = new RedoStoreIndexItem(name, indexItem.getOffset(), indexItem);
68             redoLog.storeRedoItem(redo);
69         }
70         
71         dataOut.reset();
72         indexItem.updateIndexes(dataOut);
73         file.seek(indexItem.getOffset());
74         file.write(dataOut.getData(),0,IndexItem.INDEXES_ONLY_SIZE);
75     }
76
77     public void redoStoreItem(RedoStoreIndexItem redo) throws IOException JavaDoc {
78         dataOut.reset();
79         redo.getIndexItem().write(dataOut);
80         file.seek(redo.getOffset());
81         file.write(dataOut.getData(),0,IndexItem.INDEX_SIZE);
82     }
83     
84 }
85
Popular Tags