github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/swarm/storage/ldbstore.go (about)

     1  // Copyright 2016 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  // disk storage layer for the package bzz
    18  // DbStore implements the ChunkStore interface and is used by the FileStore as
    19  // persistent storage of chunks
    20  // it implements purging based on access count allowing for external control of
    21  // max capacity
    22  
    23  package storage
    24  
    25  import (
    26  	"archive/tar"
    27  	"bytes"
    28  	"context"
    29  	"encoding/binary"
    30  	"encoding/hex"
    31  	"errors"
    32  	"fmt"
    33  	"io"
    34  	"io/ioutil"
    35  	"sync"
    36  
    37  	"github.com/insight-chain/inb-go/metrics"
    38  	"github.com/insight-chain/inb-go/rlp"
    39  	"github.com/insight-chain/inb-go/swarm/log"
    40  	"github.com/insight-chain/inb-go/swarm/storage/mock"
    41  	"github.com/syndtr/goleveldb/leveldb"
    42  )
    43  
    44  const (
    45  	defaultGCRatio    = 10
    46  	defaultMaxGCRound = 10000
    47  	defaultMaxGCBatch = 5000
    48  
    49  	wEntryCnt  = 1 << 0
    50  	wIndexCnt  = 1 << 1
    51  	wAccessCnt = 1 << 2
    52  )
    53  
    54  var (
    55  	dbEntryCount = metrics.NewRegisteredCounter("ldbstore.entryCnt", nil)
    56  )
    57  
    58  var (
    59  	keyIndex       = byte(0)
    60  	keyAccessCnt   = []byte{2}
    61  	keyEntryCnt    = []byte{3}
    62  	keyDataIdx     = []byte{4}
    63  	keyData        = byte(6)
    64  	keyDistanceCnt = byte(7)
    65  	keySchema      = []byte{8}
    66  	keyGCIdx       = byte(9) // access to chunk data index, used by garbage collection in ascending order from first entry
    67  )
    68  
    69  var (
    70  	ErrDBClosed = errors.New("LDBStore closed")
    71  )
    72  
    73  type LDBStoreParams struct {
    74  	*StoreParams
    75  	Path string
    76  	Po   func(Address) uint8
    77  }
    78  
    79  // NewLDBStoreParams constructs LDBStoreParams with the specified values.
    80  func NewLDBStoreParams(storeparams *StoreParams, path string) *LDBStoreParams {
    81  	return &LDBStoreParams{
    82  		StoreParams: storeparams,
    83  		Path:        path,
    84  		Po:          func(k Address) (ret uint8) { return uint8(Proximity(storeparams.BaseKey, k[:])) },
    85  	}
    86  }
    87  
    88  type garbage struct {
    89  	maxRound int           // maximum number of chunks to delete in one garbage collection round
    90  	maxBatch int           // maximum number of chunks to delete in one db request batch
    91  	ratio    int           // 1/x ratio to calculate the number of chunks to gc on a low capacity db
    92  	count    int           // number of chunks deleted in running round
    93  	target   int           // number of chunks to delete in running round
    94  	batch    *dbBatch      // the delete batch
    95  	runC     chan struct{} // struct in chan means gc is NOT running
    96  }
    97  
    98  type LDBStore struct {
    99  	db *LDBDatabase
   100  
   101  	// this should be stored in db, accessed transactionally
   102  	entryCnt  uint64 // number of items in the LevelDB
   103  	accessCnt uint64 // ever-accumulating number increased every time we read/access an entry
   104  	dataIdx   uint64 // similar to entryCnt, but we only increment it
   105  	capacity  uint64
   106  	bucketCnt []uint64
   107  
   108  	hashfunc SwarmHasher
   109  	po       func(Address) uint8
   110  
   111  	batchesC chan struct{}
   112  	closed   bool
   113  	batch    *dbBatch
   114  	lock     sync.RWMutex
   115  	quit     chan struct{}
   116  	gc       *garbage
   117  
   118  	// Functions encodeDataFunc is used to bypass
   119  	// the default functionality of DbStore with
   120  	// mock.NodeStore for testing purposes.
   121  	encodeDataFunc func(chunk Chunk) []byte
   122  	// If getDataFunc is defined, it will be used for
   123  	// retrieving the chunk data instead from the local
   124  	// LevelDB database.
   125  	getDataFunc func(key Address) (data []byte, err error)
   126  }
   127  
   128  type dbBatch struct {
   129  	*leveldb.Batch
   130  	err error
   131  	c   chan struct{}
   132  }
   133  
   134  func newBatch() *dbBatch {
   135  	return &dbBatch{Batch: new(leveldb.Batch), c: make(chan struct{})}
   136  }
   137  
   138  // TODO: Instead of passing the distance function, just pass the address from which distances are calculated
   139  // to avoid the appearance of a pluggable distance metric and opportunities of bugs associated with providing
   140  // a function different from the one that is actually used.
   141  func NewLDBStore(params *LDBStoreParams) (s *LDBStore, err error) {
   142  	s = new(LDBStore)
   143  	s.hashfunc = params.Hash
   144  	s.quit = make(chan struct{})
   145  
   146  	s.batchesC = make(chan struct{}, 1)
   147  	go s.writeBatches()
   148  	s.batch = newBatch()
   149  	// associate encodeData with default functionality
   150  	s.encodeDataFunc = encodeData
   151  
   152  	s.db, err = NewLDBDatabase(params.Path)
   153  	if err != nil {
   154  		return nil, err
   155  	}
   156  
   157  	s.po = params.Po
   158  	s.setCapacity(params.DbCapacity)
   159  
   160  	s.bucketCnt = make([]uint64, 0x100)
   161  	for i := 0; i < 0x100; i++ {
   162  		k := make([]byte, 2)
   163  		k[0] = keyDistanceCnt
   164  		k[1] = uint8(i)
   165  		cnt, _ := s.db.Get(k)
   166  		s.bucketCnt[i] = BytesToU64(cnt)
   167  	}
   168  	data, _ := s.db.Get(keyEntryCnt)
   169  	s.entryCnt = BytesToU64(data)
   170  	data, _ = s.db.Get(keyAccessCnt)
   171  	s.accessCnt = BytesToU64(data)
   172  	data, _ = s.db.Get(keyDataIdx)
   173  	s.dataIdx = BytesToU64(data)
   174  
   175  	// set up garbage collection
   176  	s.gc = &garbage{
   177  		maxBatch: defaultMaxGCBatch,
   178  		maxRound: defaultMaxGCRound,
   179  		ratio:    defaultGCRatio,
   180  	}
   181  
   182  	s.gc.runC = make(chan struct{}, 1)
   183  	s.gc.runC <- struct{}{}
   184  
   185  	return s, nil
   186  }
   187  
   188  // MarkAccessed increments the access counter as a best effort for a chunk, so
   189  // the chunk won't get garbage collected.
   190  func (s *LDBStore) MarkAccessed(addr Address) {
   191  	s.lock.Lock()
   192  	defer s.lock.Unlock()
   193  
   194  	if s.closed {
   195  		return
   196  	}
   197  
   198  	proximity := s.po(addr)
   199  	s.tryAccessIdx(addr, proximity)
   200  }
   201  
   202  // initialize and set values for processing of gc round
   203  func (s *LDBStore) startGC(c int) {
   204  
   205  	s.gc.count = 0
   206  	// calculate the target number of deletions
   207  	if c >= s.gc.maxRound {
   208  		s.gc.target = s.gc.maxRound
   209  	} else {
   210  		s.gc.target = c / s.gc.ratio
   211  	}
   212  	s.gc.batch = newBatch()
   213  	log.Debug("startgc", "requested", c, "target", s.gc.target)
   214  }
   215  
   216  // NewMockDbStore creates a new instance of DbStore with
   217  // mockStore set to a provided value. If mockStore argument is nil,
   218  // this function behaves exactly as NewDbStore.
   219  func NewMockDbStore(params *LDBStoreParams, mockStore *mock.NodeStore) (s *LDBStore, err error) {
   220  	s, err = NewLDBStore(params)
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  
   225  	// replace put and get with mock store functionality
   226  	if mockStore != nil {
   227  		s.encodeDataFunc = newMockEncodeDataFunc(mockStore)
   228  		s.getDataFunc = newMockGetDataFunc(mockStore)
   229  	}
   230  	return
   231  }
   232  
   233  type dpaDBIndex struct {
   234  	Idx    uint64
   235  	Access uint64
   236  }
   237  
   238  func BytesToU64(data []byte) uint64 {
   239  	if len(data) < 8 {
   240  		return 0
   241  	}
   242  	return binary.BigEndian.Uint64(data)
   243  }
   244  
   245  func U64ToBytes(val uint64) []byte {
   246  	data := make([]byte, 8)
   247  	binary.BigEndian.PutUint64(data, val)
   248  	return data
   249  }
   250  
   251  func (s *LDBStore) updateIndexAccess(index *dpaDBIndex) {
   252  	index.Access = s.accessCnt
   253  }
   254  
   255  func getIndexKey(hash Address) []byte {
   256  	hashSize := len(hash)
   257  	key := make([]byte, hashSize+1)
   258  	key[0] = keyIndex
   259  	copy(key[1:], hash[:])
   260  	return key
   261  }
   262  
   263  func getDataKey(idx uint64, po uint8) []byte {
   264  	key := make([]byte, 10)
   265  	key[0] = keyData
   266  	key[1] = po
   267  	binary.BigEndian.PutUint64(key[2:], idx)
   268  
   269  	return key
   270  }
   271  
   272  func getGCIdxKey(index *dpaDBIndex) []byte {
   273  	key := make([]byte, 9)
   274  	key[0] = keyGCIdx
   275  	binary.BigEndian.PutUint64(key[1:], index.Access)
   276  	return key
   277  }
   278  
   279  func getGCIdxValue(index *dpaDBIndex, po uint8, addr Address) []byte {
   280  	val := make([]byte, 41) // po = 1, index.Index = 8, Address = 32
   281  	val[0] = po
   282  	binary.BigEndian.PutUint64(val[1:], index.Idx)
   283  	copy(val[9:], addr)
   284  	return val
   285  }
   286  
   287  func parseIdxKey(key []byte) (byte, []byte) {
   288  	return key[0], key[1:]
   289  }
   290  
   291  func parseGCIdxEntry(accessCnt []byte, val []byte) (index *dpaDBIndex, po uint8, addr Address) {
   292  	index = &dpaDBIndex{
   293  		Idx:    binary.BigEndian.Uint64(val[1:]),
   294  		Access: binary.BigEndian.Uint64(accessCnt),
   295  	}
   296  	po = val[0]
   297  	addr = val[9:]
   298  	return
   299  }
   300  
   301  func encodeIndex(index *dpaDBIndex) []byte {
   302  	data, _ := rlp.EncodeToBytes(index)
   303  	return data
   304  }
   305  
   306  func encodeData(chunk Chunk) []byte {
   307  	// Always create a new underlying array for the returned byte slice.
   308  	// The chunk.Address array may be used in the returned slice which
   309  	// may be changed later in the code or by the LevelDB, resulting
   310  	// that the Address is changed as well.
   311  	return append(append([]byte{}, chunk.Address()[:]...), chunk.Data()...)
   312  }
   313  
   314  func decodeIndex(data []byte, index *dpaDBIndex) error {
   315  	dec := rlp.NewStream(bytes.NewReader(data), 0)
   316  	return dec.Decode(index)
   317  }
   318  
   319  func decodeData(addr Address, data []byte) (*chunk, error) {
   320  	return NewChunk(addr, data[32:]), nil
   321  }
   322  
   323  func (s *LDBStore) collectGarbage() error {
   324  
   325  	// prevent duplicate gc from starting when one is already running
   326  	select {
   327  	case <-s.gc.runC:
   328  	default:
   329  		return nil
   330  	}
   331  
   332  	s.lock.Lock()
   333  	entryCnt := s.entryCnt
   334  	s.lock.Unlock()
   335  
   336  	metrics.GetOrRegisterCounter("ldbstore.collectgarbage", nil).Inc(1)
   337  
   338  	// calculate the amount of chunks to collect and reset counter
   339  	s.startGC(int(entryCnt))
   340  	log.Debug("collectGarbage", "target", s.gc.target, "entryCnt", entryCnt)
   341  
   342  	var totalDeleted int
   343  	for s.gc.count < s.gc.target {
   344  		it := s.db.NewIterator()
   345  		ok := it.Seek([]byte{keyGCIdx})
   346  		var singleIterationCount int
   347  
   348  		// every batch needs a lock so we avoid entries changing accessidx in the meantime
   349  		s.lock.Lock()
   350  		for ; ok && (singleIterationCount < s.gc.maxBatch); ok = it.Next() {
   351  
   352  			// quit if no more access index keys
   353  			itkey := it.Key()
   354  			if (itkey == nil) || (itkey[0] != keyGCIdx) {
   355  				break
   356  			}
   357  
   358  			// get chunk data entry from access index
   359  			val := it.Value()
   360  			index, po, hash := parseGCIdxEntry(itkey[1:], val)
   361  			keyIdx := make([]byte, 33)
   362  			keyIdx[0] = keyIndex
   363  			copy(keyIdx[1:], hash)
   364  
   365  			// add delete operation to batch
   366  			s.delete(s.gc.batch.Batch, index, keyIdx, po)
   367  			singleIterationCount++
   368  			s.gc.count++
   369  			log.Trace("garbage collect enqueued chunk for deletion", "key", hash)
   370  
   371  			// break if target is not on max garbage batch boundary
   372  			if s.gc.count >= s.gc.target {
   373  				break
   374  			}
   375  		}
   376  
   377  		s.writeBatch(s.gc.batch, wEntryCnt)
   378  		s.lock.Unlock()
   379  		it.Release()
   380  		log.Trace("garbage collect batch done", "batch", singleIterationCount, "total", s.gc.count)
   381  	}
   382  
   383  	s.gc.runC <- struct{}{}
   384  	log.Debug("garbage collect done", "c", s.gc.count)
   385  
   386  	metrics.GetOrRegisterCounter("ldbstore.collectgarbage.delete", nil).Inc(int64(totalDeleted))
   387  	return nil
   388  }
   389  
   390  // Export writes all chunks from the store to a tar archive, returning the
   391  // number of chunks written.
   392  func (s *LDBStore) Export(out io.Writer) (int64, error) {
   393  	tw := tar.NewWriter(out)
   394  	defer tw.Close()
   395  
   396  	it := s.db.NewIterator()
   397  	defer it.Release()
   398  	var count int64
   399  	for ok := it.Seek([]byte{keyIndex}); ok; ok = it.Next() {
   400  		key := it.Key()
   401  		if (key == nil) || (key[0] != keyIndex) {
   402  			break
   403  		}
   404  
   405  		var index dpaDBIndex
   406  
   407  		hash := key[1:]
   408  		decodeIndex(it.Value(), &index)
   409  		po := s.po(hash)
   410  		datakey := getDataKey(index.Idx, po)
   411  		log.Trace("store.export", "dkey", fmt.Sprintf("%x", datakey), "dataidx", index.Idx, "po", po)
   412  		data, err := s.db.Get(datakey)
   413  		if err != nil {
   414  			log.Warn(fmt.Sprintf("Chunk %x found but could not be accessed: %v", key, err))
   415  			continue
   416  		}
   417  
   418  		hdr := &tar.Header{
   419  			Name: hex.EncodeToString(hash),
   420  			Mode: 0644,
   421  			Size: int64(len(data)),
   422  		}
   423  		if err := tw.WriteHeader(hdr); err != nil {
   424  			return count, err
   425  		}
   426  		if _, err := tw.Write(data); err != nil {
   427  			return count, err
   428  		}
   429  		count++
   430  	}
   431  
   432  	return count, nil
   433  }
   434  
   435  // of chunks read.
   436  func (s *LDBStore) Import(in io.Reader) (int64, error) {
   437  	tr := tar.NewReader(in)
   438  
   439  	ctx, cancel := context.WithCancel(context.Background())
   440  	defer cancel()
   441  
   442  	countC := make(chan int64)
   443  	errC := make(chan error)
   444  	var count int64
   445  	go func() {
   446  		for {
   447  			hdr, err := tr.Next()
   448  			if err == io.EOF {
   449  				break
   450  			} else if err != nil {
   451  				select {
   452  				case errC <- err:
   453  				case <-ctx.Done():
   454  				}
   455  			}
   456  
   457  			if len(hdr.Name) != 64 {
   458  				log.Warn("ignoring non-chunk file", "name", hdr.Name)
   459  				continue
   460  			}
   461  
   462  			keybytes, err := hex.DecodeString(hdr.Name)
   463  			if err != nil {
   464  				log.Warn("ignoring invalid chunk file", "name", hdr.Name, "err", err)
   465  				continue
   466  			}
   467  
   468  			data, err := ioutil.ReadAll(tr)
   469  			if err != nil {
   470  				select {
   471  				case errC <- err:
   472  				case <-ctx.Done():
   473  				}
   474  			}
   475  			key := Address(keybytes)
   476  			chunk := NewChunk(key, data[32:])
   477  
   478  			go func() {
   479  				select {
   480  				case errC <- s.Put(ctx, chunk):
   481  				case <-ctx.Done():
   482  				}
   483  			}()
   484  
   485  			count++
   486  		}
   487  		countC <- count
   488  	}()
   489  
   490  	// wait for all chunks to be stored
   491  	i := int64(0)
   492  	var total int64
   493  	for {
   494  		select {
   495  		case err := <-errC:
   496  			if err != nil {
   497  				return count, err
   498  			}
   499  			i++
   500  		case total = <-countC:
   501  		case <-ctx.Done():
   502  			return i, ctx.Err()
   503  		}
   504  		if total > 0 && i == total {
   505  			return total, nil
   506  		}
   507  	}
   508  }
   509  
   510  // Cleanup iterates over the database and deletes chunks if they pass the `f` condition
   511  func (s *LDBStore) Cleanup(f func(*chunk) bool) {
   512  	var errorsFound, removed, total int
   513  
   514  	it := s.db.NewIterator()
   515  	defer it.Release()
   516  	for ok := it.Seek([]byte{keyIndex}); ok; ok = it.Next() {
   517  		key := it.Key()
   518  		if (key == nil) || (key[0] != keyIndex) {
   519  			break
   520  		}
   521  		total++
   522  		var index dpaDBIndex
   523  		err := decodeIndex(it.Value(), &index)
   524  		if err != nil {
   525  			log.Warn("Cannot decode")
   526  			errorsFound++
   527  			continue
   528  		}
   529  		hash := key[1:]
   530  		po := s.po(hash)
   531  		datakey := getDataKey(index.Idx, po)
   532  		data, err := s.db.Get(datakey)
   533  		if err != nil {
   534  			found := false
   535  
   536  			// highest possible proximity is 255
   537  			for po = 1; po <= 255; po++ {
   538  				datakey = getDataKey(index.Idx, po)
   539  				data, err = s.db.Get(datakey)
   540  				if err == nil {
   541  					found = true
   542  					break
   543  				}
   544  			}
   545  
   546  			if !found {
   547  				log.Warn(fmt.Sprintf("Chunk %x found but count not be accessed with any po", key))
   548  				errorsFound++
   549  				continue
   550  			}
   551  		}
   552  
   553  		ck := data[:32]
   554  		c, err := decodeData(ck, data)
   555  		if err != nil {
   556  			log.Error("decodeData error", "err", err)
   557  			continue
   558  		}
   559  
   560  		cs := int64(binary.LittleEndian.Uint64(c.sdata[:8]))
   561  		log.Trace("chunk", "key", fmt.Sprintf("%x", key), "ck", fmt.Sprintf("%x", ck), "dkey", fmt.Sprintf("%x", datakey), "dataidx", index.Idx, "po", po, "len data", len(data), "len sdata", len(c.sdata), "size", cs)
   562  
   563  		// if chunk is to be removed
   564  		if f(c) {
   565  			log.Warn("chunk for cleanup", "key", fmt.Sprintf("%x", key), "ck", fmt.Sprintf("%x", ck), "dkey", fmt.Sprintf("%x", datakey), "dataidx", index.Idx, "po", po, "len data", len(data), "len sdata", len(c.sdata), "size", cs)
   566  			s.deleteNow(&index, getIndexKey(key[1:]), po)
   567  			removed++
   568  			errorsFound++
   569  		}
   570  	}
   571  
   572  	log.Warn(fmt.Sprintf("Found %v errors out of %v entries. Removed %v chunks.", errorsFound, total, removed))
   573  }
   574  
   575  // CleanGCIndex rebuilds the garbage collector index from scratch, while
   576  // removing inconsistent elements, e.g., indices with missing data chunks.
   577  // WARN: it's a pretty heavy, long running function.
   578  func (s *LDBStore) CleanGCIndex() error {
   579  	s.lock.Lock()
   580  	defer s.lock.Unlock()
   581  
   582  	batch := leveldb.Batch{}
   583  
   584  	var okEntryCount uint64
   585  	var totalEntryCount uint64
   586  
   587  	// throw out all gc indices, we will rebuild from cleaned index
   588  	it := s.db.NewIterator()
   589  	it.Seek([]byte{keyGCIdx})
   590  	var gcDeletes int
   591  	for it.Valid() {
   592  		rowType, _ := parseIdxKey(it.Key())
   593  		if rowType != keyGCIdx {
   594  			break
   595  		}
   596  		batch.Delete(it.Key())
   597  		gcDeletes++
   598  		it.Next()
   599  	}
   600  	log.Debug("gc", "deletes", gcDeletes)
   601  	if err := s.db.Write(&batch); err != nil {
   602  		return err
   603  	}
   604  	batch.Reset()
   605  
   606  	it.Release()
   607  
   608  	// corrected po index pointer values
   609  	var poPtrs [256]uint64
   610  
   611  	// set to true if chunk count not on 4096 iteration boundary
   612  	var doneIterating bool
   613  
   614  	// last key index in previous iteration
   615  	lastIdxKey := []byte{keyIndex}
   616  
   617  	// counter for debug output
   618  	var cleanBatchCount int
   619  
   620  	// go through all key index entries
   621  	for !doneIterating {
   622  		cleanBatchCount++
   623  		var idxs []dpaDBIndex
   624  		var chunkHashes [][]byte
   625  		var pos []uint8
   626  		it := s.db.NewIterator()
   627  
   628  		it.Seek(lastIdxKey)
   629  
   630  		// 4096 is just a nice number, don't look for any hidden meaning here...
   631  		var i int
   632  		for i = 0; i < 4096; i++ {
   633  
   634  			// this really shouldn't happen unless database is empty
   635  			// but let's keep it to be safe
   636  			if !it.Valid() {
   637  				doneIterating = true
   638  				break
   639  			}
   640  
   641  			// if it's not keyindex anymore we're done iterating
   642  			rowType, chunkHash := parseIdxKey(it.Key())
   643  			if rowType != keyIndex {
   644  				doneIterating = true
   645  				break
   646  			}
   647  
   648  			// decode the retrieved index
   649  			var idx dpaDBIndex
   650  			err := decodeIndex(it.Value(), &idx)
   651  			if err != nil {
   652  				return fmt.Errorf("corrupt index: %v", err)
   653  			}
   654  			po := s.po(chunkHash)
   655  			lastIdxKey = it.Key()
   656  
   657  			// if we don't find the data key, remove the entry
   658  			// if we find it, add to the array of new gc indices to create
   659  			dataKey := getDataKey(idx.Idx, po)
   660  			_, err = s.db.Get(dataKey)
   661  			if err != nil {
   662  				log.Warn("deleting inconsistent index (missing data)", "key", chunkHash)
   663  				batch.Delete(it.Key())
   664  			} else {
   665  				idxs = append(idxs, idx)
   666  				chunkHashes = append(chunkHashes, chunkHash)
   667  				pos = append(pos, po)
   668  				okEntryCount++
   669  				if idx.Idx > poPtrs[po] {
   670  					poPtrs[po] = idx.Idx
   671  				}
   672  			}
   673  			totalEntryCount++
   674  			it.Next()
   675  		}
   676  		it.Release()
   677  
   678  		// flush the key index corrections
   679  		err := s.db.Write(&batch)
   680  		if err != nil {
   681  			return err
   682  		}
   683  		batch.Reset()
   684  
   685  		// add correct gc indices
   686  		for i, okIdx := range idxs {
   687  			gcIdxKey := getGCIdxKey(&okIdx)
   688  			gcIdxData := getGCIdxValue(&okIdx, pos[i], chunkHashes[i])
   689  			batch.Put(gcIdxKey, gcIdxData)
   690  			log.Trace("clean ok", "key", chunkHashes[i], "gcKey", gcIdxKey, "gcData", gcIdxData)
   691  		}
   692  
   693  		// flush them
   694  		err = s.db.Write(&batch)
   695  		if err != nil {
   696  			return err
   697  		}
   698  		batch.Reset()
   699  
   700  		log.Debug("clean gc index pass", "batch", cleanBatchCount, "checked", i, "kept", len(idxs))
   701  	}
   702  
   703  	log.Debug("gc cleanup entries", "ok", okEntryCount, "total", totalEntryCount, "batchlen", batch.Len())
   704  
   705  	// lastly add updated entry count
   706  	var entryCount [8]byte
   707  	binary.BigEndian.PutUint64(entryCount[:], okEntryCount)
   708  	batch.Put(keyEntryCnt, entryCount[:])
   709  
   710  	// and add the new po index pointers
   711  	var poKey [2]byte
   712  	poKey[0] = keyDistanceCnt
   713  	for i, poPtr := range poPtrs {
   714  		poKey[1] = uint8(i)
   715  		if poPtr == 0 {
   716  			batch.Delete(poKey[:])
   717  		} else {
   718  			var idxCount [8]byte
   719  			binary.BigEndian.PutUint64(idxCount[:], poPtr)
   720  			batch.Put(poKey[:], idxCount[:])
   721  		}
   722  	}
   723  
   724  	// if you made it this far your harddisk has survived. Congratulations
   725  	return s.db.Write(&batch)
   726  }
   727  
   728  // Delete is removes a chunk and updates indices.
   729  // Is thread safe
   730  func (s *LDBStore) Delete(addr Address) error {
   731  	s.lock.Lock()
   732  	defer s.lock.Unlock()
   733  
   734  	ikey := getIndexKey(addr)
   735  
   736  	idata, err := s.db.Get(ikey)
   737  	if err != nil {
   738  		return err
   739  	}
   740  
   741  	var idx dpaDBIndex
   742  	decodeIndex(idata, &idx)
   743  	proximity := s.po(addr)
   744  	return s.deleteNow(&idx, ikey, proximity)
   745  }
   746  
   747  // executes one delete operation immediately
   748  // see *LDBStore.delete
   749  func (s *LDBStore) deleteNow(idx *dpaDBIndex, idxKey []byte, po uint8) error {
   750  	batch := new(leveldb.Batch)
   751  	s.delete(batch, idx, idxKey, po)
   752  	return s.db.Write(batch)
   753  }
   754  
   755  // adds a delete chunk operation to the provided batch
   756  // if called directly, decrements entrycount regardless if the chunk exists upon deletion. Risk of wrap to max uint64
   757  func (s *LDBStore) delete(batch *leveldb.Batch, idx *dpaDBIndex, idxKey []byte, po uint8) {
   758  	metrics.GetOrRegisterCounter("ldbstore.delete", nil).Inc(1)
   759  
   760  	gcIdxKey := getGCIdxKey(idx)
   761  	batch.Delete(gcIdxKey)
   762  	dataKey := getDataKey(idx.Idx, po)
   763  	batch.Delete(dataKey)
   764  	batch.Delete(idxKey)
   765  	s.entryCnt--
   766  	dbEntryCount.Dec(1)
   767  	cntKey := make([]byte, 2)
   768  	cntKey[0] = keyDistanceCnt
   769  	cntKey[1] = po
   770  	batch.Put(keyEntryCnt, U64ToBytes(s.entryCnt))
   771  	batch.Put(cntKey, U64ToBytes(s.bucketCnt[po]))
   772  }
   773  
   774  func (s *LDBStore) BinIndex(po uint8) uint64 {
   775  	s.lock.RLock()
   776  	defer s.lock.RUnlock()
   777  	return s.bucketCnt[po]
   778  }
   779  
   780  func (s *LDBStore) Size() uint64 {
   781  	s.lock.RLock()
   782  	defer s.lock.RUnlock()
   783  	return s.entryCnt
   784  }
   785  
   786  func (s *LDBStore) CurrentStorageIndex() uint64 {
   787  	s.lock.RLock()
   788  	defer s.lock.RUnlock()
   789  	return s.dataIdx
   790  }
   791  
   792  // Put adds a chunk to the database, adding indices and incrementing global counters.
   793  // If it already exists, it merely increments the access count of the existing entry.
   794  // Is thread safe
   795  func (s *LDBStore) Put(ctx context.Context, chunk Chunk) error {
   796  	metrics.GetOrRegisterCounter("ldbstore.put", nil).Inc(1)
   797  	log.Trace("ldbstore.put", "key", chunk.Address())
   798  
   799  	ikey := getIndexKey(chunk.Address())
   800  	var index dpaDBIndex
   801  
   802  	po := s.po(chunk.Address())
   803  
   804  	s.lock.Lock()
   805  
   806  	if s.closed {
   807  		s.lock.Unlock()
   808  		return ErrDBClosed
   809  	}
   810  	batch := s.batch
   811  
   812  	log.Trace("ldbstore.put: s.db.Get", "key", chunk.Address(), "ikey", fmt.Sprintf("%x", ikey))
   813  	idata, err := s.db.Get(ikey)
   814  	if err != nil {
   815  		s.doPut(chunk, &index, po)
   816  	}
   817  	idata = encodeIndex(&index)
   818  	s.batch.Put(ikey, idata)
   819  
   820  	// add the access-chunkindex index for garbage collection
   821  	gcIdxKey := getGCIdxKey(&index)
   822  	gcIdxData := getGCIdxValue(&index, po, chunk.Address())
   823  	s.batch.Put(gcIdxKey, gcIdxData)
   824  	s.lock.Unlock()
   825  
   826  	select {
   827  	case s.batchesC <- struct{}{}:
   828  	default:
   829  	}
   830  
   831  	select {
   832  	case <-batch.c:
   833  		return batch.err
   834  	case <-ctx.Done():
   835  		return ctx.Err()
   836  	}
   837  }
   838  
   839  // force putting into db, does not check or update necessary indices
   840  func (s *LDBStore) doPut(chunk Chunk, index *dpaDBIndex, po uint8) {
   841  	data := s.encodeDataFunc(chunk)
   842  	dkey := getDataKey(s.dataIdx, po)
   843  	s.batch.Put(dkey, data)
   844  	index.Idx = s.dataIdx
   845  	s.bucketCnt[po] = s.dataIdx
   846  	s.entryCnt++
   847  	dbEntryCount.Inc(1)
   848  	s.dataIdx++
   849  	index.Access = s.accessCnt
   850  	s.accessCnt++
   851  	cntKey := make([]byte, 2)
   852  	cntKey[0] = keyDistanceCnt
   853  	cntKey[1] = po
   854  	s.batch.Put(cntKey, U64ToBytes(s.bucketCnt[po]))
   855  }
   856  
   857  func (s *LDBStore) writeBatches() {
   858  	for {
   859  		select {
   860  		case <-s.quit:
   861  			log.Debug("DbStore: quit batch write loop")
   862  			return
   863  		case <-s.batchesC:
   864  			err := s.writeCurrentBatch()
   865  			if err != nil {
   866  				log.Debug("DbStore: quit batch write loop", "err", err.Error())
   867  				return
   868  			}
   869  		}
   870  	}
   871  
   872  }
   873  
   874  func (s *LDBStore) writeCurrentBatch() error {
   875  	s.lock.Lock()
   876  	defer s.lock.Unlock()
   877  	b := s.batch
   878  	l := b.Len()
   879  	if l == 0 {
   880  		return nil
   881  	}
   882  	s.batch = newBatch()
   883  	b.err = s.writeBatch(b, wEntryCnt|wAccessCnt|wIndexCnt)
   884  	close(b.c)
   885  	if s.entryCnt >= s.capacity {
   886  		go s.collectGarbage()
   887  	}
   888  	return nil
   889  }
   890  
   891  // must be called non concurrently
   892  func (s *LDBStore) writeBatch(b *dbBatch, wFlag uint8) error {
   893  	if wFlag&wEntryCnt > 0 {
   894  		b.Put(keyEntryCnt, U64ToBytes(s.entryCnt))
   895  	}
   896  	if wFlag&wIndexCnt > 0 {
   897  		b.Put(keyDataIdx, U64ToBytes(s.dataIdx))
   898  	}
   899  	if wFlag&wAccessCnt > 0 {
   900  		b.Put(keyAccessCnt, U64ToBytes(s.accessCnt))
   901  	}
   902  	l := b.Len()
   903  	if err := s.db.Write(b.Batch); err != nil {
   904  		return fmt.Errorf("unable to write batch: %v", err)
   905  	}
   906  	log.Trace(fmt.Sprintf("batch write (%d entries)", l))
   907  	return nil
   908  }
   909  
   910  // newMockEncodeDataFunc returns a function that stores the chunk data
   911  // to a mock store to bypass the default functionality encodeData.
   912  // The constructed function always returns the nil data, as DbStore does
   913  // not need to store the data, but still need to create the index.
   914  func newMockEncodeDataFunc(mockStore *mock.NodeStore) func(chunk Chunk) []byte {
   915  	return func(chunk Chunk) []byte {
   916  		if err := mockStore.Put(chunk.Address(), encodeData(chunk)); err != nil {
   917  			log.Error(fmt.Sprintf("%T: Chunk %v put: %v", mockStore, chunk.Address().Log(), err))
   918  		}
   919  		return chunk.Address()[:]
   920  	}
   921  }
   922  
   923  // tryAccessIdx tries to find index entry. If found then increments the access
   924  // count for garbage collection and returns the index entry and true for found,
   925  // otherwise returns nil and false.
   926  func (s *LDBStore) tryAccessIdx(addr Address, po uint8) (*dpaDBIndex, bool) {
   927  	ikey := getIndexKey(addr)
   928  	idata, err := s.db.Get(ikey)
   929  	if err != nil {
   930  		return nil, false
   931  	}
   932  
   933  	index := new(dpaDBIndex)
   934  	decodeIndex(idata, index)
   935  	oldGCIdxKey := getGCIdxKey(index)
   936  	s.batch.Put(keyAccessCnt, U64ToBytes(s.accessCnt))
   937  	index.Access = s.accessCnt
   938  	idata = encodeIndex(index)
   939  	s.accessCnt++
   940  	s.batch.Put(ikey, idata)
   941  	newGCIdxKey := getGCIdxKey(index)
   942  	newGCIdxData := getGCIdxValue(index, po, ikey[1:])
   943  	s.batch.Delete(oldGCIdxKey)
   944  	s.batch.Put(newGCIdxKey, newGCIdxData)
   945  	select {
   946  	case s.batchesC <- struct{}{}:
   947  	default:
   948  	}
   949  	return index, true
   950  }
   951  
   952  // GetSchema is returning the current named schema of the datastore as read from LevelDB
   953  func (s *LDBStore) GetSchema() (string, error) {
   954  	s.lock.Lock()
   955  	defer s.lock.Unlock()
   956  
   957  	data, err := s.db.Get(keySchema)
   958  	if err != nil {
   959  		if err == leveldb.ErrNotFound {
   960  			return DbSchemaNone, nil
   961  		}
   962  		return "", err
   963  	}
   964  
   965  	return string(data), nil
   966  }
   967  
   968  // PutSchema is saving a named schema to the LevelDB datastore
   969  func (s *LDBStore) PutSchema(schema string) error {
   970  	s.lock.Lock()
   971  	defer s.lock.Unlock()
   972  
   973  	return s.db.Put(keySchema, []byte(schema))
   974  }
   975  
   976  // Get retrieves the chunk matching the provided key from the database.
   977  // If the chunk entry does not exist, it returns an error
   978  // Updates access count and is thread safe
   979  func (s *LDBStore) Get(_ context.Context, addr Address) (chunk Chunk, err error) {
   980  	metrics.GetOrRegisterCounter("ldbstore.get", nil).Inc(1)
   981  	log.Trace("ldbstore.get", "key", addr)
   982  
   983  	s.lock.Lock()
   984  	defer s.lock.Unlock()
   985  	return s.get(addr)
   986  }
   987  
   988  // TODO: To conform with other private methods of this object indices should not be updated
   989  func (s *LDBStore) get(addr Address) (chunk *chunk, err error) {
   990  	if s.closed {
   991  		return nil, ErrDBClosed
   992  	}
   993  	proximity := s.po(addr)
   994  	index, found := s.tryAccessIdx(addr, proximity)
   995  	if found {
   996  		var data []byte
   997  		if s.getDataFunc != nil {
   998  			// if getDataFunc is defined, use it to retrieve the chunk data
   999  			log.Trace("ldbstore.get retrieve with getDataFunc", "key", addr)
  1000  			data, err = s.getDataFunc(addr)
  1001  			if err != nil {
  1002  				return
  1003  			}
  1004  		} else {
  1005  			// default DbStore functionality to retrieve chunk data
  1006  			datakey := getDataKey(index.Idx, proximity)
  1007  			data, err = s.db.Get(datakey)
  1008  			log.Trace("ldbstore.get retrieve", "key", addr, "indexkey", index.Idx, "datakey", fmt.Sprintf("%x", datakey), "proximity", proximity)
  1009  			if err != nil {
  1010  				log.Trace("ldbstore.get chunk found but could not be accessed", "key", addr, "err", err)
  1011  				s.deleteNow(index, getIndexKey(addr), s.po(addr))
  1012  				return
  1013  			}
  1014  		}
  1015  
  1016  		return decodeData(addr, data)
  1017  	} else {
  1018  		err = ErrChunkNotFound
  1019  	}
  1020  
  1021  	return
  1022  }
  1023  
  1024  // newMockGetFunc returns a function that reads chunk data from
  1025  // the mock database, which is used as the value for DbStore.getFunc
  1026  // to bypass the default functionality of DbStore with a mock store.
  1027  func newMockGetDataFunc(mockStore *mock.NodeStore) func(addr Address) (data []byte, err error) {
  1028  	return func(addr Address) (data []byte, err error) {
  1029  		data, err = mockStore.Get(addr)
  1030  		if err == mock.ErrNotFound {
  1031  			// preserve ErrChunkNotFound error
  1032  			err = ErrChunkNotFound
  1033  		}
  1034  		return data, err
  1035  	}
  1036  }
  1037  
  1038  func (s *LDBStore) setCapacity(c uint64) {
  1039  	s.lock.Lock()
  1040  	defer s.lock.Unlock()
  1041  
  1042  	s.capacity = c
  1043  
  1044  	for s.entryCnt > c {
  1045  		s.collectGarbage()
  1046  	}
  1047  }
  1048  
  1049  func (s *LDBStore) Close() {
  1050  	close(s.quit)
  1051  	s.lock.Lock()
  1052  	s.closed = true
  1053  	s.lock.Unlock()
  1054  	// force writing out current batch
  1055  	s.writeCurrentBatch()
  1056  	close(s.batchesC)
  1057  	s.db.Close()
  1058  }
  1059  
  1060  // SyncIterator(start, stop, po, f) calls f on each hash of a bin po from start to stop
  1061  func (s *LDBStore) SyncIterator(since uint64, until uint64, po uint8, f func(Address, uint64) bool) error {
  1062  	metrics.GetOrRegisterCounter("ldbstore.synciterator", nil).Inc(1)
  1063  
  1064  	sincekey := getDataKey(since, po)
  1065  	untilkey := getDataKey(until, po)
  1066  	it := s.db.NewIterator()
  1067  	defer it.Release()
  1068  
  1069  	for ok := it.Seek(sincekey); ok; ok = it.Next() {
  1070  		metrics.GetOrRegisterCounter("ldbstore.synciterator.seek", nil).Inc(1)
  1071  
  1072  		dbkey := it.Key()
  1073  		if dbkey[0] != keyData || dbkey[1] != po || bytes.Compare(untilkey, dbkey) < 0 {
  1074  			break
  1075  		}
  1076  		key := make([]byte, 32)
  1077  		val := it.Value()
  1078  		copy(key, val[:32])
  1079  		if !f(Address(key), binary.BigEndian.Uint64(dbkey[2:])) {
  1080  			break
  1081  		}
  1082  	}
  1083  	return it.Error()
  1084  }