nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

nostrdb.h (18293B)


      1 #ifndef NOSTRDB_H
      2 #define NOSTRDB_H
      3 
      4 #include <inttypes.h>
      5 #include "win.h"
      6 #include "cursor.h"
      7 
      8 // maximum number of filters allowed in a filter group
      9 #define NDB_PACKED_STR     0x1
     10 #define NDB_PACKED_ID      0x2
     11 
     12 #define NDB_FLAG_NOMIGRATE        (1 << 0)
     13 #define NDB_FLAG_SKIP_NOTE_VERIFY (1 << 1)
     14 #define NDB_FLAG_NO_FULLTEXT      (1 << 2)
     15 #define NDB_FLAG_NO_NOTE_BLOCKS   (1 << 3)
     16 #define NDB_FLAG_NO_STATS         (1 << 4)
     17 
     18 //#define DEBUG 1
     19 
     20 #ifdef NDB_LOG
     21 #define ndb_debug(...) printf(__VA_ARGS__)
     22 #else
     23 #define ndb_debug(...) (void)0
     24 #endif
     25 
     26 #include "str_block.h"
     27 
     28 struct ndb_json_parser;
     29 struct ndb;
     30 struct ndb_blocks;
     31 struct ndb_block;
     32 struct ndb_note;
     33 struct ndb_tag;
     34 struct ndb_tags;
     35 struct ndb_lmdb;
     36 union ndb_packed_str;
     37 struct bolt11;
     38 
     39 // some bindings like swift needs help with forward declared pointers
     40 struct ndb_tag_ptr { struct ndb_tag *ptr; };
     41 struct ndb_tags_ptr { struct ndb_tags *ptr; };
     42 struct ndb_block_ptr { struct ndb_block *ptr; };
     43 struct ndb_blocks_ptr { struct ndb_blocks *ptr; };
     44 struct ndb_note_ptr { struct ndb_note *ptr; };
     45 
     46 struct ndb_t {
     47 	struct ndb *ndb;
     48 };
     49 
     50 struct ndb_str {
     51 	unsigned char flag;
     52 	union {
     53 		const char *str;
     54 		unsigned char *id;
     55 	};
     56 };
     57 
     58 struct ndb_keypair {
     59 	unsigned char pubkey[32];
     60 	unsigned char secret[32];
     61 	
     62 	// this corresponds to secp256k1's keypair type. it's guaranteed to
     63 	// be 96 bytes according to their docs. I don't want to depend on
     64 	// the secp256k1 header here so we just use raw bytes.
     65 	unsigned char pair[96];
     66 };
     67 
     68 // function pointer for controlling what to do after we parse an id
     69 typedef enum ndb_idres (*ndb_id_fn)(void *, const char *);
     70 
     71 // callback function for when we receive new subscription results
     72 typedef void (*ndb_sub_fn)(void *, uint64_t subid);
     73 
     74 // id callback + closure data
     75 struct ndb_id_cb {
     76 	ndb_id_fn fn;
     77 	void *data;
     78 };
     79 
     80 // required to keep a read 
     81 struct ndb_txn {
     82 	struct ndb_lmdb *lmdb;
     83 	void *mdb_txn;
     84 };
     85 
     86 struct ndb_event {
     87 	struct ndb_note *note;
     88 };
     89 
     90 struct ndb_command_result {
     91 	int ok;
     92 	const char *msg;
     93 	int msglen;
     94 };
     95 
     96 // From-client event types
     97 enum fce_type {
     98 	NDB_FCE_EVENT = 0x1
     99 };
    100 
    101 // To-client event types
    102 enum tce_type {
    103 	NDB_TCE_EVENT  = 0x1,
    104 	NDB_TCE_OK     = 0x2,
    105 	NDB_TCE_NOTICE = 0x3,
    106 	NDB_TCE_EOSE   = 0x4,
    107 	NDB_TCE_AUTH   = 0x5,
    108 };
    109 
    110 enum ndb_ingest_filter_action {
    111 	NDB_INGEST_REJECT,
    112 	NDB_INGEST_ACCEPT,
    113 	NDB_INGEST_SKIP_VALIDATION
    114 };
    115 
    116 struct ndb_search_key
    117 {
    118 	char search[24];
    119 	unsigned char id[32];
    120 	uint64_t timestamp;
    121 };
    122 
    123 struct ndb_search {
    124 	struct ndb_search_key *key;
    125 	uint64_t profile_key;
    126 	void *cursor; // MDB_cursor *
    127 };
    128 
    129 // From-client event
    130 struct ndb_fce {
    131 	enum fce_type evtype;
    132 	union {
    133 		struct ndb_event event;
    134 	};
    135 };
    136 
    137 // To-client event
    138 struct ndb_tce {
    139 	enum tce_type evtype;
    140 	const char *subid;
    141 	int subid_len;
    142 
    143 	union {
    144 		struct ndb_event event;
    145 		struct ndb_command_result command_result;
    146 	};
    147 };
    148 
    149 typedef enum ndb_ingest_filter_action (*ndb_ingest_filter_fn)(void *, struct ndb_note *);
    150 
    151 enum ndb_filter_fieldtype {
    152 	NDB_FILTER_IDS     = 1,
    153 	NDB_FILTER_AUTHORS = 2,
    154 	NDB_FILTER_KINDS   = 3,
    155 	NDB_FILTER_TAGS    = 4,
    156 	NDB_FILTER_SINCE   = 5,
    157 	NDB_FILTER_UNTIL   = 6,
    158 	NDB_FILTER_LIMIT   = 7,
    159 	NDB_FILTER_SEARCH  = 8,
    160 };
    161 #define NDB_NUM_FILTERS 7
    162 
    163 // when matching generic tags, we need to know if we're dealing with
    164 // a pointer to a 32-byte ID or a null terminated string
    165 enum ndb_generic_element_type {
    166 	NDB_ELEMENT_UNKNOWN = 0,
    167 	NDB_ELEMENT_STRING  = 1,
    168 	NDB_ELEMENT_ID      = 2,
    169 	NDB_ELEMENT_INT     = 3,
    170 };
    171 
    172 enum ndb_search_order {
    173 	NDB_ORDER_DESCENDING,
    174 	NDB_ORDER_ASCENDING,
    175 };
    176 
    177 enum ndb_dbs {
    178 	NDB_DB_NOTE,
    179 	NDB_DB_META,
    180 	NDB_DB_PROFILE,
    181 	NDB_DB_NOTE_ID,
    182 	NDB_DB_PROFILE_PK, // profile pk index
    183 	NDB_DB_NDB_META,
    184 	NDB_DB_PROFILE_SEARCH,
    185 	NDB_DB_PROFILE_LAST_FETCH,
    186 	NDB_DB_NOTE_KIND, // note kind index
    187 	NDB_DB_NOTE_TEXT, // note fulltext index
    188 	NDB_DB_NOTE_BLOCKS, // parsed note blocks for rendering
    189 	NDB_DB_NOTE_TAGS,  // note tags index
    190 	NDB_DB_NOTE_PUBKEY, // note pubkey index
    191 	NDB_DB_NOTE_PUBKEY_KIND, // note pubkey kind index
    192 	NDB_DBS,
    193 };
    194 
    195 // common kinds. we collect stats on these in ndb_stat. mainly because I don't
    196 // want to deal with including a hashtable to the project.
    197 enum ndb_common_kind {
    198 	NDB_CKIND_PROFILE,
    199 	NDB_CKIND_TEXT,
    200 	NDB_CKIND_CONTACTS,
    201 	NDB_CKIND_DM,
    202 	NDB_CKIND_DELETE,
    203 	NDB_CKIND_REPOST,
    204 	NDB_CKIND_REACTION,
    205 	NDB_CKIND_ZAP,
    206 	NDB_CKIND_ZAP_REQUEST,
    207 	NDB_CKIND_NWC_REQUEST,
    208 	NDB_CKIND_NWC_RESPONSE,
    209 	NDB_CKIND_HTTP_AUTH,
    210 	NDB_CKIND_LIST,
    211 	NDB_CKIND_LONGFORM,
    212 	NDB_CKIND_STATUS,
    213 	NDB_CKIND_COUNT, // should always be last
    214 };
    215 
    216 struct ndb_builder {
    217 	struct cursor mem;
    218 	struct cursor note_cur;
    219 	struct cursor strings;
    220 	struct cursor str_indices;
    221 	struct ndb_note *note;
    222 	struct ndb_tag *current_tag;
    223 };
    224 
    225 struct ndb_iterator {
    226 	struct ndb_note *note;
    227 	struct ndb_tag *tag;
    228 
    229 	// current outer index
    230 	int index;
    231 };
    232 
    233 struct ndb_filter_string {
    234 	const char *string;
    235 	int len;
    236 };
    237 
    238 union ndb_filter_element {
    239 	struct ndb_filter_string string;
    240 	const unsigned char *id;
    241 	uint64_t integer;
    242 };
    243 
    244 struct ndb_filter_field {
    245 	enum ndb_filter_fieldtype type;
    246 	enum ndb_generic_element_type elem_type;
    247 	char tag; // for generic queries like #t
    248 };
    249 
    250 struct ndb_filter_elements {
    251 	struct ndb_filter_field field;
    252 	int count;
    253 
    254 	// this needs to be pointer size for reasons
    255 	// FIXME: what about on 32bit systems??
    256 	uint64_t elements[0];
    257 };
    258 
    259 struct ndb_filter {
    260 	struct cursor elem_buf;
    261 	struct cursor data_buf;
    262 	int num_elements;
    263 	int finalized;
    264 	int current;
    265 
    266 	// struct ndb_filter_elements offsets into elem_buf
    267 	//
    268 	// TODO(jb55): this should probably be called fields. elements are
    269 	// the things within fields
    270 	int elements[NDB_NUM_FILTERS]; 
    271 };
    272 
    273 struct ndb_config {
    274 	int flags;
    275 	int ingester_threads;
    276 	size_t mapsize;
    277 	void *filter_context;
    278 	ndb_ingest_filter_fn ingest_filter;
    279 	void *sub_cb_ctx;
    280 	ndb_sub_fn sub_cb;
    281 };
    282 
    283 struct ndb_text_search_config {
    284 	enum ndb_search_order order;
    285 	int limit;
    286 };
    287 
    288 struct ndb_stat_counts {
    289 	size_t key_size;
    290 	size_t value_size;
    291 	size_t count;
    292 };
    293 
    294 struct ndb_stat {
    295 	struct ndb_stat_counts dbs[NDB_DBS];
    296 	struct ndb_stat_counts common_kinds[NDB_CKIND_COUNT];
    297 	struct ndb_stat_counts other_kinds;
    298 };
    299 
    300 #define MAX_TEXT_SEARCH_RESULTS 128
    301 #define MAX_TEXT_SEARCH_WORDS 8
    302 
    303 // unpacked form of the actual lmdb fulltext search key
    304 // see `ndb_make_text_search_key` for how the packed version is constructed
    305 struct ndb_text_search_key
    306 {
    307 	int str_len;
    308 	const char *str;
    309 	uint64_t timestamp;
    310 	uint64_t note_id;
    311 	uint64_t word_index;
    312 };
    313 
    314 struct ndb_text_search_result {
    315 	struct ndb_text_search_key key;
    316 	int prefix_chars;
    317 
    318 	// This is only set if we passed a filter for nip50 searches
    319 	struct ndb_note *note;
    320 	uint64_t note_size;
    321 };
    322 
    323 struct ndb_text_search_results {
    324 	struct ndb_text_search_result results[MAX_TEXT_SEARCH_RESULTS];
    325 	int num_results;
    326 };
    327 
    328 enum ndb_block_type {
    329     BLOCK_HASHTAG        = 1,
    330     BLOCK_TEXT           = 2,
    331     BLOCK_MENTION_INDEX  = 3,
    332     BLOCK_MENTION_BECH32 = 4,
    333     BLOCK_URL            = 5,
    334     BLOCK_INVOICE        = 6,
    335 };
    336 #define NDB_NUM_BLOCK_TYPES 6
    337 #define NDB_MAX_RELAYS 24
    338 
    339 struct ndb_relays {
    340 	struct ndb_str_block relays[NDB_MAX_RELAYS];
    341 	int num_relays;
    342 };
    343 
    344 enum nostr_bech32_type {
    345 	NOSTR_BECH32_NOTE = 1,
    346 	NOSTR_BECH32_NPUB = 2,
    347 	NOSTR_BECH32_NPROFILE = 3,
    348 	NOSTR_BECH32_NEVENT = 4,
    349 	NOSTR_BECH32_NRELAY = 5,
    350 	NOSTR_BECH32_NADDR = 6,
    351 	NOSTR_BECH32_NSEC = 7,
    352 };
    353 #define NOSTR_BECH32_KNOWN_TYPES 7
    354 
    355 struct bech32_note {
    356 	const unsigned char *event_id;
    357 };
    358 
    359 struct bech32_npub {
    360 	const unsigned char *pubkey;
    361 };
    362 
    363 struct bech32_nsec {
    364 	const unsigned char *nsec;
    365 };
    366 
    367 struct bech32_nevent {
    368 	struct ndb_relays relays;
    369 	const unsigned char *event_id;
    370 	const unsigned char *pubkey; // optional
    371 };
    372 
    373 struct bech32_nprofile {
    374 	struct ndb_relays relays;
    375 	const unsigned char *pubkey;
    376 };
    377 
    378 struct bech32_naddr {
    379 	struct ndb_relays relays;
    380 	struct ndb_str_block identifier;
    381 	const unsigned char *pubkey;
    382 };
    383 
    384 struct bech32_nrelay {
    385 	struct ndb_str_block relay;
    386 };
    387 
    388 struct nostr_bech32 {
    389 	enum nostr_bech32_type type;
    390 
    391 	union {
    392 		struct bech32_note note;
    393 		struct bech32_npub npub;
    394 		struct bech32_nsec nsec;
    395 		struct bech32_nevent nevent;
    396 		struct bech32_nprofile nprofile;
    397 		struct bech32_naddr naddr;
    398 		struct bech32_nrelay nrelay;
    399 	};
    400 };
    401 
    402 
    403 struct ndb_mention_bech32_block {
    404 	struct ndb_str_block str;
    405 	struct nostr_bech32 bech32;
    406 };
    407 
    408 struct ndb_invoice {
    409 	unsigned char version;
    410 	uint64_t amount;
    411 	uint64_t timestamp;
    412 	uint64_t expiry;
    413 	char *description;
    414 	unsigned char *description_hash;
    415 };
    416 
    417 struct ndb_invoice_block {
    418 	struct ndb_str_block invstr;
    419 	struct ndb_invoice invoice;
    420 };
    421 
    422 struct ndb_block {
    423 	enum ndb_block_type type;
    424 	union {
    425 		struct ndb_str_block str;
    426 		struct ndb_invoice_block invoice;
    427 		struct ndb_mention_bech32_block mention_bech32;
    428 		uint32_t mention_index;
    429 	} block;
    430 };
    431 
    432 struct ndb_block_iterator {
    433 	const char *content;
    434 	struct ndb_blocks *blocks;
    435 	struct ndb_block block;
    436 	unsigned char *p;
    437 };
    438 
    439 struct ndb_query_result {
    440 	struct ndb_note *note;
    441 	uint64_t note_size;
    442 	uint64_t note_id;
    443 };
    444 
    445 struct ndb_query_results {
    446 	struct cursor cur;
    447 };
    448 
    449 // CONFIG
    450 void ndb_default_config(struct ndb_config *);
    451 void ndb_config_set_ingest_threads(struct ndb_config *config, int threads);
    452 void ndb_config_set_flags(struct ndb_config *config, int flags);
    453 void ndb_config_set_mapsize(struct ndb_config *config, size_t mapsize);
    454 void ndb_config_set_ingest_filter(struct ndb_config *config, ndb_ingest_filter_fn fn, void *);
    455 void ndb_config_set_subscription_callback(struct ndb_config *config, ndb_sub_fn fn, void *ctx);
    456 
    457 // HELPERS
    458 int ndb_calculate_id(struct ndb_note *note, unsigned char *buf, int buflen);
    459 int ndb_sign_id(struct ndb_keypair *keypair, unsigned char id[32], unsigned char sig[64]);
    460 int ndb_create_keypair(struct ndb_keypair *key);
    461 int ndb_decode_key(const char *secstr, struct ndb_keypair *keypair);
    462 int ndb_note_verify(void *secp_ctx, unsigned char pubkey[32], unsigned char id[32], unsigned char signature[64]);
    463 
    464 // NDB
    465 int ndb_init(struct ndb **ndb, const char *dbdir, const struct ndb_config *);
    466 int ndb_db_version(struct ndb_txn *txn);
    467 int ndb_process_event(struct ndb *, const char *json, int len);
    468 int ndb_process_events(struct ndb *, const char *ldjson, size_t len);
    469 #ifndef _WIN32
    470 // TODO: fix on windows
    471 int ndb_process_events_stream(struct ndb *, FILE* fp);
    472 #endif
    473 int ndb_process_client_event(struct ndb *, const char *json, int len);
    474 int ndb_process_client_events(struct ndb *, const char *json, size_t len);
    475 int ndb_begin_query(struct ndb *, struct ndb_txn *);
    476 int ndb_search_profile(struct ndb_txn *txn, struct ndb_search *search, const char *query);
    477 int ndb_search_profile_next(struct ndb_search *search);
    478 void ndb_search_profile_end(struct ndb_search *search);
    479 int ndb_end_query(struct ndb_txn *);
    480 int ndb_write_last_profile_fetch(struct ndb *ndb, const unsigned char *pubkey, uint64_t fetched_at);
    481 uint64_t ndb_read_last_profile_fetch(struct ndb_txn *txn, const unsigned char *pubkey);
    482 void *ndb_get_profile_by_pubkey(struct ndb_txn *txn, const unsigned char *pubkey, size_t *len, uint64_t *primkey);
    483 void *ndb_get_profile_by_key(struct ndb_txn *txn, uint64_t key, size_t *len);
    484 uint64_t ndb_get_notekey_by_id(struct ndb_txn *txn, const unsigned char *id);
    485 uint64_t ndb_get_profilekey_by_pubkey(struct ndb_txn *txn, const unsigned char *id);
    486 struct ndb_note *ndb_get_note_by_id(struct ndb_txn *txn, const unsigned char *id, size_t *len, uint64_t *primkey);
    487 struct ndb_note *ndb_get_note_by_key(struct ndb_txn *txn, uint64_t key, size_t *len);
    488 void *ndb_get_note_meta(struct ndb_txn *txn, const unsigned char *id, size_t *len);
    489 void ndb_destroy(struct ndb *);
    490 
    491 // BUILDER
    492 int ndb_parse_json_note(struct ndb_json_parser *, struct ndb_note **);
    493 int ndb_client_event_from_json(const char *json, int len, struct ndb_fce *fce, unsigned char *buf, int bufsize, struct ndb_id_cb *cb);
    494 int ndb_ws_event_from_json(const char *json, int len, struct ndb_tce *tce, unsigned char *buf, int bufsize, struct ndb_id_cb *);
    495 int ndb_note_from_json(const char *json, int len, struct ndb_note **, unsigned char *buf, int buflen);
    496 int ndb_builder_init(struct ndb_builder *builder, unsigned char *buf, size_t bufsize);
    497 int ndb_builder_finalize(struct ndb_builder *builder, struct ndb_note **note, struct ndb_keypair *privkey);
    498 int ndb_builder_set_content(struct ndb_builder *builder, const char *content, int len);
    499 void ndb_builder_set_created_at(struct ndb_builder *builder, uint64_t created_at);
    500 void ndb_builder_set_sig(struct ndb_builder *builder, unsigned char *sig);
    501 void ndb_builder_set_pubkey(struct ndb_builder *builder, unsigned char *pubkey);
    502 void ndb_builder_set_id(struct ndb_builder *builder, unsigned char *id);
    503 void ndb_builder_set_kind(struct ndb_builder *builder, uint32_t kind);
    504 int ndb_builder_new_tag(struct ndb_builder *builder);
    505 int ndb_builder_push_tag_str(struct ndb_builder *builder, const char *str, int len);
    506 
    507 // FILTERS
    508 int ndb_filter_init(struct ndb_filter *);
    509 
    510 /// Allocate a filter with a fixed sized buffer (where pages is number of 4096-byte sized blocks)
    511 /// You can set pages to 1 if you know you are constructing small filters
    512 // TODO: replace this with passed-in buffers
    513 int ndb_filter_init_with(struct ndb_filter *filter, int pages);
    514 
    515 int ndb_filter_add_id_element(struct ndb_filter *, const unsigned char *id);
    516 int ndb_filter_add_int_element(struct ndb_filter *, uint64_t integer);
    517 int ndb_filter_add_str_element(struct ndb_filter *, const char *str);
    518 int ndb_filter_eq(const struct ndb_filter *, const struct ndb_filter *);
    519 
    520 /// is `a` a subset of `b`
    521 int ndb_filter_is_subset_of(const struct ndb_filter *a, const struct ndb_filter *b);
    522 
    523 // filters from json
    524 int ndb_filter_from_json(const char *, int len, struct ndb_filter *filter, unsigned char *buf, int bufsize);
    525 
    526 // getting field elements
    527 unsigned char *ndb_filter_get_id_element(const struct ndb_filter *, const struct ndb_filter_elements *, int index);
    528 const char *ndb_filter_get_string_element(const struct ndb_filter *, const struct ndb_filter_elements *, int index);
    529 uint64_t ndb_filter_get_int_element(const struct ndb_filter_elements *, int index);
    530 uint64_t *ndb_filter_get_int_element_ptr(struct ndb_filter_elements *, int index);
    531 
    532 struct ndb_filter_elements *ndb_filter_current_element(const struct ndb_filter *);
    533 struct ndb_filter_elements *ndb_filter_get_elements(const struct ndb_filter *, int);
    534 int ndb_filter_start_field(struct ndb_filter *, enum ndb_filter_fieldtype);
    535 int ndb_filter_start_tag_field(struct ndb_filter *, char tag);
    536 int ndb_filter_matches(struct ndb_filter *, struct ndb_note *);
    537 int ndb_filter_clone(struct ndb_filter *dst, struct ndb_filter *src);
    538 int ndb_filter_end(struct ndb_filter *);
    539 void ndb_filter_end_field(struct ndb_filter *);
    540 void ndb_filter_destroy(struct ndb_filter *);
    541 int ndb_filter_json(const struct ndb_filter *, char *buf, int buflen);
    542 
    543 // SUBSCRIPTIONS
    544 uint64_t ndb_subscribe(struct ndb *, struct ndb_filter *, int num_filters);
    545 int ndb_wait_for_notes(struct ndb *, uint64_t subid, uint64_t *note_ids, int note_id_capacity);
    546 int ndb_poll_for_notes(struct ndb *, uint64_t subid, uint64_t *note_ids, int note_id_capacity);
    547 int ndb_unsubscribe(struct ndb *, uint64_t subid);
    548 int ndb_num_subscriptions(struct ndb *);
    549 
    550 // FULLTEXT SEARCH
    551 int ndb_text_search(struct ndb_txn *txn, const char *query, struct ndb_text_search_results *, struct ndb_text_search_config *);
    552 int ndb_text_search_with(struct ndb_txn *txn, const char *query, struct ndb_text_search_results *, struct ndb_text_search_config *, struct ndb_filter *filter);
    553 void ndb_default_text_search_config(struct ndb_text_search_config *);
    554 void ndb_text_search_config_set_order(struct ndb_text_search_config *, enum ndb_search_order);
    555 void ndb_text_search_config_set_limit(struct ndb_text_search_config *, int limit);
    556 
    557 // QUERY
    558 int ndb_query(struct ndb_txn *txn, struct ndb_filter *filters, int num_filters, struct ndb_query_result *results, int result_capacity, int *count);
    559 
    560 // STATS
    561 int ndb_stat(struct ndb *ndb, struct ndb_stat *stat);
    562 void ndb_stat_counts_init(struct ndb_stat_counts *counts);
    563 
    564 // NOTE
    565 const char *ndb_note_content(struct ndb_note *note);
    566 struct ndb_str ndb_note_str(struct ndb_note *note, union ndb_packed_str *pstr);
    567 uint32_t ndb_note_content_length(struct ndb_note *note);
    568 uint32_t ndb_note_created_at(struct ndb_note *note);
    569 uint32_t ndb_note_kind(struct ndb_note *note);
    570 unsigned char *ndb_note_id(struct ndb_note *note);
    571 unsigned char *ndb_note_pubkey(struct ndb_note *note);
    572 unsigned char *ndb_note_sig(struct ndb_note *note);
    573 void _ndb_note_set_kind(struct ndb_note *note, uint32_t kind);
    574 struct ndb_tags *ndb_note_tags(struct ndb_note *note);
    575 int ndb_str_len(struct ndb_str *str);
    576 
    577 /// write the note as json to a buffer
    578 int ndb_note_json(struct ndb_note *, char *buf, int buflen);
    579 
    580 // TAGS
    581 void ndb_tags_iterate_start(struct ndb_note *note, struct ndb_iterator *iter);
    582 uint16_t ndb_tags_count(struct ndb_tags *);
    583 uint16_t ndb_tag_count(struct ndb_tag *);
    584 
    585 // ITER
    586 int ndb_tags_iterate_next(struct ndb_iterator *iter);
    587 struct ndb_str ndb_iter_tag_str(struct ndb_iterator *iter, int ind);
    588 struct ndb_str ndb_tag_str(struct ndb_note *note, struct ndb_tag *tag, int ind);
    589 
    590 // NAMES
    591 const char *ndb_db_name(enum ndb_dbs db);
    592 const char *ndb_kind_name(enum ndb_common_kind ck);
    593 enum ndb_common_kind ndb_kind_to_common_kind(int kind);
    594 
    595 // CONTENT PARSER
    596 int ndb_parse_content(unsigned char *buf, int buf_size,
    597 		      const char *content, int content_len,
    598 		      struct ndb_blocks **blocks_p);
    599 
    600 // BLOCKS
    601 enum ndb_block_type ndb_get_block_type(struct ndb_block *block);
    602 int ndb_blocks_flags(struct ndb_blocks *block);
    603 size_t ndb_blocks_total_size(struct ndb_blocks *blocks);
    604 int ndb_blocks_word_count(struct ndb_blocks *blocks);
    605 
    606 /// Free blocks if they are owned, safe to call on unowned blocks as well.
    607 void ndb_blocks_free(struct ndb_blocks *blocks);
    608 
    609 // BLOCK DB
    610 struct ndb_blocks *ndb_get_blocks_by_key(struct ndb *ndb, struct ndb_txn *txn, uint64_t note_key);
    611 
    612 // BLOCK ITERATORS
    613 void ndb_blocks_iterate_start(const char *, struct ndb_blocks *, struct ndb_block_iterator *);
    614 struct ndb_block *ndb_blocks_iterate_next(struct ndb_block_iterator *);
    615 
    616 // STR BLOCKS
    617 struct ndb_str_block *ndb_block_str(struct ndb_block *);
    618 const char *ndb_str_block_ptr(struct ndb_str_block *);
    619 uint32_t ndb_str_block_len(struct ndb_str_block *);
    620 
    621 // BECH32 BLOCKS
    622 struct nostr_bech32 *ndb_bech32_block(struct ndb_block *block);
    623 
    624 #endif