/
mongoAPI.go
90 lines (71 loc) · 2.14 KB
/
mongoAPI.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
package mongo
import (
"fmt"
"go.mongodb.org/mongo-driver/bson"
)
const (
MONGO_HOST = "mongodb+srv://admin:{pass}@cluster0.nvd7u.mongodb.net/ImageMasterDB?retryWrites=true&w=majority"
MONGO_DBNAME = "ImageMasterDB"
COLLECTION_NAME = "ImageClassifications"
)
type ImageClassification struct {
ImagePath string
Probabilities []string
}
type Mongo struct {
}
func NewMongo() *Mongo {
return &Mongo{}
}
// insert new image classification labels for a given imagePath
func (m *Mongo) AddImageClassification(imagePath string, probabilities []string) error {
client, ctx, cancel, err := Connect(MONGO_HOST)
if err != nil {
fmt.Println(err.Error())
}
// Release resource when main function is returned.
defer Close(client, ctx, cancel)
imClassification := &ImageClassification{ImagePath: imagePath, Probabilities: probabilities}
insertOneResult, err := InsertOne(client, ctx, MONGO_DBNAME, COLLECTION_NAME, imClassification)
if err != nil {
return err
}
fmt.Print(insertOneResult)
return nil
}
// get image classification labels for a given imagePath
func (m *Mongo) GetImageClassification(imagePath string) (ic ImageClassification, err error) {
client, ctx, cancel, err := Connect(MONGO_HOST)
if err != nil {
fmt.Println(err.Error())
return ImageClassification{}, err
}
defer Close(client, ctx, cancel)
filter := bson.M{
"imagepath": imagePath,
}
//fmt.Println("Filter: ", filter)
imClassification := FindOne(client, ctx, MONGO_DBNAME, COLLECTION_NAME, filter, nil)
//fmt.Print("im classification returned: ", imClassification)
err = imClassification.Decode(&ic)
fmt.Println("Result from db: ", ic)
if err != nil {
fmt.Println(err.Error())
return ImageClassification{}, err
}
return ic, nil
}
// Get all image classifications
func (m *Mongo) GetAllImageClassifications() (ics []ImageClassification, err error) {
client, ctx, cancel, err := Connect(MONGO_HOST)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
defer Close(client, ctx, cancel)
imClassifications := GetAll(client, ctx, MONGO_DBNAME, COLLECTION_NAME)
if imClassifications == nil {
return nil, err
}
return imClassifications, nil
}