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
33#include "iceberg/result.h"
35
36namespace iceberg {
37
39enum class CounterUnit {
40 kCount,
41 kBytes,
42 kUndefined,
43};
44
46ICEBERG_EXPORT constexpr std::string_view ToString(CounterUnit unit) noexcept {
47 switch (unit) {
48 case CounterUnit::kCount:
49 return "count";
50 case CounterUnit::kBytes:
51 return "bytes";
52 case CounterUnit::kUndefined:
53 return "undefined";
54 }
56}
57
62ICEBERG_EXPORT inline Result<CounterUnit> CounterUnitFromString(std::string_view s) {
63 auto unit = StringUtils::ToLower(s);
64 if (unit == "count") return CounterUnit::kCount;
65 if (unit == "bytes") return CounterUnit::kBytes;
66 if (unit == "undefined") return CounterUnit::kUndefined;
67 return InvalidArgument("Invalid unit: {}", s);
68}
69
71class ICEBERG_EXPORT Counter {
72 public:
73 virtual ~Counter() = default;
74
76 virtual void Increment() { Increment(1); }
77
79 virtual void Increment(int64_t amount) = 0;
80
82 virtual int64_t value() const = 0;
83
85 virtual CounterUnit unit() const { return CounterUnit::kCount; }
86
88 virtual bool IsNoop() const { return false; }
89
91 static std::shared_ptr<Counter> Noop();
92};
93
95class ICEBERG_EXPORT DefaultCounter : public Counter {
96 public:
97 explicit DefaultCounter(CounterUnit unit = CounterUnit::kCount);
98
99 using Counter::Increment;
100 void Increment(int64_t amount) override;
101 int64_t value() const override;
102 CounterUnit unit() const override { return unit_; }
103
104 private:
105 std::atomic<int64_t> count_{0};
106 CounterUnit unit_;
107};
108
109} // namespace iceberg
Abstract counter for tracking event totals.
Definition counter.h:71
virtual void Increment()
Increment the counter by 1.
Definition counter.h:76
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:88
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:85
Thread-safe counter backed by std::atomic<int64_t>.
Definition counter.h:95
int64_t value() const override
Return the current count.
CounterUnit unit() const override
Return the unit for this counter.
Definition counter.h:102
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.
Definition expected.h:1199
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
ICEBERG_EXPORT std::string_view ToString(Expression::Operation op)
Returns a string representation of an expression operation.
void unreachable() noexcept
Marks a branch the program can never take.
Definition unreachable.h:35
CounterUnit
Unit for a Counter metric.
Definition counter.h:39
ICEBERG_EXPORT Result< CounterUnit > CounterUnitFromString(std::string_view s)
Parse a CounterUnit from a string.
Definition counter.h:62
Define Result, Status, and error helpers.
Provide string utility helpers.
Portable stand-in for std::unreachable.