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
61#define MAX_SIZE_OF_CELL_AS_TEXT 48
64#define NA_AS_TEXT {"NA\0"}
65#define LENGTH_NA_AS_TEXT 2
68#define PARSE_ERROR_UNEXPECTED_EOF 101
69#define PARSE_ERROR_UNEXPECTED_CHAR 102
70#define PARSE_ERROR_ITEM_NAME 103
71#define PARSE_ERROR_ITEM_NAME_MISMATCH 104
72#define PARSE_ERROR_TENSOR_EXPLORATION 105
74#define PARSE_ERROR_KIND_EXPLORATION 106
75#define PARSE_ERROR_TOO_MANY_ITEMS 107
76#define PARSE_ERROR_EXPECTED_EOF 108
77#define PARSE_ERROR_TENSOR_FILLING 109
78#define PARSE_ERROR_TEXT_FILLING 110
81#define FILL_NEW_DONT_FILL 0
82#define FILL_NEW_WITH_ZERO 1
83#define FILL_NEW_WITH_NA 2
84#define FILL_WITH_TEXTFILE 3
90#define BLOCK_STATUS_READY 0
91#define BLOCK_STATUS_EMPTY 1
92#define BLOCK_STATUS_DESTROYED 2
95#define LOCK_NUM_RETRIES_BEFORE_YIELD 100
98#define LOCK_WEIGHT_OF_WRITE 46341
101#define EIGHT_BIT_LONG 256
102#define MAX_TRANSITION_REGEX_LEN 32
103#define PSTATE_INVALID_CHAR 255
106#define WRITE_ONLY_IF_EXISTS 0x01
107#define WRITE_ONLY_IF_NOT_EXISTS 0x02
108#define WRITE_ANY_RESTRICTION 0x03
111#define WRITE_AS_BASE_DEFAULT 0x00
112#define WRITE_AS_STRING 0x04
113#define WRITE_AS_CONTENT 0x08
114#define WRITE_AS_FULL_BLOCK 0x10
115#define WRITE_AS_ANY_WRITE 0x1C
120 unsigned char next[EIGHT_BIT_LONG];
129 char rex[MAX_TRANSITION_REGEX_LEN];
205typedef std::map<std::string, int>
MapSI;
308 int stringbuff_size = 0,
309 const char *p_text =
nullptr,
338 pKind p_as_kind =
nullptr,
344 pChar p_fmt =
nullptr,
345 bool ret_as_string =
false,
380 int mode = WRITE_AS_BASE_DEFAULT);
414 int mode = WRITE_AS_BASE_DEFAULT);
432 int size = p_txn->
p_block->size;
434 if (p_blk->total_bytes == size && p_blk->check_hash()) {
436 if (p_new ==
nullptr) {
439 return SERVICE_ERROR_NO_MEM;
441 memcpy(p_new, p_blk, size);
448 return SERVICE_NO_ERROR;
450 if (strnlen((
pChar) p_blk, size) != size)
451 return SERVICE_NO_ERROR;
455 reinterpret_cast <pChar>(p_blk)[size] = 0;
457 if (
new_block(p_aux, CELL_TYPE_STRING,
nullptr, FILL_WITH_TEXTFILE, 0, (
pChar) p_blk, 0) != SERVICE_NO_ERROR) {
460 return SERVICE_ERROR_NO_MEM;
466 return SERVICE_NO_ERROR;
482 if (rec_size >
sizeof(
BlockHeader) && p_maybe_block->total_bytes == rec_size && p_maybe_block->check_hash()) {
485 if (ret != SERVICE_NO_ERROR)
489 if (p_new ==
nullptr) {
492 return SERVICE_ERROR_NO_MEM;
494 memcpy(p_new, p_maybe_block, rec_size);
497 p_txn->
status = BLOCK_STATUS_READY;
499 return SERVICE_NO_ERROR;
501 if (strnlen((
pChar) p_maybe_block, rec_size) != rec_size) {
502 int dim[MAX_TENSOR_RANK] = {rec_size, 0};
503 int ret =
new_block(p_txn, CELL_TYPE_BYTE, dim, FILL_NEW_DONT_FILL);
505 if (ret == SERVICE_NO_ERROR)
506 memcpy(&p_txn->
p_block->tensor.cell_byte[0], p_maybe_block, rec_size);
511 return new_block(p_txn, CELL_TYPE_STRING,
nullptr, FILL_WITH_TEXTFILE, 0, (
pChar) p_maybe_block, 0);
531 void * ret = std::malloc(size);
537 if ((uint64_t) ret & 0x7)
538 log(LOG_ERROR,
"Misaligned block !!");
552 if (p_blk !=
nullptr) {
553 p_blk->cell_type = 0;
554 p_blk->total_bytes = 0;
570 if (
_lock_.compare_exchange_weak(lock, next))
573 if (++retry > LOCK_NUM_RETRIES_BEFORE_YIELD) {
574 std::this_thread::yield();
594 return (c < 65) ? c - 48 : (c > 96) ? c - 87 : c - 55;
610 char HEX[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
622 while (num_bytes > 0) {
626 if (*p_in ==
' ' || *p_in ==
'\t' || *p_in ==
'\n') {
666 if (check_quotes && (
get_char(p_in, num_bytes) !=
'"'))
669 char ch =
get_char(p_in, num_bytes);
671 if (ch <
'A' || ch >
'z' || (ch >
'Z' && ch <
'a'))
676 for (
int i = 1; i < NAME_LENGTH; i++) {
677 if (!check_quotes && (*p_in ==
',' || *p_in ==
']'))
682 if (check_quotes && ch ==
'"')
685 if (ch <
'0' || ch >
'z' || (ch >
'9' && ch <
'A') || (ch >
'Z' && ch <
'_') || ch == 0x60)
693 if (check_quotes && (ch !=
'"') && (
get_char(p_in, num_bytes) !=
'"'))
700 if (
get_char(p_in, num_bytes) !=
':')
718 if (sscanf(p_in,
"%i%n", &result, &r_len) > 0) {
722 return num_bytes >= 0;
738 *(p_out++) = INTEGER_NA;
745 if (sscanf(p_st,
"%i", p_out) != 1)
764 *(p_out++) = LONG_INTEGER_NA;
771 if (sscanf(p_st,
"%lli", p_out) != 1)
790 *
reinterpret_cast<uint8_t *
>(p_out++) = BYTE_BOOLEAN_NA;
797 *(p_out++) = cell[0] ==
'1';
813 *(p_out++) = BOOLEAN_NA;
820 *(p_out++) = cell[0] ==
'1';
836 *(p_out++) = SINGLE_NA;
843 if (sscanf(p_st,
"%f", p_out) != 1)
862 *(p_out++) = DOUBLE_NA;
869 if (sscanf(p_st,
"%lf", p_out) != 1)
889 *(p_out++) = TIME_POINT_NA;
896 struct tm timeinfo = {0};
898 if (strptime(p_st, fmt, &timeinfo) ==
nullptr)
901 time_t xx = timegm(&timeinfo);
931 for (
int i = 0; i < rank; i++)
949 for (
int i = 0; i < rank; i++) {
953 strcpy(p_ret, p_dim_name);
954 p_ret += strlen(p_dim_name);
957 p_ret += sprintf(p_ret,
"%i", k);
980 *(p_dest++) =
HEX[bl >> 4];
981 *(p_dest++) =
HEX[bl & 0x0f];
992 for (
int i = rank_1; i >= 0; i--) {
995 if (idx[i] == dim[i]) {
1002 for (
int j = 0; j < rank_1 - i; j++)
1022 for (
int i = rank_1; i >= 0; i--) {
1025 if (idx[i] == dim[i]) {
1051void 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:217
Container: A Service to manage Jazz blocks. All Jazz blocks are managed by this or a descendant of th...
Definition container.h:282
StatusCode start()
Definition container.cpp:300
bool fill_text_buffer(pChar &p_in, int &num_bytes, pChar p_out, int num_cells, int is_NA[], int hasLN[])
Definition container.cpp:2636
int tensor_float_as_text(pBlock p_block, pChar p_dest, pChar p_fmt)
Definition container.cpp:3633
bool push_bool_cell(pChar cell, pChar &p_st, uint32_t *&p_out)
Definition container.h:810
int new_text_block(pTransaction &p_txn, ItemHeader &item_hea, pChar &p_in, int &num_bytes, AttributeMap *att=nullptr)
Definition container.cpp:3294
void leave_write(pTransaction p_txn)
Definition container.cpp:433
bool get_shape_and_size(pChar &p_in, int &num_bytes, int cell_type, ItemHeader *item_hea)
Definition container.cpp:2466
char get_char(pChar &p_in, int &num_bytes)
Definition container.h:643
virtual StatusCode put(pChar p_where, pBlock p_block, int mode=WRITE_AS_BASE_DEFAULT)
Definition container.cpp:1903
int skip_space(pChar &p_in, int &num_bytes)
Definition container.h:621
int tensor_kind_as_text(pKind p_kind, pChar p_dest)
Definition container.cpp:4099
StatusCode destroy_container()
Definition container.cpp:2321
bool push_real_cell(pChar cell, pChar &p_st, double *&p_out)
Definition container.h:859
int tensor_string_as_text(pBlock p_block, pChar p_dest)
Definition container.cpp:3752
virtual StatusCode as_locator(Locator &result, pChar p_what)
Definition container.cpp:2022
virtual StatusCode remove(pChar p_where)
Definition container.cpp:1941
char HEX[16]
Hex chars.
Definition container.h:610
void unlock_container()
Definition container.h:582
void lock_container()
Definition container.h:564
bool push_bool_cell(pChar cell, pChar &p_st, bool *&p_out)
Definition container.h:787
void opening_brackets(int rank, pChar &p_ret)
Definition container.h:930
int separator_len(int rank_1, int dim[], int idx[])
Definition container.h:1019
bool get_item_name(pChar &p_in, int &num_bytes, pChar p_out, bool check_quotes=true, bool check_colon=true)
Definition container.h:662
void enter_read(pTransaction p_txn)
Definition container.cpp:347
int tensor_bool_as_text(pBlock p_block, pChar p_dest)
Definition container.cpp:3528
virtual StatusCode locate(Locator &location, pChar p_what)
Definition container.cpp:1833
virtual StatusCode new_transaction(pTransaction &p_txn)
Definition container.cpp:454
virtual StatusCode new_entity(pChar p_where)
Definition container.cpp:1922
pTransaction p_free
The free list of transactions.
Definition container.h:602
StatusCode unwrap_received(pTransaction &p_txn, pBlock p_maybe_block, int rec_size)
Definition container.h:480
int tensor_int_as_text(pBlock p_block, pChar p_dest, pChar p_fmt)
Definition container.cpp:3372
bool alloc_warning_issued
True if a warning was issued for over-allocation.
Definition container.h:603
bool sscanf_int32(pChar &p_in, int &num_bytes, int &result)
Definition container.h:715
void enter_write(pTransaction p_txn)
Definition container.cpp:376
int tensor_tuple_as_text(pTuple p_tuple, pChar p_dest, pChar p_fmt, int item_len[])
Definition container.cpp:3980
StatusCode new_container()
Definition container.cpp:2279
Lock32 _lock_
A lock for the deque of transactions.
Definition container.h:604
virtual StatusCode exec(pTransaction &p_txn, Locator &function, pTuple p_args)
Definition container.cpp:1990
void separator(int rank_1, int dim[], int idx[], pChar &p_ret)
Definition container.h:991
int max_transactions
The configured ONE_SHOT_MAX_TRANSACTIONS.
Definition container.h:597
bool push_real_cell(pChar cell, pChar &p_st, float *&p_out)
Definition container.h:833
uint64_t alloc_bytes
The current allocation in bytes.
Definition container.h:600
virtual StatusCode copy(pChar p_where, pChar p_what)
Definition container.cpp:1963
void * malloc(size_t size)
Definition container.h:527
void base_names(BaseNames &base_names)
Definition container.cpp:2272
virtual StatusCode get(pTransaction &p_txn, pChar p_what)
Definition container.cpp:1760
int from_hex(char c)
Definition container.h:593
bool push_int_cell(pChar cell, pChar &p_st, int *&p_out)
Definition container.h:735
pBlock block_malloc(size_t size)
Definition container.h:549
bool push_time_cell(pChar cell, pChar &p_st, time_t *&p_out, pChar fmt)
Definition container.h:886
bool fill_tensor(pChar &p_in, int &num_bytes, pBlock p_block)
Definition container.cpp:2782
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:573
void as_hex(pChar &p_dest, uint8_t bl)
Definition container.h:977
pTransaction p_buffer
The buffer for the transactions.
Definition container.h:601
bool push_int_cell(pChar cell, pChar &p_st, long long *&p_out)
Definition container.h:761
uint64_t fail_alloc_bytes
Taken from ONE_SHOT_ERROR_BLOCK_KBYTES.
Definition container.h:599
int tensor_time_as_text(pBlock p_block, pChar p_dest, pChar p_fmt)
Definition container.cpp:3906
pChar as_shape(int rank, int dim[], pChar p_ret, pKind p_kind)
Definition container.h:946
virtual StatusCode header(StaticBlockHeader &hea, pChar p_what)
Definition container.cpp:1854
bool get_type_and_shape(pChar &p_in, int &num_bytes, ItemHeader *item_hea, MapSI &dims)
Definition container.cpp:2352
uint64_t warn_alloc_bytes
Taken from ONE_SHOT_WARN_BLOCK_KBYTES.
Definition container.h:598
void leave_read(pTransaction p_txn)
Definition container.cpp:413
virtual StatusCode modify(Locator &function, pTuple p_args)
Definition container.cpp:2007
StatusCode shut_down()
Definition container.cpp:333
StatusCode unwrap_received(pTransaction &p_txn)
Definition container.h:429
virtual void destroy_transaction(pTransaction &p_txn)
Definition container.cpp:493
Kind: A type definition for Jazz Blocks and Tuples.
Definition kind.h:83
A simple logger.
Definition utils.h:245
A Jazz Service is a globally instanced configurable object that may allocate RAM.
Definition utils.h:285
void log(int loglevel, const char *message)
Definition utils.h:305
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:1047
struct ExtraLocator * pExtraLocator
A pointer to an ExtraLocator.
Definition container.h:174
ConfigFile CONFIG(JAZZ_DEFAULT_CONFIG_PATH)
The configuration file.
Definition container.h:1046
std::map< std::string, std::string > Index
An Index kept in RAM by Volatile implemented as an stdlib map (string, string)
Definition types.h:238
Transaction * pTransaction
A pointer to a Transaction.
Definition container.h:171
Locator * pLocator
A pointer to a Locator.
Definition container.h:194
StoredTransaction * pStoredTransaction
A pointer to a StoredTransaction.
Definition container.h:201
char * pChar
A pointer to a char buffer.
Definition types.h:185
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:183
void compile_next_state_LUT(ParseNextStateLUT lut[], int num_states, ParseStateTransition trans[])
Definition container.cpp:63
std::map< std::string, pContainer > BaseNames
A map of names for the containers (or structure engines like "map" or "tree" inside Volatile).
Definition container.h:152
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:144
class Container * pContainer
A (forward defined) pointer to a Container.
Definition container.h:148
int StatusCode
Type returned by the Service API.
Definition utils.h:141
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< std::string, int > MapSI
An internal map for managing dimension parsing.
Definition container.h:205
Locator: A minimal structure to define the location of resources inside a Container.
Definition container.h:184
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:185
Name entity
Another abstraction inside node.container.base, like the name of a table in a database.
Definition container.h:186
Name key
A key identifying a block inside the entity.
Definition container.h:187
int attribute
Used by API to store the attribute.
Definition container.h:190
pExtraLocator p_extra
A pointer to extend this structure with Container specific data (like URLs, cookies,...
Definition container.h:191
A lookup table for all the possible values of a char mapped into an 8-bit state.
Definition container.h:119
unsigned char next[EIGHT_BIT_LONG]
The next state for each possible char.
Definition container.h:120
A way to build constants defining the transition from one state to the next via a regex.
Definition container.h:126
int from
The state to transition from.
Definition container.h:127
char rex[MAX_TRANSITION_REGEX_LEN]
The regex that, when matched, the transition applies.
Definition container.h:129
int to
The state to transition to.
Definition container.h:128
An internal (for Container) Transaction with pointers for a deque.
Definition container.h:198
StoredTransaction * p_next
A pointer to the next StoredTransaction.
Definition container.h:199
char buffer[]
The buffer where strings are stored starting with two zeroes for STRING_NA & STRING_EMPTY.
Definition types.h:287
Transaction: A wrapper over a Block that defines the communication of a block with a Container.
Definition container.h:162
pBlockHeader p_hea
A pointer to the Block (if status == BLOCK_STATUS_READY) for Index.
Definition container.h:165
Lock32 _lock_
An atomically updated int to lock the Transaction to support modifying the Block.
Definition container.h:167
int status
The status of the block transaction.
Definition container.h:168
pBlock p_block
A pointer to the Block (if status == BLOCK_STATUS_READY) for Tensor, Kind and Tuple.
Definition container.h:164
pContainer p_owner
A pointer to the Container instance serving API calls related to this block.
Definition container.h:169