Jazz 1.26.+
Loading...
Searching...
No Matches
utils.h
1/* Jazz (c) 2018-2026 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#define SERVICE_ERROR_TRIGGERED -37
136
139#define JAZZ_DEFAULT_CONFIG_PATH "config/jazz_config.ini"
140
142typedef int StatusCode;
143
144
145bool FileExists (const char* file_name);
146char *ExpandEscapeSequences(char *buff);
147pid_t FindProcessIdByName (const char *name);
148uint64_t MurmurHash64A (const void *key, int len);
150
151
153typedef int TenBitIntLUT[TENBITS_LUT_SIZE];
154
155
157typedef void* TenBitPtrLUT[TENBITS_LUT_SIZE];
158
159
167inline int TenBitsAtAddress(const char* str) {
168 return ((str[1] & 0x1f) << 5) | (str[0] & 0x1F);
169}
170
171
177inline int64_t elapsed_mu_sec(TimePoint big_bang) {
178 TimePoint now = std::chrono::steady_clock::now();
179
180 return std::chrono::duration_cast<std::chrono::microseconds>(now - big_bang).count();
181}
182
183
189inline bool valid_name(pChar p_name) {
190 char ch = p_name++[0];
191
192 if (!ch)
193 return false;
194
195 if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a'))
196 return false;
197
198 for (int i = 1; i < NAME_SIZE; i++) {
199 char ch = p_name++[0];
200
201 if (!ch)
202 return true;
203
204 if (ch < '0' || ch > 'z' || (ch > '9' && ch < 'A') || (ch > 'Z' && ch < '_') || ch == 0x60)
205 return false;
206 }
207 return false;
208}
209
210
219
220 public:
221
222 ConfigFile(const char *input_file_name);
223
224 bool load_config(const char *input_file_name);
225
226 int num_keys();
227
228 bool get_key(const char *key, int &value);
229 bool get_key(const char *key, double &value);
230 bool get_key(const char *key, String &value);
231
232 void debug_put(const String key, const String val);
233
234#ifndef CATCH_TEST
235 private:
236#endif
237
238 std::map<String, String> config;
239};
241
242
248class Logger {
249
250 public:
251
252 Logger(const char *output_file_name);
253 Logger( ConfigFile config,
254 const char *config_key);
255 ~Logger();
256
257 int get_output_file_name(char *buff, int buff_size);
258
259 void log (int loglevel, const char *message);
260 void log_printf(int loglevel, const char *fmt, ...);
261 void log_printf(int loglevel, const char *fmt, va_list args);
262
263#ifdef CATCH_TEST
264 bool SkipLogOnce;
265#endif
266
267 private:
268
269 void InitLogger();
270
271 char file_name [MAX_FILENAME_LENGTH];
272 std::ofstream f_stream;
273 std::filebuf *f_buff;
275};
276typedef Logger *pLogger;
277
278
288class Service {
289
290 public:
291
292 Service(pLogger a_logger,
293 pConfigFile a_config);
294
296 virtual StatusCode start ();
297 virtual StatusCode shut_down();
298
299 virtual pChar const id();
300
308 inline void log(int loglevel, const char *message) { if (p_log != nullptr) p_log->log(loglevel, message); }
309
318 inline void log_printf(int loglevel, const char *fmt, ...) {
319 if (p_log != nullptr) {
320 va_list args;
321 va_start(args, fmt);
322 p_log->log_printf(loglevel, fmt, args);
323 va_end(args);
324 }
325 }
326
335 bool get_conf_key(const char *key, int &value) {
336 if (p_conf != nullptr) return p_conf->get_key(key, value); else return false; }
337
346 bool get_conf_key(const char *key, double &value) {
347 if (p_conf != nullptr) return p_conf->get_key(key, value); else return false; }
348
357 bool get_conf_key(const char *key, String &value) {
358 if (p_conf != nullptr) return p_conf->get_key(key, value); else return false; }
359
362};
364
365} // namespace jazz_elements
366
367#endif // ifndef INCLUDED_JAZZ_ELEMENTS_UTILS
A configuration file as a key/value store.
Definition utils.h:218
int num_keys()
Definition utils.cpp:380
void debug_put(const String key, const String val)
Definition utils.cpp:470
bool get_key(const char *key, int &value)
Definition utils.cpp:391
bool load_config(const char *input_file_name)
Definition utils.cpp:339
std::map< String, String > config
The configuration key/value store.
Definition utils.h:238
A simple logger.
Definition utils.h:248
void log_printf(int loglevel, const char *fmt,...)
Definition utils.cpp:618
void log(int loglevel, const char *message)
Definition utils.cpp:576
TimePoint big_bang
The time when the logger was created.
Definition utils.h:274
void InitLogger()
Definition utils.cpp:515
std::filebuf * f_buff
The buffer for the stream.
Definition utils.h:273
char file_name[MAX_FILENAME_LENGTH]
The name of the log file.
Definition utils.h:271
~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:272
A Jazz Service is a globally instanced configurable object that may allocate RAM.
Definition utils.h:288
bool get_conf_key(const char *key, String &value)
Definition utils.h:357
bool get_conf_key(const char *key, int &value)
Definition utils.h:335
void log(int loglevel, const char *message)
Definition utils.h:308
void log_printf(int loglevel, const char *fmt,...)
Definition utils.h:318
pLogger p_log
The logger.
Definition utils.h:360
virtual pChar const id()
Definition utils.cpp:666
bool get_conf_key(const char *key, double &value)
Definition utils.h:346
virtual StatusCode start()
A simple start()/shut_down() interface (Restart is: shut_down(TRUE):start())
Definition utils.cpp:678
pConfigFile p_conf
The configuration file.
Definition utils.h:361
virtual StatusCode shut_down()
Definition utils.cpp:693
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:363
int TenBitIntLUT[TENBITS_LUT_SIZE]
A lookup table for all the possible results of a TenBitsAtAddress() call -> integer.
Definition utils.h:153
std::string String
A standard string used in many other places in Jazz.
Definition types.h:239
int TenBitsAtAddress(const char *str)
Get ten bits taking the least significant 5 of the first two characters of a string.
Definition utils.h:167
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:189
Logger * pLogger
A pointer to a Logger object.
Definition utils.h:276
char * pChar
A pointer to a char buffer.
Definition types.h:189
void * TenBitPtrLUT[TENBITS_LUT_SIZE]
A lookup table for all the possible results of a TenBitsAtAddress() call -> pointer.
Definition utils.h:157
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:176
ConfigFile * pConfigFile
A pointer to a ConfigFile object.
Definition utils.h:240
String CleanConfigArgument(String s)
Remove quotes and (space and tab) outside quotes from a string.
Definition utils.cpp:301
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:142
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:177