|
| 1 | +package mongo |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"errors" |
| 6 | +"time" |
| 7 | + |
| 8 | +"go.mongodb.org/mongo-driver/bson" |
| 9 | +"go.mongodb.org/mongo-driver/mongo" |
| 10 | +"go.mongodb.org/mongo-driver/mongo/options" |
| 11 | + |
| 12 | +"github.com/qa-dev/jsonwire-grid/pool" |
| 13 | +"github.com/qa-dev/jsonwire-grid/storage" |
| 14 | +) |
| 15 | + |
| 16 | +type Storage struct { |
| 17 | +collection *mongo.Collection |
| 18 | +ctx context.Context |
| 19 | +} |
| 20 | + |
| 21 | +func NewMongoStorage(db *mongo.Database) *Storage { |
| 22 | +return &Storage{ |
| 23 | +collection: db.Collection("grid"), |
| 24 | +ctx: context.Background(), |
| 25 | +} |
| 26 | +} |
| 27 | + |
| 28 | +func (m *Storage) Add(node pool.Node, limit int) error { |
| 29 | +opts := options.Update().SetUpsert(true) |
| 30 | +filter := bson.M{"key": node.Key} |
| 31 | +update := bson.D{{Key: "$set", Value: node}} |
| 32 | + |
| 33 | +if limit > 0 { |
| 34 | +return errors.New("[Mongo/Add] limit strategy temporary not supported ") |
| 35 | +} |
| 36 | + |
| 37 | +result, err := m.collection.UpdateOne(m.ctx, filter, update, opts) |
| 38 | +if err != nil { |
| 39 | +return errors.New("[Mongo/Add] update node, " + err.Error()) |
| 40 | +} |
| 41 | +if result.MatchedCount == 0 && result.UpsertedCount == 0 { |
| 42 | +return errors.New("[Mongo/Add] No rows was affected ") |
| 43 | +} |
| 44 | + |
| 45 | +return nil |
| 46 | +} |
| 47 | + |
| 48 | +func (m *Storage) ReserveAvailable(nodeList []pool.Node) (pool.Node, error) { |
| 49 | +nodeKeyList := make([]string, 0, len(nodeList)) |
| 50 | +node := pool.Node{} |
| 51 | +for _, node := range nodeList { |
| 52 | +nodeKeyList = append(nodeKeyList, node.Key) |
| 53 | +} |
| 54 | + |
| 55 | +filter := bson.M{"key": bson.M{"$in": nodeKeyList}, "status": pool.NodeStatusAvailable} |
| 56 | + |
| 57 | +update := bson.M{"$set": bson.M{ |
| 58 | +"updated": time.Now().Unix(), |
| 59 | +"status": string(pool.NodeStatusReserved), |
| 60 | +}} |
| 61 | + |
| 62 | +opts := options. |
| 63 | +FindOneAndUpdate(). |
| 64 | +SetReturnDocument(options.After). |
| 65 | +SetSort(bson.M{"updated": 1}) |
| 66 | + |
| 67 | +err := m.collection.FindOneAndUpdate(m.ctx, filter, update, opts).Decode(&node) |
| 68 | +if err != nil { |
| 69 | +err = errors.New("[Mongo/ReserveAvailable] find and update node, " + err.Error()) |
| 70 | +return node, err |
| 71 | +} |
| 72 | +return node, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (m *Storage) SetBusy(node pool.Node, sessionID string) error { |
| 76 | +filter := bson.M{"key": node.Key} |
| 77 | +update := bson.M{"$set": bson.M{ |
| 78 | +"session_id": sessionID, |
| 79 | +"updated": time.Now().Unix(), |
| 80 | +"status": string(pool.NodeStatusBusy), |
| 81 | +}} |
| 82 | +result, err := m.collection.UpdateOne(m.ctx, filter, update) |
| 83 | +if err != nil { |
| 84 | +err = errors.New("[Mongo/SetBusy] update node in collection, " + err.Error()) |
| 85 | +return err |
| 86 | +} |
| 87 | +if result.ModifiedCount == 0 { |
| 88 | +return storage.ErrNotFound |
| 89 | +} |
| 90 | +return nil |
| 91 | +} |
| 92 | + |
| 93 | +func (m *Storage) SetAvailable(node pool.Node) error { |
| 94 | +filter := bson.M{"key": node.Key} |
| 95 | +update := bson.M{"$set": bson.M{ |
| 96 | +"updated": time.Now().Unix(), |
| 97 | +"status": string(pool.NodeStatusAvailable), |
| 98 | +}} |
| 99 | +result, err := m.collection.UpdateOne(m.ctx, filter, update) |
| 100 | +if err != nil { |
| 101 | +err = errors.New("[Mongo/SetAvailable] update node in collection, " + err.Error()) |
| 102 | +return err |
| 103 | +} |
| 104 | +if result.ModifiedCount == 0 { |
| 105 | +return storage.ErrNotFound |
| 106 | +} |
| 107 | +return nil |
| 108 | +} |
| 109 | + |
| 110 | +func (m *Storage) GetCountWithStatus(status *pool.NodeStatus) (int, error) { |
| 111 | +var count int |
| 112 | +var err error |
| 113 | +filter := bson.M{} |
| 114 | +if status != nil { |
| 115 | +filter = bson.M{"status": status} |
| 116 | +} |
| 117 | +cnt, err := m.collection.CountDocuments(m.ctx, filter) |
| 118 | +count = int(cnt) |
| 119 | +if err != nil { |
| 120 | +return 0, errors.New("[Mongo/GetCountWithStatus] count nodes in collection, " + err.Error()) |
| 121 | +} |
| 122 | +return count, nil |
| 123 | +} |
| 124 | + |
| 125 | +func (m *Storage) GetBySession(session string) (pool.Node, error) { |
| 126 | +node, err := m.getByField("session_id", session) |
| 127 | +if err != nil { |
| 128 | +return pool.Node{}, errors.New("[Mongo/GetBySession] find node in collection, " + err.Error()) |
| 129 | +} |
| 130 | +return node, nil |
| 131 | +} |
| 132 | + |
| 133 | +func (m *Storage) GetByAddress(address string) (pool.Node, error) { |
| 134 | +node, err := m.getByField("address", address) |
| 135 | +if err != nil { |
| 136 | +return pool.Node{}, errors.New("[Mongo/GetByAddress] find node in collection, " + err.Error()) |
| 137 | +} |
| 138 | +return node, nil |
| 139 | +} |
| 140 | + |
| 141 | +func (m *Storage) GetAll() ([]pool.Node, error) { |
| 142 | +nodeList := make([]pool.Node, 0) |
| 143 | +resultCursor, err := m.collection.Find(m.ctx, bson.M{}) |
| 144 | +if err != nil { |
| 145 | +return nil, errors.New("[Mongo/GetAll] find node in collection, " + err.Error()) |
| 146 | +} |
| 147 | +defer resultCursor.Close(m.ctx) |
| 148 | +for resultCursor.Next(m.ctx) { |
| 149 | +var result pool.Node |
| 150 | +err := resultCursor.Decode(&result) |
| 151 | +if err != nil { |
| 152 | +return nil, errors.New("[Mongo/GetAll] decode node data" + err.Error()) |
| 153 | +} |
| 154 | +nodeList = append(nodeList, result) |
| 155 | +} |
| 156 | +if err := resultCursor.Err(); err != nil { |
| 157 | +return nil, errors.New("[Mongo/GetAll] iterate result" + err.Error()) |
| 158 | +} |
| 159 | +return nodeList, nil |
| 160 | +} |
| 161 | + |
| 162 | +func (m *Storage) Remove(node pool.Node) error { |
| 163 | +rowsAffected, err := m.collection.DeleteOne(m.ctx, bson.M{"key": node.Key}) |
| 164 | +if err != nil { |
| 165 | +return errors.New("[Mongo/Remove] delete from node collection, " + err.Error()) |
| 166 | +} |
| 167 | +if rowsAffected.DeletedCount == 0 { |
| 168 | +return errors.New("[Mongo/Remove] delete from node collection: affected 0 rows") |
| 169 | +} |
| 170 | +return nil |
| 171 | +} |
| 172 | + |
| 173 | +func (m *Storage) UpdateAddress(node pool.Node, newAddress string) error { |
| 174 | +filter := bson.M{"key": node.Key} |
| 175 | +update := bson.M{"$set": bson.M{"address": newAddress}} |
| 176 | +result, err := m.collection.UpdateOne(m.ctx, filter, update) |
| 177 | +if err != nil { |
| 178 | +err = errors.New("[Mongo/UpdateAddress], " + err.Error()) |
| 179 | +return err |
| 180 | +} |
| 181 | +if result.ModifiedCount == 0 { |
| 182 | +return storage.ErrNotFound |
| 183 | +} |
| 184 | +return nil |
| 185 | +} |
| 186 | + |
| 187 | +func (m *Storage) getByField(key, value string) (pool.Node, error) { |
| 188 | +node := pool.Node{} |
| 189 | +filter := bson.M{ |
| 190 | +key: bson.M{"$eq": value}, |
| 191 | +} |
| 192 | +err := m.collection.FindOne(m.ctx, filter).Decode(&node) |
| 193 | +return node, err |
| 194 | +} |
0 commit comments