Jazz 1.25.+
Loading...
Searching...
No Matches
utils.h
1/* Jazz (c) 2018-2025 kaalam.ai (The Authors of Jazz), using (under the same license):
2
3 1. Biomodelling - The AATBlockQueue class (c) Jacques BasaldĂșa, 2009-2012 licensed
4 exclusively for the use in the Jazz server software.
5
6 Copyright 2009-2012 Jacques BasaldĂșa
7
8 2. BBVA - Jazz: A lightweight analytical web server for data-driven applications.
9
10 Copyright 2016-2017 Banco Bilbao Vizcaya Argentaria, S.A.
11
12 This product includes software developed at
13
14 BBVA (https://www.bbva.com/)
15
16 3. LMDB, Copyright 2011-2017 Howard Chu, Symas Corp. All rights reserved.
17
18 Licensed under http://www.OpenLDAP.org/license.html
19
20
21 Licensed under the Apache License, Version 2.0 (the "License");
22 you may not use this file except in compliance with the License.
23 You may obtain a copy of the License at
24
25 http://www.apache.org/licenses/LICENSE-2.0
26
27 Unless required by applicable law or agreed to in writing, software
28 distributed under the License is distributed on an "AS IS" BASIS,
29 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 See the License for the specific language governing permissions and
31 limitations under the License.
32*/
33
34
35#include <chrono>
36#include <iostream>
37#include <fstream>
38#include <map>
39
40#include <string.h>
41
42#include <unistd.h>
43#include <dirent.h>
44
45#include <sys/syscall.h>
46#include <stdarg.h>
47
48
49#include "src/jazz_elements/types.h"
50
51#ifdef CATCH_TEST
52#ifndef INCLUDED_JAZZ_CATCH2
53#define INCLUDED_JAZZ_CATCH2
54
55#include "src/catch2/catch.hpp"
56
57#endif
58#endif
59
60
61#ifndef INCLUDED_JAZZ_ELEMENTS_UTILS
62#define INCLUDED_JAZZ_ELEMENTS_UTILS
63
64
65namespace jazz_elements
66{
67
68/* Miscellaneous utility functions for Jazz.
69
70 This module defines many unrelated functions needed by Jazz. The only rule is: functions and classes
71without global variables.
72*/
73
74#define TENBITS_LUT_SIZE 1024
75
76
81#define LOG_DEBUG 1
83#define LOG_INFO 2
85#define LOG_MISS 3
87#define LOG_WARN 4
89#define LOG_ERROR 5
90
92#define MAX_FILENAME_LENGTH 256
93
94#define MAX_BLOCK_SIZE 0x40000000
95
98#define SERVICE_NO_ERROR 0
99#define SERVICE_NOT_IMPLEMENTED -1
100#define SERVICE_ERROR_BAD_CONFIG -2
101#define SERVICE_ERROR_STARTING -3
102#define SERVICE_ERROR_NO_MEM -4
103#define SERVICE_ERROR_LOADING_STAT -5
104#define SERVICE_ERROR_NEW_BLOCK_ARGS -6
105#define SERVICE_ERROR_WRONG_TYPE -7
106#define SERVICE_ERROR_WRONG_NAME -8
107#define SERVICE_ERROR_WRONG_ARGUMENTS -9
108#define SERVICE_ERROR_BAD_BLOCK -10
109#define SERVICE_ERROR_BAD_NEW_KIND -11
110#define SERVICE_ERROR_BAD_KIND_ADD -12
111#define SERVICE_ERROR_WRITE_FORBIDDEN -13
112#define SERVICE_ERROR_WRITE_FAILED -14
113#define SERVICE_ERROR_REMOVE_FAILED -15
114#define SERVICE_ERROR_BLOCK_NOT_FOUND -16
115#define SERVICE_ERROR_CREATE_FAILED -17
116#define SERVICE_ERROR_PARSING_NAMES -18
117#define SERVICE_ERROR_PARSING_NUMBERS -19
118#define SERVICE_ERROR_TOO_DEEP -20
119#define SERVICE_ERROR_WRONG_BASE -21
120#define SERVICE_ERROR_PARSING_COMMAND -22
121#define SERVICE_ERROR_EMPTY_ENTITY -23
122#define SERVICE_ERROR_ENTITY_NOT_FOUND -24
123#define SERVICE_ERROR_LOW_PRIORITY -25
124#define SERVICE_ERROR_PARENT_NOT_FOUND -26
125#define SERVICE_ERROR_TOO_MANY_ENTITIES -27
126#define SERVICE_ERROR_NOT_APPLICABLE -28
127#define SERVICE_ERROR_IO_ERROR -29
128#define SERVICE_ERROR_BLOCK_TOO_BIG -30
129#define SERVICE_ERROR_BASE_FORBIDDEN -31
130#define SERVICE_ERROR_READ_FORBIDDEN -32
131#define SERVICE_ERROR_NOT_READY -33
132#define SERVICE_ERROR_MISC_SERVER -34
133#define SERVICE_ERROR_UNKNOWN_JAZZNODE -35
134#define SERVICE_ERROR_CORRUPTED -36
135
138#define JAZZ_DEFAULT_CONFIG_PATH "config/jazz_config.ini"
139
141typedef int StatusCode;
142
143
144bool FileExists (const char* file_name);
145char *ExpandEscapeSequences(char *buff);
146pid_t FindProcessIdByName (const char *name);
147uint64_t MurmurHash64A (const void *key, int len);
148std::string CleanConfigArgument (std::string s);
149
150
152typedef int TenBitIntLUT[TENBITS_LUT_SIZE];
153
154
156typedef void* TenBitPtrLUT[TENBITS_LUT_SIZE];
157
158
166inline int TenBitsAtAddress(const char* str) {
167 return ((str[1] & 0x1f) << 5) | (str[0] & 0x1F);
168}
169
170
176inline int64_t elapsed_mu_sec(TimePoint big_bang) {
177 TimePoint now = std::chrono::steady_clock::now();
178
179 return std::chrono::duration_cast<std::chrono::microseconds>(now - big_bang).count();
180}
181
182
188inline bool valid_name(pChar p_name) {
189 char ch = p_name++[0];
190
191 if (!ch)
192 return false;
193
194 if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a'))
195 return false;
196
197 for (int i = 1; i < NAME_SIZE; i++) {
198 char ch = p_name++[0];
199
200 if (!ch)
201 return true;
202
203 if (ch < '0' || ch > 'z' || (ch > '9' && ch < 'A') || (ch > 'Z' && ch < '_') || ch == 0x60)
204 return false;
205 }
206 return false;
207}
208
209
218
219 public:
220
221 ConfigFile(const char *input_file_name);
222
223 bool load_config(const char *input_file_name);
224
225 int num_keys();
226
227 bool get_key(const char *key, int &value);
228 bool get_key(const char *key, double &value);
229 bool get_key(const char *key, std::string &value);
230
231 void debug_put(const std::string key, const std::string val);
232
233 private:
234
235 std::map<std::string, std::string> config;
236};
238
239
245class Logger {
246
247 public:
248
249 Logger(const char *output_file_name);
250 Logger( ConfigFile config,
251 const char *config_key);
252 ~Logger();
253
254 int get_output_file_name(char *buff, int buff_size);
255
256 void log (int loglevel, const char *message);
257 void log_printf(int loglevel, const char *fmt, ...);
258 void log_printf(int loglevel, const char *fmt, va_list args);
259
260#ifdef CATCH_TEST
261 bool SkipLogOnce;
262#endif
263
264 private:
265
266 void InitLogger();
267
268 char file_name [MAX_FILENAME_LENGTH];
269 std::ofstream f_stream;
270 std::filebuf *f_buff;
272};
273typedef Logger *pLogger;
274
275
285class Service {
286
287 public:
288
289 Service(pLogger a_logger,
290 pConfigFile a_config);
291
293 virtual StatusCode start ();
294 virtual StatusCode shut_down();
295
296 virtual pChar const id();
297
305 inline void log(int loglevel, const char *message) { if (p_log != nullptr) p_log->log(loglevel, message); }
306
315 inline void log_printf(int loglevel, const char *fmt, ...) {
316 if (p_log != nullptr) {
317 va_list args;
318 va_start(args, fmt);
319 p_log->log_printf(loglevel, fmt, args);
320 va_end(args);
321 }
322 }
323
332 bool get_conf_key(const char *key, int &value) {
333 if (p_conf != nullptr) return p_conf->get_key(key, value); else return false; }
334
343 bool get_conf_key(const char *key, double &value) {
344 if (p_conf != nullptr) return p_conf->get_key(key, value); else return false; }
345
354 bool get_conf_key(const char *key, std::string &value) {
355 if (p_conf != nullptr) return p_conf->get_key(key, value); else return false; }
356
359};
361
362} // namespace jazz_elements
363
364#endif // ifndef INCLUDED_JAZZ_ELEMENTS_UTILS
A configuration file as a key/value store.
Definition utils.h:217
int num_keys()
Definition utils.cpp:380
std::map< std::string, std::string > config
The configuration key/value store.
Definition utils.h:235
bool get_key(const char *key, int &value)
Definition utils.cpp:391
bool load_config(const char *input_file_name)
Definition utils.cpp:339
void debug_put(const std::string key, const std::string val)
Definition utils.cpp:470
A simple logger.
Definition utils.h:245
void log_printf(int loglevel, const char *fmt,...)
Definition utils.cpp:623
void log(int loglevel, const char *message)
Definition utils.cpp:576
TimePoint big_bang
The time when the logger was created.
Definition utils.h:271
void InitLogger()
Definition utils.cpp:515
std::filebuf * f_buff
The buffer for the stream.
Definition utils.h:270
char file_name[MAX_FILENAME_LENGTH]
The name of the log file.
Definition utils.h:268
~Logger()
Definition utils.cpp:540
int get_output_file_name(char *buff, int buff_size)
Definition utils.cpp:553
std::ofstream f_stream
The stream to the log file.
Definition utils.h:269
A Jazz Service is a globally instanced configurable object that may allocate RAM.
Definition utils.h:285
bool get_conf_key(const char *key, int &value)
Definition utils.h:332
void log(int loglevel, const char *message)
Definition utils.h:305
bool get_conf_key(const char *key, std::string &value)
Definition utils.h:354
void log_printf(int loglevel, const char *fmt,...)
Definition utils.h:315
pLogger p_log
The logger.
Definition utils.h:357
virtual pChar const id()
Definition utils.cpp:671
bool get_conf_key(const char *key, double &value)
Definition utils.h:343
virtual StatusCode start()
A simple start()/shut_down() interface (Restart is: shut_down(TRUE):start())
Definition utils.cpp:683
pConfigFile p_conf
The configuration file.
Definition utils.h:358
virtual StatusCode shut_down()
Definition utils.cpp:698
The namespace for Jazz Utils, Blocks, Kinds, Tuples, Containers, etc.
Definition block.cpp:39
Service * pService
A pointer to a Service object.
Definition utils.h:360
int TenBitIntLUT[TENBITS_LUT_SIZE]
A lookup table for all the possible results of a TenBitsAtAddress() call -> integer.
Definition utils.h:152
int TenBitsAtAddress(const char *str)
Get ten bits taking the least significant 5 of the first two characters of a string.
Definition utils.h:166
uint64_t MurmurHash64A(const void *key, int len)
MurmurHash2, 64-bit versions, by Austin Appleby.
Definition utils.cpp:250
bool valid_name(pChar p_name)
Check if a name is valid (Without using the regex).
Definition utils.h:188
Logger * pLogger
A pointer to a Logger object.
Definition utils.h:273
char * pChar
A pointer to a char buffer.
Definition types.h:185
std::string CleanConfigArgument(std::string s)
Remove quotes and (space and tab) outside quotes from a string.
Definition utils.cpp:301
void * TenBitPtrLUT[TENBITS_LUT_SIZE]
A lookup table for all the possible results of a TenBitsAtAddress() call -> pointer.
Definition utils.h:156
bool FileExists(const char *file_name)
Check if a file exists.
Definition utils.cpp:46
std::chrono::steady_clock::time_point TimePoint
A time point stored as 8 bytes.
Definition types.h:172
ConfigFile * pConfigFile
A pointer to a ConfigFile object.
Definition utils.h:237
char * ExpandEscapeSequences(char *buff)
Expand escaped strings at run-time.
Definition utils.cpp:81
int StatusCode
Type returned by the Service API.
Definition utils.h:141
pid_t FindProcessIdByName(const char *name)
Find the pid of a process given its name.
Definition utils.cpp:181
int64_t elapsed_mu_sec(TimePoint big_bang)
Return server running time in microseconds as a 64 bit integer.
Definition utils.h:176