KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > data > PurgeOperation


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

21
22 package org.apache.derby.impl.store.raw.data;
23
24 import org.apache.derby.iapi.reference.SQLState;
25
26 import org.apache.derby.impl.store.raw.data.BasePage;
27
28 import org.apache.derby.iapi.services.io.FormatIdUtil;
29 import org.apache.derby.iapi.services.io.StoredFormatIds;
30 import org.apache.derby.iapi.services.io.Storable;
31
32 import org.apache.derby.iapi.store.raw.Page;
33 import org.apache.derby.iapi.store.raw.RecordHandle;
34 import org.apache.derby.iapi.store.raw.Transaction;
35 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
36
37 import org.apache.derby.iapi.store.raw.log.LogInstant;
38
39 import org.apache.derby.iapi.error.StandardException;
40 import org.apache.derby.iapi.services.sanity.SanityManager;
41
42 import org.apache.derby.iapi.services.io.CompressedNumber;
43 import org.apache.derby.iapi.services.io.FormatableBitSet;
44 import org.apache.derby.iapi.util.ByteArray;
45 import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream;
46
47
48 import java.io.OutputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectInput JavaDoc;
51 import java.io.IOException JavaDoc;
52 import org.apache.derby.iapi.services.io.LimitObjectInput;
53
54 /**
55     USE WITH EXTREME Caution: Purge records from a Page.
56
57     Represents purging of a range of rows from the page.
58
59     <PRE>
60     @format_id LOGOP_PURGE
61         the formatId is written by FormatIdOutputStream when this object is
62         written out by writeObject
63     @purpose purge num_rows from the page
64     @upgrade
65     @disk_layout
66         PagePhysicalOperation the super class
67         slot(CompressedInt) the slot to start purging
68         num_rows(CompressedInt) number of rows rows to purge
69         recordIds(CompressedInt[num_rows]) the recordIds of the purged rows
70
71         OptionalData the before images of the rows that were purged
72     @end_format
73     </PRE>
74
75    @see Page#purgeAtSlot
76 */

77 public final class PurgeOperation extends PhysicalPageOperation {
78
79     protected int slot; // purge num_rows records starting at this slot
80
// caller must guarentee that during undo of the
81
// log record, this slot is the correct slot to
82
// re-insert the purged record
83
protected int num_rows;
84     protected int[] recordIds; // record Id
85

86
87     transient protected ByteArray preparedLog;
88
89     public PurgeOperation(RawTransaction t, BasePage page, int slot, int
90                           num_rows, int[] recordIds, boolean needDataLogged)
91         throws StandardException
92     {
93         super(page);
94
95         this.slot = slot;
96         this.num_rows = num_rows;
97         this.recordIds = recordIds;
98
99         try {
100             writeOptionalDataToBuffer(t, needDataLogged);
101         } catch (IOException JavaDoc ioe) {
102             throw StandardException.newException(
103                     SQLState.DATA_UNEXPECTED_EXCEPTION, ioe);
104         }
105     
106     }
107
108     /*
109      * Formatable methods
110      */

111
112     // no-arg constructor, required by Formatable
113
public PurgeOperation() { super(); }
114
115     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
116     {
117         super.writeExternal(out);
118
119         CompressedNumber.writeInt(out, slot);
120         CompressedNumber.writeInt(out, num_rows);
121
122         for (int i = 0; i < num_rows; i++)
123             CompressedNumber.writeInt(out, recordIds[i]);
124     }
125
126     /**
127         Read this in
128         @exception IOException error reading from log stream
129         @exception ClassNotFoundException log stream corrupted
130     */

131     public void readExternal(ObjectInput JavaDoc in)
132          throws IOException JavaDoc, ClassNotFoundException JavaDoc
133     {
134         super.readExternal(in);
135         slot = CompressedNumber.readInt(in);
136         num_rows = CompressedNumber.readInt(in);
137
138         recordIds = new int[num_rows];
139         for (int i = 0; i < num_rows; i++)
140             recordIds[i] = CompressedNumber.readInt(in);
141     }
142
143     /**
144         Return my format identifier.
145     */

146     public int getTypeFormatId() {
147         return StoredFormatIds.LOGOP_PURGE;
148     }
149
150
151     /*
152      * Loggable methods
153      */

154     /**
155         Apply the purge operation to the page.
156
157         @exception IOException Can be thrown by any of the methods of ObjectInput.
158         @exception StandardException Standard Cloudscape policy.
159
160         @see org.apache.derby.iapi.store.raw.Loggable#doMe
161     */

162     public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in)
163          throws StandardException, IOException JavaDoc
164     {
165         // purge the records in the stored version
166
// we need to remove from high to low because the slots will be moved down
167
// as soon as one is removed.
168

169         // we could get the slot with the recordId but that will be a waste
170
// since the page was never unlatch and the slot number is good
171

172         for (int i = num_rows-1; i >= 0; i--)
173         {
174             this.page.purgeRecord(instant, slot+i, recordIds[i]);
175         }
176     }
177
178     /*
179      * PhysicalPageOperation methods
180      */

