github.com/Cloud-Foundations/Dominator@v0.3.4/lib/objectserver/cachingreader/new.go (about)

     1  package cachingreader
     2  
     3  import (
     4  	"sync"
     5  	"syscall"
     6  	"time"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/hash"
     9  	"github.com/Cloud-Foundations/Dominator/lib/log"
    10  	"github.com/Cloud-Foundations/Dominator/lib/objectserver/filesystem/scan"
    11  )
    12  
    13  func newObjectServer(baseDir string, maxCachedBytes uint64,
    14  	objectServerAddress string, logger log.DebugLogger) (*ObjectServer, error) {
    15  	startTime := time.Now()
    16  	var rusageStart, rusageStop syscall.Rusage
    17  	syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart)
    18  	var mutex sync.Mutex
    19  	objects := make(map[hash.Hash]*objectType)
    20  	var cachedBytes uint64
    21  	err := scan.ScanTree(baseDir, func(hashVal hash.Hash, size uint64) {
    22  		mutex.Lock()
    23  		cachedBytes += size
    24  		objects[hashVal] = &objectType{hash: hashVal, size: size}
    25  		mutex.Unlock()
    26  	})
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	plural := ""
    31  	if len(objects) != 1 {
    32  		plural = "s"
    33  	}
    34  	syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop)
    35  	userTime := time.Duration(rusageStop.Utime.Sec)*time.Second +
    36  		time.Duration(rusageStop.Utime.Usec)*time.Microsecond -
    37  		time.Duration(rusageStart.Utime.Sec)*time.Second -
    38  		time.Duration(rusageStart.Utime.Usec)*time.Microsecond
    39  	logger.Printf("Scanned %d object%s in %s (%s user CPUtime)\n",
    40  		len(objects), plural, time.Since(startTime), userTime)
    41  	lruUpdateNotifier := make(chan struct{}, 1)
    42  	objSrv := &ObjectServer{
    43  		baseDir:             baseDir,
    44  		flushTimer:          time.NewTimer(time.Minute),
    45  		logger:              logger,
    46  		lruUpdateNotifier:   lruUpdateNotifier,
    47  		maxCachedBytes:      maxCachedBytes,
    48  		objectServerAddress: objectServerAddress,
    49  		data:                Stats{CachedBytes: cachedBytes},
    50  		objects:             objects,
    51  	}
    52  	objSrv.flushTimer.Stop()
    53  	if err := objSrv.loadLru(); err != nil {
    54  		return nil, err
    55  	}
    56  	objSrv.linkOrphanedEntries()
    57  	go objSrv.flusher(lruUpdateNotifier)
    58  	return objSrv, nil
    59  }