From 3e49a044d22635157916651f0acb5a062397b34b Mon Sep 17 00:00:00 2001 From: We-unite <3205135446@qq.com> Date: Fri, 9 Aug 2024 13:56:37 +0800 Subject: Add db structure, fix filePath, start filtering This commit I made several changes: - Use structure instead of simple bson.M(interface{}). bson.M has some shortcomings: 1) It makes the database in chaos and hard to read, but this's not important; 2) Some entrys may has more or less content than others, which makes it hard to decode and filt. So I design new data structure to encode and decode. Hopes that there's no bugs. - Fix the way to calculate file path. The original method is to add all the PATH entries together, that's totally wrong! PATH entry has several types, as it shows in "objtype". I can't find it in the kernel src code, so what i know is just "PARENT" means the dir the file is in, while the filename itself has the path, so we whould ignore all "PARENT"s. When the src code is found, we should check it again. - Fix bugs in updating. The update function of mongodb is set to required to has a '$' such as 'set'/'push', so when we update a whole doc, we should use replace but not update function. And, we should never ignore the error infomation it gives us. Hope that there's no more bugs for this Big Change. Now its' time to write filter as well as viewer. Best wishes with NO BUGS! --- listener/mongo.go | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 listener/mongo.go (limited to 'listener/mongo.go') diff --git a/listener/mongo.go b/listener/mongo.go new file mode 100644 index 0000000..a51350e --- /dev/null +++ b/listener/mongo.go @@ -0,0 +1,122 @@ +package main + +import ( + "context" + "fmt" + "reflect" + "time" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +type mongoClient struct { + dbName, colName string + client *mongo.Client + col *mongo.Collection +} + +func (mc *mongoClient) init(dbName, colName string) error { + var err error + if err = mc.Connect(dbName, colName); err != nil { + return err + } + if err = mc.Drop(); err != nil { + return err + } + + return nil +} + +func (mc *mongoClient) Connect(dbName, colName string) error { + var err error + mc.client, err = mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017")) + + if err != nil { + return err + } + + ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) + err = mc.client.Connect(ctx) + if err != nil { + return err + } + + mc.col = mc.client.Database(dbName).Collection(colName) + mc.dbName = dbName + mc.colName = colName + return nil +} + +func (mc *mongoClient) InsertOne(document interface{}) error { + _, err := mc.col.InsertOne(context.Background(), document) + return err +} + +func (mc *mongoClient) UpdateOne(filter, update interface{}) error { + _, err := mc.col.UpdateOne(context.Background(), filter, update) + return err +} + +func (mc *mongoClient) UpdateMany(filter, update interface{}) error { + _, err := mc.col.UpdateMany(context.Background(), filter, update) + return err +} + +func (mc *mongoClient) ReplaceOne(filter, new interface{}) error { + _, err := mc.col.ReplaceOne(context.Background(), filter, new) + return err +} + +func (mc *mongoClient) Finddoc(filter bson.M, results interface{}) error { + sliceValue := reflect.ValueOf(results) + + if sliceValue.Kind() != reflect.Ptr || sliceValue.Elem().Kind() != reflect.Slice { + return fmt.Errorf("Error: result argument must be pointer to slice") + } + cur, err := mc.col.Find(context.TODO(), filter) + if err != nil { + return err + } + defer cur.Close(context.TODO()) + + elemType := sliceValue.Elem().Type().Elem() + + sliceValue = sliceValue.Elem() + + for cur.Next(context.TODO()) { + elem := reflect.New(elemType).Interface() + err := cur.Decode(elem) + if err != nil { + return err + } + sliceValue = reflect.Append(sliceValue, reflect.ValueOf(elem).Elem()) + } + + reflect.ValueOf(results).Elem().Set(sliceValue) + return nil +} + +func (mc *mongoClient) FindOneAndDelete(filter bson.M) (bson.M, error) { + res := mc.col.FindOneAndDelete(context.Background(), filter) + var result bson.M + err := res.Decode(&result) + return result, err +} + +func (mc *mongoClient) Drop() error { + return mc.col.Drop(context.Background()) +} + +func (mc *mongoClient) Disconnect() error { + err := mc.client.Disconnect(context.Background()) + if err != nil { + return err + } + mc.col = nil + mc.client = nil + mc.dbName = "" + mc.colName = "" + return nil +} -- cgit v1.2.3-70-g09d2