Skip to content
This repository was archived by the owner on Jun 12, 2018. It is now read-only.

Commit bae9841

Browse files
author
Tim Vaillancourt
committed
using flashback.Op for oplog entry struct and adding 'omitempty' bson-serializing hints to fields that shouldn't be serialized when undef
1 parent 56bf6bb commit bae9841

File tree

2 files changed

+27
-40
lines changed

2 files changed

+27
-40
lines changed

cmd/pcap_converter/main.go

+19-32
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"reflect"
1010
"runtime"
1111
"strings"
12-
"time"
1312

1413
"github.com/ParsePlatform/flashback"
1514
"github.com/google/gopacket/pcap"
@@ -26,35 +25,23 @@ var (
2625
debug = flag.Bool("debug", false, "Print debug-level output")
2726
)
2827

29-
type Operation struct {
30-
Ns string `bson:"ns"`
31-
Timestamp time.Time `bson:"ts"`
32-
NToSkip int32 `bson:"ntoskip,omitempty"`
33-
NToReturn int32 `bson:"ntoreturn,omitempty"`
34-
InsertDoc bson.D `bson:"o,omitempty"`
35-
QueryDoc bson.D `bson:"query,omitempty"`
36-
UpdateDoc bson.D `bson:"updateobj,omitempty"`
37-
CommandDoc bson.D `bson:"command,omitempty"`
38-
Type flashback.OpType `bson:"op"`
39-
}
40-
4128
func ParseQuery(opQuery []byte) (bson.D, error) {
4229
var query bson.D
4330
err := bson.Unmarshal(opQuery, &query)
4431
return query, err
4532
}
4633

