KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.SetReservedSpaceOperation
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
31 import org.apache.derby.iapi.store.raw.Page;
32 import org.apache.derby.iapi.store.raw.Transaction;
33
34 import org.apache.derby.iapi.store.raw.log.LogInstant;
35 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
36
37 import org.apache.derby.iapi.services.sanity.SanityManager;
38 import org.apache.derby.iapi.error.StandardException;
39
40 import org.apache.derby.iapi.services.io.CompressedNumber;
41 import org.apache.derby.iapi.util.ByteArray;
42 import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream;
43 import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream;
44
45 import java.io.OutputStream JavaDoc;
46 import java.io.ObjectOutput JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.IOException JavaDoc;
49 import org.apache.derby.iapi.services.io.LimitObjectInput;
50
51 /**
52     Represents shrinking of the reserved space of a particular row on a page.
53     This operation is not undoable.
54 */

55 public class SetReservedSpaceOperation extends PageBasicOperation {
56
57     protected int doMeSlot; // slot where record is at
58
protected int recordId; // recordId
59
protected int newValue; // the new reserved space value
60
protected int oldValue; // the old reserved space value (for BI_logging)
61

62     public SetReservedSpaceOperation(BasePage page, int slot,
63                                      int recordId, int newValue, int oldValue)
64     {
65         super(page);
66         doMeSlot = slot;
67         this.recordId = recordId;
68         this.newValue = newValue;
69         this.oldValue = oldValue;
70
71         if (SanityManager.DEBUG) // we only use this for shrinking
72
SanityManager.ASSERT(oldValue > newValue);
73     }
74
75     /*
76      * Formatable methods
77      */

78     // no-arg constructor, required by Formatable
79
public SetReservedSpaceOperation() { super(); }
80
81     /**
82         Return my format identifier.
83     */

84     public int getTypeFormatId() {
85         return StoredFormatIds.LOGOP_SET_RESERVED_SPACE;
86     }
87
88     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
89     {
90         super.writeExternal(out);
91
92         CompressedNumber.writeInt(out, doMeSlot);
93         CompressedNumber.writeInt(out, recordId);
94         CompressedNumber.writeInt(out, newValue);
95         CompressedNumber.writeInt(out, oldValue);
96     }
97
98     /**
99         Read this in
100         @exception IOException error reading from log stream
101         @exception ClassNotFoundException log stream corrupted
102     */

103     public void readExternal(ObjectInput JavaDoc in)
104          throws IOException JavaDoc, ClassNotFoundException JavaDoc
105     {
106         super.readExternal(in);
107         doMeSlot = CompressedNumber.readInt(in);
108         recordId = CompressedNumber.readInt(in);
109         newValue = CompressedNumber.readInt(in);
110         oldValue = CompressedNumber.readInt(in);
111     }
112
113     /*
114      * Loggable methods
115      */

116     /**
117         @exception IOException Can be thrown by any of the methods of ObjectInput.
118         @exception StandardException Standard Cloudscape policy.
119       
120         @see org.apache.derby.iapi.store.raw.Loggable#doMe
121     */

122     public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in)
123          throws StandardException, IOException JavaDoc
124     {
125         if (SanityManager.DEBUG)
126         {
127             SanityManager.ASSERT(oldValue ==
128                                  this.page.getReservedCount(doMeSlot));
129             SanityManager.ASSERT(newValue < oldValue,
130                 "cannot set reserved space to be bigger than before");
131         }
132
133         page.setReservedSpace(instant, doMeSlot, newValue);
134     }
135
136     /*
137      * method to support BeforeImageLogging - This log operation is not
138      * undoable in the logical sense , but all log operations that touch a page
139      * must support physical undo during RRR transaction.
140      */

141
142     /**
143      * restore the before image of the page
144      *
145      * @exception StandardException Standard Cloudscape Error Policy
146      * @exception IOException problem reading the complete log record from the
147      * input stream
148      */

149
150     public void restoreMe(Transaction xact, BasePage undoPage, LogInstant CLRinstant, LimitObjectInput in)
151          throws StandardException, IOException JavaDoc
152     {
153         int slot = undoPage.findRecordById(recordId,Page.FIRST_SLOT_NUMBER);
154         if (SanityManager.DEBUG)
155         {
156             if ( ! getPageId().equals(undoPage.getPageId()))
157                 SanityManager.THROWASSERT(
158                                 "restoreMe cannot restore to a different page. "
159                                  + "doMe page:" + getPageId() + " undoPage:" +
160                                  undoPage.getPageId());
161             if (slot != doMeSlot)
162                 SanityManager.THROWASSERT(
163                                 "restoreMe cannot restore to a different slot. "
164                                  + "doMe slot:" + doMeSlot + " undoMe slot: " +
165                                  slot + " recordId:" + recordId);
166
167         }
168
169         page.setReservedSpace(CLRinstant, slot, oldValue);
170
171     }
172
173     /**
174       DEBUG: Print self.
175     */

176     public String JavaDoc toString()
177     {
178         if (SanityManager.DEBUG)
179         {
180             return super.toString() +
181                 "Set Reserved space of recordId " + recordId + " from " + oldValue + " to " + newValue;
182         }
183         return null;
184     }
185 }
186
Popular Tags