-
Notifications
You must be signed in to change notification settings - Fork 3
/
record.go
150 lines (126 loc) · 3.28 KB
/
record.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
141
142
143
144
145
146
147
148
149
150
package service
import (
"encoding/json"
"github.com/33cn/externaldb/db"
"github.com/33cn/externaldb/db/proof/model"
)
// NewLogKey 为LogDBX数据的LogTableX表创建一个唯一的id标识log-hash
func (g *RecordGen) newLogKey(id string) *db.IKey {
return db.NewIKey(g.logDB, g.logTable, id)
}
// NewProofKey 为ProofDBX数据的ProofTableX表创建一个唯一的id标识proof-hash
func (g *RecordGen) newProofKey(id string) *db.IKey {
return db.NewIKey(g.proofDB, g.proofTable, id)
}
// NewTemplateKey 为TemplateDBX数据的TemplateTableX表创建一个唯一的id标识template-hash
func (g *RecordGen) newTemplateKey(id string) *db.IKey {
return db.NewIKey(g.TemplateDB, g.TemplateTable, id)
}
// NewProofUpdateKey 为ProofUpdateDBX数据的ProofUpdateTableX表创建一个唯一的id标识proofUpdate-hash
func (g *RecordGen) newProofUpdateRecordKey(id string) *db.IKey {
return db.NewIKey(g.ProofUpdateDB, g.ProofUpdateTable, id)
}
// LogRecord 用于db 记录
type LogRecord struct {
*db.IKey
*db.Op
Log *model.Log
}
// Value impl
func (r *LogRecord) Value() []byte {
v, _ := json.Marshal(r.Log)
return v
}
// ProofRecord 用于db 记录
type ProofRecord struct {
*db.IKey
*db.Op
Proof *model.Proof
}
// Value impl
func (r *ProofRecord) Value() []byte {
v, _ := json.Marshal(r.Proof.Proof)
return v
}
// TemplateRecord 用于db 记录
type TemplateRecord struct {
*db.IKey
*db.Op
Template *model.Template
}
// Value impl
func (r *TemplateRecord) Value() []byte {
v, _ := json.Marshal(r.Template.Template)
return v
}
// ProofUpdateRecord 用于db 记录
type ProofUpdateRecord struct {
*db.IKey
*db.Op
Proof *model.Proof
}
// Value impl
func (r *ProofUpdateRecord) Value() []byte {
v, _ := json.Marshal(r.Proof.Proof)
return v
}
// RecordGen gen record
type RecordGen struct {
proofDB string
proofTable string
logDB string
logTable string
TemplateDB string
TemplateTable string
ProofUpdateDB string
ProofUpdateTable string
}
// NewRecordGen NewRecordGen
func NewRecordGen(pdb, ptable, ldb, ltable, tdb, ttable, udb, utable string) *RecordGen {
return &RecordGen{
proofDB: pdb,
proofTable: ptable,
logDB: ldb,
logTable: ltable,
TemplateDB: tdb,
TemplateTable: ttable,
ProofUpdateDB: udb,
ProofUpdateTable: utable,
}
}
// Log gen log record
func (g *RecordGen) Log(log *model.Log, id string, op int) db.Record {
record := LogRecord{
IKey: g.newLogKey(id),
Op: db.NewOp(op),
Log: log,
}
return &record
}
// Proof gen proof record
func (g *RecordGen) Proof(proof *model.Proof, id string, op int) db.Record {
ProofRecord := ProofRecord{
IKey: g.newProofKey(id),
Op: db.NewOp(op),
Proof: proof,
}
return &ProofRecord
}
// Template gen template record
func (g *RecordGen) Template(template *model.Template, id string, op int) db.Record {
TemplateRecord := TemplateRecord{
IKey: g.newTemplateKey(id),
Op: db.NewOp(op),
Template: template,
}
return &TemplateRecord
}
// ProofUpdate gen proof update record
func (g *RecordGen) ProofUpdateRecord(proof *model.Proof, id string, op int) db.Record {
ProofUpdateRecord := ProofUpdateRecord{
IKey: g.newProofUpdateRecordKey(id),
Op: db.NewOp(op),
Proof: proof,
}
return &ProofUpdateRecord
}