Skip to content

Commit

Permalink
sqlite: generate seed
Browse files Browse the repository at this point in the history
  • Loading branch information
high-moctane committed Jan 28, 2024
1 parent 422f15e commit f6e9a31
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/klauspost/compress v1.10.3 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/pierrec/xxHash v0.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo=
github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
Expand Down
7 changes: 7 additions & 0 deletions handler/sqlite/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ const (
type SQLiteHandler struct {
db *sql.DB
eventCh chan *mocrelay.Event
seed uint32
}

func NewSQLiteHandler(ctx context.Context, db *sql.DB) (*SQLiteHandler, error) {
if err := Migrate(ctx, db); err != nil {
return nil, fmt.Errorf("failed to migrate: %w", err)
}

seed, err := getOrSetSeed(ctx, db)
if err != nil {
return nil, fmt.Errorf("failed to get or set seed: %w", err)
}

h := &SQLiteHandler{
db: db,
eventCh: make(chan *mocrelay.Event, bulkInsertNum),
seed: seed,
}

go h.serveBulkInsert(ctx)
Expand Down
36 changes: 36 additions & 0 deletions handler/sqlite/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package sqlite
import (
"bytes"
"context"
"crypto/rand"
"database/sql"
"encoding/json"
"errors"
"fmt"
"math"
"math/big"
"slices"
"text/template"

Expand Down Expand Up @@ -245,3 +248,36 @@ func buildInsertDeletedKeys(

return b.ToSQL()
}

func getOrSetSeed(ctx context.Context, db *sql.DB) (seed uint32, err error) {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return 0, fmt.Errorf("failed to begin transaction: %w", err)
}
defer func() {
if err != nil {
err = errors.Join(err, tx.Rollback())
return
}
err = tx.Commit()
}()

if err := tx.QueryRowContext(ctx, "select seed from hash_seed").Scan(&seed); err != nil {
if errors.Is(err, sql.ErrNoRows) {
n, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
if err != nil {
return 0, fmt.Errorf("failed to generate random number: %w", err)
}
seed = uint32(n.Int64())

if _, err := tx.ExecContext(ctx, "insert into hash_seed (seed) values (?)", seed); err != nil {
return 0, fmt.Errorf("failed to insert seed: %w", err)
}

} else {
return 0, fmt.Errorf("failed to query seed: %w", err)
}
}

return
}
9 changes: 9 additions & 0 deletions handler/sqlite/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,14 @@ func Migrate(ctx context.Context, db *sql.DB) error {
return fmt.Errorf("failed to create deleted_keys table: %w", err)
}

// table hash_seed
if _, err := db.ExecContext(ctx, `
create table if not exists hash_seed (
seed integer not null primary key
) strict, without rowid;
`); err != nil {
return fmt.Errorf("failed to create hash_seed table: %w", err)
}

return nil
}

0 comments on commit f6e9a31

Please sign in to comment.