Skip to content

Instantly share code, notes, and snippets.

@lewisou
Created June 19, 2015 05:02
Show Gist options
  • Save lewisou/86baa9a731bf2bb9d5af to your computer and use it in GitHub Desktop.
Save lewisou/86baa9a731bf2bb9d5af to your computer and use it in GitHub Desktop.
Source code of the server side
// Search the record in the database by the file name
recordInDB, found := search(local.FilePath)
var compareRs int // The resule, which one to use, client side version or server side version
var execRs bool // If the operation is failed due to concurrency, so the client side can re do the request again
var newVersion int64 // Tell the client side the result of new version
// If the file previously exists
if found {
// The compare logic to find out who wins (client or server)
compareRs = (&recordInDB).CompareToLocal(&local)
// If the local version win, update the database, and return a new version.
if compareRs == common.COMPARE_LOCAL_WIN {
execRs, newVersion = update(&local, recordInDB.Version)
} else {
// If the server side win, return the server side version
execRs, newVersion = true, recordInDB.Version
}
} else {
// If the file not exists in the database, the local always wins, and the init version is default to zero
compareRs = common.COMPARE_LOCAL_WIN
execRs, newVersion = insert(&local), 0
}
// return three variables to the client side, so the client side program can decide what to do accordingly
return c.RenderJson(common.FileUpdateReturn {
CompareRs: compareRs,
ExecRs: execRs,
NewVersion: newVersion,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment