...
 
Commits (2)
......@@ -13,6 +13,7 @@ func init() {
rootCmd.AddCommand(serverCmd)
rootCmd.AddCommand(webhooksCmd)
rootCmd.AddCommand(mockCmd)
rootCmd.AddCommand(storeCmd)
}
// Execute root command
......
package cmd
import (
"log"
"git.oolsa.net/jooola/jetstats/store/boltdb"
"github.com/spf13/cobra"
)
var storeCmd = &cobra.Command{
Use: "store",
Short: "Manage the store",
}
func init() {
storeCmd.AddCommand(storeImportCmd)
storeImportCmd.Flags().StringVar(&storeImportPath, "path", "", "Path to the store file")
}
var (
storeImportPath string
)
var storeImportCmd = &cobra.Command{
Use: "import",
Short: "Import store file",
Run: func(cmd *cobra.Command, args []string) {
if storeImportPath == "" {
log.Fatalf("cmd: a path is required")
}
from := boltdb.NewWithConfig(&boltdb.Config{
Database: storeImportPath,
})
to := boltdb.New()
if err := to.Import(from); err != nil {
log.Fatal(err)
}
},
}
......@@ -85,3 +85,8 @@ func Bytes(e interface{}) []byte {
return buf.Bytes()
}
// Equal return wether 2 events are identical
func Equal(e1, e2 Event) bool {
return string(e1.Bytes()) == string(e2.Bytes())
}
......@@ -37,7 +37,7 @@ func TestParse(t *testing.T) {
t.Error(err)
}
if string(event.Bytes()) != string(event2.Bytes()) {
if !Equal(event, event2) {
t.Errorf("event bytes mismatch after 2 parsing")
}
})
......
......@@ -2,7 +2,6 @@ package boltdb
import (
"bytes"
"strings"
"testing"
"git.oolsa.net/jooola/jetstats/events"
......@@ -43,7 +42,7 @@ func TestStore(t *testing.T) {
return err
}
if strings.Compare(wantEvent.String(), gotEvent.String()) != 0 {
if !events.Equal(wantEvent, gotEvent) {
t.Errorf("\nwant '%s'\ngot '%s'", wantEvent.String(), gotEvent.String())
}
}
......
package boltdb
import (
"bytes"
"git.oolsa.net/jooola/jetstats/events"
bolt "go.etcd.io/bbolt"
)
// Import events from another store
func (s *Store) Import(from *Store) error {
return s.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(eventsBucketKey)
return from.View(func(fromTx *bolt.Tx) error {
c := fromTx.Bucket(eventsBucketKey).Cursor()
for key, value := c.First(); key != nil; key, value = c.Next() {
eventType, _ := ParseKey(key)
_, err := events.Parse(eventType, bytes.NewReader(value))
if err != nil {
tx.Rollback()
return err
}
if err := bucket.Put(key, value); err != nil {
tx.Rollback()
return err
}
}
return nil
})
})
}
package boltdb
import (
"bytes"
"testing"
"git.oolsa.net/jooola/jetstats/events"
"git.oolsa.net/jooola/jetstats/x"
bolt "go.etcd.io/bbolt"
)
func TestImport(t *testing.T) {
from := NewWithConfig(&Config{
Database: x.TempFile(),
})
defer from.Close()
to := NewWithConfig(&Config{
Database: x.TempFile(),
})
defer to.Close()
event := x.RandomEvent()
from.Save(event)
if err := to.Import(from); err != nil {
t.Fatal(err)
}
if err := to.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(eventsBucketKey)
key := FormatKey(event)
eventType, _ := ParseKey(key)
gotEvent, err := events.Parse(eventType, bytes.NewReader(bucket.Get(key)))
if err != nil {
return err
}
if !events.Equal(event, gotEvent) {
t.Errorf("\nwant '%s'\ngot '%s'", event.String(), gotEvent.String())
}
return nil
}); err != nil {
t.Error(err)
}
}