github.com/gilgames000/kcc-geth@v1.0.6/core/rawdb/freezer_table.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rawdb
    18  
    19  import (
    20  	"encoding/binary"
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  	"path/filepath"
    26  	"sync"
    27  	"sync/atomic"
    28  
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/log"
    31  	"github.com/ethereum/go-ethereum/metrics"
    32  	"github.com/golang/snappy"
    33  )
    34  
    35  var (
    36  	// errClosed is returned if an operation attempts to read from or write to the
    37  	// freezer table after it has already been closed.
    38  	errClosed = errors.New("closed")
    39  
    40  	// errOutOfBounds is returned if the item requested is not contained within the
    41  	// freezer table.
    42  	errOutOfBounds = errors.New("out of bounds")
    43  
    44  	// errNotSupported is returned if the database doesn't support the required operation.
    45  	errNotSupported = errors.New("this operation is not supported")
    46  )
    47  
    48  // indexEntry contains the number/id of the file that the data resides in, aswell as the
    49  // offset within the file to the end of the data
    50  // In serialized form, the filenum is stored as uint16.
    51  type indexEntry struct {
    52  	filenum uint32 // stored as uint16 ( 2 bytes)
    53  	offset  uint32 // stored as uint32 ( 4 bytes)
    54  }
    55  
    56  const indexEntrySize = 6
    57  
    58  // unmarshallBinary deserializes binary b into the rawIndex entry.
    59  func (i *indexEntry) unmarshalBinary(b []byte) error {
    60  	i.filenum = uint32(binary.BigEndian.Uint16(b[:2]))
    61  	i.offset = binary.BigEndian.Uint32(b[2:6])
    62  	return nil
    63  }
    64  
    65  // marshallBinary serializes the rawIndex entry into binary.
    66  func (i *indexEntry) marshallBinary() []byte {
    67  	b := make([]byte, indexEntrySize)
    68  	binary.BigEndian.PutUint16(b[:2], uint16(i.filenum))
    69  	binary.BigEndian.PutUint32(b[2:6], i.offset)
    70  	return b
    71  }
    72  
    73  // freezerTable represents a single chained data table within the freezer (e.g. blocks).
    74  // It consists of a data file (snappy encoded arbitrary data blobs) and an indexEntry
    75  // file (uncompressed 64 bit indices into the data file).
    76  type freezerTable struct {
    77  	// WARNING: The `items` field is accessed atomically. On 32 bit platforms, only
    78  	// 64-bit aligned fields can be atomic. The struct is guaranteed to be so aligned,
    79  	// so take advantage of that (https://golang.org/pkg/sync/atomic/#pkg-note-BUG).
    80  	items uint64 // Number of items stored in the table (including items removed from tail)
    81  
    82  	noCompression bool   // if true, disables snappy compression. Note: does not work retroactively
    83  	maxFileSize   uint32 // Max file size for data-files
    84  	name          string
    85  	path          string
    86  
    87  	head   *os.File            // File descriptor for the data head of the table
    88  	files  map[uint32]*os.File // open files
    89  	headId uint32              // number of the currently active head file
    90  	tailId uint32              // number of the earliest file
    91  	index  *os.File            // File descriptor for the indexEntry file of the table
    92  
    93  	// In the case that old items are deleted (from the tail), we use itemOffset
    94  	// to count how many historic items have gone missing.
    95  	itemOffset uint32 // Offset (number of discarded items)
    96  
    97  	headBytes  uint32        // Number of bytes written to the head file
    98  	readMeter  metrics.Meter // Meter for measuring the effective amount of data read
    99  	writeMeter metrics.Meter // Meter for measuring the effective amount of data written
   100  	sizeGauge  metrics.Gauge // Gauge for tracking the combined size of all freezer tables
   101  
   102  	logger log.Logger   // Logger with database path and table name ambedded
   103  	lock   sync.RWMutex // Mutex protecting the data file descriptors
   104  }
   105  
   106  // NewFreezerTable opens the given path as a freezer table.
   107  func NewFreezerTable(path, name string, disableSnappy bool) (*freezerTable, error) {
   108  	return newTable(path, name, metrics.NilMeter{}, metrics.NilMeter{}, metrics.NilGauge{}, disableSnappy)
   109  }
   110  
   111  // newTable opens a freezer table with default settings - 2G files
   112  func newTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, sizeGauge metrics.Gauge, disableSnappy bool) (*freezerTable, error) {
   113  	return newCustomTable(path, name, readMeter, writeMeter, sizeGauge, 2*1000*1000*1000, disableSnappy)
   114  }
   115  
   116  // openFreezerFileForAppend opens a freezer table file and seeks to the end
   117  func openFreezerFileForAppend(filename string) (*os.File, error) {
   118  	// Open the file without the O_APPEND flag
   119  	// because it has differing behaviour during Truncate operations
   120  	// on different OS's
   121  	file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	// Seek to end for append
   126  	if _, err = file.Seek(0, io.SeekEnd); err != nil {
   127  		return nil, err
   128  	}
   129  	return file, nil
   130  }
   131  
   132  // openFreezerFileForReadOnly opens a freezer table file for read only access
   133  func openFreezerFileForReadOnly(filename string) (*os.File, error) {
   134  	return os.OpenFile(filename, os.O_RDONLY, 0644)
   135  }
   136  
   137  // openFreezerFileTruncated opens a freezer table making sure it is truncated
   138  func openFreezerFileTruncated(filename string) (*os.File, error) {
   139  	return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
   140  }
   141  
   142  // truncateFreezerFile resizes a freezer table file and seeks to the end
   143  func truncateFreezerFile(file *os.File, size int64) error {
   144  	if err := file.Truncate(size); err != nil {
   145  		return err
   146  	}
   147  	// Seek to end for append
   148  	if _, err := file.Seek(0, io.SeekEnd); err != nil {
   149  		return err
   150  	}
   151  	return nil
   152  }
   153  
   154  // newCustomTable opens a freezer table, creating the data and index files if they are
   155  // non existent. Both files are truncated to the shortest common length to ensure
   156  // they don't go out of sync.
   157  func newCustomTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, sizeGauge metrics.Gauge, maxFilesize uint32, noCompression bool) (*freezerTable, error) {
   158  	// Ensure the containing directory exists and open the indexEntry file
   159  	if err := os.MkdirAll(path, 0755); err != nil {
   160  		return nil, err
   161  	}
   162  	var idxName string
   163  	if noCompression {
   164  		// Raw idx
   165  		idxName = fmt.Sprintf("%s.ridx", name)
   166  	} else {
   167  		// Compressed idx
   168  		idxName = fmt.Sprintf("%s.cidx", name)
   169  	}
   170  	offsets, err := openFreezerFileForAppend(filepath.Join(path, idxName))
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	// Create the table and repair any past inconsistency
   175  	tab := &freezerTable{
   176  		index:         offsets,
   177  		files:         make(map[uint32]*os.File),
   178  		readMeter:     readMeter,
   179  		writeMeter:    writeMeter,
   180  		sizeGauge:     sizeGauge,
   181  		name:          name,
   182  		path:          path,
   183  		logger:        log.New("database", path, "table", name),
   184  		noCompression: noCompression,
   185  		maxFileSize:   maxFilesize,
   186  	}
   187  	if err := tab.repair(); err != nil {
   188  		tab.Close()
   189  		return nil, err
   190  	}
   191  	// Initialize the starting size counter
   192  	size, err := tab.sizeNolock()
   193  	if err != nil {
   194  		tab.Close()
   195  		return nil, err
   196  	}
   197  	tab.sizeGauge.Inc(int64(size))
   198  
   199  	return tab, nil
   200  }
   201  
   202  // repair cross checks the head and the index file and truncates them to
   203  // be in sync with each other after a potential crash / data loss.
   204  func (t *freezerTable) repair() error {
   205  	// Create a temporary offset buffer to init files with and read indexEntry into
   206  	buffer := make([]byte, indexEntrySize)
   207  
   208  	// If we've just created the files, initialize the index with the 0 indexEntry
   209  	stat, err := t.index.Stat()
   210  	if err != nil {
   211  		return err
   212  	}
   213  	if stat.Size() == 0 {
   214  		if _, err := t.index.Write(buffer); err != nil {
   215  			return err
   216  		}
   217  	}
   218  	// Ensure the index is a multiple of indexEntrySize bytes
   219  	if overflow := stat.Size() % indexEntrySize; overflow != 0 {
   220  		truncateFreezerFile(t.index, stat.Size()-overflow) // New file can't trigger this path
   221  	}
   222  	// Retrieve the file sizes and prepare for truncation
   223  	if stat, err = t.index.Stat(); err != nil {
   224  		return err
   225  	}
   226  	offsetsSize := stat.Size()
   227  
   228  	// Open the head file
   229  	var (
   230  		firstIndex  indexEntry
   231  		lastIndex   indexEntry
   232  		contentSize int64
   233  		contentExp  int64
   234  	)
   235  	// Read index zero, determine what file is the earliest
   236  	// and what item offset to use
   237  	t.index.ReadAt(buffer, 0)
   238  	firstIndex.unmarshalBinary(buffer)
   239  
   240  	t.tailId = firstIndex.filenum
   241  	t.itemOffset = firstIndex.offset
   242  
   243  	t.index.ReadAt(buffer, offsetsSize-indexEntrySize)
   244  	lastIndex.unmarshalBinary(buffer)
   245  	t.head, err = t.openFile(lastIndex.filenum, openFreezerFileForAppend)
   246  	if err != nil {
   247  		return err
   248  	}
   249  	if stat, err = t.head.Stat(); err != nil {
   250  		return err
   251  	}
   252  	contentSize = stat.Size()
   253  
   254  	// Keep truncating both files until they come in sync
   255  	contentExp = int64(lastIndex.offset)
   256  
   257  	for contentExp != contentSize {
   258  		// Truncate the head file to the last offset pointer
   259  		if contentExp < contentSize {
   260  			t.logger.Warn("Truncating dangling head", "indexed", common.StorageSize(contentExp), "stored", common.StorageSize(contentSize))
   261  			if err := truncateFreezerFile(t.head, contentExp); err != nil {
   262  				return err
   263  			}
   264  			contentSize = contentExp
   265  		}
   266  		// Truncate the index to point within the head file
   267  		if contentExp > contentSize {
   268  			t.logger.Warn("Truncating dangling indexes", "indexed", common.StorageSize(contentExp), "stored", common.StorageSize(contentSize))
   269  			if err := truncateFreezerFile(t.index, offsetsSize-indexEntrySize); err != nil {
   270  				return err
   271  			}
   272  			offsetsSize -= indexEntrySize
   273  			t.index.ReadAt(buffer, offsetsSize-indexEntrySize)
   274  			var newLastIndex indexEntry
   275  			newLastIndex.unmarshalBinary(buffer)
   276  			// We might have slipped back into an earlier head-file here
   277  			if newLastIndex.filenum != lastIndex.filenum {
   278  				// Release earlier opened file
   279  				t.releaseFile(lastIndex.filenum)
   280  				if t.head, err = t.openFile(newLastIndex.filenum, openFreezerFileForAppend); err != nil {
   281  					return err
   282  				}
   283  				if stat, err = t.head.Stat(); err != nil {
   284  					// TODO, anything more we can do here?
   285  					// A data file has gone missing...
   286  					return err
   287  				}
   288  				contentSize = stat.Size()
   289  			}
   290  			lastIndex = newLastIndex
   291  			contentExp = int64(lastIndex.offset)
   292  		}
   293  	}
   294  	// Ensure all reparation changes have been written to disk
   295  	if err := t.index.Sync(); err != nil {
   296  		return err
   297  	}
   298  	if err := t.head.Sync(); err != nil {
   299  		return err
   300  	}
   301  	// Update the item and byte counters and return
   302  	t.items = uint64(t.itemOffset) + uint64(offsetsSize/indexEntrySize-1) // last indexEntry points to the end of the data file
   303  	t.headBytes = uint32(contentSize)
   304  	t.headId = lastIndex.filenum
   305  
   306  	// Close opened files and preopen all files
   307  	if err := t.preopen(); err != nil {
   308  		return err
   309  	}
   310  	t.logger.Debug("Chain freezer table opened", "items", t.items, "size", common.StorageSize(t.headBytes))
   311  	return nil
   312  }
   313  
   314  // preopen opens all files that the freezer will need. This method should be called from an init-context,
   315  // since it assumes that it doesn't have to bother with locking
   316  // The rationale for doing preopen is to not have to do it from within Retrieve, thus not needing to ever
   317  // obtain a write-lock within Retrieve.
   318  func (t *freezerTable) preopen() (err error) {
   319  	// The repair might have already opened (some) files
   320  	t.releaseFilesAfter(0, false)
   321  	// Open all except head in RDONLY
   322  	for i := t.tailId; i < t.headId; i++ {
   323  		if _, err = t.openFile(i, openFreezerFileForReadOnly); err != nil {
   324  			return err
   325  		}
   326  	}
   327  	// Open head in read/write
   328  	t.head, err = t.openFile(t.headId, openFreezerFileForAppend)
   329  	return err
   330  }
   331  
   332  // truncate discards any recent data above the provided threshold number.
   333  func (t *freezerTable) truncate(items uint64) error {
   334  	t.lock.Lock()
   335  	defer t.lock.Unlock()
   336  
   337  	// If our item count is correct, don't do anything
   338  	existing := atomic.LoadUint64(&t.items)
   339  	if existing <= items {
   340  		return nil
   341  	}
   342  	// We need to truncate, save the old size for metrics tracking
   343  	oldSize, err := t.sizeNolock()
   344  	if err != nil {
   345  		return err
   346  	}
   347  	// Something's out of sync, truncate the table's offset index
   348  	log := t.logger.Debug
   349  	if existing > items+1 {
   350  		log = t.logger.Warn // Only loud warn if we delete multiple items
   351  	}
   352  	log("Truncating freezer table", "items", existing, "limit", items)
   353  	if err := truncateFreezerFile(t.index, int64(items+1)*indexEntrySize); err != nil {
   354  		return err
   355  	}
   356  	// Calculate the new expected size of the data file and truncate it
   357  	buffer := make([]byte, indexEntrySize)
   358  	if _, err := t.index.ReadAt(buffer, int64(items*indexEntrySize)); err != nil {
   359  		return err
   360  	}
   361  	var expected indexEntry
   362  	expected.unmarshalBinary(buffer)
   363  
   364  	// We might need to truncate back to older files
   365  	if expected.filenum != t.headId {
   366  		// If already open for reading, force-reopen for writing
   367  		t.releaseFile(expected.filenum)
   368  		newHead, err := t.openFile(expected.filenum, openFreezerFileForAppend)
   369  		if err != nil {
   370  			return err
   371  		}
   372  		// Release any files _after the current head -- both the previous head
   373  		// and any files which may have been opened for reading
   374  		t.releaseFilesAfter(expected.filenum, true)
   375  		// Set back the historic head
   376  		t.head = newHead
   377  		atomic.StoreUint32(&t.headId, expected.filenum)
   378  	}
   379  	if err := truncateFreezerFile(t.head, int64(expected.offset)); err != nil {
   380  		return err
   381  	}
   382  	// All data files truncated, set internal counters and return
   383  	atomic.StoreUint64(&t.items, items)
   384  	atomic.StoreUint32(&t.headBytes, expected.offset)
   385  
   386  	// Retrieve the new size and update the total size counter
   387  	newSize, err := t.sizeNolock()
   388  	if err != nil {
   389  		return err
   390  	}
   391  	t.sizeGauge.Dec(int64(oldSize - newSize))
   392  
   393  	return nil
   394  }
   395  
   396  // Close closes all opened files.
   397  func (t *freezerTable) Close() error {
   398  	t.lock.Lock()
   399  	defer t.lock.Unlock()
   400  
   401  	var errs []error
   402  	if err := t.index.Close(); err != nil {
   403  		errs = append(errs, err)
   404  	}
   405  	t.index = nil
   406  
   407  	for _, f := range t.files {
   408  		if err := f.Close(); err != nil {
   409  			errs = append(errs, err)
   410  		}
   411  	}
   412  	t.head = nil
   413  
   414  	if errs != nil {
   415  		return fmt.Errorf("%v", errs)
   416  	}
   417  	return nil
   418  }
   419  
   420  // openFile assumes that the write-lock is held by the caller
   421  func (t *freezerTable) openFile(num uint32, opener func(string) (*os.File, error)) (f *os.File, err error) {
   422  	var exist bool
   423  	if f, exist = t.files[num]; !exist {
   424  		var name string
   425  		if t.noCompression {
   426  			name = fmt.Sprintf("%s.%04d.rdat", t.name, num)
   427  		} else {
   428  			name = fmt.Sprintf("%s.%04d.cdat", t.name, num)
   429  		}
   430  		f, err = opener(filepath.Join(t.path, name))
   431  		if err != nil {
   432  			return nil, err
   433  		}
   434  		t.files[num] = f
   435  	}
   436  	return f, err
   437  }
   438  
   439  // releaseFile closes a file, and removes it from the open file cache.
   440  // Assumes that the caller holds the write lock
   441  func (t *freezerTable) releaseFile(num uint32) {
   442  	if f, exist := t.files[num]; exist {
   443  		delete(t.files, num)
   444  		f.Close()
   445  	}
   446  }
   447  
   448  // releaseFilesAfter closes all open files with a higher number, and optionally also deletes the files
   449  func (t *freezerTable) releaseFilesAfter(num uint32, remove bool) {
   450  	for fnum, f := range t.files {
   451  		if fnum > num {
   452  			delete(t.files, fnum)
   453  			f.Close()
   454  			if remove {
   455  				os.Remove(f.Name())
   456  			}
   457  		}
   458  	}
   459  }
   460  
   461  // Append injects a binary blob at the end of the freezer table. The item number
   462  // is a precautionary parameter to ensure data correctness, but the table will
   463  // reject already existing data.
   464  //
   465  // Note, this method will *not* flush any data to disk so be sure to explicitly
   466  // fsync before irreversibly deleting data from the database.
   467  func (t *freezerTable) Append(item uint64, blob []byte) error {
   468  	// Read lock prevents competition with truncate
   469  	t.lock.RLock()
   470  	// Ensure the table is still accessible
   471  	if t.index == nil || t.head == nil {
   472  		t.lock.RUnlock()
   473  		return errClosed
   474  	}
   475  	// Ensure only the next item can be written, nothing else
   476  	if atomic.LoadUint64(&t.items) != item {
   477  		t.lock.RUnlock()
   478  		return fmt.Errorf("appending unexpected item: want %d, have %d", t.items, item)
   479  	}
   480  	// Encode the blob and write it into the data file
   481  	if !t.noCompression {
   482  		blob = snappy.Encode(nil, blob)
   483  	}
   484  	bLen := uint32(len(blob))
   485  	if t.headBytes+bLen < bLen ||
   486  		t.headBytes+bLen > t.maxFileSize {
   487  		// we need a new file, writing would overflow
   488  		t.lock.RUnlock()
   489  		t.lock.Lock()
   490  		nextID := atomic.LoadUint32(&t.headId) + 1
   491  		// We open the next file in truncated mode -- if this file already
   492  		// exists, we need to start over from scratch on it
   493  		newHead, err := t.openFile(nextID, openFreezerFileTruncated)
   494  		if err != nil {
   495  			t.lock.Unlock()
   496  			return err
   497  		}
   498  		// Close old file, and reopen in RDONLY mode
   499  		t.releaseFile(t.headId)
   500  		t.openFile(t.headId, openFreezerFileForReadOnly)
   501  
   502  		// Swap out the current head
   503  		t.head = newHead
   504  		atomic.StoreUint32(&t.headBytes, 0)
   505  		atomic.StoreUint32(&t.headId, nextID)
   506  		t.lock.Unlock()
   507  		t.lock.RLock()
   508  	}
   509  
   510  	defer t.lock.RUnlock()
   511  	if _, err := t.head.Write(blob); err != nil {
   512  		return err
   513  	}
   514  	newOffset := atomic.AddUint32(&t.headBytes, bLen)
   515  	idx := indexEntry{
   516  		filenum: atomic.LoadUint32(&t.headId),
   517  		offset:  newOffset,
   518  	}
   519  	// Write indexEntry
   520  	t.index.Write(idx.marshallBinary())
   521  
   522  	t.writeMeter.Mark(int64(bLen + indexEntrySize))
   523  	t.sizeGauge.Inc(int64(bLen + indexEntrySize))
   524  
   525  	atomic.AddUint64(&t.items, 1)
   526  	return nil
   527  }
   528  
   529  // getBounds returns the indexes for the item
   530  // returns start, end, filenumber and error
   531  func (t *freezerTable) getBounds(item uint64) (uint32, uint32, uint32, error) {
   532  	buffer := make([]byte, indexEntrySize)
   533  	var startIdx, endIdx indexEntry
   534  	// Read second index
   535  	if _, err := t.index.ReadAt(buffer, int64((item+1)*indexEntrySize)); err != nil {
   536  		return 0, 0, 0, err
   537  	}
   538  	endIdx.unmarshalBinary(buffer)
   539  	// Read first index (unless it's the very first item)
   540  	if item != 0 {
   541  		if _, err := t.index.ReadAt(buffer, int64(item*indexEntrySize)); err != nil {
   542  			return 0, 0, 0, err
   543  		}
   544  		startIdx.unmarshalBinary(buffer)
   545  	} else {
   546  		// Special case if we're reading the first item in the freezer. We assume that
   547  		// the first item always start from zero(regarding the deletion, we
   548  		// only support deletion by files, so that the assumption is held).
   549  		// This means we can use the first item metadata to carry information about
   550  		// the 'global' offset, for the deletion-case
   551  		return 0, endIdx.offset, endIdx.filenum, nil
   552  	}
   553  	if startIdx.filenum != endIdx.filenum {
   554  		// If a piece of data 'crosses' a data-file,
   555  		// it's actually in one piece on the second data-file.
   556  		// We return a zero-indexEntry for the second file as start
   557  		return 0, endIdx.offset, endIdx.filenum, nil
   558  	}
   559  	return startIdx.offset, endIdx.offset, endIdx.filenum, nil
   560  }
   561  
   562  // Retrieve looks up the data offset of an item with the given number and retrieves
   563  // the raw binary blob from the data file.
   564  func (t *freezerTable) Retrieve(item uint64) ([]byte, error) {
   565  	t.lock.RLock()
   566  	// Ensure the table and the item is accessible
   567  	if t.index == nil || t.head == nil {
   568  		t.lock.RUnlock()
   569  		return nil, errClosed
   570  	}
   571  	if atomic.LoadUint64(&t.items) <= item {
   572  		t.lock.RUnlock()
   573  		return nil, errOutOfBounds
   574  	}
   575  	// Ensure the item was not deleted from the tail either
   576  	if uint64(t.itemOffset) > item {
   577  		t.lock.RUnlock()
   578  		return nil, errOutOfBounds
   579  	}
   580  	startOffset, endOffset, filenum, err := t.getBounds(item - uint64(t.itemOffset))
   581  	if err != nil {
   582  		t.lock.RUnlock()
   583  		return nil, err
   584  	}
   585  	dataFile, exist := t.files[filenum]
   586  	if !exist {
   587  		t.lock.RUnlock()
   588  		return nil, fmt.Errorf("missing data file %d", filenum)
   589  	}
   590  	// Retrieve the data itself, decompress and return
   591  	blob := make([]byte, endOffset-startOffset)
   592  	if _, err := dataFile.ReadAt(blob, int64(startOffset)); err != nil {
   593  		t.lock.RUnlock()
   594  		return nil, err
   595  	}
   596  	t.lock.RUnlock()
   597  	t.readMeter.Mark(int64(len(blob) + 2*indexEntrySize))
   598  
   599  	if t.noCompression {
   600  		return blob, nil
   601  	}
   602  	return snappy.Decode(nil, blob)
   603  }
   604  
   605  // has returns an indicator whether the specified number data
   606  // exists in the freezer table.
   607  func (t *freezerTable) has(number uint64) bool {
   608  	return atomic.LoadUint64(&t.items) > number
   609  }
   610  
   611  // size returns the total data size in the freezer table.
   612  func (t *freezerTable) size() (uint64, error) {
   613  	t.lock.RLock()
   614  	defer t.lock.RUnlock()
   615  
   616  	return t.sizeNolock()
   617  }
   618  
   619  // sizeNolock returns the total data size in the freezer table without obtaining
   620  // the mutex first.
   621  func (t *freezerTable) sizeNolock() (uint64, error) {
   622  	stat, err := t.index.Stat()
   623  	if err != nil {
   624  		return 0, err
   625  	}
   626  	total := uint64(t.maxFileSize)*uint64(t.headId-t.tailId) + uint64(t.headBytes) + uint64(stat.Size())
   627  	return total, nil
   628  }
   629  
   630  // Sync pushes any pending data from memory out to disk. This is an expensive
   631  // operation, so use it with care.
   632  func (t *freezerTable) Sync() error {
   633  	if err := t.index.Sync(); err != nil {
   634  		return err
   635  	}
   636  	return t.head.Sync()
   637  }
   638  
   639  // printIndex is a debug print utility function for testing
   640  func (t *freezerTable) printIndex() {
   641  	buf := make([]byte, indexEntrySize)
   642  
   643  	fmt.Printf("|-----------------|\n")
   644  	fmt.Printf("| fileno | offset |\n")
   645  	fmt.Printf("|--------+--------|\n")
   646  
   647  	for i := uint64(0); ; i++ {
   648  		if _, err := t.index.ReadAt(buf, int64(i*indexEntrySize)); err != nil {
   649  			break
   650  		}
   651  		var entry indexEntry
   652  		entry.unmarshalBinary(buf)
   653  		fmt.Printf("|  %03d   |  %03d   | \n", entry.filenum, entry.offset)
   654  		if i > 100 {
   655  			fmt.Printf(" ... \n")
   656  			break
   657  		}
   658  	}
   659  	fmt.Printf("|-----------------|\n")
   660  }