iceberg-cpp
Loading...
Searching...
No Matches
counter.h
Go to the documentation of this file.
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
24
25#include <atomic>
26#include <cstdint>
27#include <memory>
28#include <string_view>
29#include <utility>
30
32#include "iceberg/result.h"
34
35namespace iceberg {
36
38enum class CounterUnit {
39 kCount,
40 kBytes,
41 kUndefined,
42};
43
45ICEBERG_EXPORT constexpr std::string_view ToString(CounterUnit unit) noexcept {
46 switch (unit) {
47 case CounterUnit::kCount:
48 return "count";
49 case CounterUnit::kBytes:
50 return "bytes";
51 case CounterUnit::kUndefined:
52 return "undefined";
53 }
54 std::unreachable();
55}
56
61ICEBERG_EXPORT inline Result<CounterUnit> CounterUnitFromString(std::string_view s) {
62 auto unit = StringUtils::ToLower(s);
63 if (unit == "count") return CounterUnit::kCount;
64 if (unit == "bytes") return CounterUnit::kBytes;
65 if (unit == "undefined") return CounterUnit::kUndefined;
66 return InvalidArgument("Invalid unit: {}", s);
67}
68
70class ICEBERG_EXPORT Counter {
71 public:
72 virtual ~Counter() = default;
73
75 virtual void Increment() { Increment(1); }
76
78 virtual void Increment(int64_t amount) = 0;
79
81 virtual int64_t value() const = 0;
82
84 virtual CounterUnit unit() const { return CounterUnit::kCount; }
85
87 virtual bool IsNoop() const { return false; }
88
90 static std::shared_ptr<Counter> Noop();
91};
92
94class ICEBERG_EXPORT DefaultCounter : public Counter {
95 public:
96 explicit DefaultCounter(CounterUnit unit = CounterUnit::kCount);
97
98 using Counter::Increment;
99 void Increment(int64_t amount) override;
100 int64_t value() const override;
101 CounterUnit unit() const override { return unit_; }
102
103 private:
104 std::atomic<int64_t> count_{0};
105 CounterUnit unit_;
106};
107
108} // namespace iceberg
Abstract counter for tracking event totals.
Definition counter.h:70
virtual void Increment()
Increment the counter by 1.
Definition counter.h:75
static std::shared_ptr< Counter > Noop()
Return a shared no-op counter singleton.
virtual bool IsNoop() const
Return true if this counter is a no-op.
Definition counter.h:87
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:84
Thread-safe counter backed by std::atomic<int64_t>.
Definition counter.h:94
int64_t value() const override
Return the current count.
CounterUnit unit() const override
Return the unit for this counter.
Definition counter.h:101
void Increment(int64_t amount) override
Increment the counter by the given amount.
static std::string ToLower(std::string_view str)
Lower-case a UTF-8 string using Unicode simple (1:1) case mapping.
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
ICEBERG_EXPORT std::string_view ToString(Expression::Operation op)
Returns a string representation of an expression operation.
std::expected< T, E > Result
Result alias.
Definition result.h:88
CounterUnit
Unit for a Counter metric.
Definition counter.h:38
ICEBERG_EXPORT Result< CounterUnit > CounterUnitFromString(std::string_view s)
Parse a CounterUnit from a string.
Definition counter.h:61
Define Result, Status, and error helpers.
Provide string utility helpers.