code: func (e *Engine) WriteSnapshot() error {
  	// Lock and grab the cache snapshot along with all the closed WAL
  	// filenames associated with the snapshot
  
  	started := time.Now()
  
  	log, logEnd := logger.NewOperation(e.logger, "Cache snapshot", "tsm1_cache_snapshot")
  	defer func() {
  		elapsed := time.Since(started)
  		log.Info("Snapshot for path written",
  			zap.String("path", e.path),
  			zap.Duration("duration", elapsed))
  		logEnd()
  	}()
  
  	closedFiles, snapshot, err := func() (segments []string, snapshot *Cache, err error) {
  		e.mu.Lock()
  		defer e.mu.Unlock()
  
  		if err = e.WAL.CloseSegment(); err != nil {
  			return nil, nil, err
  		}
  
  		segments, err = e.WAL.ClosedSegments()
  		if err != nil {
  			return nil, nil, err
  		}
  
  		snapshot, err = e.Cache.Snapshot()
  		return segments, snapshot, err
  	}()
  
  	if err != nil {
  		return err
  	}
  
  	if snapshot.Size() == 0 {
  		e.Cache.ClearSnapshot(true)
  		return nil
  	}
  
  	// The snapshotted cache may have duplicate points and unsorted data.  We need to deduplicate
  	// it before writing the snapshot.  This can be very expensive so it's done while we are not
  	// holding the engine write lock.
  	dedup := time.Now()
  	snapshot.Deduplicate()
  	e.traceLogger.Info("Snapshot for path deduplicated",
  		zap.String("path", e.path),
  		zap.Duration("duration", time.Since(dedup)))
  
  	return e.writeSnapshotAndCommit(log, closedFiles, snapshot)
  }
docs: // WriteSnapshot will snapshot the cache and write a new TSM file with its contents, releasing the snapshot when done.