Skip to content

Commit

Permalink
makta: allow to specify different type for value column
Browse files Browse the repository at this point in the history
  • Loading branch information
miku committed Feb 9, 2022
1 parent 2b0eaf4 commit fd0e28a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
14 changes: 10 additions & 4 deletions go/ckit/cmd/makta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,30 @@ import (
"time"

"github.com/andrew-d/go-termutil"
"github.com/slub/labe/go/ckit"
"github.com/slub/labe/go/ckit/tabutils"
)

var (
Version string
Buildtime string
Version string
Buildtime string
validTypes = []string{"INTEGER", "READ", "TEXT", "BLOB"} // sqlite3

showVersion = flag.Bool("version", false, "show version and exit")
outputFile = flag.String("o", "data.db", "output filename")
bufferSize = flag.Int("B", 64*1<<20, "buffer size")
indexMode = flag.Int("I", 3, "index mode: 0=none, 1=k, 2=v, 3=kv")
cacheSize = flag.Int("C", 1000000, "sqlite3 cache size, needs memory = C x page size")
initDatabase = flag.Bool("init", false, "on start, initialize database, even when the file already exists")
valueType = flag.String("T", "TEXT", "sqlite3 type for value column")
verbose = flag.Bool("verbose", false, "be verbose")
)

func main() {
flag.Parse()
if !ckit.SliceContains(validTypes, *valueType) {
log.Fatal("invalid type for value column: %v %v", *valueType, validTypes)
}
var (
err error
initFile string
Expand All @@ -40,7 +46,7 @@ PRAGMA synchronous = 0;
PRAGMA cache_size = %d;
PRAGMA locking_mode = EXCLUSIVE;`, *cacheSize)
initSQL = `
CREATE TABLE IF NOT EXISTS map (k TEXT, v TEXT);`
CREATE TABLE IF NOT EXISTS map (k TEXT, v %s);`
keyIndexSQL = fmt.Sprintf(`
%s
CREATE INDEX IF NOT EXISTS idx_k ON map(k);`, pragma)
Expand All @@ -65,7 +71,7 @@ PRAGMA temp_store = MEMORY;
_, err = os.Stat(*outputFile)
if err != nil || *initDatabase {
if os.IsNotExist(err) || *initDatabase {
if err := tabutils.RunScript(*outputFile, initSQL, "initialized database"); err != nil {
if err := tabutils.RunScript(*outputFile, fmt.Sprintf(initSQL, *valueType), "initialized database"); err != nil {
log.Fatal(err)
}
} else {
Expand Down
24 changes: 12 additions & 12 deletions go/ckit/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (r *Response) applyInstitutionFilter(institution string) {
if err := json.Unmarshal(b, v); err != nil {
panic(fmt.Sprintf("internal data broken: %v", err))
}
if sliceContains(v.Institution, institution) {
if SliceContains(v.Institution, institution) {
citing = append(citing, b)
} else {
r.Unmatched.Citing = append(r.Unmatched.Citing, b)
Expand All @@ -164,7 +164,7 @@ func (r *Response) applyInstitutionFilter(institution string) {
if err := json.Unmarshal(b, v); err != nil {
panic(fmt.Sprintf("internal data broken: %v", err))
}
if sliceContains(v.Institution, institution) {
if SliceContains(v.Institution, institution) {
cited = append(cited, b)
} else {
r.Unmatched.Cited = append(r.Unmatched.Cited, b)
Expand Down Expand Up @@ -590,6 +590,16 @@ func OpenDatabase(filename string) (*sqlx.DB, error) {
return sqlx.Open("sqlite3", tabutils.WithReadOnly(filename))
}

// SliceContains returns true, if a string slice contains a given value.
func SliceContains(ss []string, v string) bool {
for _, s := range ss {
if v == s {
return true
}
}
return false
}

// edges returns citing (outbound) and cited (inbound) edges for a given DOI.
func (s *Server) edges(ctx context.Context, doi string) (citing, cited []Map, err error) {
t := time.Now()
Expand Down Expand Up @@ -655,16 +665,6 @@ func batchedStrings(ss []string, n int) (result [][]string) {
return
}

// sliceContains returns true, if a string slice contains a given value.
func sliceContains(ss []string, v string) bool {
for _, s := range ss {
if v == s {
return true
}
}
return false
}

// httpErrLogf is a log formatting helper.
func httpErrLogf(w http.ResponseWriter, status int, s string, a ...interface{}) {
httpErrLog(w, status, fmt.Errorf(s, a...))
Expand Down

0 comments on commit fd0e28a

Please sign in to comment.