@@ -1992,10 +1992,141 @@ static int ndb_rebuild_note_indices(struct ndb_txn *txn, enum ndb_dbs *indices,
19921992 return count ;
19931993}
19941994
1995+ int ndb_cursor_start (MDB_cursor * cur , MDB_val * k , MDB_val * v );
1996+
1997+ /* count all of the quote reposts for a note id */
1998+ static int ndb_count_quotes (struct ndb_txn * txn , const unsigned char * note_id , uint32_t * count )
1999+ {
2000+ MDB_val k , v ;
2001+ MDB_cursor * cur ;
2002+ MDB_dbi db ;
2003+ int rc ;
2004+ unsigned char * keybuf ;
2005+ char buffer [41 ]; /* 1 + 32 + 8 */
2006+
2007+ * count = 0 ;
2008+ db = txn -> lmdb -> dbs [NDB_DB_NOTE_TAGS ];
2009+
2010+ /* we will iterate q tags for this particular id */
2011+ if ((rc = mdb_cursor_open (txn -> mdb_txn , db , & cur ))) {
2012+ fprintf (stderr , "ndb_migrate_user_search_indices: mdb_cursor_open failed, error %d\n" , rc );
2013+ return 0 ;
2014+ }
2015+
2016+ buffer [0 ] = 'q' ;
2017+ memcpy (& buffer [1 ], note_id , 32 );
2018+ memset (& buffer [33 ], 0x00 , 8 );
2019+
2020+ k .mv_data = buffer ;
2021+ k .mv_size = sizeof (buffer );
2022+ v .mv_data = NULL ;
2023+ v .mv_size = 0 ;
2024+
2025+ if (mdb_cursor_get (cur , & k , & v , MDB_SET_RANGE ))
2026+ goto cleanup ;
2027+
2028+ for (;;) {
2029+ keybuf = (unsigned char * )k .mv_data ;
2030+ if (k .mv_size < sizeof (buffer ))
2031+ break ;
2032+ if (keybuf [0 ] != 'q' )
2033+ break ;
2034+ if (memcmp (& keybuf [1 ], note_id , 32 ) != 0 )
2035+ break ;
2036+ (* count )++ ;
2037+
2038+ if (mdb_cursor_get (cur , & k , & v , MDB_NEXT ))
2039+ break ;
2040+ }
2041+
2042+ cleanup :
2043+ mdb_cursor_close (cur );
2044+ return 1 ;
2045+ }
2046+
2047+ /* count quotes and add them to a metadata builder.
2048+ * we assume there is no existing quotes entry */
2049+ static int ndb_note_meta_builder_count_quotes (struct ndb_txn * txn ,
2050+ unsigned char * note_id ,
2051+ struct ndb_note_meta_builder * builder )
2052+ {
2053+ uint32_t count ;
2054+ struct ndb_note_meta_entry * entry ;
2055+
2056+ if (!ndb_count_quotes (txn , note_id , & count ))
2057+ return 0 ;
2058+
2059+ if (count == 0 )
2060+ return 1 ;
2061+
2062+ if (!(entry = ndb_note_meta_add_entry (builder )))
2063+ return 0 ;
2064+
2065+ ndb_note_meta_quotes_set (entry , count );
2066+
2067+ return 1 ;
2068+ }
2069+
2070+ /* count all of the reactions on a note and add it to metadata in progress */
2071+ static void ndb_note_meta_builder_count_reactions (struct ndb_txn * txn , struct ndb_note_meta_builder * builder )
2072+ {
2073+ }
2074+
19952075
19962076// Migrations
19972077//
19982078
2079+ /* switch from flatbuffer stats to custom v2 */
2080+ static int ndb_migrate_reaction_stats (struct ndb_txn * txn )
2081+ {
2082+ MDB_val k , k2 , v , v2 ;
2083+ MDB_cursor * cur ;
2084+ MDB_dbi db ;
2085+ unsigned char * id ;
2086+ unsigned char buffer [4096 ];
2087+ int rc ;
2088+ struct ndb_note_meta_builder builder ;
2089+ struct ndb_note_meta * meta ;
2090+
2091+ db = txn -> lmdb -> dbs [NDB_DB_META ];
2092+
2093+ if ((rc = mdb_cursor_open (txn -> mdb_txn , db , & cur ))) {
2094+ fprintf (stderr , "ndb_migrate_reaction_stats: mdb_cursor_open failed, error %d\n" , rc );
2095+ return -1 ;
2096+ }
2097+
2098+ /* loop through every metadata entry */
2099+ while (mdb_cursor_get (cur , & k , & v , MDB_NEXT ) == 0 ) {
2100+ ndb_note_meta_builder_init (& builder , buffer , sizeof (buffer ));
2101+
2102+ id = (unsigned char * )k .mv_data ;
2103+ ndb_note_meta_builder_count_reactions (txn , & builder );
2104+ ndb_note_meta_builder_count_quotes (txn , id , & builder );
2105+ ndb_note_meta_build (& builder , & meta );
2106+
2107+ /* no counts found, just delete this entry */
2108+ if (ndb_note_meta_entries_count (meta ) == 0 ) {
2109+ if ((rc = mdb_del (txn -> mdb_txn , db , & k , & v ))) {
2110+ ndb_debug ("delete old metadata entry failed: %s\n" , mdb_strerror (rc ));
2111+ return -1 ;
2112+ }
2113+ continue ;
2114+ }
2115+
2116+ k2 .mv_data = (unsigned char * )id ;
2117+ k2 .mv_size = 32 ;
2118+ v2 .mv_data = meta ;
2119+ v2 .mv_size = ndb_note_meta_total_size (meta );
2120+
2121+ /* set entry */
2122+ if ((rc = mdb_put (txn -> mdb_txn , db , & k2 , & v2 , 0 ))) {
2123+ ndb_debug ("migrate metadata entry failed on write: %s\n" , mdb_strerror (rc ));
2124+ }
2125+ }
2126+
2127+ return 1 ;
2128+ }
2129+
19992130// This was before we had note_profile_pubkey{,_kind} indices. Let's create them.
20002131static int ndb_migrate_profile_indices (struct ndb_txn * txn )
20012132{
@@ -2330,6 +2461,7 @@ static struct ndb_migration MIGRATIONS[] = {
23302461 { .fn = ndb_migrate_lower_user_search_indices },
23312462 { .fn = ndb_migrate_utf8_profile_names },
23322463 { .fn = ndb_migrate_profile_indices },
2464+ //{ .fn = ndb_migrate_reaction_stats },
23332465};
23342466
23352467
@@ -2451,7 +2583,7 @@ int ndb_write_last_profile_fetch(struct ndb *ndb, const unsigned char *pubkey,
24512583// When doing cursor scans from greatest to lowest, this function positions the
24522584// cursor at the first element before descending. MDB_SET_RANGE puts us right
24532585// after the first element, so we have to go back one.
2454- static int ndb_cursor_start (MDB_cursor * cur , MDB_val * k , MDB_val * v )
2586+ int ndb_cursor_start (MDB_cursor * cur , MDB_val * k , MDB_val * v )
24552587{
24562588 int rc ;
24572589 // Position cursor at the next key greater than or equal to the
@@ -3354,6 +3486,7 @@ int ndb_set_note_meta(struct ndb *ndb, const unsigned char *id, void *meta, size
33543486 return ndb_writer_queue_msg (& ndb -> writer , & msg );
33553487}
33563488
3489+
33573490int ndb_writer_set_note_meta (struct ndb_txn * txn , const unsigned char * id , struct ndb_note_meta * meta )
33583491{
33593492 MDB_val k , v ;
0 commit comments