code: func (fs *FileSorter) flushToFile() error {
  	var (
  		err        error
  		outputFile *os.File
  		outputByte []byte
  	)
  
  	sort.Sort(fs)
  	if fs.err != nil {
  		return errors.Trace(fs.err)
  	}
  
  	fileName := fs.getUniqueFileName()
  
  	outputFile, err = os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  	if err != nil {
  		return errors.Trace(err)
  	}
  	defer outputFile.Close()
  
  	for _, row := range fs.buf {
  		var body []byte
  		var head = make([]byte, 8)
  
  		body, err = codec.EncodeKey(body, row.key...)
  		if err != nil {
  			return errors.Trace(err)
  		}
  		body, err = codec.EncodeKey(body, row.val...)
  		if err != nil {
  			return errors.Trace(err)
  		}
  		body, err = codec.EncodeKey(body, types.NewIntDatum(row.handle))
  		if err != nil {
  			return errors.Trace(err)
  		}
  
  		binary.BigEndian.PutUint64(head, uint64(len(body)))
  
  		outputByte = append(outputByte, head...)
  		outputByte = append(outputByte, body...)
  	}
  
  	_, err = outputFile.Write(outputByte)
  	if err != nil {
  		return errors.Trace(err)
  	}
  
  	fs.files = append(fs.files, fileName)
  	fs.buf = fs.buf[:0]
  	return nil
  }
docs: // Flush the buffer to file if it is full.