Skip to content

Commit

Permalink
Add some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Poitrey committed Aug 27, 2015
1 parent 3e62f95 commit 09d7c61
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
19 changes: 19 additions & 0 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ type OperationData struct {
Ref string `bson:"-,omitempty" json:"ref,omitempty"`
}

// NewOperation creates an new operation from given information.
//
// The event argument can be one of "insert", "update" or "delete". The time
// defines the exact modification date of the object (must be the exact same time
// as stored in the database).
func NewOperation(event string, time time.Time, objID, objType string, objParents []string) *Operation {
id := bson.NewObjectId()
return &Operation{
ID: &id,
Event: event,
Data: &OperationData{
Timestamp: time,
ID: objID,
Type: objType,
Parents: objParents,
},
}
}

// GetEventID returns an SSE last event id for the operation
func (op Operation) GetEventID() LastID {
return &OperationLastID{op.ID}
Expand Down
52 changes: 52 additions & 0 deletions oplog_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package oplog_test

import (
"log"
"os"
"strconv"
"time"

"github.com/dailymotion/oplog"
)

func ExampleOpLog_Append() {
ol, err := oplog.New("mongodb://localhost/oplog", 1048576)
if err != nil {
log.Fatal(err)
}
op := oplog.NewOperation("insert", time.Now(), "123", "user", nil)
ol.Append(op)
}

func ExampleOpLog_Ingest() {
ol, err := oplog.New("mongodb://localhost/oplog", 1048576)
if err != nil {
log.Fatal(err)
}
ops := make(chan *oplog.Operation)
done := make(chan bool, 1)
go ol.Ingest(ops, nil)
// Insert a large number of operations
for i := 0; i < 1000; i++ {
ops <- oplog.NewOperation("insert", time.Now(), strconv.FormatInt(int64(i), 10), "user", nil)
}
done <- true
}

func ExampleOpLog_Tail() {
ol, err := oplog.New("mongodb://localhost/oplog", 1048576)
if err != nil {
log.Fatal(err)
}
ops := make(chan oplog.GenericEvent)
stop := make(chan bool)
// Tail all future events with no filters
go ol.Tail(nil, oplog.Filter{}, ops, stop)
// Read 100 events
for i := 0; i < 100; i++ {
op := <-ops
op.WriteTo(os.Stdout)
}
// Stop the tail
stop <- true
}

0 comments on commit 09d7c61

Please sign in to comment.