diff options
author | Steve <nilslice@gmail.com> | 2016-11-02 02:27:29 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-02 02:27:29 -0700 |
commit | bd27ac1f0dbff856ca3e96d1df636f3b02d61522 (patch) | |
tree | be9b5b86b7da34a150e228a817335d368b8705ca /system/api/analytics/batch.go | |
parent | ac647e2f77d05cc015a369832c2d428ec1cd6567 (diff) | |
parent | 759506ca799c443402fed9c6413a8b49406e197f (diff) |
Merge pull request #12 from bosssauce/ponzu-dev
[core] More efficient DB queries
Diffstat (limited to 'system/api/analytics/batch.go')
-rw-r--r-- | system/api/analytics/batch.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/system/api/analytics/batch.go b/system/api/analytics/batch.go new file mode 100644 index 0000000..1fee247 --- /dev/null +++ b/system/api/analytics/batch.go @@ -0,0 +1,47 @@ +package analytics + +import ( + "encoding/json" + "strconv" + + "github.com/boltdb/bolt" +) + +// batchInsert is effectively a specialized version of SetContentMulti from the +// db package, iterating over a []apiRequest instead of []url.Values +func batchInsert(batch []apiRequest) error { + err := store.Update(func(tx *bolt.Tx) error { + b, err := tx.CreateBucketIfNotExists([]byte("requests")) + if err != nil { + return err + } + + for _, apiReq := range batch { + // get the next available ID and convert to string + // also set effectedID to int of ID + id, err := b.NextSequence() + if err != nil { + return err + } + cid := strconv.FormatUint(id, 10) + + j, err := json.Marshal(apiReq) + if err != nil { + return err + } + + err = b.Put([]byte(cid), j) + if err != nil { + return err + } + } + + return nil + + }) + if err != nil { + return err + } + + return nil +} |