Skip to content

Commit

Permalink
Fix duplicate add request
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonaka committed Jun 29, 2023
1 parent 95a3e4c commit 6475808
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package l2mserver

import (
"encoding/json"
"errors"
"fmt"
"sync"

Expand All @@ -29,7 +30,10 @@ func (localDb) GetSharePieceSize(dataID string) (int32, error) {
}
mu.Lock()
defer mu.Unlock()
mp, _ := db[dataID]
mp, ok := db[dataID]
if !ok {
return 0, fmt.Errorf("データ未登録エラー: %sは登録されていません.", dataID)
}
return int32(len(mp)), nil
}
func (localDb) InsertShares(dataID string, schema []*pb_types.Schema, pieceID int32, shares string, sentAt string, matchingColumn int32) error {
Expand All @@ -54,6 +58,9 @@ func (localDb) InsertShares(dataID string, schema []*pb_types.Schema, pieceID in
if !ok {
db[dataID] = map[int32]m2db.Share{}
}
if _, ok := db[dataID][pieceID]; ok {
return errors.New("重複データ登録エラー: " + dataID + "は既に登録されています.")
}
db[dataID][pieceID] = share
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func (p processer) addShareDataFrame(baseDataID string, addDataID string) (strin
s := fmt.Sprintf("%s+%s", baseDataID, addDataID)
r := sha256.Sum256([]byte(s))
dataID := hex.EncodeToString(r[:])
_, err := p.m2dbclient.GetSharePieceSize(dataID)
if err == nil {
// errがない(既に登録されている)場合は既に加算されてるはずなのでそのままreturn
return dataID, nil
}

// pieceのindexと今見ているpieceデータのindex
basePieceID := int32(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAddShareDataFrameSave(t *testing.T) {
{"13.00000000000000000000000000000000000000000000000000",
"104.00000000000000000000000000000000000000000000000000"},
},
DataID: "6a8787d0da13f1dc67fce89cbb217aeeb5473fb1dce55e846ab65f35135dd215",
DataID: "8bf179ec763fd05e429a2ae5847cb47c2a116709e5f5df9b3c9256b278cefe51",
Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}},
},
}
Expand All @@ -37,8 +37,8 @@ func TestAddShareDataFrameSave(t *testing.T) {
t.Parallel()

// テスト用データをテスト用DBに保存しておく
dataID1 := fmt.Sprintf("%s%d", name, 1)
dataID2 := fmt.Sprintf("%s%d", name, 2)
dataID1 := fmt.Sprintf("TestAddShareDataFrameSave%s%d", name, 1)
dataID2 := fmt.Sprintf("TestAddShareDataFrameSave%s%d", name, 2)
mu.Lock()
db[dataID1] = map[int32]m2db.Share{}
db[dataID1][0] = tt.baseData
Expand Down Expand Up @@ -85,17 +85,17 @@ func TestAddPieceShareDataFrameSave(t *testing.T) {
{Value: [][]string{{
"11.00000000000000000000000000000000000000000000000000",
"102.00000000000000000000000000000000000000000000000000"}},
DataID: "6a8787d0da13f1dc67fce89cbb217aeeb5473fb1dce55e846ab65f35135dd215",
DataID: "197425564f8a14fcbbf165a88daa75afc83d8ffb4a2085610b08282349d936ed",
Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}},
{Value: [][]string{{
"13.00000000000000000000000000000000000000000000000000",
"104.00000000000000000000000000000000000000000000000000"}},
DataID: "6a8787d0da13f1dc67fce89cbb217aeeb5473fb1dce55e846ab65f35135dd215",
DataID: "197425564f8a14fcbbf165a88daa75afc83d8ffb4a2085610b08282349d936ed",
Meta: m2db.ShareMeta{PieceID: 1, MatchingColumn: 1}},
{Value: [][]string{{
"15.00000000000000000000000000000000000000000000000000",
"106.00000000000000000000000000000000000000000000000000"}},
DataID: "6a8787d0da13f1dc67fce89cbb217aeeb5473fb1dce55e846ab65f35135dd215",
DataID: "197425564f8a14fcbbf165a88daa75afc83d8ffb4a2085610b08282349d936ed",
Meta: m2db.ShareMeta{PieceID: 2, MatchingColumn: 1}},
},
},
Expand All @@ -113,17 +113,17 @@ func TestAddPieceShareDataFrameSave(t *testing.T) {
{Value: [][]string{{
"11.00000000000000000000000000000000000000000000000000",
"102.00000000000000000000000000000000000000000000000000"}},
DataID: "bbcc21770ff7bf4b711243017e94d8493437d92a8152aa7da7b0be276096fc80",
DataID: "7817e5c7355dd7439e28d3af0d7720f128d320f1c9fe6a3837522fff87f68411",
Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}},
{Value: [][]string{{
"13.00000000000000000000000000000000000000000000000000",
"104.00000000000000000000000000000000000000000000000000"}},
DataID: "bbcc21770ff7bf4b711243017e94d8493437d92a8152aa7da7b0be276096fc80",
DataID: "7817e5c7355dd7439e28d3af0d7720f128d320f1c9fe6a3837522fff87f68411",
Meta: m2db.ShareMeta{PieceID: 1, MatchingColumn: 1}},
{Value: [][]string{{
"15.00000000000000000000000000000000000000000000000000",
"106.00000000000000000000000000000000000000000000000000"}},
DataID: "bbcc21770ff7bf4b711243017e94d8493437d92a8152aa7da7b0be276096fc80",
DataID: "7817e5c7355dd7439e28d3af0d7720f128d320f1c9fe6a3837522fff87f68411",
Meta: m2db.ShareMeta{PieceID: 2, MatchingColumn: 1}},
},
},
Expand All @@ -141,14 +141,14 @@ func TestAddPieceShareDataFrameSave(t *testing.T) {
{Value: [][]string{{
"11.00000000000000000000000000000000000000000000000000",
"102.00000000000000000000000000000000000000000000000000"}},
DataID: "184642e3b73a954582fb59d6feb3f0a95a458923bc3ef033059f4b64e6032cc1",
DataID: "083ee381e2733c9a6147ea050b2d6e79b494e61b34c046b4dc110b7a40d8aa91",
Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}},
{Value: [][]string{
{"13.00000000000000000000000000000000000000000000000000",
"104.00000000000000000000000000000000000000000000000000"},
{"15.00000000000000000000000000000000000000000000000000",
"106.00000000000000000000000000000000000000000000000000"}},
DataID: "184642e3b73a954582fb59d6feb3f0a95a458923bc3ef033059f4b64e6032cc1",
DataID: "083ee381e2733c9a6147ea050b2d6e79b494e61b34c046b4dc110b7a40d8aa91",
Meta: m2db.ShareMeta{PieceID: 1, MatchingColumn: 1}},
},
},
Expand All @@ -160,8 +160,8 @@ func TestAddPieceShareDataFrameSave(t *testing.T) {
t.Parallel()

// テスト用データを1行ずつテスト用DBに保存しておく
baseDataID := fmt.Sprintf("%s%d", name, 1)
addDataID := fmt.Sprintf("%s%d", name, 2)
baseDataID := fmt.Sprintf("TestAddPieceShareDataFrameSave%s%d", name, 1)
addDataID := fmt.Sprintf("TestAddPieceShareDataFrameSave%s%d", name, 2)
mu.Lock()
db[baseDataID] = map[int32]m2db.Share{}
for pieceID, share := range tt.baseData {
Expand Down Expand Up @@ -191,3 +191,60 @@ func TestAddPieceShareDataFrameSave(t *testing.T) {
})
}
}

