KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.RemoveFileOperation
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.Loggable;
25 import org.apache.derby.iapi.store.raw.Undoable;
26 import org.apache.derby.iapi.store.raw.Transaction;
27 import org.apache.derby.iapi.store.raw.Compensation;
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.xact.RawTransaction;
32
33 import org.apache.derby.iapi.error.StandardException;
34 import org.apache.derby.iapi.services.sanity.SanityManager;
35 import org.apache.derby.iapi.store.access.FileResource;
36 import org.apache.derby.iapi.store.raw.log.LogInstant;
37
38 import org.apache.derby.io.StorageFile;
39
40 import org.apache.derby.iapi.util.ByteArray;
41
42 import java.io.ObjectInput JavaDoc;
43 import java.io.ObjectOutput JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import org.apache.derby.iapi.services.io.LimitObjectInput;
47
48 /**
49 */

50
51 public class RemoveFileOperation implements Undoable
52 {
53     private String JavaDoc name;
54     private long generationId;
55     private boolean removeAtOnce;
56
57     transient private StorageFile fileToGo;
58
59     // no-arg constructor, required by Formatable
60
public RemoveFileOperation()
61     {
62     }
63
64     RemoveFileOperation(String JavaDoc name, long generationId, boolean removeAtOnce)
65     {
66         this.name = name;
67         this.generationId = generationId;
68         this.removeAtOnce = removeAtOnce;
69     }
70
71     /*
72      * Formatable methods
73      */

74     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
75     {
76         out.writeUTF(name);
77         out.writeLong(generationId);
78         out.writeBoolean(removeAtOnce);
79     }
80
81     public void readExternal(ObjectInput JavaDoc in)
82          throws IOException JavaDoc, ClassNotFoundException JavaDoc
83     {
84         name = in.readUTF();
85         generationId = in.readLong();
86         removeAtOnce = in.readBoolean();
87     }
88     /**
89         Return my format identifier.
90     */

91     public int getTypeFormatId() {
92         return StoredFormatIds.LOGOP_REMOVE_FILE;
93     }
94
95
96     /**
97         Loggable methods
98     */

99
100     /**
101         the default for prepared log is always null for all the operations
102         that don't have optionalData. If an operation has optional data,
103         the operation need to prepare the optional data for this method.
104
105         Space Operation has no optional data to write out
106     */

107     public ByteArray getPreparedLog()
108     {
109         return null;
110     }
111
112     public void releaseResource(Transaction tran)
113     {
114     }
115
116     /**
117         A space operation is a RAWSTORE log record
118     */

119     public int group()
120     {
121         return Loggable.FILE_RESOURCE | Loggable.RAWSTORE ;
122     }
123
124     public void doMe(Transaction xact, LogInstant instant,
125                            LimitObjectInput in)
126          throws StandardException
127     {
128         if (fileToGo == null)
129             return;
130
131         BaseDataFileFactory bdff =
132             (BaseDataFileFactory) ((RawTransaction) xact).getDataFactory();
133         
134         bdff.fileToRemove(fileToGo, true);
135     }
136
137
138     /**
139         @exception StandardException Standard Cloudscape error policy
140     */

141     public boolean needsRedo(Transaction xact)
142          throws StandardException
143     {
144         if (!removeAtOnce)
145             return false;
146
147         FileResource fr = ((RawTransaction) xact).getDataFactory().getFileHandler();
148
149         fileToGo = fr.getAsFile(name, generationId);
150
151         if (fileToGo == null)
152             return false;
153
154         return fileToGo.exists();
155     }
156
157
158     public Compensation generateUndo(Transaction xact, LimitObjectInput in)
159         throws StandardException, IOException JavaDoc {
160
161
162         if (fileToGo != null) {
163             BaseDataFileFactory bdff =
164                 (BaseDataFileFactory) ((RawTransaction) xact).getDataFactory();
165         
166             bdff.fileToRemove(fileToGo, false);
167         }
168
169         return null;
170     }
171 }
172
173
174
Popular Tags