github.com/PDOK/gokoala@v0.50.6/internal/ogc/features/datasources/geopackage/warmup.go (about)

     1  package geopackage
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/PDOK/gokoala/config"
     9  
    10  	"github.com/jmoiron/sqlx"
    11  )
    12  
    13  // warmUpFeatureTables executes a warmup query to speedup subsequent queries.
    14  // This encompasses traversing index(es) to fill the local cache.
    15  func warmUpFeatureTables(
    16  	configuredCollections config.GeoSpatialCollections,
    17  	featureTableByCollectionID map[string]*featureTable,
    18  	db *sqlx.DB) error {
    19  
    20  	for collID, table := range featureTableByCollectionID {
    21  		if table == nil {
    22  			return errors.New("given table can't be nil")
    23  		}
    24  		for _, coll := range configuredCollections {
    25  			if coll.ID == collID && coll.Features != nil {
    26  				if err := warmUpFeatureTable(table.TableName, db); err != nil {
    27  					return err
    28  				}
    29  				break
    30  			}
    31  		}
    32  	}
    33  	return nil
    34  }
    35  
    36  func warmUpFeatureTable(tableName string, db *sqlx.DB) error {
    37  	query := fmt.Sprintf(`
    38  select minx,maxx,miny,maxy from %[1]s where minx <= 0 and maxx >= 0 and miny <= 0 and maxy >= 0
    39  `, tableName)
    40  
    41  	log.Printf("start warm-up of feature table '%s'", tableName)
    42  	_, err := db.Exec(query)
    43  	if err != nil {
    44  		return fmt.Errorf("failed to warm-up feature table '%s': %w", tableName, err)
    45  	}
    46  	log.Printf("end warm-up of feature table '%s'", tableName)
    47  	return nil
    48  }