jcs
/subtext
/amendments
/435
db: When caching boards, reindex if needed and store latest post date
Move these cache operations to main after logger has inited since
        these can take a while.
    jcs made amendment 435 over 2 years ago
--- db.c	Wed Mar 15 15:29:46 2023
+++ db.c	Fri Mar 17 15:32:41 2023
@@ -23,6 +23,7 @@
 #include "bile.h"
 #include "db.h"
 #include "folder.h"
+#include "logger.h"
 #include "mail.h"
 #include "user.h"
 #include "util.h"
@@ -256,11 +257,8 @@ db_init(Str255 path, short vrefnum, struct bile *bile)
 		return NULL;
 	}
 	
-	if (!was_new) {
+	if (!was_new)
 		db_config_load(tdb);
-		db_cache_boards(tdb);
-		db_cache_folders(tdb);
-	}
 	
 	memcpy(newfullpath, fullpath, sizeof(newfullpath));
 	PtoCstr(newfullpath);
@@ -752,6 +750,7 @@ void
 db_cache_boards(struct db *tdb)
 {
 	Str255 db_filename, board_filename;
+	struct board_id_time_map first_map;
 	size_t n, size;
 	unsigned long *ids;
 	struct bile_object *obj;
@@ -806,7 +805,7 @@ db_cache_boards(struct db *tdb)
 		tdb->boards[n].bile = bile_open(board_filename,
 		  tdb->bile->vrefnum);
 		if (tdb->boards[n].bile != NULL)
-			continue;
+			goto opened;
 
 		PtoCstr(board_filename);
 		
@@ -829,6 +828,22 @@ db_cache_boards(struct db *tdb)
 		if (tdb->boards[n].bile == NULL)
 			panic("Failed to create board file %s: %d",
 			  board_filename, bile_error(NULL));
+
+opened:
+		size = bile_read(tdb->boards[n].bile,
+		  BOARD_SORTED_ID_MAP_RTYPE, 1, &first_map, sizeof(first_map));
+		if (size != sizeof(first_map)) {
+			logger_printf("[db] Reindexing ids on board %s",
+			  tdb->boards[n].name);
+			board_index_sorted_post_ids(&tdb->boards[n], NULL);
+			size = bile_read(tdb->boards[n].bile,
+			  BOARD_SORTED_ID_MAP_RTYPE, 1, &first_map, sizeof(first_map));
+			if (size != sizeof(first_map)) {
+				tdb->boards[n].last_post_at = 0;
+				continue;
+			}
+		}
+		tdb->boards[n].last_post_at = first_map.time;
 	}
 }
 
--- main.c	Wed Mar 15 17:28:49 2023
+++ main.c	Fri Mar 17 15:29:35 2023
@@ -104,6 +104,9 @@ main(void)
 	logger_init();
 	logger_update_title();
 
+	db_cache_boards(db);
+	db_cache_folders(db);
+
 	main_menu_init();
 	
 	zone_size = (unsigned long)CurStackBase - (unsigned long)ApplZone;