41#include "src/jazz_elements/tuple.h"
44#ifndef INCLUDED_JAZZ_CATCH2
45#define INCLUDED_JAZZ_CATCH2
47#include "src/catch2/catch.hpp"
53#ifndef INCLUDED_JAZZ_ELEMENTS_CONTAINER
54#define INCLUDED_JAZZ_ELEMENTS_CONTAINER
60#define MAX_SIZE_OF_CELL_AS_TEXT 48
63#define NA_AS_TEXT {"NA\0"}
64#define LENGTH_NA_AS_TEXT 2
67#define PARSE_ERROR_UNEXPECTED_EOF 101
68#define PARSE_ERROR_UNEXPECTED_CHAR 102
69#define PARSE_ERROR_ITEM_NAME 103
70#define PARSE_ERROR_ITEM_NAME_MISMATCH 104
71#define PARSE_ERROR_TENSOR_EXPLORATION 105
73#define PARSE_ERROR_KIND_EXPLORATION 106
74#define PARSE_ERROR_TOO_MANY_ITEMS 107
75#define PARSE_ERROR_EXPECTED_EOF 108
76#define PARSE_ERROR_TENSOR_FILLING 109
77#define PARSE_ERROR_TEXT_FILLING 110
80#define FILL_NEW_DONT_FILL 0
81#define FILL_NEW_WITH_ZERO 1
82#define FILL_NEW_WITH_NA 2
83#define FILL_WITH_TEXTFILE 3
89#define BLOCK_STATUS_READY 0
90#define BLOCK_STATUS_EMPTY 1
91#define BLOCK_STATUS_DESTROYED 2
94#define LOCK_NUM_RETRIES_BEFORE_YIELD 100
97#define LOCK_WEIGHT_OF_WRITE 46341
100#define EIGHT_BIT_LONG 256
101#define MAX_TRANSITION_REGEX_LEN 32
102#define PSTATE_INVALID_CHAR 255
105#define WRITE_ONLY_IF_EXISTS 0x01
106#define WRITE_ONLY_IF_NOT_EXISTS 0x02
107#define WRITE_ANY_RESTRICTION 0x03
110#define WRITE_AS_BASE_DEFAULT 0x00
111#define WRITE_AS_STRING 0x04
112#define WRITE_AS_CONTENT 0x08
113#define WRITE_AS_FULL_BLOCK 0x10
114#define WRITE_AS_ANY_WRITE 0x1C
117#define TRIGGER_FAIL_TEXT_BLOCK 0x01
118#define TRIGGER_FAIL_FILL_TENSOR 0x02
119#define TRIGGER_FAIL_NEW_STRING_BLOCK 0x04
125 unsigned char next[EIGHT_BIT_LONG];
134 char rex[MAX_TRANSITION_REGEX_LEN];
313 int stringbuff_size = 0,
314 const char *p_text =
nullptr,
343 pKind p_as_kind =
nullptr,
349 pChar p_fmt =
nullptr,
350 bool ret_as_string =
false,
385 int mode = WRITE_AS_BASE_DEFAULT);
419 int mode = WRITE_AS_BASE_DEFAULT);
440 if (p_txn->
p_block->cell_type == CELL_TYPE_STRING)
442 return SERVICE_NO_ERROR;
446 return SERVICE_ERROR_WRONG_TYPE;
449 if ((p_txn->
p_block->cell_type != CELL_TYPE_BYTE) || (p_txn->
p_block->rank != 1)) {
452 return SERVICE_ERROR_WRONG_TYPE;
456 int size = p_txn->
p_block->size;
458 if (p_blk->total_bytes == size && p_blk->check_hash()) {
460 if (p_new ==
nullptr) {
463 return SERVICE_ERROR_NO_MEM;
465 memcpy(p_new, p_blk, size);
472 return SERVICE_NO_ERROR;
475 int block_len = strnlen((
pChar) p_blk, size);
476 if (block_len < size - 1)
477 return SERVICE_NO_ERROR;
481 reinterpret_cast <pChar>(p_blk)[size] = 0;
483 if (
new_block(p_aux, CELL_TYPE_STRING,
nullptr, FILL_WITH_TEXTFILE, 0, (
pChar) p_blk, 0) != SERVICE_NO_ERROR) {
486 return SERVICE_ERROR_NO_MEM;
492 return SERVICE_NO_ERROR;
508 if (rec_size >
sizeof(
BlockHeader) && p_maybe_block->total_bytes == rec_size && p_maybe_block->check_hash()) {
511 if (ret != SERVICE_NO_ERROR)
515 if (p_new ==
nullptr) {
518 return SERVICE_ERROR_NO_MEM;
520 memcpy(p_new, p_maybe_block, rec_size);
523 p_txn->
status = BLOCK_STATUS_READY;
525 return SERVICE_NO_ERROR;
527 if (strnlen((
pChar) p_maybe_block, rec_size) != rec_size) {
528 int dim[MAX_TENSOR_RANK] = {rec_size, 0};
529 int ret =
new_block(p_txn, CELL_TYPE_BYTE, dim, FILL_NEW_DONT_FILL);
531 if (ret == SERVICE_NO_ERROR)
532 memcpy(&p_txn->
p_block->tensor.cell_byte[0], p_maybe_block, rec_size);
537 return new_block(p_txn, CELL_TYPE_STRING,
nullptr, FILL_WITH_TEXTFILE, 0, (
pChar) p_maybe_block, 0);
557 void * ret = std::malloc(size);
573 if (p_blk !=
nullptr) {
574 p_blk->cell_type = 0;
575 p_blk->total_bytes = 0;
593 if (
_lock_.compare_exchange_weak(lock, next))
596 if (++retry > LOCK_NUM_RETRIES_BEFORE_YIELD) {
597 std::this_thread::yield();
600 if (--total_times == 0)
621 return (c < 65) ? c - 48 : (c > 96) ? c - 87 : c - 55;
638 char HEX[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
650 while (num_bytes > 0) {
654 if (*p_in ==
' ' || *p_in ==
'\t' || *p_in ==
'\n') {
694 bool check_quotes =
true,
695 bool check_colon =
true,
696 bool dot_ended =
false) {
700 if (check_quotes && (
get_char(p_in, num_bytes) !=
'"'))
703 char ch =
get_char(p_in, num_bytes);
705 if (ch <
'A' || ch >
'z' || (ch >
'Z' && ch <
'a'))
710 for (
int i = 1; i < NAME_LENGTH; i++) {
711 if (!check_quotes && (*p_in ==
',' || *p_in ==
']'))
714 if (dot_ended && (*p_in ==
'.'))
719 if ((check_quotes || dot_ended) && ch ==
'"')
722 if (ch <
'0' || ch >
'z' || (ch >
'9' && ch <
'A') || (ch >
'Z' && ch <
'_') || ch == 0x60)
730 if (check_quotes && (ch !=
'"') && (
get_char(p_in, num_bytes) !=
'"'))
737 if (
get_char(p_in, num_bytes) !=
':')
755 if (sscanf(p_in,
"%i%n", &result, &r_len) > 0) {
759 return num_bytes >= 0;
775 *(p_out++) = INTEGER_NA;
782 if (sscanf(p_st,
"%i", p_out) != 1)
801 *(p_out++) = LONG_INTEGER_NA;
808 if (sscanf(p_st,
"%lli", p_out) != 1)
827 *
reinterpret_cast<uint8_t *
>(p_out++) = BYTE_BOOLEAN_NA;
834 *(p_out++) = cell[0] ==
'1';
850 *(p_out++) = BOOLEAN_NA;
857 *(p_out++) = cell[0] ==
'1';
873 *(p_out++) = SINGLE_NA;
880 if (sscanf(p_st,
"%f", p_out) != 1)
899 *(p_out++) = DOUBLE_NA;
906 if (sscanf(p_st,
"%lf", p_out) != 1)
926 *(p_out++) = TIME_POINT_NA;
933 struct tm timeinfo = {0};
935 if (strptime(p_st, fmt, &timeinfo) ==
nullptr)
938 time_t xx = timegm(&timeinfo);
939 if (xx < 0)
return false;
967 for (
int i = 0; i < rank; i++)
985 for (
int i = 0; i < rank; i++) {
989 strcpy(p_ret, p_dim_name);
990 p_ret += strlen(p_dim_name);
993 p_ret += sprintf(p_ret,
"%i", k);
1016 *(p_dest++) =
HEX[bl >> 4];
1017 *(p_dest++) =
HEX[bl & 0x0f];
1028 for (
int i = rank_1; i >= 0; i--) {
1031 if (idx[i] == dim[i]) {
1038 for (
int j = 0; j < rank_1 - i; j++)
1058 for (
int i = rank_1; i >= 0; i--) {
1061 if (idx[i] == dim[i]) {
1079 uint32_t debug_trigger_failure = 0;
1092void compare_full_blocks(
pBlock p_bl1,
pBlock p_bl2,
bool skip_value_check =
false);
pStringBuffer p_string_buffer()
Definition block.h:363
A configuration file as a key/value store.
Definition utils.h:218
Container: A Service to manage Jazz blocks. All Jazz blocks are managed by this or a descendant of th...
Definition container.h:287
StatusCode start()
Definition container.cpp:292
bool fill_text_buffer(pChar &p_in, int &num_bytes, pChar p_out, int num_cells, int is_NA[], int hasLN[])
Definition container.cpp:2638
int tensor_float_as_text(pBlock p_block, pChar p_dest, pChar p_fmt)
Definition container.cpp:3623
bool push_bool_cell(pChar cell, pChar &p_st, uint32_t *&p_out)
Definition container.h:847
int tuple_as_text(pTuple p_tuple, pChar p_dest, pChar p_fmt, int item_len[])
Definition container.cpp:3968
int new_text_block(pTransaction &p_txn, ItemHeader &item_hea, pChar &p_in, int &num_bytes, AttributeMap *att=nullptr)
Definition container.cpp:3288
void leave_write(pTransaction p_txn)
Definition container.cpp:434
bool get_shape_and_size(pChar &p_in, int &num_bytes, int cell_type, ItemHeader *item_hea)
Definition container.cpp:2473
char get_char(pChar &p_in, int &num_bytes)
Definition container.h:671
virtual StatusCode put(pChar p_where, pBlock p_block, int mode=WRITE_AS_BASE_DEFAULT)
Definition container.cpp:1927
int skip_space(pChar &p_in, int &num_bytes)
Definition container.h:649
StatusCode destroy_container()
Definition container.cpp:2338
bool push_real_cell(pChar cell, pChar &p_st, double *&p_out)
Definition container.h:896
int tensor_string_as_text(pBlock p_block, pChar p_dest)
Definition container.cpp:3742
virtual StatusCode as_locator(Locator &result, pChar p_what)
Definition container.cpp:2046
virtual StatusCode remove(pChar p_where)
Definition container.cpp:1965
char HEX[16]
Hex chars.
Definition container.h:638
void unlock_container()
Definition container.h:609
bool push_bool_cell(pChar cell, pChar &p_st, bool *&p_out)
Definition container.h:824
void opening_brackets(int rank, pChar &p_ret)
Definition container.h:966
int separator_len(int rank_1, int dim[], int idx[])
Definition container.h:1055
void enter_read(pTransaction p_txn)
Definition container.cpp:339
int kind_as_text(pKind p_kind, pChar p_dest)
Definition container.cpp:4087
int tensor_bool_as_text(pBlock p_block, pChar p_dest)
Definition container.cpp:3518
virtual StatusCode locate(Locator &location, pChar p_what)
Definition container.cpp:1857
virtual StatusCode new_transaction(pTransaction &p_txn)
Definition container.cpp:449
virtual StatusCode new_entity(pChar p_where)
Definition container.cpp:1946
pTransaction p_free
The free list of transactions.
Definition container.h:629
int log_error_level
The log level for LMDB errors made a variable to silence it in tests.
Definition container.h:632
StatusCode unwrap_received(pTransaction &p_txn, pBlock p_maybe_block, int rec_size)
Definition container.h:506
int tensor_int_as_text(pBlock p_block, pChar p_dest, pChar p_fmt)
Definition container.cpp:3362
bool alloc_warning_issued
True if a warning was issued for over-allocation.
Definition container.h:630
bool sscanf_int32(pChar &p_in, int &num_bytes, int &result)
Definition container.h:752
StatusCode new_container()
Definition container.cpp:2296
Lock32 _lock_
A lock for the deque of transactions.
Definition container.h:631
virtual StatusCode exec(pTransaction &p_txn, Locator &function, pTuple p_args)
Definition container.cpp:2014
void separator(int rank_1, int dim[], int idx[], pChar &p_ret)
Definition container.h:1027
int max_transactions
The configured ONE_SHOT_MAX_TRANSACTIONS.
Definition container.h:624
bool push_real_cell(pChar cell, pChar &p_st, float *&p_out)
Definition container.h:870
uint64_t alloc_bytes
The current allocation in bytes.
Definition container.h:627
virtual StatusCode copy(pChar p_where, pChar p_what)
Definition container.cpp:1987
void enter_write(pTransaction p_txn, int total_times=0)
Definition container.cpp:369
void * malloc(size_t size)
Definition container.h:553
void base_names(BaseNames &base_names)
Definition container.cpp:2289
virtual StatusCode get(pTransaction &p_txn, pChar p_what)
Definition container.cpp:1784
int from_hex(char c)
Definition container.h:620
bool push_int_cell(pChar cell, pChar &p_st, int *&p_out)
Definition container.h:772
pBlock block_malloc(size_t size)
Definition container.h:570
bool push_time_cell(pChar cell, pChar &p_st, time_t *&p_out, pChar fmt)
Definition container.h:923
bool fill_tensor(pChar &p_in, int &num_bytes, pBlock p_block)
Definition container.cpp:2786
StatusCode new_block(pTransaction &p_txn, int cell_type, int *dim, int fill_tensor=FILL_NEW_DONT_FILL, int stringbuff_size=0, const char *p_text=nullptr, char eol='\n', AttributeMap *att=nullptr)
Definition container.cpp:568
void as_hex(pChar &p_dest, uint8_t bl)
Definition container.h:1013
pTransaction p_buffer
The buffer for the transactions.
Definition container.h:628
bool push_int_cell(pChar cell, pChar &p_st, long long *&p_out)
Definition container.h:798
bool get_item_name(pChar &p_in, int &num_bytes, pChar p_out, bool check_quotes=true, bool check_colon=true, bool dot_ended=false)
Definition container.h:691
uint64_t fail_alloc_bytes
Taken from ONE_SHOT_ERROR_BLOCK_KBYTES.
Definition container.h:626
int tensor_time_as_text(pBlock p_block, pChar p_dest, pChar p_fmt)
Definition container.cpp:3896
pChar as_shape(int rank, int dim[], pChar p_ret, pKind p_kind)
Definition container.h:982
virtual StatusCode header(StaticBlockHeader &hea, pChar p_what)
Definition container.cpp:1878
bool get_type_and_shape(pChar &p_in, int &num_bytes, ItemHeader *item_hea, MapSI &dims)
Definition container.cpp:2369
void lock_container(int total_times=0)
Definition container.h:587
uint64_t warn_alloc_bytes
Taken from ONE_SHOT_WARN_BLOCK_KBYTES.
Definition container.h:625
void leave_read(pTransaction p_txn)
Definition container.cpp:414
virtual StatusCode modify(Locator &function, pTuple p_args)
Definition container.cpp:2031
StatusCode shut_down()
Definition container.cpp:325
StatusCode unwrap_received(pTransaction &p_txn)
Definition container.h:438
virtual void destroy_transaction(pTransaction &p_txn)
Definition container.cpp:488
Kind: A type definition for Jazz Blocks and Tuples.
Definition kind.h:96
A simple logger.
Definition utils.h:248
A Jazz Service is a globally instanced configurable object that may allocate RAM.
Definition utils.h:288
Tuple: A Jazz Block with multiple Tensors.
Definition tuple.h:94
The namespace for Jazz Utils, Blocks, Kinds, Tuples, Containers, etc.
Definition block.cpp:39
Logger LOGGER(CONFIG, "LOGGER_PATH")
The logger.
Definition container.h:1088
struct ExtraLocator * pExtraLocator
A pointer to an ExtraLocator.
Definition container.h:179
ConfigFile CONFIG(JAZZ_DEFAULT_CONFIG_PATH)
The configuration file.
Definition container.h:1087
Transaction * pTransaction
A pointer to a Transaction.
Definition container.h:176
Locator * pLocator
A pointer to a Locator.
Definition container.h:199
std::map< String, pContainer > BaseNames
A map of names for the containers (or structure engines like "map" or "tree" inside Volatile).
Definition container.h:157
StoredTransaction * pStoredTransaction
A pointer to a StoredTransaction.
Definition container.h:206
char * pChar
A pointer to a char buffer.
Definition types.h:189
class Block * pBlock
A (forward defined) pointer to a Block.
Definition block.h:66
char Name[NAME_SIZE]
A short identifier used in Blocks, Containers and API.
Definition types.h:187
void compile_next_state_LUT(ParseNextStateLUT lut[], int num_states, ParseStateTransition trans[])
Definition container.cpp:59
std::map< String, int > MapSI
An internal map for managing dimension parsing.
Definition container.h:210
std::atomic< int32_t > Lock32
An atomically increased (via fetch_add() and fetch_sub()) 32 bit signed integer to use as a lock.
Definition container.h:149
class Container * pContainer
A (forward defined) pointer to a Container.
Definition container.h:153
int StatusCode
Type returned by the Service API.
Definition utils.h:142
std::map< int, const char * > AttributeMap
An stdlib map to store all the attributes of a Block at the same time used by the some Block methods.
Definition block.h:63
std::map< String, String > Index
An Index kept in RAM by Volatile implemented as an stdlib map (string, string)
Definition types.h:243
Locator: A minimal structure to define the location of resources inside a Container.
Definition container.h:189
char base[SHORT_NAME_SIZE]
A Jazz node level unique name to locate a Container and possibly a type of service inside it.
Definition container.h:190
Name entity
Another abstraction inside node.container.base, like the name of a table in a database.
Definition container.h:191
Name key
A key identifying a block inside the entity.
Definition container.h:192
int attribute
Used by API to store the attribute.
Definition container.h:195
pExtraLocator p_extra
A pointer to extend this structure with Container specific data (like URLs, cookies,...
Definition container.h:196
A lookup table for all the possible values of a char mapped into an 8-bit state.
Definition container.h:124
unsigned char next[EIGHT_BIT_LONG]
The next state for each possible char.
Definition container.h:125
A way to build constants defining the transition from one state to the next via a regex.
Definition container.h:131
int from
The state to transition from.
Definition container.h:132
char rex[MAX_TRANSITION_REGEX_LEN]
The regex that, when matched, the transition applies.
Definition container.h:134
int to
The state to transition to.
Definition container.h:133
An internal (for Container) Transaction with pointers for a deque.
Definition container.h:203
StoredTransaction * p_next
A pointer to the next StoredTransaction.
Definition container.h:204
char buffer[]
The buffer where strings are stored starting with two zeroes for STRING_NA & STRING_EMPTY.
Definition types.h:292
Transaction: A wrapper over a Block that defines the communication of a block with a Container.
Definition container.h:167
pBlockHeader p_hea
A pointer to the Block (if status == BLOCK_STATUS_READY) for Index.
Definition container.h:170
Lock32 _lock_
An atomically updated int to lock the Transaction to support modifying the Block.
Definition container.h:172
int status
The status of the block transaction.
Definition container.h:173
pBlock p_block
A pointer to the Block (if status == BLOCK_STATUS_READY) for Tensor, Kind and Tuple.
Definition container.h:169
pContainer p_owner
A pointer to the Container instance serving API calls related to this block.
Definition container.h:174