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.DataInput;
021import java.io.DataOutput;
022import java.io.IOException;
023
024import org.apache.hadoop.classification.InterfaceAudience;
025import org.apache.hadoop.classification.InterfaceStability;
026import org.apache.hadoop.io.Writable;
027
028/** Store the summary of a content (a directory or a file). */
029@InterfaceAudience.Public
030@InterfaceStability.Evolving
031public class ContentSummary implements Writable{
032  private long length;
033  private long fileCount;
034  private long directoryCount;
035  private long quota;
036  private long spaceConsumed;
037  private long spaceQuota;
038  
039
040  /** Constructor */
041  public ContentSummary() {}
042  
043  /** Constructor */
044  public ContentSummary(long length, long fileCount, long directoryCount) {
045    this(length, fileCount, directoryCount, -1L, length, -1L);
046  }
047
048  /** Constructor */
049  public ContentSummary(
050      long length, long fileCount, long directoryCount, long quota,
051      long spaceConsumed, long spaceQuota) {
052    this.length = length;
053    this.fileCount = fileCount;
054    this.directoryCount = directoryCount;
055    this.quota = quota;
056    this.spaceConsumed = spaceConsumed;
057    this.spaceQuota = spaceQuota;
058  }
059
060  /** @return the length */
061  public long getLength() {return length;}
062
063  /** @return the directory count */
064  public long getDirectoryCount() {return directoryCount;}
065
066  /** @return the file count */
067  public long getFileCount() {return fileCount;}
068  
069  /** Return the directory quota */
070  public long getQuota() {return quota;}
071  
072  /** Retuns (disk) space consumed */ 
073  public long getSpaceConsumed() {return spaceConsumed;}
074
075  /** Returns (disk) space quota */
076  public long getSpaceQuota() {return spaceQuota;}
077  
078  /** {@inheritDoc} */
079  @InterfaceAudience.Private
080  public void write(DataOutput out) throws IOException {
081    out.writeLong(length);
082    out.writeLong(fileCount);
083    out.writeLong(directoryCount);
084    out.writeLong(quota);
085    out.writeLong(spaceConsumed);
086    out.writeLong(spaceQuota);
087  }
088
089  /** {@inheritDoc} */
090  @InterfaceAudience.Private
091  public void readFields(DataInput in) throws IOException {
092    this.length = in.readLong();
093    this.fileCount = in.readLong();
094    this.directoryCount = in.readLong();
095    this.quota = in.readLong();
096    this.spaceConsumed = in.readLong();
097    this.spaceQuota = in.readLong();
098  }
099  
100  /** 
101   * Output format:
102   * <----12----> <----12----> <-------18------->
103   *    DIR_COUNT   FILE_COUNT       CONTENT_SIZE FILE_NAME    
104   */
105  private static final String STRING_FORMAT = "%12d %12d %18d ";
106  /** 
107   * Output format:
108   * <----12----> <----15----> <----15----> <----15----> <----12----> <----12----> <-------18------->
109   *    QUOTA   REMAINING_QUATA SPACE_QUOTA SPACE_QUOTA_REM DIR_COUNT   FILE_COUNT   CONTENT_SIZE     FILE_NAME    
110   */
111  private static final String QUOTA_STRING_FORMAT = "%12s %15s ";
112  private static final String SPACE_QUOTA_STRING_FORMAT = "%15s %15s ";
113  
114  /** The header string */
115  private static final String HEADER = String.format(
116      STRING_FORMAT.replace('d', 's'), "directories", "files", "bytes");
117
118  private static final String QUOTA_HEADER = String.format(
119      QUOTA_STRING_FORMAT + SPACE_QUOTA_STRING_FORMAT, 
120      "quota", "remaining quota", "space quota", "reamaining quota") +
121      HEADER;
122  
123  /** Return the header of the output.
124   * if qOption is false, output directory count, file count, and content size;
125   * if qOption is true, output quota and remaining quota as well.
126   * 
127   * @param qOption a flag indicating if quota needs to be printed or not
128   * @return the header of the output
129   */
130  public static String getHeader(boolean qOption) {
131    return qOption ? QUOTA_HEADER : HEADER;
132  }
133  
134  /** {@inheritDoc} */
135  public String toString() {
136    return toString(true);
137  }
138
139  /** Return the string representation of the object in the output format.
140   * if qOption is false, output directory count, file count, and content size;
141   * if qOption is true, output quota and remaining quota as well.
142   * 
143   * @param qOption a flag indicating if quota needs to be printed or not
144   * @return the string representation of the object
145   */
146  public String toString(boolean qOption) {
147    String prefix = "";
148    if (qOption) {
149      String quotaStr = "none";
150      String quotaRem = "inf";
151      String spaceQuotaStr = "none";
152      String spaceQuotaRem = "inf";
153      
154      if (quota>0) {
155        quotaStr = Long.toString(quota);
156        quotaRem = Long.toString(quota-(directoryCount+fileCount));
157      }
158      if (spaceQuota>0) {
159        spaceQuotaStr = Long.toString(spaceQuota);
160        spaceQuotaRem = Long.toString(spaceQuota - spaceConsumed);        
161      }
162      
163      prefix = String.format(QUOTA_STRING_FORMAT + SPACE_QUOTA_STRING_FORMAT, 
164                             quotaStr, quotaRem, spaceQuotaStr, spaceQuotaRem);
165    }
166    
167    return prefix + String.format(STRING_FORMAT, directoryCount, 
168                                  fileCount, length);
169  }
170}