KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.ChainAllocPageOperation
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.impl.store.raw.data.PhysicalPageOperation;
25 import org.apache.derby.impl.store.raw.data.BasePage;
26
27 import org.apache.derby.iapi.services.io.FormatIdUtil;
28 import org.apache.derby.iapi.services.io.StoredFormatIds;
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30
31 import org.apache.derby.iapi.error.StandardException;
32 import org.apache.derby.iapi.store.raw.ContainerHandle;
33 import org.apache.derby.iapi.store.raw.data.RawContainerHandle;
34 import org.apache.derby.iapi.store.raw.Transaction;
35 import org.apache.derby.iapi.store.raw.log.LogInstant;
36
37 import org.apache.derby.iapi.services.io.CompressedNumber;
38
39 import java.io.OutputStream JavaDoc;
40 import java.io.ObjectOutput JavaDoc;
41 import java.io.IOException JavaDoc;
42 import org.apache.derby.iapi.services.io.LimitObjectInput;
43 import java.io.ObjectInput JavaDoc;
44
45
46 /**
47
48 Log operation to implement compressing space from a container and returning
49 it to the operating system.
50
51 **/

52
53 public final class CompressSpacePageOperation extends PhysicalPageOperation
54 {
55     /**************************************************************************
56      * Fields of the class
57      **************************************************************************
58      */

59
60     /**
61      * The new highest page on this allocation page. The number is the
62      * offset of the page in the array of pages maintained by this
63      * allocation page, for instance a value of 0 indicates all page except
64      * the first one are to be truncated. If all pages are truncated then
65      * the offset is set to -1.
66      **/

67     protected int newHighestPage;
68
69     /**
70      * The number of allocated pages in this allocation page prior to
71      * the truncate. Note that all pages from NewHighestPage+1 through
72      * newHighestPage+num_pages_truncated should be FREE.
73      **/

74     protected int num_pages_truncated;
75
76     /**************************************************************************
77      * Constructors for This class:
78      **************************************************************************
79      */

80     public CompressSpacePageOperation(
81     AllocPage allocPage,
82     int highest_page,
83     int num_truncated)
84          throws StandardException
85     {
86         super(allocPage);
87
88         newHighestPage = highest_page;
89         num_pages_truncated = num_truncated;
90     }
91     
92     /**************************************************************************
93      * Public Methods of Formatable interface.
94      **************************************************************************
95      */

96
97     // no-arg constructor, required by Formatable
98
public CompressSpacePageOperation() { super(); }
99
100     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
101     {
102         super.writeExternal(out);
103         CompressedNumber.writeInt(out, newHighestPage);
104         CompressedNumber.writeInt(out, num_pages_truncated);
105     }
106
107     /**
108         @exception IOException error reading from log stream
109         @exception ClassNotFoundException cannot read object from input
110     */

111     public void readExternal(ObjectInput in)
112          throws IOException JavaDoc, ClassNotFoundException JavaDoc
113     {
114         super.readExternal(in);
115         newHighestPage = CompressedNumber.readInt(in);
116         num_pages_truncated = CompressedNumber.readInt(in);
117     }
118
119     /**
120         Return my format identifier.
121     */

122     public int getTypeFormatId() {
123         return StoredFormatIds.LOGOP_COMPRESS_SPACE;
124     }
125
126     /**************************************************************************
127      * Public Methods of Loggable interface.
128      **************************************************************************
129      */

130
131     /**
132      * Compress space from container.
133      * <p>
134      * Compress the indicate space from the container, returning the free
135      * pages to the OS. Update the allocation page to reflect the file
136      * change.
137      *
138      * @param tran transaction doing the operation.
139      * @param instant log instant for this operation.
140      * @param in unused by this log operation.
141      *
142      * @exception StandardException Standard exception policy.
143      **/

144     public final void doMe(
145     Transaction tran,
146     LogInstant instant,
147     LimitObjectInput in)
148          throws StandardException
149     {
150         if (SanityManager.DEBUG)
151         {
152             SanityManager.ASSERT(this.page instanceof AllocPage);
153         }
154
155         ((AllocPage)page).compressSpace(
156              instant, newHighestPage, num_pages_truncated);
157     }
158
159     /**************************************************************************
160      * Public Methods of Undoable interface.
161      **************************************************************************
162      */

163
164     /**
165      * Compress space undo.
166      * <p>
167      *
168      * @exception StandardException Thrown by methods I call
169      * @see PhysicalPageOperation#undoMe
170      **/

171     public void undoMe(
172     Transaction xact,
173     BasePage undoPage,
174     LogInstant CLRInstant,
175     LimitObjectInput in)
176          throws StandardException
177     {
178         if (SanityManager.DEBUG)
179         {
180             SanityManager.ASSERT(undoPage != null, "undo Page null");
181             SanityManager.ASSERT(
182                 undoPage instanceof AllocPage,
183                 "undo Page is not an allocPage");
184         }
185
186         ((AllocPage)undoPage).undoCompressSpace(
187              CLRInstant, newHighestPage, num_pages_truncated);
188     }
189
190     /*
191      * method to support BeforeImageLogging
192      */

193     public void restoreMe(
194     Transaction xact,
195     BasePage undoPage,
196     LogInstant CLRinstant,
197     LimitObjectInput in)
198     {
199         // nobody should be calling this since there is no corresponding
200
// BI operation.
201
if (SanityManager.DEBUG)
202             SanityManager.THROWASSERT(
203                 "cannot call restoreMe on CompressSpaceOperation.");
204     }
205
206
207     /** debug */
208     public String JavaDoc toString()
209     {
210         if (SanityManager.DEBUG)
211         {
212             String JavaDoc str = super.toString();
213             str += " CompressSpaceOperation: " +
214                 "newHighestPage = " + newHighestPage +
215                 ";num_pages_truncated = " + num_pages_truncated +
216                 " to " + getPageId();
217
218             return str;
219         }
220         else
221             return null;
222     }
223 }
224
Popular Tags