Repository: oom-ai/oomstore
Pull request #860 (2021-12-30 06:35:59)

Chore/code clean

codecov[bot]: # [Codecov](https://codecov.io/gh/oom-ai/oomstore/pull/860?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai) Report
> Merging [#860](https://codecov.io/gh/oom-ai/oomstore/pull/860?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai) (5c40214) into [main](https://codecov.io/gh/oom-ai/oomstore/commit/1cc6fe911352c2f5b4b9778b51b6ec8eaae8ed83?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai) (1cc6fe9) will **increase** coverage by `0.09%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/oom-ai/oomstore/pull/860/graphs/tree.svg?width=650&height=150&src=pr&token=C59L7LTRM4&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai)](https://codecov.io/gh/oom-ai/oomstore/pull/860?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai)

```diff
@@            Coverage Diff             @@
##             main     #860      +/-   ##
==========================================
+ Coverage   51.07%   51.16%   +0.09%     
==========================================
  Files          81       81              
  Lines        3252     3252              
==========================================
+ Hits         1661     1664       +3     
+ Misses       1396     1394       -2     
+ Partials      195      194       -1     
```


| [Impacted Files](https://codecov.io/gh/oom-ai/oomstore/pull/860?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai) | Coverage Δ | |
|---|---|---|
| [internal/database/dbutil/util.go](https://codecov.io/gh/oom-ai/oomstore/pull/860/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai#diff-aW50ZXJuYWwvZGF0YWJhc2UvZGJ1dGlsL3V0aWwuZ28=) | `52.94% <100.00%> (+32.94%)` | :arrow_up: |
| [internal/database/online/mysql/store.go](https://codecov.io/gh/oom-ai/oomstore/pull/860/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai#diff-aW50ZXJuYWwvZGF0YWJhc2Uvb25saW5lL215c3FsL3N0b3JlLmdv) | `100.00% <100.00%> (+7.31%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/oom-ai/oomstore/pull/860?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/oom-ai/oomstore/pull/860?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai). Last update [1cc6fe9...5c40214](https://codecov.io/gh/oom-ai/oomstore/pull/860?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oom-ai).


Patch:
diff --git a/internal/database/dbutil/util.go b/internal/database/dbutil/util.go
index 62a79420..0a7d24cb 100644
--- a/internal/database/dbutil/util.go
+++ b/internal/database/dbutil/util.go
@@ -3,6 +3,7 @@ package dbutil
 import (
 	"fmt"
 	"math/rand"
+	"strings"
 	"time"
 )
 
@@ -27,3 +28,13 @@ func RandString(n int) string {
 	}
 	return string(b)
 }
+
+func Placeholders(size int, elem string, sep string) string {
+	r := make([]string, 0, size)
+
+	for i := 0; i < size; i++ {
+		r = append(r, elem)
+	}
+
+	return strings.Join(r, sep)
+}
diff --git a/internal/database/online/mysql/store.go b/internal/database/online/mysql/store.go
index 35bdb7c6..bf45b636 100644
--- a/internal/database/online/mysql/store.go
+++ b/internal/database/online/mysql/store.go
@@ -51,16 +51,7 @@ func (db *DB) Push(ctx context.Context, opt online.PushOpt) error {
 	tableName := sqlutil.OnlineStreamTableName(opt.GroupID)
 
 	insertColumns := append([]string{opt.Entity.Name}, opt.FeatureNames...)
-	insertColumnPlaceholders := make([]string, 0, len(insertColumns))
-	for i := 0; i < len(insertColumnPlaceholders); i++ {
-		insertColumnPlaceholders = append(insertColumnPlaceholders, "?")
-	}
-
 	insertValues := append([]interface{}{opt.EntityKey}, opt.FeatureValues...)
-	insertValuePlaceholders := make([]string, 0, len(insertValues))
-	for i := 0; i < len(insertValues); i++ {
-		insertValuePlaceholders = append(insertValuePlaceholders, "?")
-	}
 
 	updateValues := opt.FeatureValues
 	updatePlaceholders := make([]string, 0, len(opt.FeatureNames))
@@ -71,7 +62,7 @@ func (db *DB) Push(ctx context.Context, opt online.PushOpt) error {
 	query := fmt.Sprintf(`INSERT INTO %s (%s) VALUES(%s) ON DUPLICATE KEY UPDATE %s`,
 		tableName,
 		strings.Join(insertColumns, ","),
-		strings.Join(insertValuePlaceholders, ","),
+		dbutil.Placeholders(len(insertColumns), "?", ","),
 		strings.Join(updatePlaceholders, ","),
 	)
 	_, err := db.ExecContext(ctx, query, append(insertValues, updateValues...)...)
diff --git a/pkg/oomstore/snapshot.go b/pkg/oomstore/snapshot.go
deleted file mode 100644
index 04b3dfb5..00000000
--- a/pkg/oomstore/snapshot.go
+++ /dev/null
@@ -1,46 +0,0 @@
-package oomstore
-
-import (
-	"context"
-	"sort"
-
-	"github.com/oom-ai/oomstore/internal/database/metadata/sqlutil"
-
-	"github.com/oom-ai/oomstore/internal/database/metadata"
-
-	"github.com/oom-ai/oomstore/internal/database/offline"
-)
-
-func (s *OomStore) Snapshot(ctx context.Context, groupName string) error {
-	group, err := s.metadata.GetGroupByName(ctx, groupName)
-	if err != nil {
-		return err
-	}
-	revisions, err := s.metadata.ListRevision(ctx, &group.ID)
-	if err != nil {
-		return err
-	}
-	sort.Slice(revisions, func(i, j int) bool {
-		return revisions[i].Revision < revisions[j].Revision
-	})
-	for i, revision := range revisions {
-		if revision.SnapshotTable != "" {
-			continue
-		}
-		tableName := sqlutil.OfflineStreamSnapshotTableName(group.ID, revision.ID)
-		if err = s.offline.Snapshot(ctx, offline.SnapshotOpt{
-			Group:          group,
-			RevisionID:     revisions[i].ID,
-			PrevRevisionID: revisions[i-1].ID,
-		}); err != nil {
-			return err
-		}
-		if err = s.metadata.UpdateRevision(ctx, metadata.UpdateRevisionOpt{
-			RevisionID:       revision.ID,
-			NewSnapshotTable: &tableName,
-		}); err != nil {
-			return err
-		}
-	}
-	return nil
-}


Test patch:
diff --git a/internal/database/dbutil/util_test.go b/internal/database/dbutil/util_test.go
new file mode 100644
index 00000000..930891fb
--- /dev/null
+++ b/internal/database/dbutil/util_test.go
@@ -0,0 +1,59 @@
+package dbutil_test
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+
+	"github.com/oom-ai/oomstore/internal/database/dbutil"
+)
+
+func TestPlaceholders(t *testing.T) {
+	cases := []struct {
+		describe string
+		elem     string
+		sep      string
+		size     int
+		want     string
+	}{
+		{
+			describe: "one elem ?",
+
+			elem: "?",
+			sep:  ",",
+			size: 1,
+			want: "?",
+		},
+		{
+			describe: "four elem ?",
+
+			elem: "?",
+			sep:  ",",
+			size: 4,
+			want: "?,?,?,?",
+		},
+		{
+			describe: "one elem a",
+
+			elem: "a",
+			sep:  "-",
+			size: 5,
+			want: "a-a-a-a-a",
+		},
+		{
+			describe: "empty elem",
+
+			elem: "",
+			sep:  ",",
+			size: 4,
+			want: ",,,",
+		},
+	}
+
+	for _, c := range cases {
+		t.Run(c.describe, func(t *testing.T) {
+			got := dbutil.Placeholders(c.size, c.elem, c.sep)
+			assert.Equal(t, c.want, got)
+		})
+	}
+}