-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx_log_manager.go
More file actions
57 lines (48 loc) · 1.58 KB
/
tx_log_manager.go
File metadata and controls
57 lines (48 loc) · 1.58 KB
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
package main
import (
"errors"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type TransactionLogEntry struct {
QueryResponseEntry `bson:",inline"`
EE string `bson:"ee"`
Error string `bson:"error,omitempty"`
FailureCount int `bson:"failureCount"`
Date time.Time `bson:"date"`
}
type TransactionLogManager interface {
FindEntriesByEE(ee string) (entries []*TransactionLogEntry, err error)
StoreEntry(entry *TransactionLogEntry) error
}
type MgoTransactionLogManager struct {
txCollection *mgo.Collection
}
func NewMgoTransactionLogManager(db *mgo.Database) (*MgoTransactionLogManager, error) {
if db == nil || db.Session == nil {
return nil, errors.New("The Mongo DB must be configured")
}
return &MgoTransactionLogManager{
txCollection: db.C("transactions"),
}, nil
}
func (t *MgoTransactionLogManager) FindEntriesByEE(ee string) (entries []*TransactionLogEntry, err error) {
if t.txCollection == nil {
return nil, errors.New("The transaction database collection is not configured")
}
entries = []*TransactionLogEntry{}
if err := t.txCollection.Find(bson.M{"ee": ee}).All(&entries); err != nil {
return nil, err
}
return entries, nil
}
func (t *MgoTransactionLogManager) StoreEntry(entry *TransactionLogEntry) error {
if t.txCollection == nil {
return errors.New("The transaction database collection is not configured")
} else if entry.DocumentID == "" {
return errors.New("Cannot store a transaction without a valid document ID")
}
_, err := t.txCollection.UpsertId(entry.DocumentID, entry)
return err
}