db operation context 수정

This commit is contained in:
2024-06-25 10:13:50 +09:00
parent f4e6f8a415
commit 5352e6ba59

View File

@ -21,6 +21,7 @@ import (
type MongoClient struct {
db *mongo.Database
c *mongo.Client
ctx context.Context
}
type ConnectionInfo struct {
@ -132,17 +133,17 @@ func (mc MongoClient) Connected() bool {
func (mc MongoClient) Close() {
if mc.c != nil {
mc.c.Disconnect(context.Background())
mc.c.Disconnect(mc.ctx)
}
}
func (mc MongoClient) Drop() error {
return mc.db.Drop(context.Background())
return mc.db.Drop(mc.ctx)
}
func (mc MongoClient) DropIndex(coll CollectionName, name string) error {
matchcoll := mc.Collection(coll)
_, err := matchcoll.Indexes().DropOne(context.Background(), name)
_, err := matchcoll.Indexes().DropOne(mc.ctx, name)
if commanderr, ok := err.(mongo.CommandError); ok {
if commanderr.Code == 27 {
// 인덱스가 없는 것이므로 그냥 성공
@ -156,7 +157,7 @@ func (mc MongoClient) Watch(coll CollectionName, pipeline mongo.Pipeline, opts .
if len(opts) == 0 {
opts = []*options.ChangeStreamOptions{options.ChangeStream().SetFullDocument(options.UpdateLookup).SetMaxAwaitTime(0)}
}
return mc.Collection(coll).Watch(context.Background(), pipeline, opts...)
return mc.Collection(coll).Watch(mc.ctx, pipeline, opts...)
}
func (mc MongoClient) Collection(collname CollectionName) *mongo.Collection {
@ -164,13 +165,13 @@ func (mc MongoClient) Collection(collname CollectionName) *mongo.Collection {
}
func (mc MongoClient) AllAs(coll CollectionName, output any, opts ...*options.FindOptions) error {
cursor, err := mc.Collection(coll).Find(context.Background(), bson.D{}, opts...)
cursor, err := mc.Collection(coll).Find(mc.ctx, bson.D{}, opts...)
if err != nil {
return err
}
defer cursor.Close(context.Background())
defer cursor.Close(mc.ctx)
err = cursor.All(context.Background(), output)
err = cursor.All(mc.ctx, output)
if err != nil {
return err
}
@ -185,7 +186,7 @@ func (mc MongoClient) All(coll CollectionName, opts ...*options.FindOptions) ([]
}
func (mc MongoClient) FindOneAndDelete(coll CollectionName, filter bson.M, opts ...*options.FindOneAndDeleteOptions) (bson.M, error) {
result := mc.Collection(coll).FindOneAndDelete(context.Background(), filter, opts...)
result := mc.Collection(coll).FindOneAndDelete(mc.ctx, filter, opts...)
err := result.Err()
if err != nil {
if err == mongo.ErrNoDocuments {
@ -204,7 +205,7 @@ func (mc MongoClient) FindOneAndDelete(coll CollectionName, filter bson.M, opts
}
func (mc MongoClient) Delete(coll CollectionName, filter bson.M, opts ...*options.DeleteOptions) (bool, error) {
r, err := mc.Collection(coll).DeleteOne(context.Background(), filter, opts...)
r, err := mc.Collection(coll).DeleteOne(mc.ctx, filter, opts...)
if err != nil {
return false, err
}
@ -213,7 +214,7 @@ func (mc MongoClient) Delete(coll CollectionName, filter bson.M, opts ...*option
}
func (mc MongoClient) UnsetField(coll CollectionName, filter bson.M, doc bson.M) error {
_, err := mc.Collection(coll).UpdateOne(context.Background(), filter, bson.M{
_, err := mc.Collection(coll).UpdateOne(mc.ctx, filter, bson.M{
"$unset": doc,
})
return err
@ -225,7 +226,7 @@ func (mc MongoClient) DeleteMany(coll CollectionName, filters bson.D, opts ...*o
return 0, nil
}
result, err := mc.Collection(coll).DeleteMany(context.Background(), filters, opts...)
result, err := mc.Collection(coll).DeleteMany(mc.ctx, filters, opts...)
if err != nil {
return 0, err
}
@ -248,7 +249,7 @@ func (c *CommandInsertMany[T]) Exec(opts ...*options.InsertManyOptions) (int, er
}
func (mc MongoClient) InsertMany(coll CollectionName, documents []interface{}, opts ...*options.InsertManyOptions) (int, error) {
result, err := mc.Collection(coll).InsertMany(context.Background(), documents, opts...)
result, err := mc.Collection(coll).InsertMany(mc.ctx, documents, opts...)
if err != nil {
return 0, err
}
@ -257,7 +258,7 @@ func (mc MongoClient) InsertMany(coll CollectionName, documents []interface{}, o
}
func (mc MongoClient) UpdateMany(coll CollectionName, filter bson.M, doc bson.M, opts ...*options.UpdateOptions) (count int, err error) {
result, e := mc.Collection(coll).UpdateMany(context.Background(), filter, doc, opts...)
result, e := mc.Collection(coll).UpdateMany(mc.ctx, filter, doc, opts...)
if e != nil {
return 0, e
@ -281,7 +282,7 @@ func (m *JsonDefaultMashaller) MarshalBSON() ([]byte, error) {
}
func (mc MongoClient) Update(coll CollectionName, filter bson.M, doc interface{}, opts ...*options.UpdateOptions) (worked bool, newid interface{}, err error) {
result, e := mc.Collection(coll).UpdateOne(context.Background(), filter, doc, opts...)
result, e := mc.Collection(coll).UpdateOne(mc.ctx, filter, doc, opts...)
if e != nil {
return false, "", e
@ -304,7 +305,7 @@ func (mc MongoClient) UpsertOne(coll CollectionName, filter bson.M, doc interfac
}
func (mc MongoClient) FindOneAs(coll CollectionName, filter bson.M, out interface{}, opts ...*options.FindOneOptions) error {
err := mc.Collection(coll).FindOne(context.Background(), filter, opts...).Decode(out)
err := mc.Collection(coll).FindOne(mc.ctx, filter, opts...).Decode(out)
if err == mongo.ErrNoDocuments {
err = nil
}
@ -312,7 +313,7 @@ func (mc MongoClient) FindOneAs(coll CollectionName, filter bson.M, out interfac
}
func (mc MongoClient) FindOne(coll CollectionName, filter bson.M, opts ...*options.FindOneOptions) (doc bson.M, err error) {
result := mc.Collection(coll).FindOne(context.Background(), filter, opts...)
result := mc.Collection(coll).FindOne(mc.ctx, filter, opts...)
tmp := make(map[string]interface{})
err = result.Decode(&tmp)
if err == nil {
@ -325,7 +326,7 @@ func (mc MongoClient) FindOne(coll CollectionName, filter bson.M, opts ...*optio
}
func (mc MongoClient) FindOneAndUpdateAs(coll CollectionName, filter bson.M, doc bson.M, out interface{}, opts ...*options.FindOneAndUpdateOptions) error {
result := mc.Collection(coll).FindOneAndUpdate(context.Background(), filter, doc, opts...)
result := mc.Collection(coll).FindOneAndUpdate(mc.ctx, filter, doc, opts...)
err := result.Decode(out)
if err == nil {
return nil
@ -339,7 +340,7 @@ func (mc MongoClient) FindOneAndUpdateAs(coll CollectionName, filter bson.M, doc
}
func (mc MongoClient) FindOneAndUpdate(coll CollectionName, filter bson.M, doc bson.M, opts ...*options.FindOneAndUpdateOptions) (olddoc bson.M, err error) {
result := mc.Collection(coll).FindOneAndUpdate(context.Background(), filter, doc, opts...)
result := mc.Collection(coll).FindOneAndUpdate(mc.ctx, filter, doc, opts...)
tmp := make(map[string]interface{})
err = result.Decode(&tmp)
if err == nil {
@ -352,7 +353,7 @@ func (mc MongoClient) FindOneAndUpdate(coll CollectionName, filter bson.M, doc b
}
func (mc MongoClient) Exists(coll CollectionName, filter bson.M) (bool, error) {
cnt, err := mc.Collection(coll).CountDocuments(context.Background(), filter, options.Count().SetLimit(1))
cnt, err := mc.Collection(coll).CountDocuments(mc.ctx, filter, options.Count().SetLimit(1))
if err != nil {
return false, err
}
@ -360,14 +361,14 @@ func (mc MongoClient) Exists(coll CollectionName, filter bson.M) (bool, error) {
}
func (mc MongoClient) SearchText(coll CollectionName, text string, opts ...*options.FindOptions) ([]bson.M, error) {
cursor, err := mc.Collection(coll).Find(context.Background(), bson.M{"$text": bson.M{"$search": text}}, opts...)
cursor, err := mc.Collection(coll).Find(mc.ctx, bson.M{"$text": bson.M{"$search": text}}, opts...)
if err != nil {
return nil, err
}
defer cursor.Close(context.Background())
defer cursor.Close(mc.ctx)
var output []bson.M
err = cursor.All(context.Background(), &output)
err = cursor.All(mc.ctx, &output)
if err != nil {
return nil, err
}
@ -376,14 +377,14 @@ func (mc MongoClient) SearchText(coll CollectionName, text string, opts ...*opti
}
func (mc MongoClient) FindAll(coll CollectionName, filter bson.M, opts ...*options.FindOptions) ([]bson.M, error) {
cursor, err := mc.Collection(coll).Find(context.Background(), filter, opts...)
cursor, err := mc.Collection(coll).Find(mc.ctx, filter, opts...)
if err != nil {
return nil, err
}
defer cursor.Close(context.Background())
defer cursor.Close(mc.ctx)
var output []bson.M
err = cursor.All(context.Background(), &output)
err = cursor.All(mc.ctx, &output)
if err != nil {
return nil, err
}
@ -392,13 +393,13 @@ func (mc MongoClient) FindAll(coll CollectionName, filter bson.M, opts ...*optio
}
func (mc MongoClient) FindAllAs(coll CollectionName, filter bson.M, output interface{}, opts ...*options.FindOptions) error {
cursor, err := mc.Collection(coll).Find(context.Background(), filter, opts...)
cursor, err := mc.Collection(coll).Find(mc.ctx, filter, opts...)
if err != nil {
return err
}
defer cursor.Close(context.Background())
defer cursor.Close(mc.ctx)
err = cursor.All(context.Background(), output)
err = cursor.All(mc.ctx, output)
if err != nil {
return err
}
@ -407,13 +408,13 @@ func (mc MongoClient) FindAllAs(coll CollectionName, filter bson.M, output inter
func (mc MongoClient) MakeExpireIndex(coll CollectionName, expireSeconds int32) error {
matchcoll := mc.Collection(coll)
indices, err := matchcoll.Indexes().List(context.Background(), options.ListIndexes().SetMaxTime(time.Second))
indices, err := matchcoll.Indexes().List(mc.ctx, options.ListIndexes().SetMaxTime(time.Second))
if err != nil {
return err
}
allindices := make([]interface{}, 0)
err = indices.All(context.Background(), &allindices)
err = indices.All(mc.ctx, &allindices)
if err != nil {
return err
}
@ -445,7 +446,7 @@ IndexSearchLabel:
if exp == expireSeconds {
return nil
}
_, err = matchcoll.Indexes().DropOne(context.Background(), tsname)
_, err = matchcoll.Indexes().DropOne(mc.ctx, tsname)
if err != nil {
return err
}
@ -456,24 +457,24 @@ IndexSearchLabel:
Options: options.Index().SetExpireAfterSeconds(expireSeconds),
}
_, err = matchcoll.Indexes().CreateOne(context.Background(), mod)
_, err = matchcoll.Indexes().CreateOne(mc.ctx, mod)
return err
}
func (mc MongoClient) makeIndicesWithOption(coll CollectionName, indices map[string]bson.D, opts ...*options.IndexOptions) error {
collection := mc.Collection(coll)
cursor, err := collection.Indexes().List(context.Background(), options.ListIndexes().SetMaxTime(time.Second))
cursor, err := collection.Indexes().List(mc.ctx, options.ListIndexes().SetMaxTime(time.Second))
if err != nil {
return err
}
defer cursor.Close(context.Background())
defer cursor.Close(mc.ctx)
found := make(map[string]bool)
for k := range indices {
found[k] = false
}
for cursor.TryNext(context.Background()) {
for cursor.TryNext(mc.ctx) {
rawval := cursor.Current
name := rawval.Lookup("name").StringValue()
if _, ok := indices[name]; ok {
@ -497,7 +498,7 @@ func (mc MongoClient) makeIndicesWithOption(coll CollectionName, indices map[str
}
}
_, err = collection.Indexes().CreateOne(context.Background(), mod)
_, err = collection.Indexes().CreateOne(mc.ctx, mod)
if err != nil {
return err
}