in nimo-shake/checkpoint/fileWriter.go [180:218]
func (fw *FileWriter) Update(shardId string, ckpt *Checkpoint, table string) error {
fw.lockFile(table)
defer fw.unlockFile(table)
file := fmt.Sprintf("%s/%s", fw.dir, table)
jsonFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
defer jsonFile.Close()
data, err := fw.readJsonList(jsonFile)
if err != nil {
return err
}
if len(data) == 0 {
return fmt.Errorf("empty data")
}
match := false
for i := range data {
if data[i].ShardId == shardId {
match = true
data[i] = ckpt
break
}
}
if !match {
return fmt.Errorf("shardId[%v] not exists", shardId)
}
// truncate file
jsonFile.Truncate(0)
jsonFile.Seek(0, 0)
// write
return fw.writeJsonList(jsonFile, data)
}