Merge pull request #1130 from krishnasrinivas/session-datafile-optimization

Avoid syncing session datafile when there is no change to the datafile
This commit is contained in:
Harshavardhana
2015-09-25 20:42:48 -07:00
3 changed files with 26 additions and 9 deletions
+2 -1
View File
@@ -254,13 +254,14 @@ func doCopyCmdSession(session *sessionV2) {
}
if cpURLs.Error == nil {
session.Header.LastCopied = cpURLs.SourceContent.Name
session.Save()
} else {
// Print in new line and adjust to top so that we don't print over the ongoing progress bar
if !globalQuietFlag && !globalJSONFlag {
console.Printf("%c[2K\n", 27)
console.Printf("%c[A", 27)
}
errorIf(cpURLs.Error.Trace(), fmt.Sprintf("Failed to copy ‘%s’.", cpURLs.SourceContent.Name))
fatalIf(cpURLs.Error.Trace(), fmt.Sprintf("Failed to copy ‘%s’.", cpURLs.SourceContent.Name))
}
case <-trapCh: // Receive interrupt notification.
// Print in new line and adjust to top so that we don't print over the ongoing progress bar
+2 -1
View File
@@ -252,13 +252,14 @@ func doMirrorCmdSession(session *sessionV2) {
}
if sURLs.Error == nil {
session.Header.LastCopied = sURLs.SourceContent.Name
session.Save()
} else {
// Print in new line and adjust to top so that we don't print over the ongoing progress bar
if !globalQuietFlag && !globalJSONFlag {
console.Printf("%c[2K\n", 27)
console.Printf("%c[A", 27)
}
errorIf(sURLs.Error.Trace(), "Failed to mirror.")
fatalIf(sURLs.Error.Trace(), "Failed to mirror.")
}
case <-trapCh: // Receive interrupt notification.
// Print in new line and adjust to top so that we don't print over the ongoing progress bar
+22 -7
View File
@@ -69,10 +69,20 @@ type sessionV2 struct {
Header *sessionV2Header
SessionID string
mutex *sync.Mutex
DataFP *os.File
DataFP *sessionDataFP
sigCh bool
}
type sessionDataFP struct {
dirty bool
*os.File
}
func (file *sessionDataFP) Write(p []byte) (int, error) {
file.dirty = true
return file.File.Write(p)
}
// String colorized session message
func (s sessionV2) String() string {
message := console.Colorize("SessionID", fmt.Sprintf("%s -> ", s.SessionID))
@@ -109,10 +119,10 @@ func newSessionV2() *sessionV2 {
sessionDataFile, perr := getSessionDataFile(s.SessionID)
fatalIf(perr.Trace(s.SessionID), "Unable to create session data file \""+sessionDataFile+"\".")
var err error
s.DataFP, err = os.Create(sessionDataFile)
dataFile, err := os.Create(sessionDataFile)
fatalIf(probe.NewError(err), "Unable to create session data file \""+sessionDataFile+"\".")
s.DataFP = &sessionDataFP{false, dataFile}
return s
}
@@ -148,8 +158,11 @@ func (s *sessionV2) Save() *probe.Error {
s.mutex.Lock()
defer s.mutex.Unlock()
if err := s.DataFP.Sync(); err != nil {
return probe.NewError(err)
if s.DataFP.dirty {
if err := s.DataFP.Sync(); err != nil {
return probe.NewError(err)
}
s.DataFP.dirty = false
}
qs, err := quick.New(s.Header)
@@ -251,8 +264,10 @@ func loadSessionV2(sid string) (*sessionV2, *probe.Error) {
}
var e error
s.DataFP, e = os.Open(sessionDataFile)
fatalIf(probe.NewError(e), "Unable to open session data file ‘"+sessionDataFile+"’.")
dataFile, e := os.Open(sessionDataFile)
fatalIf(probe.NewError(e), "Unable to open session data file \""+sessionDataFile+"\".")
s.DataFP = &sessionDataFP{false, dataFile}
return s, nil
}