@@ -702,7 +702,6 @@ int main(int argc, char *argv[]){
702702 }
703703
704704 // Check if this is a single DB or multiple DB instances
705- // Try to detect db/0, db/1, db/2, etc.
706705 std::vector<std::string> db_paths;
707706
708707 // First, check if db_path itself is a valid RocksDB
@@ -712,21 +711,34 @@ int main(int argc, char *argv[]){
712711 db_paths.push_back (test_path);
713712 std::cout << " Detected single database instance" << std::endl;
714713 } else {
715- // Check for multiple database instances (db/0, db/1, db/2, ...)
716- int db_index = 0 ;
717- while (true ) {
718- std::string db_inst_path = config.db_path + " /db/" + std::to_string (db_index);
714+ // Check if each subdirectory is a valid RocksDB (direct subdirectories like 0/, 1/, 2/)
715+ for (int db_index = 0 ; db_index < 1000 ; db_index++) { // 防止无限循环,设置上限
716+ std::string db_inst_path = config.db_path + " /" + std::to_string (db_index);
719717 if (DirectoryExists (db_inst_path) && DirectoryExists (db_inst_path + " /CURRENT" )) {
720718 db_paths.push_back (db_inst_path);
721- db_index++;
722- } else {
719+ } else if ( db_index > 0 && ! DirectoryExists (db_inst_path)) {
720+ // 如果目录不存在且已找到至少一个DB,则认为已到达末尾
723721 break ;
724722 }
725723 }
726724
725+ // 如果上面的检测失败,尝试经典的db/N格式
726+ if (db_paths.empty ()) {
727+ int db_index = 0 ;
728+ while (true ) {
729+ std::string db_inst_path = config.db_path + " /db/" + std::to_string (db_index);
730+ if (DirectoryExists (db_inst_path) && DirectoryExists (db_inst_path + " /CURRENT" )) {
731+ db_paths.push_back (db_inst_path);
732+ db_index++;
733+ } else {
734+ break ;
735+ }
736+ }
737+ }
738+
727739 if (db_paths.empty ()) {
728740 std::cerr << " Error: No valid database found at " << config.db_path << std::endl;
729- std::cerr << " Checked for single instance and db/0, db/1, ... directories" << std::endl;
741+ std::cerr << " Checked for single instance, direct subdirectories (0, 1, 2...), and db/0, db/1, ... directories" << std::endl;
730742 return 1 ;
731743 }
732744
0 commit comments