Refactor index rebuilding

This commit is contained in:
Nicolas Dextraze 2018-03-11 10:27:12 -07:00
parent 84e8e469ad
commit 819a3dcf1b

View File

@ -283,8 +283,51 @@ func (me DailyDiskStorage) RebuildTypeIndexes() {
fmt.Println("Done.")
}
func (me DailyDiskStorage) decomposeFilePath(filePath string) (time.Time, string, error) {
if filePath[0:len(me.storagePath)] == me.storagePath {
filePath = filePath[len(me.storagePath)+1:]
}
filePath = strings.Replace(filePath, "/", "", -1)
parts := strings.Split(filePath, "_")
typeId := parts[1]
year, err := strconv.Atoi(filePath[0:4])
if err != nil {
return time.Time{}, "", err
}
month, err := strconv.Atoi(filePath[4:6])
if err != nil {
return time.Time{}, "", err
}
day, err := strconv.Atoi(filePath[6:8])
if err != nil {
return time.Time{}, "", err
}
hour, err := strconv.Atoi(filePath[8:10])
if err != nil {
return time.Time{}, "", err
}
min, err := strconv.Atoi(filePath[10:12])
if err != nil {
return time.Time{}, "", err
}
sec, err := strconv.Atoi(filePath[12:14])
if err != nil {
return time.Time{}, "", err
}
nsec, err := strconv.Atoi(filePath[14:23])
if err != nil {
return time.Time{}, "", err
}
loc, err := time.LoadLocation("Local")
if err != nil {
return time.Time{}, "", err
}
creationTime := time.Date(year, time.Month(month), day, hour, min, sec, nsec, loc)
return creationTime, typeId, nil
}
func (me DailyDiskStorage) RebuildIndexes() {
fmt.Print("Rebuilding indexes...");
fmt.Print("Rebuilding indexes...")
err := os.RemoveAll(me.indexesPath)
if err != nil {
@ -312,6 +355,8 @@ func (me DailyDiskStorage) RebuildIndexes() {
} else if err := json.Unmarshal(parts[0], &event); err != nil {
panic(err)
} else {
//TODO this assume first key = primary key, should allow interactive mode to select pk for each event type
// {"key":"value",...}
firstKey := string(bytes.Split(parts[0], []byte("\""))[1])
streamId := event[firstKey].(string)
streamUuid, err := uuid.FromString(streamId)
@ -319,46 +364,11 @@ func (me DailyDiskStorage) RebuildIndexes() {
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])
creationTime, typeId, err := me.decomposeFilePath(file)
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)
fmt.Printf("%s=%s %s %s\n", firstKey, streamId, creationTime.String(), typeId)
index := &IndexEntry{streamUuid, creationTime, typeId}