github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/swarm/storage/netstore.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  package storage
    18  
    19  import (
    20  	"fmt"
    21  	"path/filepath"
    22  	"time"
    23  
    24  	"github.com/vntchain/go-vnt/log"
    25  )
    26  
    27  /*
    28  NetStore is a cloud storage access abstaction layer for swarm
    29  it contains the shared logic of network served chunk store/retrieval requests
    30  both local (coming from DPA api) and remote (coming from peers via bzz protocol)
    31  it implements the ChunkStore interface and embeds LocalStore
    32  
    33  It is called by the bzz protocol instances via Depo (the store/retrieve request handler)
    34  a protocol instance is running on each peer, so this is heavily parallelised.
    35  NetStore falls back to a backend (CloudStorage interface)
    36  implemented by bzz/network/forwarder. forwarder or IPFS or IPΞS
    37  */
    38  type NetStore struct {
    39  	hashfunc   SwarmHasher
    40  	localStore *LocalStore
    41  	cloud      CloudStore
    42  }
    43  
    44  // backend engine for cloud store
    45  // It can be aggregate dispatching to several parallel implementations:
    46  // bzz/network/forwarder. forwarder or IPFS or IPΞS
    47  type CloudStore interface {
    48  	Store(*Chunk)
    49  	Deliver(*Chunk)
    50  	Retrieve(*Chunk)
    51  }
    52  
    53  type StoreParams struct {
    54  	ChunkDbPath   string
    55  	DbCapacity    uint64
    56  	CacheCapacity uint
    57  	Radius        int
    58  }
    59  
    60  //create params with default values
    61  func NewDefaultStoreParams() (self *StoreParams) {
    62  	return &StoreParams{
    63  		DbCapacity:    defaultDbCapacity,
    64  		CacheCapacity: defaultCacheCapacity,
    65  		Radius:        defaultRadius,
    66  	}
    67  }
    68  
    69  //this can only finally be set after all config options (file, cmd line, env vars)
    70  //have been evaluated
    71  func (self *StoreParams) Init(path string) {
    72  	self.ChunkDbPath = filepath.Join(path, "chunks")
    73  }
    74  
    75  // netstore contructor, takes path argument that is used to initialise dbStore,
    76  // the persistent (disk) storage component of LocalStore
    77  // the second argument is the hive, the connection/logistics manager for the node
    78  func NewNetStore(hash SwarmHasher, lstore *LocalStore, cloud CloudStore, params *StoreParams) *NetStore {
    79  	return &NetStore{
    80  		hashfunc:   hash,
    81  		localStore: lstore,
    82  		cloud:      cloud,
    83  	}
    84  }
    85  
    86  var (
    87  	// timeout interval before retrieval is timed out
    88  	searchTimeout = 3 * time.Second
    89  )
    90  
    91  // store logic common to local and network chunk store requests
    92  // ~ unsafe put in localdb no check if exists no extra copy no hash validation
    93  // the chunk is forced to propagate (Cloud.Store) even if locally found!
    94  // caller needs to make sure if that is wanted
    95  func (self *NetStore) Put(entry *Chunk) {
    96  	self.localStore.Put(entry)
    97  
    98  	// handle deliveries
    99  	if entry.Req != nil {
   100  		log.Trace(fmt.Sprintf("NetStore.Put: localStore.Put %v hit existing request...delivering", entry.Key.Log()))
   101  		// closing C signals to other routines (local requests)
   102  		// that the chunk is has been retrieved
   103  		close(entry.Req.C)
   104  		// deliver the chunk to requesters upstream
   105  		go self.cloud.Deliver(entry)
   106  	} else {
   107  		log.Trace(fmt.Sprintf("NetStore.Put: localStore.Put %v stored locally", entry.Key.Log()))
   108  		// handle propagating store requests
   109  		// go self.cloud.Store(entry)
   110  		go self.cloud.Store(entry)
   111  	}
   112  }
   113  
   114  // retrieve logic common for local and network chunk retrieval requests
   115  func (self *NetStore) Get(key Key) (*Chunk, error) {
   116  	var err error
   117  	chunk, err := self.localStore.Get(key)
   118  	if err == nil {
   119  		if chunk.Req == nil {
   120  			log.Trace(fmt.Sprintf("NetStore.Get: %v found locally", key))
   121  		} else {
   122  			log.Trace(fmt.Sprintf("NetStore.Get: %v hit on an existing request", key))
   123  			// no need to launch again
   124  		}
   125  		return chunk, err
   126  	}
   127  	// no data and no request status
   128  	log.Trace(fmt.Sprintf("NetStore.Get: %v not found locally. open new request", key))
   129  	chunk = NewChunk(key, newRequestStatus(key))
   130  	self.localStore.memStore.Put(chunk)
   131  	go self.cloud.Retrieve(chunk)
   132  	return chunk, nil
   133  }
   134  
   135  // Close netstore
   136  func (self *NetStore) Close() {}