Commit 521888c4 authored by Christopher Schinnerl's avatar Christopher Schinnerl

change readlocks to write locks

parent e21f45f8
......@@ -45,7 +45,9 @@ type (
// Cached fields. These fields are cached fields and are only meant to be used
// to create FileInfos for file related API endpoints. There is no guarantee
// that these fields are up-to-date. Neither in memory nor on disk. Updates to
// these fields aren't persisted immediately.
// these fields aren't persisted immediately. Instead they will only be
// persisted whenever another method persists the metadata or when the SiaFile
// is closed.
//
// CachedRedundancy is the redundancy of the file on the network and is
// updated within the 'Redundancy' method which is periodically called by the
......
......@@ -362,11 +362,18 @@ func (sf *SiaFile) ErasureCode() modules.ErasureCoder {
return sf.staticMetadata.staticErasureCode
}
// Save saves the entire file to disk.
func (sf *SiaFile) Save() error {
sf.mu.Lock()
defer sf.mu.Unlock()
return sf.saveFile()
}
// UpdateExpiration updates CachedExpiration with the lowest height at which any
// of the file's contracts will expire.
func (sf *SiaFile) UpdateExpiration(contracts map[string]modules.RenterContract) {
sf.mu.RLock()
defer sf.mu.RUnlock()
sf.mu.Lock()
defer sf.mu.Unlock()
if len(sf.pubKeyTable) == 0 {
sf.staticMetadata.CachedExpiration = 0
return
......@@ -397,8 +404,8 @@ func (sf *SiaFile) Health(offline map[string]bool, goodForRenew map[string]bool)
minPieces := float64(sf.staticMetadata.staticErasureCode.MinPieces())
worstHealth := 1 - ((0 - minPieces) / (numPieces - minPieces))
sf.mu.RLock()
defer sf.mu.RUnlock()
sf.mu.Lock()
defer sf.mu.Unlock()
// Update the cache.
defer func() {
......@@ -603,8 +610,8 @@ func (sf *SiaFile) Pieces(chunkIndex uint64) ([][]Piece, error) {
// takes two arguments, a map of offline contracts for this file and a map that
// indicates if a contract is goodForRenew.
func (sf *SiaFile) Redundancy(offlineMap map[string]bool, goodForRenewMap map[string]bool) (r float64) {
sf.mu.RLock()
defer sf.mu.RUnlock()
sf.mu.Lock()
defer sf.mu.Unlock()
// Update the cache.
defer func() {
sf.staticMetadata.CachedRedundancy = r
......@@ -889,7 +896,7 @@ func (sf *SiaFile) updateUploadProgressAndBytes() {
sf.staticMetadata.CachedUploadProgress = 100
return
}
desired := sf.NumChunks() * modules.SectorSize * uint64(sf.ErasureCode().NumPieces())
desired := uint64(len(sf.staticChunks)) * modules.SectorSize * uint64(sf.staticMetadata.staticErasureCode.NumPieces())
// Update cache.
sf.staticMetadata.CachedUploadProgress = math.Min(100*(float64(uploaded)/float64(desired)), 100)
}
......
......@@ -152,11 +152,13 @@ func (sfs *SiaFileSet) closeEntry(entry *SiaFileSetEntry) {
return
}
// If there are no more threads that have the current entry open, delete
// this entry from the set cache.
// If there are no more threads that have the current entry open, delete this
// entry from the set cache and save the file to make sure all changes are
// persisted.
if len(currentEntry.threadMap) == 0 {
delete(sfs.siaFileMap, entry.Metadata().StaticUniqueID)
delete(sfs.siapathToUID, sfs.siaPath(entry.siaFileSetEntry))
_ = entry.Save() // TODO: add logging
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment