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 0x001
118#define TRIGGER_FAIL_FILL_TENSOR 0x002
119#define TRIGGER_FAIL_NEW_STRING_BLOCK 0x004
120#define TRIGGER_FAIL_NEW_BLOCK_ANY 0x008
121#define TRIGGER_FAIL_NEW_BLOCK_1 0x010
122#define TRIGGER_FAIL_NEW_BLOCK_2 0x020
123#define TRIGGER_FAIL_NEW_BLOCK_3 0x040
124#define TRIGGER_FAIL_NEW_BLOCK_4 0x080
125#define TRIGGER_FAIL_NEW_BLOCK_5 0x100
126#define TRIGGER_FAIL_NEW_BLOCK_6 0x200
127#define TRIGGER_FAIL_NEW_BLOCK_7 0x400
128#define TRIGGER_FAIL_NEW_BLOCK_8 0x800
134 unsigned char next[EIGHT_BIT_LONG];
143 char rex[MAX_TRANSITION_REGEX_LEN];
322 int stringbuff_size = 0,
323 const char *p_text =
nullptr,
352 pKind p_as_kind =
nullptr,
358 pChar p_fmt =
nullptr,
359 bool ret_as_string =
false,
394 int mode = WRITE_AS_BASE_DEFAULT);
428 int mode = WRITE_AS_BASE_DEFAULT);
449 if (p_txn->
p_block->cell_type == CELL_TYPE_STRING)
451 return SERVICE_NO_ERROR;
455 return SERVICE_ERROR_WRONG_TYPE;
458 if ((p_txn->
p_block->cell_type != CELL_TYPE_BYTE) || (p_txn->
p_block->rank != 1)) {
461 return SERVICE_ERROR_WRONG_TYPE;
465 int size = p_txn->
p_block->size;
467 if (p_blk->total_bytes == size && p_blk->check_hash()) {
469 if (p_new ==
nullptr) {
472 return SERVICE_ERROR_NO_MEM;
474 memcpy(p_new, p_blk, size);
481 return SERVICE_NO_ERROR;
484 int block_len = strnlen((
pChar) p_blk, size);
485 if (block_len < size - 1)
486 return SERVICE_NO_ERROR;
490 reinterpret_cast <pChar>(p_blk)[size] = 0;
492 if (
new_block(p_aux, CELL_TYPE_STRING,
nullptr, FILL_WITH_TEXTFILE, 0, (
pChar) p_blk, 0) != SERVICE_NO_ERROR) {
495 return SERVICE_ERROR_NO_MEM;
501 return SERVICE_NO_ERROR;
517 if (rec_size >
sizeof(
BlockHeader) && p_maybe_block->total_bytes == rec_size && p_maybe_block->check_hash()) {
520 if (ret != SERVICE_NO_ERROR)
524 if (p_new ==
nullptr) {
527 return SERVICE_ERROR_NO_MEM;
529 memcpy(p_new, p_maybe_block, rec_size);
532 p_txn->
status = BLOCK_STATUS_READY;
534 return SERVICE_NO_ERROR;
536 if (strnlen((
pChar) p_maybe_block, rec_size) != rec_size) {
537 int dim[MAX_TENSOR_RANK] = {rec_size, 0};
538 int ret =
new_block(p_txn, CELL_TYPE_BYTE, dim, FILL_NEW_DONT_FILL);
540 if (ret == SERVICE_NO_ERROR)
541 memcpy(&p_txn->
p_block->tensor.cell_byte[0], p_maybe_block, rec_size);
546 return new_block(p_txn, CELL_TYPE_STRING,
nullptr, FILL_WITH_TEXTFILE, 0, (
pChar) p_maybe_block, 0);
566 void * ret = std::malloc(size);
582 if (p_blk !=
nullptr) {
583 p_blk->cell_type = 0;
584 p_blk->total_bytes = 0;
602 if (
_lock_.compare_exchange_weak(lock, next))
605 if (++retry > LOCK_NUM_RETRIES_BEFORE_YIELD) {
606 std::this_thread::yield();
609 if (--total_times == 0)
630 return (c < 65) ? c - 48 : (c > 96) ? c - 87 : c - 55;
647 char HEX[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
659 while (num_bytes > 0) {
663 if (*p_in ==
' ' || *p_in ==
'\t' || *p_in ==
'\n') {
703 bool check_quotes =
true,
704 bool check_colon =
true,
705 bool dot_ended =
false) {
709 if (check_quotes && (
get_char(p_in, num_bytes) !=
'"'))
712 char ch =
get_char(p_in, num_bytes);
714 if (ch <
'A' || ch >
'z' || (ch >
'Z' && ch <
'a'))
719 for (
int i = 1; i < NAME_LENGTH; i++) {
720 if (!check_quotes && (*p_in ==
',' || *p_in ==
']'))
723 if (dot_ended && (*p_in ==
'.'))
728 if ((check_quotes || dot_ended) && ch ==
'"')
731 if (ch <
'0' || ch >
'z' || (ch >
'9' && ch <
'A') || (ch >
'Z' && ch <
'_') || ch == 0x60)
739 if (check_quotes && (ch !=
'"') && (
get_char(p_in, num_bytes) !=
'"'))
746 if (
get_char(p_in, num_bytes) !=
':')
764 if (sscanf(p_in,
"%i%n", &result, &r_len) > 0) {
768 return num_bytes >= 0;
784 *(p_out++) = INTEGER_NA;
791 if (sscanf(p_st,
"%i", p_out) != 1)
810 *(p_out++) = LONG_INTEGER_NA;
817 if (sscanf(p_st,
"%lli", p_out) != 1)
836 *
reinterpret_cast<uint8_t *
>(p_out++) = BYTE_BOOLEAN_NA;
843 *(p_out++) = cell[0] ==
'1';
859 *(p_out++) = BOOLEAN_NA;
866 *(p_out++) = cell[0] ==
'1';
882 *(p_out++) = SINGLE_NA;
889 if (sscanf(p_st,
"%f", p_out) != 1)
908 *(p_out++) = DOUBLE_NA;
915 if (sscanf(p_st,
"%lf", p_out) != 1)
935 *(p_out++) = TIME_POINT_NA;
942 struct tm timeinfo = {0};
944 if (strptime(p_st, fmt, &timeinfo) ==
nullptr)
947 time_t xx = timegm(&timeinfo);
948 if (xx < 0)
return false;
976 for (
int i = 0; i < rank; i++)
994 for (
int i = 0; i < rank; i++) {
998 strcpy(p_ret, p_dim_name);
999 p_ret += strlen(p_dim_name);
1002 p_ret += sprintf(p_ret,
"%i", k);
1025 *(p_dest++) =
HEX[bl >> 4];
1026 *(p_dest++) =
HEX[bl & 0x0f];
1037 for (
int i = rank_1; i >= 0; i--) {
1040 if (idx[i] == dim[i]) {
1047 for (
int j = 0; j < rank_1 - i; j++)
1067 for (
int i = rank_1; i >= 0; i--) {
1070 if (idx[i] == dim[i]) {
1088 uint32_t debug_trigger_failure = 0;
1101void 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:296
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:2676
int tensor_float_as_text(pBlock p_block, pChar p_dest, pChar p_fmt)
Definition container.cpp:3661
bool push_bool_cell(pChar cell, pChar &p_st, uint32_t *&p_out)
Definition container.h:856
int tuple_as_text(pTuple p_tuple, pChar p_dest, pChar p_fmt, int item_len[])
Definition container.cpp:4006
int new_text_block(pTransaction &p_txn, ItemHeader &item_hea, pChar &p_in, int &num_bytes, AttributeMap *att=nullptr)
Definition container.cpp:3326
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:2511
char get_char(pChar &p_in, int &num_bytes)
Definition container.h:680
virtual StatusCode put(pChar p_where, pBlock p_block, int mode=WRITE_AS_BASE_DEFAULT)
Definition container.cpp:1965
int skip_space(pChar &p_in, int &num_bytes)
Definition container.h:658
StatusCode destroy_container()
Definition container.cpp:2376
bool push_real_cell(pChar cell, pChar &p_st, double *&p_out)
Definition container.h:905
int tensor_string_as_text(pBlock p_block, pChar p_dest)
Definition container.cpp:3780
virtual StatusCode as_locator(Locator &result, pChar p_what)
Definition container.cpp:2084
virtual StatusCode remove(pChar p_where)
Definition container.cpp:2003
char HEX[16]
Hex chars.
Definition container.h:647
void unlock_container()
Definition container.h:618
bool push_bool_cell(pChar cell, pChar &p_st, bool *&p_out)
Definition container.h:833
void opening_brackets(int rank, pChar &p_ret)
Definition container.h:975
int separator_len(int rank_1, int dim[], int idx[])
Definition container.h:1064
void enter_read(pTransaction p_txn)
Definition container.cpp:339
int kind_as_text(pKind p_kind, pChar p_dest)
Definition container.cpp:4125
int tensor_bool_as_text(pBlock p_block, pChar p_dest)
Definition container.cpp:3556
virtual StatusCode locate(Locator &location, pChar p_what)
Definition container.cpp:1895
virtual StatusCode new_transaction(pTransaction &p_txn)
Definition container.cpp:449
virtual StatusCode new_entity(pChar p_where)
Definition container.cpp:1984
pTransaction p_free
The free list of transactions.
Definition container.h:638
int log_error_level
The log level for Container and LMDB errors made a variable to silence it in tests.
Definition container.h:641
StatusCode unwrap_received(pTransaction &p_txn, pBlock p_maybe_block, int rec_size)
Definition container.h:515
int tensor_int_as_text(pBlock p_block, pChar p_dest, pChar p_fmt)
Definition container.cpp:3400
bool alloc_warning_issued
True if a warning was issued for over-allocation.
Definition container.h:639
bool sscanf_int32(pChar &p_in, int &num_bytes, int &result)
Definition container.h:761
StatusCode new_container()
Definition container.cpp:2334
Lock32 _lock_
A lock for the deque of transactions.
Definition container.h:640
virtual StatusCode exec(pTransaction &p_txn, Locator &function, pTuple p_args)
Definition container.cpp:2052
void separator(int rank_1, int dim[], int idx[], pChar &p_ret)
Definition container.h:1036
int max_transactions
The configured ONE_SHOT_MAX_TRANSACTIONS.
Definition container.h:633
bool push_real_cell(pChar cell, pChar &p_st, float *&p_out)
Definition container.h:879
uint64_t alloc_bytes
The current allocation in bytes.
Definition container.h:636
virtual StatusCode copy(pChar p_where, pChar p_what)
Definition container.cpp:2025
void enter_write(pTransaction p_txn, int total_times=0)
Definition container.cpp:369
void * malloc(size_t size)
Definition container.h:562
virtual void base_names(BaseNames &base_names)
Definition container.cpp:2327
virtual StatusCode get(pTransaction &p_txn, pChar p_what)
Definition container.cpp:1822
int from_hex(char c)
Definition container.h:629
bool push_int_cell(pChar cell, pChar &p_st, int *&p_out)
Definition container.h:781
pBlock block_malloc(size_t size)
Definition container.h:579
bool push_time_cell(pChar cell, pChar &p_st, time_t *&p_out, pChar fmt)
Definition container.h:932
bool fill_tensor(pChar &p_in, int &num_bytes, pBlock p_block)
Definition container.cpp:2824
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:1022
pTransaction p_buffer
The buffer for the transactions.
Definition container.h:637
bool push_int_cell(pChar cell, pChar &p_st, long long *&p_out)
Definition container.h:807
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:700
uint64_t fail_alloc_bytes
Taken from ONE_SHOT_ERROR_BLOCK_KBYTES.
Definition container.h:635
int tensor_time_as_text(pBlock p_block, pChar p_dest, pChar p_fmt)
Definition container.cpp:3934
pChar as_shape(int rank, int dim[], pChar p_ret, pKind p_kind)
Definition container.h:991
virtual StatusCode header(StaticBlockHeader &hea, pChar p_what)
Definition container.cpp:1916
bool get_type_and_shape(pChar &p_in, int &num_bytes, ItemHeader *item_hea, MapSI &dims)
Definition container.cpp:2407
void lock_container(int total_times=0)
Definition container.h:596
uint64_t warn_alloc_bytes
Taken from ONE_SHOT_WARN_BLOCK_KBYTES.
Definition container.h:634
void leave_read(pTransaction p_txn)
Definition container.cpp:414
virtual StatusCode modify(Locator &function, pTuple p_args)
Definition container.cpp:2069
StatusCode shut_down()
Definition container.cpp:325
StatusCode unwrap_received(pTransaction &p_txn)
Definition container.h:447
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:1097
struct ExtraLocator * pExtraLocator
A pointer to an ExtraLocator.
Definition container.h:188
ConfigFile CONFIG(JAZZ_DEFAULT_CONFIG_PATH)
The configuration file.
Definition container.h:1096
Transaction * pTransaction
A pointer to a Transaction.
Definition container.h:185
Locator * pLocator
A pointer to a Locator.
Definition container.h:208
std::map< String, pContainer > BaseNames
A map of names for the containers (or structure engines like "map" or "tree" inside Volatile).
Definition container.h:166
StoredTransaction * pStoredTransaction
A pointer to a StoredTransaction.
Definition container.h:215
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:219
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:158
class Container * pContainer
A (forward defined) pointer to a Container.
Definition container.h:162
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:198
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:199
Name entity
Another abstraction inside node.container.base, like the name of a table in a database.
Definition container.h:200
Name key
A key identifying a block inside the entity.
Definition container.h:201
int attribute
Used by API to store the attribute.
Definition container.h:204
pExtraLocator p_extra
A pointer to extend this structure with Container specific data (like URLs, cookies,...
Definition container.h:205
A lookup table for all the possible values of a char mapped into an 8-bit state.
Definition container.h:133
unsigned char next[EIGHT_BIT_LONG]
The next state for each possible char.
Definition container.h:134
A way to build constants defining the transition from one state to the next via a regex.
Definition container.h:140
int from
The state to transition from.
Definition container.h:141
char rex[MAX_TRANSITION_REGEX_LEN]
The regex that, when matched, the transition applies.
Definition container.h:143
int to
The state to transition to.
Definition container.h:142
An internal (for Container) Transaction with pointers for a deque.
Definition container.h:212
StoredTransaction * p_next
A pointer to the next StoredTransaction.
Definition container.h:213
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:176
pBlockHeader p_hea
A pointer to the Block (if status == BLOCK_STATUS_READY) for Index.
Definition container.h:179
Lock32 _lock_
An atomically updated int to lock the Transaction to support modifying the Block.
Definition container.h:181
int status
The status of the block transaction.
Definition container.h:182
pBlock p_block
A pointer to the Block (if status == BLOCK_STATUS_READY) for Tensor, Kind and Tuple.
Definition container.h:178
pContainer p_owner
A pointer to the Container instance serving API calls related to this block.
Definition container.h:183