KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.ReclaimSpace
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.store.raw.ContainerKey;
25 import org.apache.derby.iapi.store.raw.RecordHandle;
26 import org.apache.derby.iapi.store.raw.PageTimeStamp;
27 import org.apache.derby.iapi.store.raw.PageKey;
28
29 import org.apache.derby.iapi.services.context.ContextManager;
30 import org.apache.derby.iapi.services.daemon.Serviceable;
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32
33 import org.apache.derby.iapi.error.StandardException;
34 import org.apache.derby.iapi.store.raw.data.DataFactory;
35 import org.apache.derby.iapi.store.raw.ContainerHandle;
36
37
38 /**
39     Post commit work to reclaim some space from the raw store. This is a
40     wrapper class for the real serviceable class who wraps this on top of
41     itself so different things can be identified.
42 */

43 public final class ReclaimSpace implements Serviceable
44 {
45     private boolean serviceASAP;
46
47     private ContainerKey containerId;
48     private PageKey pageId; // Not used for reclaiming container.
49
private RecordHandle headRowHandle; // Used for reclaiming overflow page
50
// and row reserved space.
51

52     // The following is used for reclaiming column chain only.
53
private int columnId; // Which column in the row to reclaim.
54
private long columnPageId; // Where the column chain pointer
55
private int columnRecordId; // is pointed at.
56
private PageTimeStamp timeStamp; // Time stamp of columnPageId to make sure
57
// the post commit work doesn't get
58
// exercised more then once.
59

60     private int attempts;
61
62     private DataFactory processor; // processor knows how to reclaim file space
63

64     private int reclaim; // what is it we should be reclaiming
65
public static final int CONTAINER = 1; // reclaim the entire container
66
public static final int PAGE = 2; // reclaim an overflow page
67
public static final int ROW_RESERVE = 3; // reclaim reserved space on a row
68
public static final int COLUMN_CHAIN = 4; // reclaim a column chain
69

70
71     private void initContainerInfo(ContainerKey containerId, int reclaim,
72                               DataFactory processor, boolean serviceASAP)
73     {
74         this.containerId = containerId;
75         this.reclaim = reclaim;
76         this.attempts = 0;
77
78         this.processor = processor;
79         this.serviceASAP = serviceASAP;
80     }
81
82     // reclaim container
83
public ReclaimSpace(int reclaim, ContainerKey containerId,
84                         DataFactory processor, boolean serviceASAP)
85     {
86         if (SanityManager.DEBUG)
87             SanityManager.ASSERT(reclaim == CONTAINER);
88         initContainerInfo(containerId, reclaim, processor, serviceASAP);
89     }
90
91     // reclaim page - undo of insert into overflow page
92
public ReclaimSpace(int reclaim, PageKey pageId,
93                         DataFactory processor, boolean serviceASAP)
94     {
95         if (SanityManager.DEBUG)
96             SanityManager.ASSERT(reclaim == PAGE);
97         initContainerInfo(pageId.getContainerId(), reclaim, processor, serviceASAP);
98
99         this.pageId = pageId;
100     }
101
102     // reclaim row reserved space
103
public ReclaimSpace(int reclaim, RecordHandle headRowHandle,
104                         DataFactory processor, boolean serviceASAP)
105     {
106         if (SanityManager.DEBUG)
107             SanityManager.ASSERT(reclaim == ROW_RESERVE);
108
109         initContainerInfo(headRowHandle.getContainerId(), reclaim, processor, serviceASAP);
110
111         this.headRowHandle = headRowHandle;
112     }
113
114     // reclaim column chain
115
public ReclaimSpace(int reclaim, RecordHandle headRowHandle,
116                         int columnId, long ovPageId, int ovRecordId,
117                         PageTimeStamp timeStamp,
118                         DataFactory processor, boolean serviceASAP)
119     {
120         if (SanityManager.DEBUG)
121             SanityManager.ASSERT(reclaim == COLUMN_CHAIN);
122         initContainerInfo(headRowHandle.getContainerId(), reclaim, processor, serviceASAP);
123
124         this.headRowHandle = headRowHandle;
125         this.columnId = columnId;
126         this.columnPageId = ovPageId;
127         this.columnRecordId = ovRecordId;
128         this.timeStamp = timeStamp;
129     }
130
131     /*
132      * Serviceable methods
133      */

134
135     public boolean serviceASAP()
136     {
137         return serviceASAP;
138     }
139
140     public int performWork(ContextManager context) throws StandardException
141     {
142         if (SanityManager.DEBUG)
143         {
144             SanityManager.ASSERT(context != null, "context is null");
145             SanityManager.ASSERT(processor != null, "processor is null");
146         }
147
148         return processor.reclaimSpace(this, context);
149     }
150
151
152     // @return true, if this work needs to be done on a user thread immediately
153
public boolean serviceImmediately()
154     {
155
156         //It's very important that we reclaim container space immediately
157
//as part of post commit cleanup. Because typically could typically
158
//involve large amount of space freed and
159
//we don't want conatiner reclaim requests lost if the server crashes
160
//for some reasom before Container Reclaim requests could be
161
//processed successfully by an asynchronous thread.
162
//if(reclaim == CONTAINER)
163
// return true; else return false;
164
return true;
165     }
166
167
168     /*
169      * class specific methods
170      */

171
172     public final ContainerKey getContainerId()
173     {
174         return containerId;
175     }
176
177     public final PageKey getPageId()
178     {
179         return pageId;
180     }
181
182     public final RecordHandle getHeadRowHandle()
183     {
184         return headRowHandle;
185     }
186
187     public final int getColumnId()
188     {
189         return columnId;
190     }
191
192     public final long getColumnPageId()
193     {
194         return columnPageId;
195     }
196
197     public final int getColumnRecordId()
198     {
199         return columnRecordId;
200     }
201
202     public final PageTimeStamp getPageTimeStamp()
203     {
204         return timeStamp;
205     }
206
207     public final int reclaimWhat()
208     {
209         return reclaim;
210     }
211
212     public final int incrAttempts()
213     {
214         return ++attempts;
215     }
216
217     // debug
218
public String JavaDoc toString()
219     {
220         if (SanityManager.DEBUG)
221         {
222             if (reclaim == CONTAINER)
223                 return "Reclaim CONTAINER (" + containerId + ")";
224
225             if (reclaim == PAGE)
226                 return "Reclaim PAGE (" + pageId + ") head row at " + headRowHandle;
227
228             if (reclaim == ROW_RESERVE)
229                 return "Reclaim ROW_RESERVE (" + pageId + ")." + headRowHandle + ")";
230
231             if (reclaim == COLUMN_CHAIN)
232                 return "Reclaim COLUMN_CHAIN ("+ pageId + ").(" + headRowHandle
233                                   + "," + columnId + ") at (" + columnPageId +
234                                   "," + columnRecordId + ")";
235         }
236         return null;
237
238     }
239
240 }
241
Popular Tags