MCPcopy Create free account
hub / github.com/XTXMarkets/ternfs / sampleFiles

Method sampleFiles

cpp/dbtools/ShardDBTools.cpp:519–694  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

517static constexpr uint64_t SAMPLE_RATE_SIZE_LIMIT = 10ull << 30;
518
519void ShardDBTools::sampleFiles(const std::string& dbPath, const std::string& outputFilePath) {
520 Logger logger(LogLevel::LOG_INFO, STDERR_FILENO, false, false);
521 std::ofstream outputStream(outputFilePath);
522 ALWAYS_ASSERT(outputStream.is_open(), "Failed to open output file for sampling results");
523 std::shared_ptr<XmonAgent> xmon;
524 Env env(logger, xmon, "ShardDBTools");
525 SharedRocksDB sharedDb(logger, xmon, dbPath, "");
526 sharedDb.registerCFDescriptors(ShardDB::getColumnFamilyDescriptors());
527 sharedDb.registerCFDescriptors(LogsDB::getColumnFamilyDescriptors());
528 rocksdb::Options rocksDBOptions;
529 rocksDBOptions.compression = rocksdb::kLZ4Compression;
530 rocksDBOptions.bottommost_compression = rocksdb::kZSTD;
531 sharedDb.openForReadOnly(rocksDBOptions);
532 auto db = sharedDb.db();
533 rocksdb::ReadOptions options;
534 auto edgesCf = sharedDb.getCF("edges");
535 auto filesCf = sharedDb.getCF("files");
536 auto spansCf = sharedDb.getCF("spans");
537 std::unordered_map<InodeId, FileInfo> files;
538 std::random_device rd;
539 std::mt19937 gen(rd());
540 std::uniform_real_distribution<double> realDis(0.0, 1.0);
541 {
542 std::unique_ptr<rocksdb::Iterator> it(db->NewIterator(options, filesCf));
543 for (it->SeekToFirst(); it->Valid(); it->Next()) {
544 auto fileK = ExternalValue<InodeIdKey>::FromSlice(it->key());
545 InodeId fileId = fileK().id();
546 if (fileId.type() != InodeType::FILE) {
547 continue;
548 }
549 auto fileV = ExternalValue<FileBody>::FromSlice(it->value());
550 auto logicalSize = fileV().fileSize();
551 if ( logicalSize == 0) { continue; } // nothing in this
552 auto probability = (double)logicalSize / SAMPLE_RATE_SIZE_LIMIT;
553 if( probability > realDis(gen)) {
554 files.insert(std::make_pair(fileId, FileInfo{fileV().mtime(), fileV().atime(), SizePerStorageClass{logicalSize,{StorageClassSize{0,0},StorageClassSize{0,0}},0}, std::max(logicalSize, SAMPLE_RATE_SIZE_LIMIT)}));
555 }
556 }
557 ROCKS_DB_CHECKED(it->status());
558 }
559 {
560 std::unique_ptr<rocksdb::Iterator> it(db->NewIterator(options, spansCf));
561 InodeId thisFile = NULL_INODE_ID;
562 LocationSize thisFileSize{StorageClassSize{0,0},StorageClassSize{0,0}, StorageClassSize{0,0}};
563 uint64_t thisFileInlineSize{0};
564 for (it->SeekToFirst(); it->Valid(); it->Next()) {
565 auto spanK = ExternalValue<SpanKey>::FromSlice(it->key());
566 if (spanK().fileId().type() != InodeType::FILE) {
567 continue;
568 }
569 if (spanK().fileId() != thisFile) {
570 auto file_it = files.find(thisFile);
571 if (file_it != files.end()) {
572 file_it->second.size.size = thisFileSize;
573 file_it->second.size.inMetadata = thisFileInlineSize;
574 }
575 thisFile = spanK().fileId();
576 thisFileSize = LocationSize{StorageClassSize{0,0},StorageClassSize{0,0}, StorageClassSize{0,0}};

Callers

nothing calls this directly

Calls 15

registerCFDescriptorsMethod · 0.80
openForReadOnlyMethod · 0.80
dbMethod · 0.80
getCFMethod · 0.80
NextMethod · 0.80
keyMethod · 0.80
typeMethod · 0.80
findMethod · 0.80
isInlineStorageMethod · 0.80
locationCountMethod · 0.80
blocksBodyReadOnlyMethod · 0.80
stripesMethod · 0.80

Tested by

no test coverage detected