From 5846de2344483f3f83f2a429e486370b453dfeba Mon Sep 17 00:00:00 2001 From: Ryo Kitagawa Date: Sun, 15 Dec 2024 21:11:51 +0900 Subject: [PATCH] refactor: speed up ListProjects --- langserver/internal/bigquery/bigquery.go | 2 +- langserver/internal/bigquery/cache.go | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/langserver/internal/bigquery/bigquery.go b/langserver/internal/bigquery/bigquery.go index 6882308..0da1e4b 100644 --- a/langserver/internal/bigquery/bigquery.go +++ b/langserver/internal/bigquery/bigquery.go @@ -77,7 +77,7 @@ func (c *client) GetDefaultProject() string { } func (c *client) ListProjects(ctx context.Context) ([]*cloudresourcemanager.Project, error) { - caller := c.cloudresourcemanagerService.Projects.List().Context(ctx) + caller := c.cloudresourcemanagerService.Projects.List().PageSize(1000).Context(ctx) list, err := caller.Do() if err != nil { diff --git a/langserver/internal/bigquery/cache.go b/langserver/internal/bigquery/cache.go index c97369a..9fbb86a 100644 --- a/langserver/internal/bigquery/cache.go +++ b/langserver/internal/bigquery/cache.go @@ -83,10 +83,14 @@ func (c *cache) callListProjects(ctx context.Context) ([]*cloudresourcemanager.P } if len(result) > 0 { - err := c.db.InsertProjects(ctx, result) - if err != nil { - // TODO - fmt.Fprintf(os.Stderr, "failed to insert projects: %v\n", err) + insertSize := 1000 + for i := 0; i < len(result); i += insertSize { + insertItems := result[i:min(len(result), i+insertSize)] + err := c.db.InsertProjects(ctx, insertItems) + if err != nil { + // TODO + fmt.Fprintf(os.Stderr, "failed to insert projects: %v\n", err) + } } } return result, nil