iceberg-cpp
Loading...
Searching...
No Matches
counter.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <atomic>
23#include <cstdint>
24#include <memory>
25#include <string_view>
26#include <utility>
27
28#include "iceberg/iceberg_export.h"
29#include "iceberg/result.h"
30#include "iceberg/util/string_util.h"
31
32namespace iceberg {
33
35enum class CounterUnit {
36 kCount,
37 kBytes,
38 kUndefined,
39};
40
42ICEBERG_EXPORT constexpr std::string_view ToString(CounterUnit unit) noexcept {
43 switch (unit) {
44 case CounterUnit::kCount:
45 return "count";
46 case CounterUnit::kBytes:
47 return "bytes";
48 case CounterUnit::kUndefined:
49 return "undefined";
50 }
51 std::unreachable();
52}
53
58ICEBERG_EXPORT inline Result<CounterUnit> CounterUnitFromString(std::string_view s) {
59 auto unit = StringUtils::ToLower(s);
60 if (unit == "count") return CounterUnit::kCount;
61 if (unit == "bytes") return CounterUnit::kBytes;
62 if (unit == "undefined") return CounterUnit::kUndefined;
63 return InvalidArgument("Invalid unit: {}", s);
64}
65
67class ICEBERG_EXPORT Counter {
68 public:
69 virtual ~Counter() = default;
70
72 virtual void Increment() { Increment(1); }
73
75 virtual void Increment(int64_t amount) = 0;
76
78 virtual int64_t value() const = 0;
79
81 virtual CounterUnit unit() const { return CounterUnit::kCount; }
82
84 virtual bool IsNoop() const { return false; }
85
87 static std::shared_ptr<Counter> Noop();
88};
89
91class ICEBERG_EXPORT DefaultCounter : public Counter {
92 public:
93 explicit DefaultCounter(CounterUnit unit = CounterUnit::kCount);
94
95 using Counter::Increment;
96 void Increment(int64_t amount) override;
97 int64_t value() const override;
98 CounterUnit unit() const override { return unit_; }
99
100 private:
101 std::atomic<int64_t> count_{0};
102 CounterUnit unit_;
103};
104
105} // namespace iceberg
Abstract counter for tracking event totals.
Definition counter.h:67
virtual void Increment()
Increment the counter by 1.
Definition counter.h:72
virtual bool IsNoop() const
Return true if this counter is a no-op.
Definition counter.h:84
virtual int64_t value() const =0
Return the current count.
virtual void Increment(int64_t amount)=0
Increment the counter by the given amount.
virtual CounterUnit unit() const
Return the unit for this counter.
Definition counter.h:81
Thread-safe counter backed by std::atomic<int64_t>.
Definition counter.h:91
CounterUnit unit() const override
Return the unit for this counter.
Definition counter.h:98