github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/lib/objectserver/filesystem/new.go (about)

     1  package filesystem
     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, logger log.Logger) (
    14  	*ObjectServer, error) {
    15  	startTime := time.Now()
    16  	var rusageStart, rusageStop syscall.Rusage
    17  	syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart)
    18  	sizesMap := make(map[hash.Hash]uint64)
    19  	var mutex sync.Mutex
    20  	err := scan.ScanTree(baseDir, func(hashVal hash.Hash, size uint64) {
    21  		mutex.Lock()
    22  		sizesMap[hashVal] = size
    23  		mutex.Unlock()
    24  	})
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	plural := ""
    29  	if len(sizesMap) != 1 {
    30  		plural = "s"
    31  	}
    32  	syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop)
    33  	userTime := time.Duration(rusageStop.Utime.Sec)*time.Second +
    34  		time.Duration(rusageStop.Utime.Usec)*time.Microsecond -
    35  		time.Duration(rusageStart.Utime.Sec)*time.Second -
    36  		time.Duration(rusageStart.Utime.Usec)*time.Microsecond
    37  	logger.Printf("Scanned %d object%s in %s (%s user CPUtime)\n",
    38  		len(sizesMap), plural, time.Since(startTime), userTime)
    39  	return &ObjectServer{
    40  		baseDir:               baseDir,
    41  		logger:                logger,
    42  		sizesMap:              sizesMap,
    43  		lastGarbageCollection: time.Now(),
    44  		lastMutationTime:      time.Now(),
    45  	}, nil
    46  }