@@ -48,24 +48,31 @@ class NodeImpl : public Object {
4848 const static size_t MAX_VALUE_OFFSET = 4095 ;
4949 const static size_t MAX_VALUE_LENGTH = MAX_KEY_OFFSET ;
5050
51+ enum TYPE : uint8_t {STRING , NUMBER , null, BOOLEAN , OBJECT , ARRAY };
52+
53+ union {
54+ const char * _text_begin; // addr of the text (only for root node)
55+ const NodeImpl* _root; // root node (only for non-root nodes)
56+ };
5157union { struct { // for non-root nodes
52- struct {
53- uint8_t _flags;
54- uint16_t _k_len : 12 ; // key length (12 bits)
55- uint16_t _v_off : 12 ; // value offset (12 bits) to key end
56- }__attribute__((packed));
57- const NodeImpl* _root; // root node
58- }; // packed as 20 bytes
58+ uint8_t _flags;
59+ uint16_t _k_len : 12 ; // key length (12 bits)
60+ uint16_t _v_off : 12 ; // value offset (12 bits) to key end
61+ }__attribute__ ((packed));
5962struct { // for the root node
6063 uint8_t _flags_; // the same as _flags
6164 uint8_t _node_size; // sizeof(the node implementation)
6265 mutable uint16_t _refcnt; // reference counter of the document
63- const char * _text_begin;
6466}; };
6567 uint32_t _k_off; // key offset to _text_begin
6668 uint32_t _v_len; // value length
6769 uint32_t _nchildren; // for all nodes
6870
71+ void set_type (uint8_t type) {
72+ _flags &= (1 << 5 ) - 1 ;
73+ _flags |= (type & 0x7 ) << 5 ;
74+ }
75+
6976 using AT16 = std::atomic<uint16_t >;
7077 static_assert (sizeof (AT16 ) == sizeof (_refcnt), " ..." );
7178
@@ -102,6 +109,9 @@ struct { // for the root node
102109 bool is_root () const {
103110 return _flags & FLAG_IS_ROOT ;
104111 }
112+ uint8_t get_type () const {
113+ return _flags >> 5 ;
114+ }
105115 const NodeImpl* get_root () const {
106116 return is_root () ? this : _root;
107117 }
@@ -139,6 +149,7 @@ struct { // for the root node
139149 int init_non_root (str key, str value, const NodeImpl* root, uint32_t flags);
140150};
141151
152+ static_assert (sizeof (NodeImpl) == 32 , " " );
142153
143154}
144155}
0 commit comments