func (fs *FileSystem) flusher() {
	fs.Lock()
	defer fs.Unlock()
	start := time.Now()
	count, err := fs.doFlush()
	end := time.Since(start)
	if err != nil {
		log.Errorf("flusher: %v", err)
	}
	log.Infof("Flusher: directories %v in %v", count, end)
}
flusher is called periodically to flush the current timestamp to disk.
package filesystem
import (
	"bytes"
	"crypto/sha256"
	"encoding/binary"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"sync"
	"time"
	"github.com/decred/dcrtime/dcrtimed/backend"
	"github.com/decred/dcrtime/dcrtimed/dcrtimewallet"
	"github.com/decred/dcrtime/merkle"
	"github.com/robfig/cron"
	"github.com/syndtr/goleveldb/leveldb"
	"github.com/syndtr/goleveldb/leveldb/opt"
)
const (
	fStr          = "20060102.150405"
	globalDBDir   = "global"
	flushedKey    = "flushed"
	confirmations = 6
	foundGlobal = 1000
	foundLocal = 1001
	foundPrevious = 1002
)
var (
	_ backend.Backend = (*FileSystem)(nil)
	flushSchedule = "10 0 * * * *"  
	duration      = time.Hour       
	errInvalidDB      = errors.New("not a database")  
	errAlreadyFlushed = errors.New("already flushed")
	errEmptySet       = errors.New("empty set")
)
type FileSystem struct {
	sync.RWMutex
	cron     *cron.Cron     
	root     string         
	db       *leveldb.DB    
	duration time.Duration  
	commit   uint           
	enableCollections bool  
	wallet *dcrtimewallet.DcrtimeWallet  
	myNow   func() time.Time  
	testing bool              
}
func ts2dirname(ts int64) string {
	return time.Unix(ts, 0).UTC().Format(fStr)
}
func EncodeFlushRecord(fr backend.FlushRecord) ([]byte, error) {
	b, err := json.Marshal(fr)
	if err != nil {
		return nil, err
	}
	return b, nil
}
func DecodeFlushRecord(payload []byte) (*backend.FlushRecord, error) {
	var fr backend.FlushRecord
	err := json.Unmarshal(payload, &fr)
	if err != nil {
		return nil, err
	}
	return &fr, nil
}
func (fs *FileSystem) now() time.Time {
	return fs.truncate(fs.myNow().UTC(), fs.duration)
}
func (fs *FileSystem) truncate(t time.Time, d time.Duration) time.Time {
	return t.Truncate(d)
}
func (fs *FileSystem) openRead(ts int64) (*leveldb.DB, error) {
	path := filepath.Join(fs.root, ts2dirname(ts))
	fi, err := os.Stat(path)
	if err != nil {
		return nil, os.ErrNotExist
	}
	if !fi.Mode().IsDir() {
		return nil, errInvalidDB
	}
	db, err := leveldb.OpenFile(path, &opt.Options{
		ErrorIfMissing: true,
	})
	if err != nil {
		return nil, err
	}
	return db, nil
}
func (fs *FileSystem) openWrite(ts int64, create bool) (*leveldb.DB, error) {
	path := filepath.Join(fs.root, ts2dirname(ts))
	err := os.MkdirAll(path, 0700)
	if err != nil {
		return nil, err
	}
	db, err := leveldb.OpenFile(path, &opt.Options{
		ErrorIfMissing: !create,
	})
	if err != nil {
		return nil, err
	}
	return db, nil
}
func isFlushed(db *leveldb.DB) bool {
	found, _ := db.Has([]byte(flushedKey), nil)
	return found
}
func (fs *FileSystem) isFlushed(ts int64) bool {
	db, err := fs.openRead(ts)
	if err != nil {
		return false
	}
	defer db.Close()
	return isFlushed(db)
}
func (fs *FileSystem) flush(ts int64) error {
	db, err := fs.openWrite(ts, false)
	if err != nil {
		return err
	}
	defer db.Close()
	if isFlushed(db) {
		return errAlreadyFlushed
	}
	hashes := make([]*[sha256.Size]byte, 0, 4096)
	timestamp := make([]byte, 8)
	binary.LittleEndian.PutUint64(timestamp, uint64(ts))
	files := 0
	batch := new(leveldb.Batch)
	iter := db.NewIterator(nil, nil)
	for iter.Next() {
		var digest [sha256.Size]byte
		hash := iter.Key()
		batch.Put(hash, timestamp)
		copy(digest[:], hash)
		hashes = append(hashes, &digest)
		files++
	}
	iter.Release()
	err = iter.Error()
	if err != nil {
		return err
	}
	if len(hashes) == 0 {
		return errEmptySet
	}
	mt := merkle.Tree(hashes)
	root := *mt[len(mt)-1]  
	fr := backend.FlushRecord{
		Root:           root,
		Hashes:         mt[:len(hashes)],  
		FlushTimestamp: time.Now().Unix(),
	}
	if !fs.testing {
		tx, err := fs.wallet.Construct(root)
		if err != nil {
			return fmt.Errorf("flush Construct tx: %v", err)
		}
		log.Infof("Flush timestamp: %v digests %v merkle: %x tx: %v",
			ts2dirname(ts), files, root, tx.String())
		fr.Tx = *tx
	}
	payload, err := EncodeFlushRecord(fr)
	if err != nil {
		return err
	}
	err = fs.db.Write(batch, nil)
	if err != nil {
		return err
	}
	err = db.Put([]byte(flushedKey), payload, nil)
	if err != nil {
		return err
	}
	fs.commit++
	return nil
}
func (fs *FileSystem) doFlush() (int, error) {
	now := fs.now().Format(fStr)
	files, err := ioutil.ReadDir(fs.root)
	if err != nil {
		return 0, err
	}
	dirs := make([]string, 0, len(files))
	for _, file := range files {
		if file.Name() == globalDBDir {
			continue
		}
		if !file.IsDir() {
			continue
		}
		if file.Name() == now {
			continue
		}
		dirs = append(dirs, file.Name())
	}
	sort.Sort(sort.Reverse(sort.StringSlice(dirs)))
	count := 0
	for _, dir := range dirs {
		timestamp, err := time.Parse(fStr, dir)
		if err != nil {
			continue
		}
		ts := timestamp.Unix()
		if fs.isFlushed(ts) {
			break
		}
		err = fs.flush(ts)
		if err != nil {
			e := fmt.Sprintf("flush %v: %v", ts2dirname(ts), err)
			if fs.testing {
				panic(e)
			}
			log.Error(e)
		} else {
			count++
		}
	}
	return count, nil
}