KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > data > SyncDataFileWriter


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.data;
19
20 import java.io.IOException JavaDoc;
21 import java.io.RandomAccessFile JavaDoc;
22
23 import org.apache.activemq.kaha.Marshaller;
24 import org.apache.activemq.util.DataByteArrayOutputStream;
25 /**
26  * Optimized Store writer. Synchronously marshalls and writes to the data file. Simple but
27  * may introduce a bit of contention when put under load.
28  *
29  * @version $Revision: 1.1.1.1 $
30  */

31 final public class SyncDataFileWriter {
32     
33     private DataByteArrayOutputStream buffer;
34     private DataManagerImpl dataManager;
35
36
37     /**
38      * Construct a Store writer
39      *
40      * @param fileId
41      */

42     SyncDataFileWriter(DataManagerImpl fileManager){
43         this.dataManager=fileManager;
44         this.buffer=new DataByteArrayOutputStream();
45     }
46
47     /* (non-Javadoc)
48      * @see org.apache.activemq.kaha.impl.data.DataFileWriter#storeItem(org.apache.activemq.kaha.Marshaller, java.lang.Object, byte)
49      */

50     public synchronized DataItem storeItem(Marshaller marshaller, Object JavaDoc payload, byte type) throws IOException JavaDoc {
51         
52         // Write the packet our internal buffer.
53
buffer.reset();
54         buffer.position(DataManagerImpl.ITEM_HEAD_SIZE);
55         marshaller.writePayload(payload,buffer);
56         int size=buffer.size();
57         int payloadSize=size-DataManagerImpl.ITEM_HEAD_SIZE;
58         buffer.reset();
59         buffer.writeByte(type);
60         buffer.writeInt(payloadSize);
61
62         // Find the position where this item will land at.
63
DataItem item=new DataItem();
64         item.setSize(payloadSize);
65         DataFile dataFile=dataManager.findSpaceForData(item);
66         
67         // Now splat the buffer to the file.
68
dataFile.getRandomAccessFile().seek(item.getOffset());
69         dataFile.getRandomAccessFile().write(buffer.getData(),0,size);
70         dataFile.setWriterData(Boolean.TRUE); // Use as dirty marker..
71

72         dataManager.addInterestInFile(dataFile);
73         return item;
74     }
75     
76     /* (non-Javadoc)
77      * @see org.apache.activemq.kaha.impl.data.DataFileWriter#updateItem(org.apache.activemq.kaha.StoreLocation, org.apache.activemq.kaha.Marshaller, java.lang.Object, byte)
78      */

79     public synchronized void updateItem(DataItem item,Marshaller marshaller, Object JavaDoc payload, byte type) throws IOException JavaDoc {
80         //Write the packet our internal buffer.
81
buffer.reset();
82         buffer.position(DataManagerImpl.ITEM_HEAD_SIZE);
83         marshaller.writePayload(payload,buffer);
84         int size=buffer.size();
85         int payloadSize=size-DataManagerImpl.ITEM_HEAD_SIZE;
86         buffer.reset();
87         buffer.writeByte(type);
88         buffer.writeInt(payloadSize);
89         item.setSize(payloadSize);
90         DataFile dataFile = dataManager.getDataFile(item);
91         RandomAccessFile JavaDoc file = dataFile.getRandomAccessFile();
92         file.seek(item.getOffset());
93         file.write(buffer.getData(),0,size);
94         dataFile.setWriterData(Boolean.TRUE); // Use as dirty marker..
95
}
96
97     public synchronized void force(DataFile dataFile) throws IOException JavaDoc {
98         // If our dirty marker was set.. then we need to sync
99
if( dataFile.getWriterData()!=null && dataFile.isDirty()) {
100             dataFile.getRandomAccessFile().getFD().sync();
101             dataFile.setWriterData(null);
102             dataFile.setDirty(false);
103         }
104     }
105
106     public void close() throws IOException JavaDoc {
107     }
108 }
109
Popular Tags