perf: do a bulk insert in fetchCollection

Improve performance over inserting each item individually
This commit is contained in:
Alexander Navarro 2024-11-22 20:20:22 -03:00
parent 8c660053e5
commit 3cf643c83d
5 changed files with 118 additions and 12 deletions

View file

@ -32,7 +32,6 @@ type Pokemon struct {
}
func getPokemons(
sync *synchronizator.DB,
pagination synchronizator.Pagination,
) ([]*synchronizator.Collection, synchronizator.Pagination, error) {
var collections []*synchronizator.Collection
@ -57,12 +56,9 @@ func getPokemons(
collections = make([]*synchronizator.Collection, 0, len(data.Results))
// TODO: this writes to the database in each collection creation
// add better strategies, like returning a collection to the platform and the
// platform do a final bulk update
for _, pokedex := range data.Results {
collection_name := "Pokedex_" + pokedex.Name
collection, err := sync.NewCollection(collection_name, nil)
collection := synchronizator.NewCollection(collection_name, nil)
if err != nil {
return nil, pagination, err
}
@ -89,7 +85,7 @@ func main() {
defer connection.Close()
opts := synchronizator.DefaultOptions
// opts.Log_level = synchronizator.DEBUG
opts.Log_level = synchronizator.DEBUG
opts.DANGEROUSLY_DROP_TABLES = true
sync, err := synchronizator.New(connection, opts)
@ -105,11 +101,11 @@ func main() {
return
}
fmt.Println(pokeApi)
err = pokeApi.FetchCollections(getPokemons, synchronizator.StartPagination)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(pokeApi)
}