@@ -2178,7 +2178,7 @@ int ndb_count_replies(struct ndb_txn *txn, const unsigned char *note_id, uint16_
21782178}
21792179
21802180/* count all of the reactions for a note */
2181- int ndb_count_reactions (struct ndb_txn * txn , const unsigned char * note_id , uint32_t * count )
2181+ int ndb_rebuild_reaction_metadata (struct ndb_txn * txn , const unsigned char * note_id , struct ndb_note_meta_builder * builder , uint32_t * count )
21822182{
21832183 MDB_val k , v ;
21842184 MDB_cursor * cur ;
@@ -2189,6 +2189,8 @@ int ndb_count_reactions(struct ndb_txn *txn, const unsigned char *note_id, uint3
21892189 size_t size ;
21902190 struct ndb_note * note ;
21912191 unsigned char * keybuf , * last_id ;
2192+ struct ndb_note_meta_entry * entry ;
2193+ union ndb_reaction_str reaction_str ;
21922194 char buffer [41 ]; /* 1 + 32 + 8 */
21932195 * count = 0 ;
21942196
@@ -2227,6 +2229,21 @@ int ndb_count_reactions(struct ndb_txn *txn, const unsigned char *note_id, uint3
22272229 continue ;
22282230 if (memcmp (last_id , note_id , 32 ))
22292231 continue ;
2232+
2233+ if (builder ) {
2234+ if (!ndb_reaction_set (& reaction_str , ndb_note_content (note )))
2235+ ndb_reaction_set (& reaction_str , "+" );
2236+
2237+ if ((entry = ndb_note_meta_builder_find_entry (builder , NDB_NOTE_META_REACTION , & reaction_str .binmoji ))) {
2238+ (* ndb_note_meta_reaction_count (entry ))++ ;
2239+ } else if ((entry = ndb_note_meta_add_entry (builder ))) {
2240+ ndb_note_meta_reaction_set (entry , 1 , reaction_str );
2241+ } else {
2242+ /* couldn't add reaction entry ? */
2243+ ndb_debug ("ndb_rebuild_note_indices: couldn't add reaction count entry to metadata builder\n" );
2244+ }
2245+ }
2246+
22302247 (* count )++ ;
22312248 } while (mdb_cursor_get (cur , & k , & v , MDB_NEXT ) == 0 );
22322249
@@ -2301,19 +2318,22 @@ static int ndb_note_meta_builder_counts(struct ndb_txn *txn,
23012318 thread_replies = 0 ;
23022319 total_reactions = 0 ;
23032320
2304- if (!(entry = ndb_note_meta_add_entry (builder )))
2305- return 0 ;
2306-
2307- rcs [0 ] = ndb_count_reactions (txn , note_id , & total_reactions );
2321+ rcs [0 ] = ndb_rebuild_reaction_metadata (txn , note_id , builder , & total_reactions );
23082322 rcs [1 ] = ndb_count_quotes (txn , note_id , & quotes );
23092323 rcs [2 ] = ndb_count_replies (txn , note_id , & direct_replies , & thread_replies );
23102324
2311- if (!rcs [0 ] && !rcs [1 ] && !rcs [2 ])
2325+ if (!rcs [0 ] && !rcs [1 ] && !rcs [2 ]) {
23122326 return 0 ;
2327+ }
23132328
23142329 /* no entry needed */
2315- if (quotes == 0 && direct_replies == 0 && thread_replies == 0 && quotes == 0 )
2316- return 1 ;
2330+ if (quotes == 0 && direct_replies == 0 && thread_replies == 0 && quotes == 0 ) {
2331+ return 0 ;
2332+ }
2333+
2334+ if (!(entry = ndb_note_meta_add_entry (builder ))) {
2335+ return 0 ;
2336+ }
23172337
23182338 ndb_note_meta_counts_set (entry , total_reactions , quotes , direct_replies , thread_replies );
23192339
@@ -2334,49 +2354,58 @@ static int ndb_migrate_metadata(struct ndb_txn *txn)
23342354{
23352355 MDB_val k , k2 , v , v2 ;
23362356 MDB_cursor * cur ;
2337- MDB_dbi db ;
2357+ MDB_dbi note_db , meta_db ;
23382358 unsigned char * id ;
2339- unsigned char buffer [4096 ];
2340- int rc ;
2359+ size_t scratch_size = 1024 * 1024 ;
2360+ unsigned char * buffer = malloc (scratch_size );
2361+ int rc , count ;
23412362 struct ndb_note_meta_builder builder ;
2363+ struct ndb_note * note ;
23422364 struct ndb_note_meta * meta ;
23432365
2344- db = txn -> lmdb -> dbs [NDB_DB_META ];
2366+ meta_db = txn -> lmdb -> dbs [NDB_DB_META ];
2367+ note_db = txn -> lmdb -> dbs [NDB_DB_NOTE ];
23452368
2346- if ((rc = mdb_cursor_open (txn -> mdb_txn , db , & cur ))) {
2369+ if ((rc = mdb_cursor_open (txn -> mdb_txn , note_db , & cur ))) {
23472370 fprintf (stderr , "ndb_migrate_reaction_stats: mdb_cursor_open failed, error %d\n" , rc );
23482371 return -1 ;
23492372 }
23502373
2374+ count = 0 ;
2375+
23512376 /* loop through every metadata entry */
23522377 while (mdb_cursor_get (cur , & k , & v , MDB_NEXT ) == 0 ) {
2353- ndb_note_meta_builder_init (& builder , buffer , sizeof ( buffer ) );
2378+ ndb_note_meta_builder_init (& builder , buffer , scratch_size );
23542379
2355- id = (unsigned char * )k .mv_data ;
2356- ndb_note_meta_builder_count_reactions ( txn , & builder );
2357- ndb_note_meta_builder_counts ( txn , id , & builder ) ;
2358- ndb_note_meta_build ( & builder , & meta ) ;
2380+ note = (struct ndb_note * )v .mv_data ;
2381+ id = ndb_note_id ( note );
2382+ k2 . mv_data = ( unsigned char * ) id ;
2383+ k2 . mv_size = 32 ;
23592384
2360- /* no counts found, just delete this entry */
2361- if (ndb_note_meta_entries_count (meta ) == 0 ) {
2362- if ((rc = mdb_del (txn -> mdb_txn , db , & k , & v ))) {
2363- ndb_debug ("delete old metadata entry failed: %s\n" , mdb_strerror (rc ));
2364- return -1 ;
2365- }
2385+ rc = ndb_note_meta_builder_counts (txn , id , & builder );
2386+ if (!rc ) {
2387+ mdb_del (txn -> mdb_txn , meta_db , & k2 , NULL );
23662388 continue ;
23672389 }
23682390
2369- k2 .mv_data = (unsigned char * )id ;
2370- k2 .mv_size = 32 ;
2391+ ndb_note_meta_build (& builder , & meta );
2392+ assert (ndb_note_meta_entries (meta )-> type != 0 );
2393+
23712394 v2 .mv_data = meta ;
23722395 v2 .mv_size = ndb_note_meta_total_size (meta );
23732396
23742397 /* set entry */
2375- if ((rc = mdb_put (txn -> mdb_txn , db , & k2 , & v2 , 0 ))) {
2398+ if ((rc = mdb_put (txn -> mdb_txn , meta_db , & k2 , & v2 , 0 ))) {
23762399 ndb_debug ("migrate metadata entry failed on write: %s\n" , mdb_strerror (rc ));
23772400 }
2401+
2402+ count ++ ;
23782403 }
23792404
2405+ fprintf (stderr , "nostrdb: migrated %d metadata entries\n" , count );
2406+
2407+ free (buffer );
2408+ mdb_cursor_close (cur );
23802409 return 1 ;
23812410}
23822411
@@ -2713,7 +2742,7 @@ static struct ndb_migration MIGRATIONS[] = {
27132742 { .fn = ndb_migrate_lower_user_search_indices },
27142743 { .fn = ndb_migrate_utf8_profile_names },
27152744 { .fn = ndb_migrate_profile_indices },
2716- // { .fn = ndb_migrate_metadata },
2745+ { .fn = ndb_migrate_metadata },
27172746};
27182747
27192748
@@ -8515,6 +8544,29 @@ void ndb_config_set_ingest_filter(struct ndb_config *config,
85158544 config -> filter_context = filter_ctx ;
85168545}
85178546
8547+ int ndb_print_note_metadata (struct ndb_txn * txn )
8548+ {
8549+ MDB_cursor * cur ;
8550+ MDB_val k , v ;
8551+ int i ;
8552+
8553+ if (mdb_cursor_open (txn -> mdb_txn , txn -> lmdb -> dbs [NDB_DB_META ], & cur ))
8554+ return 0 ;
8555+
8556+ i = 1 ;
8557+ while (mdb_cursor_get (cur , & k , & v , MDB_NEXT ) == 0 ) {
8558+ print_hex (k .mv_data , 32 );
8559+ printf ("\t" );
8560+ print_note_meta ((struct ndb_note_meta * )v .mv_data );
8561+ i ++ ;
8562+ }
8563+
8564+ mdb_cursor_close (cur );
8565+
8566+ return i ;
8567+ }
8568+
8569+
85188570int ndb_print_author_kind_index (struct ndb_txn * txn )
85198571{
85208572 MDB_cursor * cur ;
0 commit comments