code: func (c *dbCache) flush() error {
  	c.lastFlush = time.Now()
  
  	// Sync the current write file associated with the block store.  This is
  	// necessary before writing the metadata to prevent the case where the
  	// metadata contains information about a block which actually hasn't
  	// been written yet in unexpected shutdown scenarios.
  	if err := c.store.syncBlocks(); err != nil {
  		return err
  	}
  
  	// Since the cached keys to be added and removed use an immutable treap,
  	// a snapshot is simply obtaining the root of the tree under the lock
  	// which is used to atomically swap the root.
  	c.cacheLock.RLock()
  	cachedKeys := c.cachedKeys
  	cachedRemove := c.cachedRemove
  	c.cacheLock.RUnlock()
  
  	// Nothing to do if there is no data to flush.
  	if cachedKeys.Len() == 0 && cachedRemove.Len() == 0 {
  		return nil
  	}
  
  	// Perform all leveldb updates using an atomic transaction.
  	if err := c.commitTreaps(cachedKeys, cachedRemove); err != nil {
  		return err
  	}
  
  	// Clear the cache since it has been flushed.
  	c.cacheLock.Lock()
  	c.cachedKeys = treap.NewImmutable()
  	c.cachedRemove = treap.NewImmutable()
  	c.cacheLock.Unlock()
  
  	return nil
  }
docs: // flush flushes the database cache to persistent storage.  This involes syncing
  // the block store and replaying all transactions that have been applied to the
  // cache to the underlying database.
  //
  // This function MUST be called with the database write lock held.