001: /*
002:
003: Derby - Class org.apache.derby.impl.store.raw.data.RemoveFileOperation
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.store.raw.data;
023:
024: import org.apache.derby.iapi.store.raw.Loggable;
025: import org.apache.derby.iapi.store.raw.Undoable;
026: import org.apache.derby.iapi.store.raw.Transaction;
027: import org.apache.derby.iapi.store.raw.Compensation;
028: import org.apache.derby.iapi.services.io.FormatIdUtil;
029: import org.apache.derby.iapi.services.io.StoredFormatIds;
030:
031: import org.apache.derby.iapi.store.raw.xact.RawTransaction;
032:
033: import org.apache.derby.iapi.error.StandardException;
034: import org.apache.derby.iapi.services.sanity.SanityManager;
035: import org.apache.derby.iapi.store.access.FileResource;
036: import org.apache.derby.iapi.store.raw.log.LogInstant;
037:
038: import org.apache.derby.io.StorageFile;
039:
040: import org.apache.derby.iapi.util.ByteArray;
041:
042: import java.io.ObjectInput;
043: import java.io.ObjectOutput;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import org.apache.derby.iapi.services.io.LimitObjectInput;
047:
048: /**
049: */
050:
051: public class RemoveFileOperation implements Undoable {
052: private String name;
053: private long generationId;
054: private boolean removeAtOnce;
055:
056: transient private StorageFile fileToGo;
057:
058: // no-arg constructor, required by Formatable
059: public RemoveFileOperation() {
060: }
061:
062: RemoveFileOperation(String name, long generationId,
063: boolean removeAtOnce) {
064: this .name = name;
065: this .generationId = generationId;
066: this .removeAtOnce = removeAtOnce;
067: }
068:
069: /*
070: * Formatable methods
071: */
072: public void writeExternal(ObjectOutput out) throws IOException {
073: out.writeUTF(name);
074: out.writeLong(generationId);
075: out.writeBoolean(removeAtOnce);
076: }
077:
078: public void readExternal(ObjectInput in) throws IOException,
079: ClassNotFoundException {
080: name = in.readUTF();
081: generationId = in.readLong();
082: removeAtOnce = in.readBoolean();
083: }
084:
085: /**
086: Return my format identifier.
087: */
088: public int getTypeFormatId() {
089: return StoredFormatIds.LOGOP_REMOVE_FILE;
090: }
091:
092: /**
093: Loggable methods
094: */
095:
096: /**
097: the default for prepared log is always null for all the operations
098: that don't have optionalData. If an operation has optional data,
099: the operation need to prepare the optional data for this method.
100:
101: Space Operation has no optional data to write out
102: */
103: public ByteArray getPreparedLog() {
104: return null;
105: }
106:
107: public void releaseResource(Transaction tran) {
108: }
109:
110: /**
111: A space operation is a RAWSTORE log record
112: */
113: public int group() {
114: return Loggable.FILE_RESOURCE | Loggable.RAWSTORE;
115: }
116:
117: public void doMe(Transaction xact, LogInstant instant,
118: LimitObjectInput in) throws StandardException {
119: if (fileToGo == null)
120: return;
121:
122: BaseDataFileFactory bdff = (BaseDataFileFactory) ((RawTransaction) xact)
123: .getDataFactory();
124:
125: bdff.fileToRemove(fileToGo, true);
126: }
127:
128: /**
129: @exception StandardException Standard Cloudscape error policy
130: */
131: public boolean needsRedo(Transaction xact) throws StandardException {
132: if (!removeAtOnce)
133: return false;
134:
135: FileResource fr = ((RawTransaction) xact).getDataFactory()
136: .getFileHandler();
137:
138: fileToGo = fr.getAsFile(name, generationId);
139:
140: if (fileToGo == null)
141: return false;
142:
143: return fileToGo.exists();
144: }
145:
146: public Compensation generateUndo(Transaction xact,
147: LimitObjectInput in) throws StandardException, IOException {
148:
149: if (fileToGo != null) {
150: BaseDataFileFactory bdff = (BaseDataFileFactory) ((RawTransaction) xact)
151: .getDataFactory();
152:
153: bdff.fileToRemove(fileToGo, false);
154: }
155:
156: return null;
157: }
158: }
|