docstore/pkg/server/helper.go
2021-02-18 22:52:36 +01:00

68 lines
1.4 KiB
Go

// Copyright (C) 2021 Marius Schellenberger
package server
import (
"runtime"
"strconv"
"git.giftfish.de/ston1th/docstore/pkg/core"
bleve "git.giftfish.de/ston1th/docstore/pkg/index"
)
const div = 1048576
func fmtMem(i uint64) string {
return strconv.FormatFloat(float64(i)/div, 'f', 2, 64)
}
func getStats(i *bleve.Index) *core.Stats {
mem := new(runtime.MemStats)
runtime.ReadMemStats(mem)
return &core.Stats{
Goroutines: runtime.NumGoroutine(),
Alloc: fmtMem(mem.Alloc),
Sys: fmtMem(mem.Sys),
Docs: i.DocCount(),
GoVersion: runtime.Version(),
}
}
func scanFile(srv *HTTPServer, path string) (err error) {
err = srv.FS.AddScan(path)
if err != nil {
return
}
go func() {
defer srv.FS.RemoveScan(path)
l := srv.Log
file, txt, err := srv.Scanner.Scan(path)
if err != nil {
l.Printf("scanner: %s: %s", path, err)
return
}
tags, err := srv.DB.GetAllRTags()
if err != nil {
l.Printf("getTags: %s: %s", path, err)
}
found := tags.Match(txt)
id, err := srv.DB.Index.Add(txt, found)
if err != nil {
l.Printf("index: %s: %s", path, err)
return
}
err = srv.DB.NewFile(id, file, found)
if err != nil {
l.Printf("newFile: %s: %s", path, err)
}
}()
return
}
// Parts taken from strings.TrimPrefix
func hasTrimPrefix(s, prefix string) (string, bool) {
if len(s) >= len(prefix) && s[0:len(prefix)] == prefix {
return s[len(prefix):], true
}
return s, false
}