181     
182     /**
183         Undo the purge operation on the page.
184
185         @exception IOException Can be thrown by any of the methods of ObjectInput.
186         @exception StandardException Standard Cloudscape policy.
187
188         @see PhysicalPageOperation#undoMe
189     */

190     public void undoMe(Transaction xact, BasePage undoPage,
191                        LogInstant CLRInstant, LimitObjectInput in)
192          throws StandardException, IOException JavaDoc
193     {
194         for (int i = 0; i < num_rows; i++)
195         {
196             undoPage.storeRecord(CLRInstant, slot+i, true, in);
197         }
198         undoPage.setAuxObject(null);
199     }
200
201
202     /*
203      * PageBasicOperation
204      */

205
206     /**
207      * restore the before image of the page
208      *
209      * @exception StandardException Standard Cloudscape Error Policy
210      * @exception IOException problem reading the complete log record from the
211      * input stream
212      */

213     public void restoreMe(Transaction xact, BasePage undoPage,
214                        LogInstant CLRInstant, LimitObjectInput in)
215          throws StandardException, IOException JavaDoc
216     {
217         undoMe(xact, undoPage, CLRInstant, in);
218     }
219
220     /*
221         methods to support prepared log
222         
223         the following two methods should not be called during recover
224     */

225
226     public ByteArray getPreparedLog()
227     {
228         return (this.preparedLog);
229     }
230
231     /**
232         Write out the purged record from the page. Used for undo only.
233
234         @exception IOException Can be thrown by any of the methods of ObjectOutput.
235         @exception StandardException Standard Cloudscape policy.
236     */

237     private void writeOptionalDataToBuffer(RawTransaction t, boolean needDataLogged)
238         throws StandardException, IOException JavaDoc
239     {
240
241         if (SanityManager.DEBUG) {
242             SanityManager.ASSERT(this.page != null);
243         }
244
245         DynamicByteArrayOutputStream logBuffer = t.getLogBuffer();
246         int optionalDataStart = logBuffer.getPosition();
247
248         if (SanityManager.DEBUG) {
249             SanityManager.ASSERT(optionalDataStart == 0,
250                 "Buffer for writing the optional data should start at position 0");
251         }
252
253         for (int i = 0; i < num_rows; i++)
254         {
255             if(needDataLogged)
256             {
257                 this.page.logRecord(i+slot, BasePage.LOG_RECORD_DEFAULT,
258                                     recordIds[i], (FormatableBitSet) null, logBuffer,
259                                     (RecordHandle)null);
260             }else
261             {
262                 this.page.logRecord(i+slot, BasePage.LOG_RECORD_FOR_PURGE,
263                                     recordIds[i], (FormatableBitSet) null, logBuffer,
264                                     (RecordHandle)null);
265             }
266         }
267         
268         int optionalDataLength = logBuffer.getPosition() - optionalDataStart;
269
270         if (SanityManager.DEBUG) {
271             if (optionalDataLength != logBuffer.getUsed())
272                 SanityManager.THROWASSERT("wrong optional data length, optionalDataLength = "
273                     + optionalDataLength + ", logBuffer.getUsed() = " + logBuffer.getUsed());
274         }
275
276         // set the position to the beginning of the buffer
277
logBuffer.setPosition(optionalDataStart);
278
279         this.preparedLog = new ByteArray(logBuffer.getByteArray(), optionalDataStart,
280             optionalDataLength);
281     }
282
283     /**
284       DEBUG: Print self.
285     */

286     public String JavaDoc toString()
287     {
288         if (SanityManager.DEBUG)
289         {
290             String JavaDoc str = super.toString() +
291                 "Purge : " + num_rows + " slots starting at " + slot;
292
293             for (int i = 0; i < num_rows; i++)
294             {
295                 str += " (recordId=" + recordIds[i] + ")";
296             }
297             return str;
298         }
299         else
300             return null;
301     }
302 }
303
Popular Tags