#if FIKO_ISTAKIP_MONGO #include "fiko/istakip/depo_mongo.hpp" #include #include #include #include #include #include #include #include namespace fiko::istakip { using bsoncxx::builder::basic::kvp; using bsoncxx::builder::basic::make_array; using bsoncxx::builder::basic::make_document; struct DepoMongo::Ic { mongocxx::client istemci; mongocxx::database db; mongocxx::collection kayitlar; mongocxx::collection sistem; }; static mongocxx::instance& mongoOrnek() { static mongocxx::instance ornek; // süreç başına bir kez (mongocxx şartı) return ornek; } DepoMongo::DepoMongo(const std::string& uri) : ic_(std::make_unique()) { try { mongoOrnek(); mongocxx::uri u{uri}; ic_->istemci = mongocxx::client{u}; const auto dbAd = u.database().empty() ? std::string("fiko_istakip") : u.database(); ic_->db = ic_->istemci[dbAd]; ic_->kayitlar = ic_->db["kayitlar"]; ic_->sistem = ic_->db["sistem"]; ic_->kayitlar.create_index(make_document(kvp("surum", 1))); ic_->kayitlar.create_index(make_document(kvp("tur", 1))); acik_ = true; } catch (const std::exception& e) { hata_ = e.what(); } } DepoMongo::~DepoMongo() = default; static bsoncxx::document::value kayitBson(const IsKayit& k) { bsoncxx::builder::basic::array alanlar; for (const auto& [a, d] : k.alanlar) alanlar.append(make_document(kvp("a", a), kvp("d", d))); return make_document(kvp("_id", (int64_t)k.kimlik), kvp("tur", k.tur), kvp("surum", (int64_t)k.surum), kvp("silindi", k.silindi), kvp("alanlar", alanlar.extract())); } static IsKayit bsonKayit(const bsoncxx::document::view& v) { IsKayit k; k.kimlik = (uint64_t)v["_id"].get_int64().value; k.tur = std::string(v["tur"].get_string().value); k.surum = (uint64_t)v["surum"].get_int64().value; k.silindi = v["silindi"].get_bool().value; for (const auto& e : v["alanlar"].get_array().value) { const auto alan = e.get_document().value; k.alanlar.emplace_back(std::string(alan["a"].get_string().value), std::string(alan["d"].get_string().value)); } return k; } bool DepoMongo::kaydet(const IsKayit& k) { try { mongocxx::options::replace sec; sec.upsert(true); ic_->kayitlar.replace_one(make_document(kvp("_id", (int64_t)k.kimlik)), kayitBson(k), sec); return true; } catch (const std::exception& e) { hata_ = e.what(); return false; } } bool DepoMongo::bul(uint64_t kimlik, IsKayit& out) const { try { const auto v = ic_->kayitlar.find_one(make_document(kvp("_id", (int64_t)kimlik))); if (!v) return false; out = bsonKayit(v->view()); return true; } catch (const std::exception& e) { hata_ = e.what(); return false; } } std::vector DepoMongo::surumdenBeri(uint64_t surum) const { std::vector sonuc; try { mongocxx::options::find sec; sec.sort(make_document(kvp("surum", 1))); auto imlec = ic_->kayitlar.find( make_document(kvp("surum", make_document(kvp("$gt", (int64_t)surum)))), sec); for (const auto& v : imlec) sonuc.push_back(bsonKayit(v)); } catch (const std::exception& e) { hata_ = e.what(); } return sonuc; } static uint64_t enBuyuk(mongocxx::collection& kol, const std::string& alan, std::string& hata) { try { mongocxx::options::find sec; sec.sort(make_document(kvp(alan, -1))); sec.limit(1); auto imlec = kol.find(make_document(), sec); for (const auto& v : imlec) return (uint64_t)v[alan].get_int64().value; } catch (const std::exception& e) { hata = e.what(); } return 0; } uint64_t DepoMongo::sonSurum() const { return enBuyuk(ic_->kayitlar, "surum", hata_); } uint64_t DepoMongo::sonKimlik() const { return enBuyuk(ic_->kayitlar, "_id", hata_); } uint64_t DepoMongo::istemciSira(uint64_t istemciNo) const { try { const auto v = ic_->sistem.find_one(make_document(kvp("_id", (int64_t)istemciNo))); if (v) return (uint64_t)(*v)["sira"].get_int64().value; } catch (const std::exception& e) { hata_ = e.what(); } return 0; } bool DepoMongo::istemciSiraKaydet(uint64_t istemciNo, uint64_t sira) { try { mongocxx::options::replace sec; sec.upsert(true); ic_->sistem.replace_one( make_document(kvp("_id", (int64_t)istemciNo)), make_document(kvp("_id", (int64_t)istemciNo), kvp("sira", (int64_t)sira)), sec); return true; } catch (const std::exception& e) { hata_ = e.what(); return false; } } } // namespace fiko::istakip #endif // FIKO_ISTAKIP_MONGO