-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.c
More file actions
188 lines (162 loc) · 4.97 KB
/
bytes.c
File metadata and controls
188 lines (162 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2023 Mats Kindahl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <postgres.h>
#include <fmgr.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include "uint8.h"
#include <common/shortest_dec.h>
#include <utils/fmgrprotos.h>
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(bytes_in);
PG_FUNCTION_INFO_V1(bytes_out);
PG_FUNCTION_INFO_V1(bytes_round);
/* Unit enum with value also being the power-of-two for the
value. Note that the "UNIT_" prefix is used below. */
enum Unit {
UNIT_BASE,
UNIT_KILO,
UNIT_MEGA,
UNIT_GIGA,
UNIT_TERA,
UNIT_PETA,
UNIT_EXA,
MAX_UNIT
};
#define UNIT_SUFFIX(S, U) \
{ .str = #S, .unit = UNIT_##U, .len = sizeof(#S), }
/*
* Structure containing unit name, length, and enum. Note that we here
* use SI prefixes to denote base 2^10, not 10^3, so kB is 1024
* bytes. This is the same as pg_pretty_size uses.
*/
struct {
const char* str;
size_t len;
enum Unit unit;
} units[] = {
UNIT_SUFFIX(B, BASE),
UNIT_SUFFIX(bytes, BASE),
UNIT_SUFFIX(EB, EXA),
UNIT_SUFFIX(EiB, EXA),
UNIT_SUFFIX(GB, GIGA),
UNIT_SUFFIX(GiB, GIGA),
UNIT_SUFFIX(KB, KILO),
UNIT_SUFFIX(KiB, KILO),
UNIT_SUFFIX(MB, MEGA),
UNIT_SUFFIX(MiB, MEGA),
UNIT_SUFFIX(PB, PETA),
UNIT_SUFFIX(PiB, PETA),
UNIT_SUFFIX(TB, TERA),
UNIT_SUFFIX(TiB, TERA),
UNIT_SUFFIX(kB, KILO),
};
/*
* We use the same suffixes as pg_size_pretty() uses for the
* printout. This is not entirely correct since most disk vendors use
* KB and MB to denote 1000 and 1000000 bytes rather than 1024 and
* 1024*1024 bytes respectively (and in a similar manner for the other
* suffixes).
*/
const char* names[] = {
[UNIT_BASE] = "B",
[UNIT_KILO] = "KB",
[UNIT_MEGA] = "MB",
[UNIT_GIGA] = "GB",
[UNIT_TERA] = "TB",
[UNIT_PETA] = "PB",
[UNIT_EXA] = "EB",
};
typedef struct Bytes {
double num;
enum Unit unit;
} Bytes;
static inline Bytes bytes_apply(Bytes val, double (*f)(double)) {
return (Bytes){
.num = (*f)(val.num),
.unit = val.unit,
};
}
static Bytes uint64_get_bytes(uint64 bytes) {
Bytes val = {.num = bytes};
/* Reduce the precision until reducing it gives fractions on the
* base type, or we run out of units */
while (val.num >= 1024.0 && val.unit + 1 < sizeof(names) / sizeof(*names)) {
val.num /= 1024.0;
++val.unit;
}
return val;
}
static uint64 bytes_get_uint64(Bytes bytes) {
return trunc(bytes.num * exp2(10 * bytes.unit));
}
Datum bytes_in(PG_FUNCTION_ARGS) {
char* str = PG_GETARG_CSTRING(0);
double num;
char unit[16];
if (sscanf(str, "%lf%15s", &num, unit) != 2)
goto error;
for (int i = 0; i < sizeof(units) / sizeof(*units); ++i)
if (strncmp(units[i].str, unit, units[i].len) == 0) {
double value = trunc(num * exp2(10 * units[i].unit));
if (value >= exp2(CHAR_BIT * sizeof(uint64)))
ereport(ERROR,
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bytes value out of range"));
PG_RETURN_UINT64(value);
}
error:
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for size type: \"%s\"", str)));
}
static void double_with_unit(const Bytes* bytes, char* result) {
const int index = double_to_shortest_decimal_bufn(bytes->num, result);
result[index] = ' ';
strcpy(&result[index + 1], names[bytes->unit]);
}
Datum bytes_out(PG_FUNCTION_ARGS) {
const uint64 bytes = PG_GETARG_UINT64(0);
char* ascii = palloc(DOUBLE_SHORTEST_DECIMAL_LEN + 1 + 5);
const Bytes val = uint64_get_bytes(bytes);
double_with_unit(&val, ascii);
PG_RETURN_CSTRING(ascii);
}
Datum bytes_round(PG_FUNCTION_ARGS) {
const uint64 bytes = PG_GETARG_UINT64(0);
const struct Bytes val = bytes_apply(uint64_get_bytes(bytes), round);
PG_RETURN_UINT64(bytes_get_uint64(val));
}
/*
* Arithmetic function maker that will generate a function that
* forwards the call to another type.
*/
#define MAKE_FORWARD_FUNC(NAME, FWD, SFX) \
PG_FUNCTION_INFO_V1(NAME); \
Datum NAME(PG_FUNCTION_ARGS) { \
PG_RETURN_##SFX((FWD)(fcinfo)); \
} \
extern int no_such_variable
/*
* Arithmetic functions for bytes type.
*/
MAKE_FORWARD_FUNC(bytes_pl, uint8pl, UINT64);
MAKE_FORWARD_FUNC(bytes_mi, uint8mi, UINT64);
MAKE_FORWARD_FUNC(bytes_lt, uint8lt, UINT64);
MAKE_FORWARD_FUNC(bytes_gt, uint8gt, UINT64);
MAKE_FORWARD_FUNC(bytes_div, uint8div, UINT64);
MAKE_FORWARD_FUNC(bytes_mul, uint8mul, UINT64);