Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update sqlite to use a hand build schema #2305

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 11 additions & 53 deletions cmd/deltadb/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
## deltadb

This application can build a standalone sqlite database which can be used as desired in third party applications.
It also adds an example of a limited REST API server as a technology demonstation.

This has been made possible by using the pure go module `modernc.org/sqlite`, which uses some interesting automatic
conversion of the core c-code to go to build a version that doesn't require the cgo library wrapper.

### options

```
Build a DELTA Sqlite DB and optional REST API service
Build a DELTA Sqlite DB

Usage:

Expand All @@ -23,12 +22,8 @@ Options:
name of the database file on disk
-debug
add extra operational info
-hostport string
base directory of delta files on disk (default ":8080")
-init
initialise the database if a file on disk
-listen
should a web service be enabled
-resp string
base directory of resp files on disk
-response string
Expand All @@ -51,55 +46,18 @@ and then to examine the file
sqlite3 delta.db
SQLite version 3.46.1 2024-08-13 09:16:08
Enter ".help" for usage hints.
sqlite> .schema Network
CREATE TABLE Network (
Code TEXT PRIMARY KEY,
External TEXT,
Description TEXT,
Restricted TEXT
sqlite> .schema network
CREATE TABLE network (
network_id INTEGER PRIMARY KEY NOT NULL,
network TEXT NOT NULL,
external TEXT NOT NULL,
description TEXT DEFAULT "" NOT NULL,
restricted BOOLEAN DEFAULT false NOT NULL,
UNIQUE (network)
);
sqlite>
```

Another example which uses the built in REST API, e.g.
## schema

```
./deltadb -debug -listen
2024/11/03 20:55:11 initialise database
2024/11/03 20:55:12 database initialised in 1.181009504s
2024/11/03 20:55:12 handling requests on :8080
```

and then the service can be accessed via curl, e.g.

```
curl -s localhost:8080/network|jq .|head
[
{
"network": "AK",
"external": "NZ",
"description": "Auckland volcano seismic network"
},
{
"network": "BC",
"external": "BC",
"description": "Building monitoring camera network"
...
```

or

```
curl -s localhost:8080/station/WEL/site/20/sensor/|jq . | head
[
{
"make": "Kinemetrics",
"model": "FBA-23",
"serial": "25073",
"start": "1990-10-03T03:04:00Z",
"end": "2003-04-08T00:00:00Z",
"dip": 0,
"azimuth": 0,
"factor": 0,
...
```
[![Schema](delta.svg)](delta.svg)
79 changes: 0 additions & 79 deletions cmd/deltadb/handler.go

This file was deleted.

27 changes: 3 additions & 24 deletions cmd/deltadb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"
Expand All @@ -27,9 +26,7 @@ type Settings struct {
db string // name of the database file
response string // name of the database response table

init bool // should the database be updated
listen bool // should a web service be enabled
hostport string // hostport to listen on for the web service
init bool // should the database be updated
}

func main() {
Expand All @@ -38,7 +35,7 @@ func main() {

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Build a DELTA Sqlite DB and optional REST API service\n")
fmt.Fprintf(os.Stderr, "Build a DELTA Sqlite DB file\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, "\n")
Expand All @@ -52,13 +49,10 @@ func main() {

flag.BoolVar(&settings.debug, "debug", false, "add extra operational info")
flag.BoolVar(&settings.init, "init", false, "initialise the database if a file on disk")
flag.BoolVar(&settings.listen, "listen", false, "should a web service be enabled")

flag.StringVar(&settings.base, "base", "", "base directory of delta files on disk")
flag.StringVar(&settings.resp, "resp", "", "base directory of resp files on disk")
flag.StringVar(&settings.db, "db", "", "name of the database file on disk")
flag.StringVar(&settings.response, "response", "Response", "optional database response table name to use")
flag.StringVar(&settings.hostport, "hostport", ":8080", "base directory of delta files on disk")

flag.Parse()

Expand Down Expand Up @@ -106,7 +100,7 @@ func main() {
if settings.db == "" || settings.init {

// insert extra response files
extra := set.KeyValue(settings.response, "Name", "Response", values)
extra := set.KeyValue(settings.response, "Response", "XML", values)

log.Println("initialise database")
start := time.Now()
Expand All @@ -115,19 +109,4 @@ func main() {
}
log.Printf("database initialised in %s", time.Since(start).String())
}

if !settings.listen {
return
}

log.Printf("handling requests on %s", settings.hostport)
server := &http.Server{
Addr: settings.hostport,
Handler: newHandler(db),
ReadHeaderTimeout: 3 * time.Second,
}

if err := server.ListenAndServe(); err != nil {
panic(err)
}
}
43 changes: 0 additions & 43 deletions cmd/deltadb/mark.go

This file was deleted.

43 changes: 0 additions & 43 deletions cmd/deltadb/monument.go

This file was deleted.

Loading
Loading