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

add mongodb implementation for the db package #1089

Merged
merged 1 commit into from
Sep 8, 2023
Merged

Conversation

p4u
Copy link
Member

@p4u p4u commented Sep 5, 2023

The db interface has now an implementation for mongodb. So instead of pebble a user or consumer of the vocdoni-node API can choose to use mongodb. Its not faster than local KV, but useful when the host runing the database has no persistent storage available, so a remote storage is required.

The mongodb url must be set on a env var, such as:

MONGODB_URL="mongodb://root:vocdoni@localhost:27017/" go run ./cmd/node -t mongodb -c dev

@p4u p4u force-pushed the f/add_mongo_as_kv branch 3 times, most recently from c83dc63 to fbebe9c Compare September 5, 2023 16:49
@p4u p4u force-pushed the f/add_mongo_as_kv branch 3 times, most recently from f2bc278 to 34650cd Compare September 6, 2023 15:39
Copy link
Contributor

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, though I'm not familiar with mongo, and I have no idea how performant this might be. If it's over the network, compared to pebble or leveldb on disk, we might see significantly worse performance just due to the round-trip nature of the network.

db/mongodb/mongodb.go Outdated Show resolved Hide resolved
db/mongodb/mongodb.go Outdated Show resolved Hide resolved
db/mongodb/mongodb.go Outdated Show resolved Hide resolved
db/mongodb/mongodb.go Outdated Show resolved Hide resolved
db/mongodb/mongodb.go Show resolved Hide resolved

filter := bson.M{}
if len(prefix) > 0 {
filter = bson.M{"_id": bson.M{"$regex": primitive.Regex{Pattern: "^" + string(prefix)}}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how slow this might be...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope mongo people made it efficient enough :/ But there is not other way, I have been looking for a better (non string regex bassed) mechanism, but there is nothing we can use.

primitive.E{Key: "value", Value: v},
}},
}
model := mongo.NewUpdateOneModel().SetFilter(bson.D{primitive.E{Key: "_id", Value: string(k)}}).SetUpsert(true).SetUpdate(update)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the point of a WriteTx is to bulk/batch writes; why are you using NewUpdateOneModel instead of NewUpdateManyModel? note that I'm not familiar with how mongo works at all, just wondering outloud.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the strategy is to accumulate single model updates in the memory batch.

Then on commit, I use collection.BulkWrite() that takes as parameter a list of models to apply.

This is the way it usually works for mongo, at least what I found it in the official docs.

The db interface has now an implementation for mongodb. So instead
of pebble a user or consumer of the vocdoni-node API can choose
to use mongodb. Its not faster than local KV, but useful when the host
runing the database has no persistent storage available, so a remote
storage is required.

The mongodb url must be set on a env var, such as:

MONGODB_URL="mongodb://root:vocdoni@localhost:27017/" go run ./cmd/node -t mongodb -c dev

Signed-off-by: p4u <[email protected]>
@p4u p4u merged commit ddea83d into main Sep 8, 2023
9 checks passed
@p4u p4u deleted the f/add_mongo_as_kv branch September 8, 2023 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants