Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use latest version of MongoDB API for v5.1+ compatibility #544

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors "Sönke Ludwig" "Martin Nowak" "Anton Fediushin aka ohdatboi" \
"see GitHub for more"
license "BSL-1.0"

dependency "vibe-d" version="~>0.9.5"
dependency "vibe-d" version="~>0.9.7-alpha.3"
dependency "dub" version="~>1.33.0"
dependency "userman" version="~>0.4.0"
dependency "uritemplate" version="~>1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"uritemplate": "1.0.0",
"userman": "0.4.2",
"vibe-core": "2.2.0",
"vibe-d": "0.9.6"
"vibe-d": "0.9.7-alpha.3"
}
}
12 changes: 7 additions & 5 deletions source/dubregistry/cache.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ 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)
{
m_entries.remove(["url": url.toString()]);
m_entries.deleteOne(["url": url.toString()]);
}

void get(URL url, scope void delegate(scope InputStream str) @safe callback,
Expand Down Expand Up @@ -94,7 +94,7 @@ class URLCache {
if (!be.isNull()) {
// invalidate out of date cache entries
if (be["_id"].get!BsonObjectID.timeStamp < now - m_maxCacheTime)
m_entries.remove(["_id": be["_id"]]);
m_entries.deleteOne(["_id": be["_id"]]);

deserializeBson(entry, be);
if (mode == CacheMatchMode.always) {
Expand Down Expand Up @@ -129,6 +129,8 @@ class URLCache {
result_headers[header] = v;
}

UpdateOptions upsert;
upsert.upsert = true;
switch (res.statusCode) {
default:
throw new Exception("Unexpected reply for '"~url.toString().black~"': "~httpStatusText(res.statusCode));
Expand All @@ -153,7 +155,7 @@ class URLCache {

entry.redirectURL = url.toString();
entry.headers = result_headers;
m_entries.update(["_id": entry._id], entry, UpdateFlags.Upsert);
m_entries.replaceOne(["_id": entry._id], entry, upsert);
break;
case HTTPStatus.ok:
auto pet = "ETag" in res.headers;
Expand All @@ -165,7 +167,7 @@ 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);
m_entries.replaceOne(["_id": entry._id], entry, upsert);
result = createMemoryStream(rawdata, false);
break;
}
Expand Down
46 changes: 26 additions & 20 deletions source/dubregistry/dbcontroller.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ 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)]);
IndexOptions opt;
opt.unique = true;
m_packages.createIndexes([
IndexModel().add("name", 1).withOptions(opt),
IndexModel().add("stats.score", 1)
]);
m_downloads.createIndex(IndexModel().add("package", 1).add("version", 1));

// add current text index
immutable keyWeights = [
Expand All @@ -63,22 +67,24 @@ class DbController {
auto cmd = Bson.emptyObject;
cmd["createIndexes"] = Bson("packages");
cmd["indexes"] = [Bson(fts)];
auto res = db.runCommand(cmd);
enforce(res["ok"].opt!double == 1.0, "Failed to create search index.\n"~res.toString);
// Create search index
db.runCommandChecked(cmd);
}

void addPackage(ref DbPackage pack)
{
enforce(m_packages.findOne(["name": pack.name], ["_id": true]).isNull(), "A package with the same name is already registered.");
if (pack._id == BsonObjectID.init)
pack._id = BsonObjectID.generate();
m_packages.insert(pack);
m_packages.insertOne(pack);
}

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 +143,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 +167,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 +180,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 @@ -237,13 +243,13 @@ class DbController {
void removeVersion(string packname, string ver)
{
assert(ver.startsWith("~") || ver.isValidVersion());
m_packages.update(["name": packname], ["$pull": ["versions": ["version": ver]]]);
m_packages.updateOne(["name": packname], ["$pull": ["versions": ["version": ver]]]);
}

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 +312,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 +329,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 +423,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
5 changes: 4 additions & 1 deletion source/dubregistry/mongodb.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import vibe.core.log;
import vibe.db.mongo.client : MongoClient;
import vibe.db.mongo.mongo : connectMongoDB;
import vibe.db.mongo.settings : MongoClientSettings, MongoAuthMechanism, parseMongoDBUrl;
import std.exception : enforce;
import std.typecons : Nullable;

string databaseName = "vpmreg";
Expand All @@ -17,7 +18,9 @@ MongoClientSettings mongoSettings() {
auto mongodbURI = environment.get("MONGODB_URI", environment.get("MONGO_URI", "mongodb://127.0.0.1"));
logInfo("Found mongodbURI: %s", mongodbURI);
MongoClientSettings settings;
parseMongoDBUrl(settings, mongodbURI);
enforce(parseMongoDBUrl(settings, mongodbURI),
"Could not parse connection string (check MONGODB_URI or MONGO_URI): "
~ mongodbURI);
settings.authMechanism = MongoAuthMechanism.scramSHA1;
if (settings.database.length > 0)
databaseName = settings.database;
Expand Down
Loading