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.hdfs.server.namenode;
019
020import org.apache.hadoop.fs.StorageType;
021import org.apache.hadoop.hdfs.util.EnumCounters;
022
023/**
024 * The counter to be computed for content types such as file, directory and symlink,
025 * and the storage type usage such as SSD, DISK, ARCHIVE.
026 */
027public class ContentCounts {
028  private EnumCounters<Content> contents;
029  private EnumCounters<StorageType> types;
030
031  public static class Builder {
032    private EnumCounters<Content> contents;
033    // storage spaces used by corresponding storage types
034    private EnumCounters<StorageType> types;
035
036    public Builder() {
037      contents = new EnumCounters<Content>(Content.class);
038      types = new EnumCounters<StorageType>(StorageType.class);
039    }
040
041    public Builder file(long file) {
042      contents.set(Content.FILE, file);
043      return this;
044    }
045
046    public Builder directory(long directory) {
047      contents.set(Content.DIRECTORY, directory);
048      return this;
049    }
050
051    public Builder symlink(long symlink) {
052      contents.set(Content.SYMLINK, symlink);
053      return this;
054    }
055
056    public Builder length(long length) {
057      contents.set(Content.LENGTH, length);
058      return this;
059    }
060
061    public Builder storagespace(long storagespace) {
062      contents.set(Content.DISKSPACE, storagespace);
063      return this;
064    }
065
066    public Builder snapshot(long snapshot) {
067      contents.set(Content.SNAPSHOT, snapshot);
068      return this;
069    }
070
071    public Builder snapshotable_directory(long snapshotable_directory) {
072      contents.set(Content.SNAPSHOTTABLE_DIRECTORY, snapshotable_directory);
073      return this;
074    }
075
076    public ContentCounts build() {
077      return new ContentCounts(contents, types);
078    }
079  }
080
081  private ContentCounts(EnumCounters<Content> contents,
082      EnumCounters<StorageType> types) {
083    this.contents = contents;
084    this.types = types;
085  }
086
087  // Get the number of files.
088  public long getFileCount() {
089    return contents.get(Content.FILE);
090  }
091
092  // Get the number of directories.
093  public long getDirectoryCount() {
094    return contents.get(Content.DIRECTORY);
095  }
096
097  // Get the number of symlinks.
098  public long getSymlinkCount() {
099    return contents.get(Content.SYMLINK);
100  }
101
102  // Get the total of file length in bytes.
103  public long getLength() {
104    return contents.get(Content.LENGTH);
105  }
106
107  // Get the total of storage space usage in bytes including replication.
108  public long getStoragespace() {
109    return contents.get(Content.DISKSPACE);
110  }
111
112  // Get the number of snapshots
113  public long getSnapshotCount() {
114    return contents.get(Content.SNAPSHOT);
115  }
116
117  // Get the number of snapshottable directories.
118  public long getSnapshotableDirectoryCount() {
119    return contents.get(Content.SNAPSHOTTABLE_DIRECTORY);
120  }
121
122  public long[] getTypeSpaces() {
123    return types.asArray();
124  }
125
126  public long getTypeSpace(StorageType t) {
127    return types.get(t);
128  }
129
130  public void addContent(Content c, long val) {
131    contents.add(c, val);
132  }
133
134  public void addContents(ContentCounts that) {
135    contents.add(that.contents);
136    types.add(that.types);
137  }
138
139  public void addTypeSpace(StorageType t, long val) {
140    types.add(t, val);
141  }
142
143  public void addTypeSpaces(EnumCounters<StorageType> that) {
144    this.types.add(that);
145  }
146}