Rebuild indexes

This commit is contained in:
Nicolas Dextraze 2018-03-08 14:59:07 -08:00
parent 6713362868
commit 84e8e469ad
4 changed files with 650 additions and 539 deletions

View File

@ -14,6 +14,7 @@ import (
var addr = flag.String("addr", "tcp://127.0.0.1:12345", "zeromq address to listen to") var addr = flag.String("addr", "tcp://127.0.0.1:12345", "zeromq address to listen to")
var db = flag.String("db", fmt.Sprintf(".%cevents", os.PathSeparator), "path for storage") var db = flag.String("db", fmt.Sprintf(".%cevents", os.PathSeparator), "path for storage")
var buildTypeIndexes = flag.Bool("buildTypeIndexes", false, "Build type indexes") var buildTypeIndexes = flag.Bool("buildTypeIndexes", false, "Build type indexes")
var buildIndexes = flag.Bool("buildIndexes", false, "Build indexes")
func PathIsAbsolute(s string) bool { func PathIsAbsolute(s string) bool {
if len(s) > 1 && s[1] == ':' { if len(s) > 1 && s[1] == ':' {
@ -36,6 +37,10 @@ func main() {
} }
diskStorage := storage.NewDailyDiskStorage(storagePath) diskStorage := storage.NewDailyDiskStorage(storagePath)
if *buildIndexes {
diskStorage.RebuildIndexes()
return
}
if *buildTypeIndexes { if *buildTypeIndexes {
diskStorage.RebuildTypeIndexes() diskStorage.RebuildTypeIndexes()
return return

View File

@ -9,6 +9,10 @@ import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"bytes" "bytes"
"path/filepath"
"encoding/json"
"strings"
"strconv"
) )
const EMPTY_STREAM = uint32(0) const EMPTY_STREAM = uint32(0)
@ -278,3 +282,100 @@ func (me DailyDiskStorage) RebuildTypeIndexes() {
fmt.Println("Done.") fmt.Println("Done.")
} }
func (me DailyDiskStorage) RebuildIndexes() {
fmt.Print("Rebuilding indexes...");
err := os.RemoveAll(me.indexesPath)
if err != nil {
panic(err)
}
err = os.MkdirAll(me.typesIndexesPath, 0777)
if err != nil {
panic(err)
}
fileList := make([]string, 0)
err = filepath.Walk(me.storagePath, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
fileList = append(fileList, path)
}
return nil
})
for _, file := range fileList {
event := map[string]interface{} {}
if content, err := ioutil.ReadFile(file); err != nil {
panic(err)
} else if parts := bytes.Split(content, CRLF); len(parts) == 0 {
panic(errors.New(fmt.Sprintf("Empty event file %s", file)))
} else if err := json.Unmarshal(parts[0], &event); err != nil {
panic(err)
} else {
firstKey := string(bytes.Split(parts[0], []byte("\""))[1])
streamId := event[firstKey].(string)
streamUuid, err := uuid.FromString(streamId)
if err != nil {
panic(err)
}
if file[0:len(me.storagePath)] == me.storagePath {
file = file[len(me.storagePath)+1:]
}
file = strings.Replace(file, "/", "", -1)
parts := strings.Split(file, "_")
typeId := parts[1]
year, err := strconv.Atoi(file[0:4])
if err != nil {
panic(err)
}
month, err := strconv.Atoi(file[4:6])
if err != nil {
panic(err)
}
day, err := strconv.Atoi(file[6:8])
if err != nil {
panic(err)
}
hour, err := strconv.Atoi(file[8:10])
if err != nil {
panic(err)
}
min, err := strconv.Atoi(file[10:12])
if err != nil {
panic(err)
}
sec, err := strconv.Atoi(file[12:14])
if err != nil {
panic(err)
}
nsec, err := strconv.Atoi(file[14:23])
if err != nil {
panic(err)
}
loc, err := time.LoadLocation("Local")
if err != nil {
panic(err)
}
creationTime := time.Date(year, time.Month(month), day, hour, min, sec, nsec, loc)
fmt.Printf("%s=%s %d %d %d %d %d %d %d %s\n", firstKey, streamId, year, month, day, hour, min, sec, nsec, typeId)
index := &IndexEntry{streamUuid, creationTime, typeId}
err = appendIndex(me.globalIndexFilename, index)
if err != nil {
panic(err)
}
err = appendIndex(me.getStreamIndexFilename(streamUuid), index)
if err != nil {
panic(err)
}
err = me.appendTypeIndex(index)
if err != nil {
panic(err)
}
}
}
}

View File

@ -232,3 +232,7 @@ func getStoredData(eventsFile *os.File) (creationTime time.Time, typeId string,
func (me SimpleDiskStorage) RebuildTypeIndexes() { func (me SimpleDiskStorage) RebuildTypeIndexes() {
} }
func (me SimpleDiskStorage) RebuildIndexes() {
}

View File

@ -24,4 +24,5 @@ type Storage interface {
ReadAll() ([]*StoredEvent, error) ReadAll() ([]*StoredEvent, error)
StreamVersion(streamId uuid.UUID) (uint32, error) StreamVersion(streamId uuid.UUID) (uint32, error)
RebuildTypeIndexes() RebuildTypeIndexes()
RebuildIndexes()
} }