001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.fs;
019
020import java.io.FileNotFoundException;
021import java.io.IOException;
022import java.util.EnumSet;
023
024import org.apache.hadoop.HadoopIllegalArgumentException;
025import org.apache.hadoop.classification.InterfaceAudience;
026import org.apache.hadoop.classification.InterfaceStability;
027
028/****************************************************************
029 * CreateFlag specifies the file create semantic. Users can combine flags like: <br>
030 * <code>
031 * EnumSet.of(CreateFlag.CREATE, CreateFlag.APPEND)
032 * <code>
033 * <p>
034 * 
035 * Use the CreateFlag as follows:
036 * <ol>
037 * <li> CREATE - to create a file if it does not exist, 
038 * else throw FileAlreadyExists.</li>
039 * <li> APPEND - to append to a file if it exists, 
040 * else throw FileNotFoundException.</li>
041 * <li> OVERWRITE - to truncate a file if it exists, 
042 * else throw FileNotFoundException.</li>
043 * <li> CREATE|APPEND - to create a file if it does not exist, 
044 * else append to an existing file.</li>
045 * <li> CREATE|OVERWRITE - to create a file if it does not exist, 
046 * else overwrite an existing file.</li>
047 * <li> SYNC_BLOCK - to force closed blocks to the disk device.
048 * In addition {@link Syncable#hsync()} should be called after each write,
049 * if true synchronous behavior is required.</li>
050 * <li> LAZY_PERSIST - Create the block on transient storage (RAM) if
051 * available.</li>
052 * <li> APPEND_NEWBLOCK - Append data to a new block instead of end of the last
053 * partial block.</li>
054 * </ol>
055 * 
056 * Following combinations are not valid and will result in
057 * {@link HadoopIllegalArgumentException}:
058 * <ol>
059 * <li> APPEND|OVERWRITE</li>
060 * <li> CREATE|APPEND|OVERWRITE</li>
061 * </ol>
062 *****************************************************************/
063@InterfaceAudience.Public
064@InterfaceStability.Evolving
065public enum CreateFlag {
066
067  /**
068   * Create a file. See javadoc for more description
069   * already exists
070   */
071  CREATE((short) 0x01),
072
073  /**
074   * Truncate/overwrite a file. Same as POSIX O_TRUNC. See javadoc for description.
075   */
076  OVERWRITE((short) 0x02),
077
078  /**
079   * Append to a file. See javadoc for more description.
080   */
081  APPEND((short) 0x04),
082
083  /**
084   * Force closed blocks to disk. Similar to POSIX O_SYNC. See javadoc for description.
085   */
086  SYNC_BLOCK((short) 0x08),
087
088  /**
089   * Create the block on transient storage (RAM) if available. If
090   * transient storage is unavailable then the block will be created
091   * on disk.
092   *
093   * HDFS will make a best effort to lazily write these files to persistent
094   * storage, however file contents may be lost at any time due to process/
095   * node restarts, hence there is no guarantee of data durability.
096   *
097   * This flag must only be used for intermediate data whose loss can be
098   * tolerated by the application.
099   */
100  LAZY_PERSIST((short) 0x10),
101
102  /**
103   * Append data to a new block instead of the end of the last partial block.
104   * This is only useful for APPEND.
105   */
106  NEW_BLOCK((short) 0x20),
107
108  /**
109   * Advise that a block replica NOT be written to the local DataNode where
110   * 'local' means the same host as the client is being run on.
111   */
112  @InterfaceAudience.LimitedPrivate({"HBase"})
113  NO_LOCAL_WRITE((short) 0x40);
114
115  private final short mode;
116
117  private CreateFlag(short mode) {
118    this.mode = mode;
119  }
120
121  short getMode() {
122    return mode;
123  }
124  
125  /**
126   * Validate the CreateFlag and throw exception if it is invalid
127   * @param flag set of CreateFlag
128   * @throws HadoopIllegalArgumentException if the CreateFlag is invalid
129   */
130  public static void validate(EnumSet<CreateFlag> flag) {
131    if (flag == null || flag.isEmpty()) {
132      throw new HadoopIllegalArgumentException(flag
133          + " does not specify any options");
134    }
135    final boolean append = flag.contains(APPEND);
136    final boolean overwrite = flag.contains(OVERWRITE);
137    
138    // Both append and overwrite is an error
139    if (append && overwrite) {
140      throw new HadoopIllegalArgumentException(
141          flag + "Both append and overwrite options cannot be enabled.");
142    }
143  }
144  
145  /**
146   * Validate the CreateFlag for create operation
147   * @param path Object representing the path; usually String or {@link Path}
148   * @param pathExists pass true if the path exists in the file system
149   * @param flag set of CreateFlag
150   * @throws IOException on error
151   * @throws HadoopIllegalArgumentException if the CreateFlag is invalid
152   */
153  public static void validate(Object path, boolean pathExists,
154      EnumSet<CreateFlag> flag) throws IOException {
155    validate(flag);
156    final boolean append = flag.contains(APPEND);
157    final boolean overwrite = flag.contains(OVERWRITE);
158    if (pathExists) {
159      if (!(append || overwrite)) {
160        throw new FileAlreadyExistsException("File already exists: "
161            + path.toString()
162            + ". Append or overwrite option must be specified in " + flag);
163      }
164    } else if (!flag.contains(CREATE)) {
165      throw new FileNotFoundException("Non existing file: " + path.toString()
166          + ". Create option is not specified in " + flag);
167    }
168  }
169
170  /**
171   * Validate the CreateFlag for the append operation. The flag must contain
172   * APPEND, and cannot contain OVERWRITE.
173   */
174  public static void validateForAppend(EnumSet<CreateFlag> flag) {
175    validate(flag);
176    if (!flag.contains(APPEND)) {
177      throw new HadoopIllegalArgumentException(flag
178          + " does not contain APPEND");
179    }
180  }
181}