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 */
018
019package org.apache.hadoop.hdfs.server.namenode;
020
021import org.apache.hadoop.fs.StorageType;
022import org.apache.hadoop.hdfs.util.EnumCounters;
023
024/**
025 * Counters for namespace, storage space and storage type space quota and usage.
026 */
027public class QuotaCounts {
028  // Name space and storage space counts (HDFS-7775 refactors the original disk
029  // space count to storage space counts)
030  private EnumCounters<Quota> nsSsCounts;
031  // Storage type space counts
032  private EnumCounters<StorageType> tsCounts;
033
034  public static class Builder {
035    private EnumCounters<Quota> nsSsCounts;
036    private EnumCounters<StorageType> tsCounts;
037
038    public Builder() {
039      this.nsSsCounts = new EnumCounters<Quota>(Quota.class);
040      this.tsCounts = new EnumCounters<StorageType>(StorageType.class);
041    }
042
043    public Builder nameSpace(long val) {
044      this.nsSsCounts.set(Quota.NAMESPACE, val);
045      return this;
046    }
047
048    public Builder storageSpace(long val) {
049      this.nsSsCounts.set(Quota.STORAGESPACE, val);
050      return this;
051    }
052
053    public Builder typeSpaces(EnumCounters<StorageType> val) {
054      if (val != null) {
055        this.tsCounts.set(val);
056      }
057      return this;
058    }
059
060    public Builder typeSpaces(long val) {
061      this.tsCounts.reset(val);
062      return this;
063    }
064
065    public Builder quotaCount(QuotaCounts that) {
066      this.nsSsCounts.set(that.nsSsCounts);
067      this.tsCounts.set(that.tsCounts);
068      return this;
069    }
070
071    public QuotaCounts build() {
072      return new QuotaCounts(this);
073    }
074  }
075
076  private QuotaCounts(Builder builder) {
077    this.nsSsCounts = builder.nsSsCounts;
078    this.tsCounts = builder.tsCounts;
079  }
080
081  public void add(QuotaCounts that) {
082    this.nsSsCounts.add(that.nsSsCounts);
083    this.tsCounts.add(that.tsCounts);
084  }
085
086  public void subtract(QuotaCounts that) {
087    this.nsSsCounts.subtract(that.nsSsCounts);
088    this.tsCounts.subtract(that.tsCounts);
089  }
090
091  /**
092   * Returns a QuotaCounts whose value is {@code (-this)}.
093   *
094   * @return {@code -this}
095   */
096  public QuotaCounts negation() {
097    QuotaCounts ret = new QuotaCounts.Builder().quotaCount(this).build();
098    ret.nsSsCounts.negation();
099    ret.tsCounts.negation();
100    return ret;
101  }
102
103  public long getNameSpace(){
104    return nsSsCounts.get(Quota.NAMESPACE);
105  }
106
107  public void setNameSpace(long nameSpaceCount) {
108    this.nsSsCounts.set(Quota.NAMESPACE, nameSpaceCount);
109  }
110
111  public void addNameSpace(long nsDelta) {
112    this.nsSsCounts.add(Quota.NAMESPACE, nsDelta);
113  }
114
115  public long getStorageSpace(){
116    return nsSsCounts.get(Quota.STORAGESPACE);
117  }
118
119  public void setStorageSpace(long spaceCount) {
120    this.nsSsCounts.set(Quota.STORAGESPACE, spaceCount);
121  }
122
123  public void addStorageSpace(long dsDelta) {
124    this.nsSsCounts.add(Quota.STORAGESPACE, dsDelta);
125  }
126
127  public EnumCounters<StorageType> getTypeSpaces() {
128    EnumCounters<StorageType> ret =
129        new EnumCounters<StorageType>(StorageType.class);
130    ret.set(tsCounts);
131    return ret;
132  }
133
134  void setTypeSpaces(EnumCounters<StorageType> that) {
135    if (that != null) {
136      this.tsCounts.set(that);
137    }
138  }
139
140  long getTypeSpace(StorageType type) {
141    return this.tsCounts.get(type);
142  }
143
144  void setTypeSpace(StorageType type, long spaceCount) {
145    this.tsCounts.set(type, spaceCount);
146  }
147
148  public void addTypeSpace(StorageType type, long delta) {
149    this.tsCounts.add(type, delta);
150  }
151
152  public boolean anyNsSsCountGreaterOrEqual(long val) {
153    return nsSsCounts.anyGreaterOrEqual(val);
154  }
155
156  public boolean anyTypeSpaceCountGreaterOrEqual(long val) {
157    return tsCounts.anyGreaterOrEqual(val);
158  }
159
160  @Override
161  public boolean equals(Object obj) {
162    if (obj == this) {
163      return true;
164    } else if (obj == null || !(obj instanceof QuotaCounts)) {
165      return false;
166    }
167    final QuotaCounts that = (QuotaCounts)obj;
168    return this.nsSsCounts.equals(that.nsSsCounts)
169        && this.tsCounts.equals(that.tsCounts);
170  }
171
172  @Override
173  public int hashCode() {
174    assert false : "hashCode not designed";
175    return 42; // any arbitrary constant will do
176  }
177}