Skip to content

Commit

Permalink
Updated main function to use the sqlite database
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsanjoseph committed Mar 20, 2022
1 parent bc09860 commit 469ff79
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
db.sqlite
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ func handleClose(db riemann.DivisorDb) {
func main() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGINT)
var db = riemann.DivisorDb(&riemann.InMemoryDivisorDb{})
sqlDB := riemann.SqliteDivisorDb{DBPath: "db.sqlite"}
var db = riemann.DivisorDb(&sqlDB)
db.Initialize()
defer sqlDB.Close()

go riemann.PopulateDB(db, 10081, -1, 1000000)
<-sigCh
handleClose(db)
Expand Down
24 changes: 17 additions & 7 deletions riemann/database_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package riemann_test

import (
"os"
"sort"

"github.com/alexsanjoseph/riemann-divisor-sum-go/riemann"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

const DBPath = ":memory:"

// const DBPath = "test.sqlite"

// var _ = AfterEach(func() {
// os.Remove(DBPath)
// })
const DBPath = "test.sqlite"

func beforeEachFunc(inputDb riemann.DivisorDb) riemann.DivisorDb {
os.Remove(DBPath)
db := riemann.DivisorDb(inputDb)
db.Initialize()
return db
}

var _ = AfterEach(func() {
os.Remove(DBPath)
})

var _ = Describe("Parametrized Database Tests", func() {

DescribeTable("is initially empty", func(inputDb riemann.DivisorDb) {
Expand All @@ -34,6 +34,16 @@ var _ = Describe("Parametrized Database Tests", func() {
Entry("In-Memory", &riemann.InMemoryDivisorDb{}),
)

DescribeTable("Multiple initializations should be Idempotent", func(inputDb riemann.DivisorDb) {
db := beforeEachFunc(inputDb)
db.Initialize()
db.Initialize()
Expect(true)
},
Entry("SQLite", &riemann.SqliteDivisorDb{DBPath: DBPath}),
Entry("In-Memory", &riemann.InMemoryDivisorDb{}),
)

DescribeTable("Upserts correctly", func(inputDb riemann.DivisorDb) {
db := beforeEachFunc(inputDb)
records := []riemann.RiemannDivisorSum{
Expand Down
18 changes: 14 additions & 4 deletions riemann/sqlite_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ type SqliteDivisorDb struct {
func (sqdb *SqliteDivisorDb) Initialize() {

db, err := sql.Open("sqlite3", sqdb.DBPath)
sqdb.db = db
if err != nil {
log.Fatal(err)
panic(err)
}

sqlStmt := `
CREATE TABLE RiemannDivisorSums (
n UNSIGNED BIG INT CONSTRAINT divisor_sum_pk PRIMARY KEY,
divisor_sum UNSIGNED BIG INT,
witness_value REAL
);
`
_, err = db.Exec(sqlStmt)
if err != nil {
_, err = sqdb.db.Exec(sqlStmt)

if err != nil && err.Error() != "table RiemannDivisorSums already exists" {
panic(err)
}
sqdb.db = db
}

func (sqdb SqliteDivisorDb) Load() []RiemannDivisorSum {
Expand Down Expand Up @@ -141,3 +143,11 @@ func (sqdb SqliteDivisorDb) Summarize() SummaryStats {
}

}

func (sqdb *SqliteDivisorDb) Close() {
err := sqdb.db.Close()
if err != nil {
panic(err)
}
fmt.Println("DB Closed!")
}

0 comments on commit 469ff79

Please sign in to comment.