func (vlog *valueLog) sync(fid uint32) error {
	if vlog.opt.SyncWrites {
		return nil
	}

	vlog.filesLock.RLock()
	maxFid := atomic.LoadUint32(&vlog.maxFid)
	// During replay it is possible to get sync call with fid less than maxFid.
	// Because older file has already been synced, we can return from here.
	if fid < maxFid || len(vlog.filesMap) == 0 {
		vlog.filesLock.RUnlock()
		return nil
	}
	curlf := vlog.filesMap[maxFid]
	// Sometimes it is possible that vlog.maxFid has been increased but file creation
	// with same id is still in progress and this function is called. In those cases
	// entry for the file might not be present in vlog.filesMap.
	if curlf == nil {
		vlog.filesLock.RUnlock()
		return nil
	}
	curlf.lock.RLock()
	vlog.filesLock.RUnlock()

	err := curlf.sync()
	curlf.lock.RUnlock()
	return err
}
func (vlog *valueLog) sync(fid uint32) error {
	if vlog.opt.SyncWrites {
		return nil
	}

	vlog.filesLock.RLock()
	maxFid := atomic.LoadUint32(&vlog.maxFid)
	
	
	if fid < maxFid || len(vlog.filesMap) == 0 {
		vlog.filesLock.RUnlock()
		return nil
	}
	curlf := vlog.filesMap[maxFid]
	
	
	
	if curlf == nil {
		vlog.filesLock.RUnlock()
		return nil
	}
	curlf.lock.RLock()
	vlog.filesLock.RUnlock()

	err := curlf.sync()
	curlf.lock.RUnlock()
	return err
}
func ( vlog * valueLog ) sync ( fid uint32 ) error { if vlog . opt . SyncWrites { return nil } vlog . filesLock . RLock ( ) maxFid := atomic . LoadUint32 ( & vlog . maxFid ) // During replay it is possible to get sync call with fid less than maxFid. // Because older file has already been synced, we can return from here. if fid < maxFid || len ( vlog . filesMap ) == 0 { vlog . filesLock . RUnlock ( ) return nil } curlf := vlog . filesMap [ maxFid ] // Sometimes it is possible that vlog.maxFid has been increased but file creation // with same id is still in progress and this function is called. In those cases // entry for the file might not be present in vlog.filesMap. if curlf == nil { vlog . filesLock . RUnlock ( ) return nil } curlf . lock . RLock ( ) vlog . filesLock . RUnlock ( ) err := curlf . sync ( ) curlf . lock . RUnlock ( ) return err }
// sync function syncs content of latest value log file to disk. Syncing of value log directory is
// not required here as it happens every time a value log file rotation happens(check createVlogFile
// function). During rotation, previous value log file also gets synced to disk. It only syncs file
// if fid >= vlog.maxFid. In some cases such as replay(while openning db), it might be called with
// fid < vlog.maxFid. To sync irrespective of file id just call it with math.MaxUint32.
sync function syncs content of latest value log file to disk . Syncing of value log directory is not required here as it happens every time a value log file rotation happens ( check createVlogFile function ) . During rotation previous value log file also gets synced to disk . It only syncs file if fid > = vlog . maxFid . In some cases such as replay ( while openning db ) it might be called with fid < vlog . maxFid . To sync irrespective of file id just call it with math . MaxUint32 .