Commit a8558e8f authored by Valentina Fioretti's avatar Valentina Fioretti
Browse files

writing bson to file

parent f6879f20
Loading
Loading
Loading
Loading
+34 −10
Original line number Diff line number Diff line
@@ -69,6 +69,24 @@ MongoOutput_XYZ::~MongoOutput_XYZ() {
}

bool MongoOutput_XYZ::init() {
    
    gm.config->readInto(writeToMongoDB, "IO.FILETYPE.MONGODB.WriteToServer");

    auto now = chrono::system_clock::now();
    auto t = chrono::system_clock::to_time_t(now);
    stringstream ss;
    ss << "bson_output_" 
       << hash<std::thread::id>{}(this_thread::get_id()) << "_"
       << put_time(localtime(&t), "%Y%m%d%H%M%S") << ".bson";
    local_bson_filename = ss.str();

    if (!writeToMongoDB) {
        bson_local_file.open(local_bson_filename, ios::binary);
        if (!bson_local_file) {
            cerr << "Failed to open BSON output file: " << local_bson_filename << endl;
            return false;
        }
    }
    cout << "MongoDB init end" << endl;
    return true;
}
@@ -181,16 +199,7 @@ void MongoOutput_XYZ::writeData(CXYZHit* hitting, G4int i) {
        gtime_exit = time; //18

        // Construct MongoDB document from data
        //auto client = pool.acquire();  // Get thread-safe client
        //auto database = (*client)[database_name];
        outputXYZCollectionNameMongoDB_base = (char*)gm.GetOutputMongoXYZCollectionName().data();
        //collection = database[outputXYZCollectionNameMongoDB_base];
        
        //auto client = pool.acquire();
        //auto database = client[database_name];
        auto client = pool.acquire();  // Get thread-safe client
        auto database = (*client)[database_name];
        auto collection = database[outputXYZCollectionNameMongoDB_base];
        
        bsoncxx::builder::basic::document doc{};
        doc.append(kvp("EVT_ID", std::to_string(evtid)));
@@ -222,16 +231,31 @@ void MongoOutput_XYZ::writeData(CXYZHit* hitting, G4int i) {
        doc.append(kvp("PROCESS_NAME", tprocname));

        if (!doc.view().empty()) {
            if (writeToMongoDB) {
                auto client = pool.acquire();  // Get thread-safe client
                auto database = (*client)[database_name];
                auto collection = database[outputXYZCollectionNameMongoDB_base];
                collection.insert_one(doc.view());
            } else {
                if (bson_local_file.is_open()) {
                    auto view = doc.view();
                    bson_local_file.write(reinterpret_cast<const char*>(view.data()), view.length());
                }
            }
        } else {
            cerr << "Error: Empty BSON document" << endl;
        }
        
        nrows_file++;
         
    }
}

bool MongoOutput_XYZ::close() {
    if (!writeToMongoDB && bson_local_file.is_open()) {
        bson_local_file.close();
        cout << "Local BSON file written: " << local_bson_filename << endl;
    }
    cout << "MongoDB: closing connection" << endl;
    return true;
}
+4 −0
Original line number Diff line number Diff line
@@ -44,6 +44,10 @@ public:

    mongocxx::pool pool{uri};
    
    bool writeToMongoDB; // true = upload, false = write local file only
    ofstream bson_local_file;
    string local_bson_filename;
    
protected:

    long lastevent;