Skip to content

Commit

Permalink
update vendor deps, add suport for temporary databases
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Dec 20, 2024
1 parent e85bcc0 commit ce7f4f7
Show file tree
Hide file tree
Showing 75 changed files with 28,547 additions and 11,297 deletions.
79 changes: 66 additions & 13 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"log"
"os"
"log/slog"
"net/url"
"strconv"
Expand Down Expand Up @@ -52,6 +53,8 @@ type SQLiteSpatialDatabase struct {
geojson_table database_sql.Table
gocache *gocache.Cache
dsn string
is_tmp bool
tmp_path string
}

// RTreeSpatialIndex is a struct representing an RTree based spatial index
Expand Down Expand Up @@ -104,30 +107,61 @@ func NewSQLiteSpatialDatabaseWriter(ctx context.Context, uri string) (writer.Wri
// instance for performing spatial operations derived from 'uri'.
func NewSQLiteSpatialDatabase(ctx context.Context, uri string) (database.SpatialDatabase, error) {

u, err := url.Parse(uri)

if err != nil {
return nil, fmt.Errorf("Failed to parse URI, %w", err)
}

q := u.Query()
dsn := q.Get("dsn")

is_tmp := false
tmp_path := ""

if dsn == "{tmp}" {

f, err := os.CreateTemp("", ".db")

if err != nil {
return nil, fmt.Errorf("Failed to create temp file, %w", err)
}

tmp_path = f.Name()
is_tmp = true

q.Del("dsn")
q.Set("dsn", tmp_path)

u.RawQuery = q.Encode()
uri = u.String()
}

db, err := database_sql.OpenWithURI(ctx, uri)

if err != nil {
return nil, fmt.Errorf("Failed to create new database, %w", err)
}

return NewSQLiteSpatialDatabaseWithDatabase(ctx, uri, db)
spatial_db, err := NewSQLiteSpatialDatabaseWithDatabase(ctx, uri, db)

if err != nil {
return nil, err
}

if is_tmp {
spatial_db.(*SQLiteSpatialDatabase).is_tmp = is_tmp
spatial_db.(*SQLiteSpatialDatabase).tmp_path = tmp_path
}

return spatial_db, nil
}

// NewSQLiteSpatialDatabaseWithDatabase returns a new `whosonfirst/go-whosonfirst-spatial/database.database.SpatialDatabase`
// instance for performing spatial operations derived from 'uri' and an existing `aaronland/go-sqlite/database.SQLiteDatabase`
// instance defined by 'sqlite_db'.
func NewSQLiteSpatialDatabaseWithDatabase(ctx context.Context, uri string, db *sql.DB) (database.SpatialDatabase, error) {

u, err := url.Parse(uri)

if err != nil {
return nil, fmt.Errorf("Failed to parse URI, %w", err)
}

q := u.Query()

dsn := q.Get("dsn")

rtree_table, err := tables.NewRTreeTableWithDatabase(ctx, db)

if err != nil {
Expand Down Expand Up @@ -178,7 +212,6 @@ func NewSQLiteSpatialDatabaseWithDatabase(ctx context.Context, uri string, db *s
spr_table: spr_table,
geojson_table: geojson_table,
gocache: gc,
dsn: dsn,
mu: mu,
}

Expand All @@ -187,7 +220,17 @@ func NewSQLiteSpatialDatabaseWithDatabase(ctx context.Context, uri string, db *s

// Disconnect will close the underlying database connection.
func (r *SQLiteSpatialDatabase) Disconnect(ctx context.Context) error {
return r.db.Close()

if r.is_tmp {

err := os.Remove(r.tmp_path)

if err != nil {
slog.Error("Failed to remove tmp db", "pth", r.tmp_path, "error", err)
}
}

return r.db.Close()
}

// IndexFeature will index a Who's On First GeoJSON Feature record, defined in 'body', in the spatial database.
Expand Down Expand Up @@ -217,6 +260,16 @@ func (r *SQLiteSpatialDatabase) IndexFeature(ctx context.Context, body []byte) e
}
}

/*
q := "SELECT COUNT(id) FROM geojson"
row := r.db.QueryRowContext(ctx, q)
var c int
row.Scan(&c)
slog.Info("INDEX", "count", c)
*/

return nil
}

Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module github.com/whosonfirst/go-whosonfirst-spatial-sqlite
go 1.23.3

require (
github.com/mattn/go-sqlite3 v1.14.16
github.com/mattn/go-sqlite3 v1.14.24
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/paulmach/orb v0.11.1
github.com/pkg/errors v0.9.1
github.com/sfomuseum/go-database v0.0.7
github.com/sfomuseum/go-database v0.0.10
github.com/whosonfirst/go-ioutil v1.0.2
github.com/whosonfirst/go-reader v1.0.2
github.com/whosonfirst/go-whosonfirst-database v0.0.2
github.com/whosonfirst/go-whosonfirst-database v0.0.8
github.com/whosonfirst/go-whosonfirst-spatial v0.11.1
github.com/whosonfirst/go-whosonfirst-spatial-grpc v0.2.1
github.com/whosonfirst/go-whosonfirst-spatial-www v0.3.1
github.com/whosonfirst/go-whosonfirst-spatial-www v0.4.0
github.com/whosonfirst/go-whosonfirst-spr/v2 v2.3.7
github.com/whosonfirst/go-whosonfirst-sqlite-spr/v2 v2.1.0
github.com/whosonfirst/go-whosonfirst-uri v1.3.0
Expand Down Expand Up @@ -101,7 +101,7 @@ require (
github.com/schollz/progressbar/v3 v3.13.1 // indirect
github.com/sfomuseum/go-edtf v1.2.1 // indirect
github.com/sfomuseum/go-flags v0.10.0 // indirect
github.com/sfomuseum/go-http-auth v0.11.0 // indirect
github.com/sfomuseum/go-http-auth v0.12.0 // indirect
github.com/sfomuseum/go-http-protomaps v0.3.0 // indirect
github.com/sfomuseum/go-http-rollup v0.0.3 // indirect
github.com/sfomuseum/go-sfomuseum-mapshaper v0.0.3 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPn
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
Expand Down Expand Up @@ -262,15 +262,15 @@ github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU
github.com/schollz/progressbar/v3 v3.13.1 h1:o8rySDYiQ59Mwzy2FELeHY5ZARXZTVJC7iHD6PEFUiE=
github.com/schollz/progressbar/v3 v3.13.1/go.mod h1:xvrbki8kfT1fzWzBT/UZd9L6GA+jdL7HAgq2RFnO6fQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sfomuseum/go-database v0.0.7 h1:+eeWhkMMLQ32hNdXCJiINsWS7EEhqmrTFPNEH93lwTg=
github.com/sfomuseum/go-database v0.0.7/go.mod h1:c1oZwb0M0aYSI48SgE8b5iIrGrLFA8eJFk46E5g4JOM=
github.com/sfomuseum/go-database v0.0.10 h1:r6jogMcmELDsc+bngW1vrRWz5mEDegogcVq5t4DtFtE=
github.com/sfomuseum/go-database v0.0.10/go.mod h1:c1oZwb0M0aYSI48SgE8b5iIrGrLFA8eJFk46E5g4JOM=
github.com/sfomuseum/go-edtf v1.2.1 h1:vyKvoNa4p6mmwp9AbbEdJoj+A5YK4G7S1QgmSNpXh7s=
github.com/sfomuseum/go-edtf v1.2.1/go.mod h1:1rP0EJZ/84j3HO80vGcnG2T9MFBDAFyTNtjrr8cv3T4=
github.com/sfomuseum/go-flags v0.8.3/go.mod h1:VXOnnX1/yxQpX2yiwHaBV6aCmhtszQOL5bL1/nNo3co=
github.com/sfomuseum/go-flags v0.10.0 h1:1OC1ACxpWMsl3XQ9OeNVMQj7Zi2CzufP3Rym3mPI8HU=
github.com/sfomuseum/go-flags v0.10.0/go.mod h1:VXOnnX1/yxQpX2yiwHaBV6aCmhtszQOL5bL1/nNo3co=
github.com/sfomuseum/go-http-auth v0.11.0 h1:4p4SynsSVR+s5tc4L3SAQ5nCaf5VD/331sxR9EFmgyE=
github.com/sfomuseum/go-http-auth v0.11.0/go.mod h1:m0I60r/aVlWXia43eoZOp/d0u7M7pURgJEYElvVVe98=
github.com/sfomuseum/go-http-auth v0.12.0 h1:eoWEpjwcZ6IsCtCXaOxUVtVpLIKR5lG9dlHjGHcyDak=
github.com/sfomuseum/go-http-auth v0.12.0/go.mod h1:m0I60r/aVlWXia43eoZOp/d0u7M7pURgJEYElvVVe98=
github.com/sfomuseum/go-http-protomaps v0.3.0 h1:e89s6wsDHyCXDgB6hFARMjsdrgn/lG4BRybRJuXTktY=
github.com/sfomuseum/go-http-protomaps v0.3.0/go.mod h1:K/PDDhy0c3xcPK2Bot7+oIz2l8QQLZys9SqAblhHT6I=
github.com/sfomuseum/go-http-rollup v0.0.3 h1:N3CnjZluhzRhqPpdM+GTuzCnjM0V5xtPX+LeCgZWhTA=
Expand Down Expand Up @@ -328,8 +328,8 @@ github.com/whosonfirst/go-sanitize v0.1.0 h1:ygSqCnakwdzH/m8UEa15zXGDsoo5/JJeRkg
github.com/whosonfirst/go-sanitize v0.1.0/go.mod h1:p/emgbafMM0p5iVAz2XWwecYPl06Tw4Jos9rhTKIrt8=
github.com/whosonfirst/go-whosonfirst-crawl v0.2.2 h1:7nwpNV/BFoPR0R7KMMy1iiYAer7wlHJBUOiL+NLzIFs=
github.com/whosonfirst/go-whosonfirst-crawl v0.2.2/go.mod h1:2GZkaK9jaOisWRnBQGWzmb7H55TUFl9y9F30lrk2hwk=
github.com/whosonfirst/go-whosonfirst-database v0.0.2 h1:LDWTgrT8A+BD+cvw2rjzg4mfkimmBSKAqwwSj7y7IJM=
github.com/whosonfirst/go-whosonfirst-database v0.0.2/go.mod h1:wguz5cQeZjm+0FvmOcYMV6eFx085f1NQO3gPYq654ME=
github.com/whosonfirst/go-whosonfirst-database v0.0.8 h1:L83SifUUvEicwf/SuwLO44rzeEdR9ZPgEXfs9qe9kSU=
github.com/whosonfirst/go-whosonfirst-database v0.0.8/go.mod h1:00U9I4jFD7ob2lxV1sL6TN0rME+4GxXcQssZR9WYnPs=
github.com/whosonfirst/go-whosonfirst-export/v2 v2.8.3 h1:mq1OjMP2Trenb+1wJGrYdKaFzkwWWHYSjdfPjok437A=
github.com/whosonfirst/go-whosonfirst-export/v2 v2.8.3/go.mod h1:e75gUUyEq8WhG+BVQ0wktZ6IqDLeJZlONDtXHpjxGtg=
github.com/whosonfirst/go-whosonfirst-feature v0.0.28 h1:XCNNzjnk1i50UnqrNtrN9xNvJDwZGjsPHbb3HRPixXA=
Expand All @@ -354,8 +354,8 @@ github.com/whosonfirst/go-whosonfirst-spatial v0.11.1 h1:EUYDE5I6ojL+70VL+B3unmC
github.com/whosonfirst/go-whosonfirst-spatial v0.11.1/go.mod h1:ja79l9imEriYcWFVX1vbW4eMAf407/GqEQ3q5Fpa/1A=
github.com/whosonfirst/go-whosonfirst-spatial-grpc v0.2.1 h1:FHADyxeRmrDaj9w7N8DzuNqN4nWURggh3ir22noC7kM=
github.com/whosonfirst/go-whosonfirst-spatial-grpc v0.2.1/go.mod h1:4Jz+k9hc7E1vdB7gHMotwURKYIAkTDXgzwaarlpVTMU=
github.com/whosonfirst/go-whosonfirst-spatial-www v0.3.1 h1:rNs19ctrgOEgKUqs09jeaeNfEKVoP3f4W77sVhD9Glc=
github.com/whosonfirst/go-whosonfirst-spatial-www v0.3.1/go.mod h1:vL0+l8ECOMJSl//KWUq1xVcuCpzfWkYZE+GV95lss0k=
github.com/whosonfirst/go-whosonfirst-spatial-www v0.4.0 h1:HGkyR71rtXCuVdWLw3Ur7HvUAo32WPXFR75cROzaov4=
github.com/whosonfirst/go-whosonfirst-spatial-www v0.4.0/go.mod h1:k0IDQFYXM1LVGneGeLWo+VJvh4lfJ87lR4R5OC3qodw=
github.com/whosonfirst/go-whosonfirst-spelunker v0.0.5 h1:cZZNft+n3eJ1UFcJIPfHudobUOTvJsx6FDismS4OiUA=
github.com/whosonfirst/go-whosonfirst-spelunker v0.0.5/go.mod h1:/piw3kZ2BSx320SShlHTPHQkHPJJUUizBBzKmOPaiog=
github.com/whosonfirst/go-whosonfirst-spr-geojson v0.0.8 h1:K0rCaKo0xvOtuS/x9r62bEfIK3OTdZqbDmgG+A3xDSs=
Expand Down
40 changes: 25 additions & 15 deletions vendor/github.com/mattn/go-sqlite3/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions vendor/github.com/mattn/go-sqlite3/callback.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ce7f4f7

Please sign in to comment.