// 同じIDを指定して加算したテーブルが正しく保存されてるかテスト
func TestAddShareDataFrameSaveTwice(t *testing.T) {
testcases := map[string]struct {
baseData m2db.Share
addData m2db.Share
expected m2db.Share
}{
"NormalCase": {
baseData: m2db.Share{Value: [][]string{{"1", "2"}, {"3", "4"}},
Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}},
addData: m2db.Share{Value: [][]string{{"10", "100"}, {"10", "100"}},
Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}},
expected: m2db.Share{Value: [][]string{
{"11.00000000000000000000000000000000000000000000000000",
"102.00000000000000000000000000000000000000000000000000"},
{"13.00000000000000000000000000000000000000000000000000",
"104.00000000000000000000000000000000000000000000000000"},
},
DataID: "8508368950a44ff61bcf86db613fcf9bc2c4b409d039dec6efcfc0a25765be5c",
Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}},
},
}
for name, tt := range testcases {
name := name
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()

// テスト用データをテスト用DBに保存しておく
dataID1 := fmt.Sprintf("TestAddShareDataFrameSaveTwice%s%d", name, 1)
dataID2 := fmt.Sprintf("TestAddShareDataFrameSaveTwice%s%d", name, 2)
mu.Lock()
db[dataID1] = map[int32]m2db.Share{}
db[dataID1][0] = tt.baseData
db[dataID2] = map[int32]m2db.Share{}
db[dataID2][0] = tt.addData
mu.Unlock()

// 何回も加算処理をしてDBに加算された値が保存されているか確認
for i := 0; i <= 5; i++ {
p := processer{m2dbclient: localDb{}}
dataID, errAdd := p.addShareDataFrame(dataID1, dataID2)
if errAdd != nil {
t.Fatal(errAdd)
}
share, errShare := p.m2dbclient.GetSharePiece(dataID, 0)
if errShare != nil {
t.Fatal(errShare)
}
if !reflect.DeepEqual(share, tt.expected) {
t.Fatalf("share must be %v, but %v", tt.expected, share)
}
}
})
}
}

0 comments on commit 6475808

Please sign in to comment.