/
mongoDB-antipode.go
140 lines (109 loc) · 3.47 KB
/
mongoDB-antipode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package antipode
import (
"context"
"errors"
"fmt"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MongoDB struct {
clientOptions *options.ClientOptions
database string
}
type Document struct {
Key string `bson:"key"`
Value AntiObj `bson:"value"`
}
func CreateMongoDB(host string, port string, database string) MongoDB {
return MongoDB{options.Client().ApplyURI("mongodb://" + host + ":" + port + "/?directConnection=true"),
database,
}
}
// devo verificar primeiro se já existe essa key? E caso exista fazer update?
// falta fechar a conecção
func (m MongoDB) write(ctx context.Context, collectionName string, key string, obj AntiObj) error {
client, err := mongo.Connect(ctx, m.clientOptions)
if err != nil {
return err
}
// Disconnect from MongoDB when the function returns
defer func() {
if err := client.Disconnect(ctx); err != nil {
log.Fatal(err)
}
}()
collection := client.Database(m.database).Collection(collectionName)
filter := bson.D{{"key", key}}
replacement := Document{
Key: key,
Value: obj,
}
_, err = collection.ReplaceOne(context.Background(), filter, replacement, options.Replace().SetUpsert(true))
/*mongoObj := Document{
Key: key,
Value: obj,
}
_, err = client.Database(m.database).Collection(collection).InsertOne(ctx, mongoObj)*/
return err
}
// posso assumir que não há mais do que um objeto com a mesma key?
func (m MongoDB) read(ctx context.Context, collection string, key string) (AntiObj, error) {
client, err := mongo.Connect(ctx, m.clientOptions)
if err != nil {
return AntiObj{}, err
}
// Disconnect from MongoDB when the function returns
defer func() {
if err := client.Disconnect(ctx); err != nil {
log.Fatal(err)
}
}()
filter := bson.D{{"key", key}}
var result Document
err = client.Database(m.database).Collection(collection).FindOne(context.Background(), filter).Decode(&result)
if err != nil {
return AntiObj{}, err
}
return result.Value, nil
}
func (m MongoDB) barrier(ctx context.Context, lineage []WriteIdentifier, datastoreID string) error {
fmt.Println("start barrier")
client, err := mongo.Connect(ctx, m.clientOptions)
if err != nil {
return err
}
// Disconnect from MongoDB when the function returns
defer func() {
if err := client.Disconnect(ctx); err != nil {
log.Fatal(err)
}
}()
for _, writeIdentifier := range lineage {
fmt.Println("key after for: ", writeIdentifier.Key)
if writeIdentifier.Dtstid == datastoreID {
for {
filter := bson.D{{"key", writeIdentifier.Key}}
fmt.Println("key: ", writeIdentifier.Key)
var result Document
err = client.Database(m.database).Collection(writeIdentifier.TableId).FindOne(context.Background(), filter).Decode(&result)
if !errors.Is(err, mongo.ErrNoDocuments) && err != nil {
return err
} else if errors.Is(err, mongo.ErrNoDocuments) { //the version replication process is not yet completed
fmt.Println("replication in progress")
continue
} else {
if result.Value.Version == writeIdentifier.Version { //the version replication process is already completed
fmt.Println("replication done: ", result.Value.Version)
break
} else { //the version replication process is not yet completed
fmt.Println("replication of the new version in progress! Old Version: ", result.Value.Version, " new version: ", writeIdentifier.Version)
continue
}
}
}
}
}
return nil
}