-
Notifications
You must be signed in to change notification settings - Fork 2
/
doc_base.go
139 lines (113 loc) · 3.02 KB
/
doc_base.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
package mongodbstorage
import (
bsonenc "github.com/ProtoconNet/mitum-currency/v3/digest/util/bson"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/encoder"
"github.com/ProtoconNet/mitum2/util/hint"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
type Doc interface {
ID() interface{}
}
type BaseDoc struct {
id interface{}
encoderHint hint.Hint
data interface{}
isHinted bool
}
func NewBaseDoc(id, data interface{}, enc encoder.Encoder) (BaseDoc, error) {
_, isHinted := data.(hint.Hinter)
return BaseDoc{
id: id,
encoderHint: enc.Hint(),
isHinted: isHinted,
data: data,
}, nil
}
func (do BaseDoc) ID() interface{} {
return do.id
}
func (do BaseDoc) M() (bson.M, error) {
m := bson.M{
"_e": do.encoderHint.String(),
"_hinted": do.isHinted,
"d": do.data,
}
if do.id != nil {
m["_id"] = do.id
}
return m, nil
}
type BaseDocBSONUnMarshaler struct {
I bson.Raw `bson:"_id,omitempty"`
E string `bson:"_e"`
D bson.RawValue `bson:"d"`
H bool `bson:"_hinted"`
}
func LoadDataFromDoc(b []byte, encs *encoder.Encoders) (bson.Raw /* id */, interface{} /* data */, error) {
var bd BaseDocBSONUnMarshaler
if err := bsonenc.Unmarshal(b, &bd); err != nil {
return nil, nil, err
}
ht, err := hint.ParseHint(bd.E)
if err != nil {
return nil, nil, err
}
enc, found := encs.Find(ht)
if !found {
return nil, nil, util.ErrNotFound.Errorf("Encoder not found for %q", ht)
}
if !bd.H {
return bd.I, bd.D, nil
}
doc, ok := bd.D.DocumentOK()
if !ok {
return nil, nil, errors.Errorf("Hinted should be mongodb Document")
}
var data interface{}
if i, err := enc.Decode([]byte(doc)); err != nil {
return nil, nil, err
} else {
data = i
}
return bd.I, data, nil
}
type BaseManifestDocBSONUnMarshaler struct {
I bson.Raw `bson:"_id,omitempty"`
E string `bson:"_e"`
D bson.RawValue `bson:"d"`
H bool `bson:"_hinted"`
O uint64 `bson:"operations"`
T string `bson:"confirmed_at"`
P string `bson:"proposer"`
R uint64 `bson:"round"`
}
func LoadManifestDataFromDoc(b []byte, encs *encoder.Encoders) (bson.Raw /* id */, interface{} /* data */, uint64 /* operations */, string /* confirmed_at */, string /* proposer */, uint64 /* round */, error) {
var bd BaseManifestDocBSONUnMarshaler
if err := bsonenc.Unmarshal(b, &bd); err != nil {
return nil, nil, 0, "", "", 0, err
}
ht, err := hint.ParseHint(bd.E)
if err != nil {
return nil, nil, 0, "", "", 0, err
}
enc, found := encs.Find(ht)
if !found {
return nil, nil, 0, "", "", 0, util.ErrNotFound.Errorf("Encoder not found for %q", ht)
}
if !bd.H {
return bd.I, bd.D, 0, "", "", 0, nil
}
doc, ok := bd.D.DocumentOK()
if !ok {
return nil, nil, 0, "", "", 0, errors.Errorf("Hinted should be mongodb Document")
}
var data interface{}
if i, err := enc.Decode([]byte(doc)); err != nil {
return nil, nil, 0, "", "", 0, err
} else {
data = i
}
return bd.I, data, bd.O, bd.T, bd.P, bd.R, nil
}