@@ -204,6 +204,10 @@ VxCoreError BundledFolderManager::GetFolderConfig(const std::string &folder_path
204204 return error;
205205 }
206206
207+ // Lazy sync: populate MetadataStore with this folder's data
208+ std::string parent_id = GetParentFolderId (folder_path);
209+ SyncFolderToStore (folder_path, *config, parent_id);
210+
207211 *out_config = config.get ();
208212 config_cache_[folder_path] = std::move (config);
209213 return VXCORE_OK;
@@ -1391,4 +1395,69 @@ VxCoreError BundledFolderManager::SyncMetadataStoreFromConfigs() {
13911395 }
13921396}
13931397
1398+ std::string BundledFolderManager::GetParentFolderId (const std::string &folder_path) {
1399+ if (folder_path.empty () || folder_path == " ." ) {
1400+ return " " ; // Root folder has no parent
1401+ }
1402+
1403+ const auto [parent_path, folder_name] = SplitPath (folder_path);
1404+ (void )folder_name; // Unused
1405+
1406+ FolderConfig *parent_config = GetCachedConfig (parent_path);
1407+ if (parent_config) {
1408+ return parent_config->id ;
1409+ }
1410+
1411+ // Parent not in cache - try to load it (which will also trigger lazy sync for parent)
1412+ VxCoreError error = GetFolderConfig (parent_path, &parent_config);
1413+ if (error == VXCORE_OK && parent_config) {
1414+ return parent_config->id ;
1415+ }
1416+
1417+ return " " ; // Parent not found
1418+ }
1419+
1420+ void BundledFolderManager::SyncFolderToStore (const std::string &folder_path,
1421+ const FolderConfig &config,
1422+ const std::string &parent_folder_id) {
1423+ auto *store = notebook_->GetMetadataStore ();
1424+ if (!store) {
1425+ return ; // No store available
1426+ }
1427+
1428+ // Check if folder already exists in store
1429+ auto existing_folder = store->GetFolder (config.id );
1430+ if (existing_folder.has_value ()) {
1431+ // Folder exists - check if we need to update sync state
1432+ // For now, we assume the store is up-to-date if folder exists
1433+ // (write-through cache should keep it in sync)
1434+ VXCORE_LOG_DEBUG (" SyncFolderToStore: Folder already in store: id=%s, path=%s" ,
1435+ config.id .c_str (), folder_path.c_str ());
1436+ return ;
1437+ }
1438+
1439+ VXCORE_LOG_INFO (" SyncFolderToStore: Adding folder to store: id=%s, path=%s" , config.id .c_str (),
1440+ folder_path.c_str ());
1441+
1442+ // Create folder record in store
1443+ StoreFolderRecord folder_record = ToStoreFolderRecord (config, parent_folder_id);
1444+ if (!store->CreateFolder (folder_record)) {
1445+ VXCORE_LOG_WARN (" SyncFolderToStore: Failed to create folder in store: id=%s" ,
1446+ config.id .c_str ());
1447+ return ;
1448+ }
1449+
1450+ // Create file records in store
1451+ for (const auto &file : config.files ) {
1452+ StoreFileRecord file_record = ToStoreFileRecord (file, config.id );
1453+ if (!store->CreateFile (file_record)) {
1454+ VXCORE_LOG_WARN (" SyncFolderToStore: Failed to create file in store: id=%s" , file.id .c_str ());
1455+ // Continue anyway - best effort
1456+ }
1457+ }
1458+
1459+ VXCORE_LOG_DEBUG (" SyncFolderToStore: Synced folder with %zu files: path=%s" , config.files .size (),
1460+ folder_path.c_str ());
1461+ }
1462+
13941463} // namespace vxcore
0 commit comments