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
|
package main
import (
"context"
"errors"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const defaultConnectionTimeout = 10*time.Second
const defaultOperationTimeout = 3*time.Second
type Storage struct {
client *mongo.Client
collections map[string]*mongo.Collection
}
type OrderedDocument = bson.D
type UnorderedDocument = bson.M
func NewStorage(uri string, port int, database string) Storage {
opt := options.Client()
opt.ApplyURI(fmt.Sprintf("mongodb://%s:%v", uri, port))
client, err := mongo.NewClient(opt)
if err != nil {
panic("Failed to create mongo client")
}
db := client.Database(database)
colls := map[string]*mongo.Collection{
"imported_pcaps": db.Collection("imported_pcaps"),
"connections": db.Collection("connections"),
}
return Storage{
client: client,
collections: colls,
}
}
func (storage *Storage) Connect(ctx context.Context) error {
if ctx == nil {
ctx, _ = context.WithTimeout(context.Background(), defaultConnectionTimeout)
}
return storage.client.Connect(ctx)
}
func (storage *Storage) InsertOne(ctx context.Context, collectionName string,
document interface{}) (interface{}, error) {
collection, ok := storage.collections[collectionName]
if !ok {
return nil, errors.New("invalid collection: " + collectionName)
}
if ctx == nil {
ctx, _ = context.WithTimeout(context.Background(), defaultOperationTimeout)
}
result, err := collection.InsertOne(ctx, document)
if err != nil {
return nil, err
}
return result.InsertedID, nil
}
func (storage *Storage) UpdateOne(ctx context.Context, collectionName string,
filter interface{}, update interface {}, upsert bool) (interface{}, error) {
collection, ok := storage.collections[collectionName]
if !ok {
return nil, errors.New("invalid collection: " + collectionName)
}
if ctx == nil {
ctx, _ = context.WithTimeout(context.Background(), defaultOperationTimeout)
}
opts := options.Update().SetUpsert(upsert)
update = bson.D{{"$set", update}}
result, err := collection.UpdateOne(ctx, filter, update, opts)
if err != nil {
return nil, err
}
if upsert {
return result.UpsertedID, nil
}
return result.ModifiedCount == 1, nil
}
func (storage *Storage) FindOne(ctx context.Context, collectionName string,
filter interface{}) (UnorderedDocument, error) {
collection, ok := storage.collections[collectionName]
if !ok {
return nil, errors.New("invalid collection: " + collectionName)
}
if ctx == nil {
ctx, _ = context.WithTimeout(context.Background(), defaultOperationTimeout)
}
var result bson.M
err := collection.FindOne(ctx, filter).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
return nil, err
}
return result, nil
}
func testStorage() {
storage := NewStorage("localhost", 27017, "testing")
_ = storage.Connect(nil)
id, err := storage.InsertOne(nil, "connections", bson.M{"_id": "provaaa"})
if err != nil {
panic(err)
} else {
fmt.Println(id)
}
}
|