Skip to content

Commit

Permalink
BatchGetの実装
Browse files Browse the repository at this point in the history
  • Loading branch information
ebakazu committed Feb 7, 2024
1 parent 1838590 commit 889a69c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
3 changes: 2 additions & 1 deletion k6/load-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const options = {

export function load_test() {
const seriesURL = new URL(`${__ENV.API_BASE_URL}/series`);
const offset = Math.floor(Math.random() * 2183)
const offset = Math.floor(Math.random() * 20)
seriesURL.searchParams.append(`limit`, `20`);
seriesURL.searchParams.append(`offset`, `${offset}`);

Expand Down Expand Up @@ -63,6 +63,7 @@ export function load_test() {
url.searchParams.append(`limit`, `20`)
url.searchParams.append(`offset`, `0`)
url.searchParams.append(`seasonId`, `${season.id}`)
url.searchParams.append(`seriesId`, `${season.seriesId}`)
episodeRequests.push(['GET', url.toString()])
})
}
Expand Down
52 changes: 50 additions & 2 deletions pkg/repository/database/season.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package database
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"

"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/entity"
"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/errcode"
"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/repository"
"github.com/aws/aws-xray-sdk-go/xray"
"go.uber.org/multierr"
)

type Season struct {
Expand Down Expand Up @@ -123,5 +123,53 @@ func (e *Season) BatchGet(ctx context.Context, ids []string) (entity.Seasons, er
ctx, seg := xray.BeginSubsegment(ctx, "database.Season#BatchGet")
defer seg.Close(nil)

return nil, errcode.New(errors.New("not implemtented yet"))
if len(ids) == 0 {
return nil, nil
}

newIDs := make([]string, 0, len(ids))
for _, id := range ids {
if id != "" {
newIDs = append(newIDs, id)
}
}

fields := []string{
"seasonID",
"seriesID",
"displayName",
"imageURL",
"displayOrder",
}

query := fmt.Sprintf(
"SELECT %s FROM seasons WHERE seasonID IN(?%s)",
strings.Join(fields, ","),
strings.Repeat(",?", len(newIDs)-1),
)
rows, err := e.db.QueryContext(ctx, query, newIDs)
if err != nil {
return nil, errcode.New(err)
}

var seasons entity.Seasons
var multiErr error
for rows.Next() {
var s entity.Season
if err = rows.Scan(&s); err != nil {
multiErr = multierr.Append(multiErr, err)
continue
}
seasons = append(seasons, &s)
}

if cerr := rows.Close(); cerr != nil {
return nil, errcode.New(cerr)
}

if multiErr != nil {
return nil, errcode.New(multiErr)
}

return seasons, nil
}
43 changes: 41 additions & 2 deletions pkg/repository/database/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package database
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"

"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/entity"
"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/errcode"
"github.com/CyberAgentHack/server-performance-tuning-2023/pkg/repository"
"github.com/aws/aws-xray-sdk-go/xray"
"go.uber.org/multierr"
)

type Series struct {
Expand Down Expand Up @@ -102,5 +102,44 @@ func (e *Series) BatchGet(ctx context.Context, ids []string) (entity.SeriesMulti
ctx, seg := xray.BeginSubsegment(ctx, "database.Series#BatchGet")
defer seg.Close(nil)

return nil, errcode.New(errors.New("not implemtented yet"))
if len(ids) == 0 {
return nil, nil
}

newIDs := make([]string, 0, len(ids))
for _, id := range ids {
if id != "" {
newIDs = append(newIDs, id)
}
}

fields := []string{"id", "displayName", "description", "imageUrl", "genreId"}
query := fmt.Sprintf(
"SELECT %s FROM series WHERE seriesID IN(?%s)",
strings.Join(fields, ", "),
strings.Repeat(",?", len(newIDs)-1),
)

rows, err := e.db.QueryContext(ctx, query, newIDs)

var seriesMulti entity.SeriesMulti
var multiErr error
for rows.Next() {
var series entity.Series
if err := rows.Scan(&series); err != nil {
multiErr = multierr.Append(multiErr, err)
continue
}
seriesMulti = append(seriesMulti, &series)
}

if err := rows.Close(); err != nil {
return nil, errcode.New(err)
}

if multiErr != nil {
return nil, errcode.New(multiErr)
}

return seriesMulti, errcode.New(err)
}

0 comments on commit 889a69c

Please sign in to comment.