47-
func (op *Operation) HandleCommand(opCommand *mongoproto.OpQuery, f *os.File) error {
34+
func HandleCommand(op *flashback.Op, opCommand *mongoproto.OpQuery, f *os.File) error {
4835
var err error
4936
op.Type = flashback.Command
5037
op.CommandDoc, err = ParseQuery(opCommand.Query)
5138
if err != nil {
5239
return err
5340
}
54-
return op.Write(f)
41+
return Write(op, f)
5542
}
5643

57-
func (op *Operation) HandleQuery(opQuery *mongoproto.OpQuery, f *os.File) error {
44+
func HandleQuery(op *flashback.Op, opQuery *mongoproto.OpQuery, f *os.File) error {
5845
var err error
5946
op.Ns = opQuery.FullCollectionName
6047
op.Type = flashback.Query
@@ -63,32 +50,32 @@ func (op *Operation) HandleQuery(opQuery *mongoproto.OpQuery, f *os.File) error
6350
collection, exists := flashback.GetElem(op.QueryDoc, "insert")
6451
if exists == true {
6552
opQuery.FullCollectionName = strings.Replace(opQuery.FullCollectionName, "$cmd", collection.(string), 1)
66-
return op.HandleInsertFromQuery(opQuery, f)
53+
return HandleInsertFromQuery(op, opQuery, f)
6754
}
68-
return op.HandleCommand(opQuery, f)
55+
return HandleCommand(op, opQuery, f)
6956
}
7057
op.QueryDoc, err = ParseQuery(opQuery.Query)
7158
if err != nil {
7259
return err
7360
}
74-
return op.Write(f)
61+
return Write(op, f)
7562
}
7663

77-
func (op *Operation) HandleInsertDocument(document bson.D, f *os.File) error {
64+
func HandleInsertDocument(op *flashback.Op, document bson.D, f *os.File) error {
7865
op.Type = flashback.Insert
7966
op.InsertDoc = document
80-
return op.Write(f)
67+
return Write(op, f)
8168
}
8269

83-
func (op *Operation) HandleInsert(opInsert *mongoproto.OpInsert, f *os.File) error {
70+
func HandleInsert(op *flashback.Op, opInsert *mongoproto.OpInsert, f *os.File) error {
8471
op.Ns = opInsert.FullCollectionName
8572
if opInsert.Documents != nil {
8673
for _, document := range opInsert.Documents {
8774
insert, err := ParseQuery(document)
8875
if err != nil {
8976
return err
9077
}
91-
err = op.HandleInsertDocument(insert, f)
78+
err = HandleInsertDocument(op, insert, f)
9279
if err != nil {
9380
return err
9481
}
@@ -97,7 +84,7 @@ func (op *Operation) HandleInsert(opInsert *mongoproto.OpInsert, f *os.File) err
9784
return nil
9885
}
9986

100-
func (op *Operation) HandleInsertFromQuery(opQuery *mongoproto.OpQuery, f *os.File) error {
87+
func HandleInsertFromQuery(op *flashback.Op, opQuery *mongoproto.OpQuery, f *os.File) error {
10188
op.Ns = opQuery.FullCollectionName
10289
query, err := ParseQuery(opQuery.Query)
10390
if err != nil {
@@ -107,13 +94,13 @@ func (op *Operation) HandleInsertFromQuery(opQuery *mongoproto.OpQuery, f *os.Fi
10794
if exists == true {
10895
if (reflect.TypeOf(documents).Kind() == reflect.Slice) {
10996
for _, document := range documents.([]interface{}) {
110-
err = op.HandleInsertDocument(document.(bson.D), f)
97+
err = HandleInsertDocument(op, document.(bson.D), f)
11198
if err != nil {
11299
return err
113100
}
114101
}
115102
} else {
116-
err = op.HandleInsertDocument(documents.(bson.D), f)
103+
err = HandleInsertDocument(op, documents.(bson.D), f)
117104
if err != nil {
118105
return err
119106
}
@@ -122,18 +109,18 @@ func (op *Operation) HandleInsertFromQuery(opQuery *mongoproto.OpQuery, f *os.Fi
122109
return nil
123110
}
124111

125-
func (op *Operation) HandleDelete(opDelete *mongoproto.OpDelete, f *os.File) error {
112+
func HandleDelete(op *flashback.Op, opDelete *mongoproto.OpDelete, f *os.File) error {
126113
var err error
127114
op.Type = flashback.Remove
128115
op.Ns = opDelete.FullCollectionName
129116
op.QueryDoc, err = ParseQuery(opDelete.Selector)
130117
if err != nil {
131118
return err
132119
}
133-
return op.Write(f)
120+
return Write(op, f)
134121
}
135122

136-
func (op *Operation) Write(f *os.File) error {
123+
func Write(op *flashback.Op, f *os.File) error {
137124
opBson, err := bson.Marshal(op)
138125
if err != nil {
139126
return err
@@ -166,14 +153,14 @@ func main() {
166153
defer f.Close()
167154
for op := range m.Ops {
168155
var err error
169-
fbOp := &Operation{
156+
fbOp := &flashback.Op{
170157
Timestamp: op.Seen,
171158
}
172159
// todo: fix mongoproto.OpUpdate and mongoproto.OpDelete so they can be added
173160
if opInsert, ok := op.Op.(*mongoproto.OpInsert); ok {
174-
err = fbOp.HandleInsert(opInsert, f)
161+
err = HandleInsert(fbOp, opInsert, f)
175162
} else if opQuery, ok := op.Op.(*mongoproto.OpQuery); ok {
176-
err = fbOp.HandleQuery(opQuery, f)
163+
err = HandleQuery(fbOp, opQuery, f)
177164
} else if *debug == true {
178165
if _, ok := op.Op.(*mongoproto.OpUnknown); ok {
179166
fmt.Println("Found mongoproto.OpUnknown operation: ", op)

op.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ type Op struct {
4141
Ns string `bson:"ns"`
4242
Timestamp time.Time `bson:"ts"`
4343
Type OpType `bson:"op"`
44-
NToSkip int64 `bson:"ntoskip"`
45-
NToReturn int64 `bson:"ntoreturn"`
46-
QueryDoc bson.D `bson:"query"`
47-
CommandDoc bson.D `bson:"command"`
48-
InsertDoc bson.D `bson:"o"`
49-
UpdateDoc bson.D `bson:"updateobj"`
50-
Database string
51-
Collection string
44+
NToSkip int64 `bson:"ntoskip,omitempty"`
45+
NToReturn int64 `bson:"ntoreturn,omitempty"`
46+
QueryDoc bson.D `bson:"query,omitempty"`
47+
CommandDoc bson.D `bson:"command,omitempty"`
48+
InsertDoc bson.D `bson:"o,omitempty"`
49+
UpdateDoc bson.D `bson:"updateobj,omitempty"`
50+
Database string `bson:",omitempty"`
51+
Collection string `bson:",omitempty"`
5252
}
5353

5454
// GetElem is a helper to fetch a specific key from bson.D

0 commit comments

Comments
 (0)