ActiveIO - 1.0

Clover coverage report - ActiveIO - 1.0 Coverage timestamp: Fri Apr 22 2005 14:27:22 PDT FRAMES NO FRAMES |
file stats: | LOC: | 144 |  | Methods: | 10
| NCLOC: | 77 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
BatchedWrite.java | 45% | 63.9% | 80% | 60.6% |

1 |  |
     /**
2 |  |
      *
3 |  |
      * Copyright 2004 Hiram Chirino
4 |  |
      *
5 |  |
      * Licensed under the Apache License, Version 2.0 (the "License");
6 |  |
      * you may not use this file except in compliance with the License.
7 |  |
      * You may obtain a copy of the License at
8 |  |
      *
9 |  |
      * http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
16 |  |
      *
17 |  |
      **/
18 |  |
     package org.activeio.journal.active;
19 |  |
20 |  |
     import org.activeio.Packet;
21 |  |
22 |  |
     import EDU.oswego.cs.dl.util.concurrent.Latch;
23 |  |
24 |  |
     /**
25 |  |
      * This contains all the data needed to write and force a list of records to a
26 |  |
      * LogFile. The more records that can be cramed into a single BatchedWrite, the
27 |  |
      * higher throughput that can be achived by a write and force operation.
28 |  |
      *
29 |  |
      * @version $Revision: 1.1 $
30 |  |
      */
31 |  |
     final public class BatchedWrite {
32 |  |
33 |  |
         private final Packet packet;
34 |  |
         public Throwable error;
35 |  |
         private Location mark;
36 |  |
         private boolean appendDisabled = false;
37 |  |
         private boolean appendInProgress = false;
38 |  |
         private Latch writeDoneLatch;
39 |  |
40 |  |
         /**
41 |  |
          * @param packet
42 |  |
          */
43 | 20 |
         public BatchedWrite(Packet packet) {
44 | 20 |
             this.packet = packet;
45 |  |
         }
46 |  |
47 |  |
         /**
48 |  |
          * @throws InterruptedException
49 |  |
          *
50 |  |
          */
51 | 20 |
         synchronized private void disableAppend() throws InterruptedException {
52 | 20 |
             appendDisabled = true;
53 | 20 |
             while (appendInProgress) {
54 | 0 |
                 wait();
55 |  |
             }
56 |  |
         }
57 |  |
58 |  |
         /**
59 |  |
          * @param packet2
60 |  |
          * @param mark2
61 |  |
          * @return
62 |  |
          */
63 | 24 |
         public boolean append(Record record, Location recordMark, boolean force) {
64 |  |
65 | 24 |
             synchronized (this) {
66 | 24 |
                 if (appendDisabled)
67 | 2 |
                     return false;
68 | 22 |
                 appendInProgress = true;
69 |  |
             }
70 |  |
71 |  |
72 | 22 |
             if( force && writeDoneLatch==null)
73 | 16 |
                 writeDoneLatch = new Latch();
74 |  |
75 | 22 |
             record.read(packet);
76 |  |
77 |  |
             // if we fit the record in this batch
78 | 22 |
             if ( !record.hasRemaining() ) {
79 | 22 |
                 if (recordMark != null)
80 | 0 |
                     mark = recordMark;
81 |  |
             }
82 |  |
83 | 22 |
             synchronized (this) {
84 | 22 |
                 appendInProgress = false;
85 | 22 |
                 this.notify();
86 |  |
87 | 22 |
                 if (appendDisabled)
88 | 0 |
                     return false;
89 |  |
                 else
90 | 22 |
                     return packet.remaining() > 0;
91 |  |
             }
92 |  |
         }
93 |  |
94 | 0 |
         public void waitForForce() throws Throwable {
95 | 0 |
             if( writeDoneLatch!=null ) {
96 | 0 |
                 writeDoneLatch.acquire();
97 | 0 |
                 synchronized (this) {
98 | 0 |
                     if (error != null)
99 | 0 |
                         throw error;
100 |  |
                 }
101 |  |
             }
102 |  |
         }
103 |  |
104 | 4 |
         public void forced() {
105 | 4 |
             if( writeDoneLatch!=null ) {
106 | 0 |
                 writeDoneLatch.release();
107 |  |
             }
108 |  |
         }
109 |  |
110 | 0 |
         public void writeFailed(Throwable error) {
111 | 0 |
             if( writeDoneLatch!=null ) {
112 | 0 |
                 synchronized (this) {
113 | 0 |
                     this.error = error;
114 |  |
                 }
115 | 0 |
                 writeDoneLatch.release();
116 |  |
             }
117 |  |
         }
118 |  |
119 | 24 |
         public Packet getPacket() {
120 | 24 |
             return packet;
121 |  |
         }
122 |  |
123 |  |
         /**
124 |  |
          * @return
125 |  |
          */
126 | 20 |
         public Location getMark() {
127 | 20 |
             return mark;
128 |  |
         }
129 |  |
130 |  |
         /**
131 |  |
          * @throws InterruptedException
132 |  |
          *
133 |  |
          */
134 | 20 |
         public void flip() throws InterruptedException {
135 | 20 |
             disableAppend();
136 | 20 |
             packet.flip();
137 |  |
         }
138 |  |
139 | 20 |
         public boolean getForce() {
140 | 20 |
             return writeDoneLatch!=null;
141 |  |
         }
142 |  |
143 |  |
     }
144 |  |