2023-10-18 18:44:32 +00:00
|
|
|
#include "StarBTreeDatabase.hpp"
|
|
|
|
#include "StarTime.hpp"
|
|
|
|
#include "StarFile.hpp"
|
|
|
|
#include "StarVersionOptionParser.hpp"
|
|
|
|
|
|
|
|
using namespace Star;
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
try {
|
|
|
|
double startTime = Time::monotonicTime();
|
|
|
|
|
|
|
|
VersionOptionParser optParse;
|
|
|
|
optParse.setSummary("Repacks a Starbound BTree file to shrink its file size");
|
|
|
|
optParse.addArgument("input file path", OptionParser::Required, "Path to the BTree to be repacked");
|
|
|
|
optParse.addArgument("output filename", OptionParser::Optional, "Output BTree file");
|
|
|
|
|
|
|
|
auto opts = optParse.commandParseOrDie(argc, argv);
|
|
|
|
|
|
|
|
String bTreePath = opts.arguments.at(0);
|
|
|
|
String outputFilename = opts.arguments.get(1, bTreePath + ".repack");
|
|
|
|
|
|
|
|
outputFilename = File::relativeTo(File::fullPath(File::dirName(outputFilename)), File::baseName(outputFilename));
|
|
|
|
//open the old db
|
|
|
|
BTreeDatabase db;
|
2024-02-19 15:55:19 +00:00
|
|
|
db.setIODevice(File::open(bTreePath, IOMode::Read));
|
2023-10-18 18:44:32 +00:00
|
|
|
db.open();
|
|
|
|
|
|
|
|
//make a new db
|
|
|
|
BTreeDatabase newDb;
|
|
|
|
newDb.setBlockSize(db.blockSize());
|
|
|
|
newDb.setContentIdentifier(db.contentIdentifier());
|
|
|
|
newDb.setKeySize(db.keySize());
|
|
|
|
newDb.setAutoCommit(false);
|
|
|
|
|
2024-02-19 15:55:19 +00:00
|
|
|
newDb.setIODevice(File::open(outputFilename, IOMode::ReadWrite | IOMode::Truncate));
|
2023-10-18 18:44:32 +00:00
|
|
|
newDb.open();
|
2023-10-22 17:39:15 +00:00
|
|
|
coutf("Repacking {}...\n", bTreePath);
|
2023-10-18 18:44:32 +00:00
|
|
|
//copy the data over
|
2024-08-08 02:09:47 +00:00
|
|
|
unsigned count = 0, overwritten = 0;
|
|
|
|
auto visitor = [&](ByteArray key, ByteArray data) {
|
|
|
|
if (newDb.insert(key, data))
|
|
|
|
++overwritten;
|
2023-10-18 18:44:32 +00:00
|
|
|
++count;
|
2024-08-08 02:09:47 +00:00
|
|
|
};
|
|
|
|
auto errorHandler = [&](String const& error, std::exception const& e) {
|
|
|
|
coutf("{}: {}\n", error, e.what());
|
|
|
|
};
|
|
|
|
db.recoverAll(visitor, errorHandler);
|
2023-10-18 18:44:32 +00:00
|
|
|
|
|
|
|
//close the old db
|
|
|
|
db.close();
|
|
|
|
//commit and close the new db
|
|
|
|
newDb.commit();
|
|
|
|
newDb.close();
|
|
|
|
|
2024-08-08 02:09:47 +00:00
|
|
|
coutf("Repacked BTree to {} in {:.6f}s\n({} inserts, {} overwritten)\n", outputFilename, Time::monotonicTime() - startTime, count, overwritten);
|
2023-10-18 18:44:32 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
} catch (std::exception const& e) {
|
2023-10-22 17:39:15 +00:00
|
|
|
cerrf("Exception caught: {}\n", outputException(e, true));
|
2023-10-18 18:44:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|