Skip to content

Commit

Permalink
Use latest version of MongoDB API for v5.1+ compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Geod24 committed Jun 25, 2023
1 parent 93c6af1 commit 0268b22
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
6 changes: 4 additions & 2 deletions source/dubregistry/cache.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class URLCache {
import dubregistry.mongodb : databaseName, getMongoClient;
m_db = getMongoClient();
m_entries = m_db.getDatabase(databaseName)["urlcache.entries"];
m_entries.ensureIndex([tuple("url", 1)]);
m_entries.createIndex(IndexModel().add("url", 1));
}

void clearEntry(URL url)
Expand Down Expand Up @@ -165,7 +165,9 @@ class URLCache {
if (pet) entry.etag = *pet;
entry.data = BsonBinData(BsonBinData.Type.Generic, cast(immutable)rawdata);
entry.headers = result_headers;
m_entries.update(["_id": entry._id], entry, UpdateFlags.Upsert);
UpdateOptions opts;
opts.upsert = true;
m_entries.replaceOne(["_id": entry._id], entry, opts);
result = createMemoryStream(rawdata, false);
break;
}
Expand Down
36 changes: 20 additions & 16 deletions source/dubregistry/dbcontroller.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ class DbController {
//

// create indices
m_packages.ensureIndex([tuple("name", 1)], IndexFlags.Unique);
m_packages.ensureIndex([tuple("stats.score", 1)]);
m_downloads.ensureIndex([tuple("package", 1), tuple("version", 1)]);
m_packages.createIndexes([
IndexModel().add("name", 1).withOptions(IndexOptions.unique),
IndexModel().add("stats.score", 1)
]);
m_downloads.createIndexes(IndexModel().add("package", 1).add("version", 1));

// add current text index
immutable keyWeights = [
Expand Down Expand Up @@ -78,7 +80,9 @@ class DbController {
void addOrSetPackage(ref DbPackage pack)
{
enforce(pack._id != BsonObjectID.init, "Cannot update a packag with no ID.");
m_packages.update(["_id": pack._id], pack, UpdateFlags.upsert);
UpdateOptions opts;
opts.upsert = true;
m_packages.replaceOne(["_id": pack._id], pack, opts);
}

DbPackage getPackage(string packname)
Expand Down Expand Up @@ -137,22 +141,22 @@ class DbController {

void removePackage(string packname, BsonObjectID user)
{
m_packages.remove(["name": Bson(packname), "owner": Bson(user)]);
m_packages.deleteOne(["name": Bson(packname), "owner": Bson(user)]);
}

void setPackageErrors(string packname, string[] error...)
{
m_packages.update(["name": packname], ["$set": ["errors": error]]);
m_packages.updateOne(["name": packname], ["$set": ["errors": error]]);
}

void setPackageCategories(string packname, string[] categories...)
{
m_packages.update(["name": packname], ["$set": ["categories": categories]]);
m_packages.updateOne(["name": packname], ["$set": ["categories": categories]]);
}

void setPackageRepository(string packname, DbRepository repo)
{
m_packages.update(["name": packname], ["$set": ["repository": repo]]);
m_packages.updateOne(["name": packname], ["$set": ["repository": repo]]);
}

void setPackageLogo(string packname, bdata_t png)
Expand All @@ -161,7 +165,7 @@ class DbController {

if (png.length) {
auto id = BsonObjectID.generate();
m_files.insert([
m_files.insertOne([
"_id": Bson(id),
"data": Bson(BsonBinData(BsonBinData.Type.generic, png))
]);
Expand All @@ -174,15 +178,15 @@ class DbController {
// remove existing logo file
auto l = m_packages.findOne(["name": packname], ["logo": 1]);
if (!l.isNull && !l.tryIndex("logo").isNull)
m_files.remove(["_id": l["logo"]]);
m_files.deleteOne(["_id": l["logo"]]);

// set the new logo
m_packages.update(["name": packname], update);
m_packages.updateOne(["name": packname], update);
}

void setDocumentationURL(string packname, string documentationURL)
{
m_packages.update(["name": packname], ["$set": ["documentationURL": documentationURL]]);
m_packages.updateOne(["name": packname], ["$set": ["documentationURL": documentationURL]]);
}

bdata_t getPackageLogo(string packname, out bdata_t rev)
Expand Down Expand Up @@ -243,7 +247,7 @@ class DbController {
void updateVersion(string packname, DbPackageVersion ver)
{
assert(ver.version_.startsWith("~") || ver.version_.isValidVersion());
m_packages.update(["name": packname, "versions.version": ver.version_], ["$set": ["versions.$": ver]]);
m_packages.updateOne(["name": packname, "versions.version": ver.version_], ["$set": ["versions.$": ver]]);
}

bool hasVersion(string packname, string ver)
Expand Down Expand Up @@ -306,7 +310,7 @@ class DbController {
download.version_ = ver;
download.time = Clock.currTime(UTC());
download.userAgent = user_agent;
m_downloads.insert(download);
m_downloads.insertOne(download);
return download._id;
}

Expand All @@ -323,7 +327,7 @@ class DbController {
{
stats.updatedAt = Clock.currTime(UTC());
logDebug("updatePackageStats(%s, %s)", packId, stats);
m_packages.update(["_id": packId], ["$set": ["stats": stats]]);
m_packages.updateOne(["_id": packId], ["$set": ["stats": stats]]);
}

DbDownloadStats aggregateDownloadStats(BsonObjectID packId, string ver = null)
Expand Down Expand Up @@ -417,7 +421,7 @@ class DbController {
.uniq!((a, b) => a.version_ == b.version_)
.array;
if (p.versions != newversions)
m_packages.update(["_id": p._id], ["$set": ["versions": newversions]]);
m_packages.updateOne(["_id": p._id], ["$set": ["versions": newversions]]);
}
}
}
Expand Down

0 comments on commit 0268b22

Please sign in to comment.