github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/swarm/storage/mru/resource.go (about)

     1  // Copyright 2018 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 mru
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"time"
    23  
    24  	"github.com/FusionFoundation/efsn/swarm/storage"
    25  )
    26  
    27  const (
    28  	defaultStoreTimeout    = 4000 * time.Millisecond
    29  	hasherCount            = 8
    30  	resourceHashAlgorithm  = storage.SHA3Hash
    31  	defaultRetrieveTimeout = 100 * time.Millisecond
    32  )
    33  
    34  // resource caches resource data and the metadata of its root chunk.
    35  type resource struct {
    36  	resourceUpdate
    37  	ResourceMetadata
    38  	*bytes.Reader
    39  	lastKey storage.Address
    40  	updated time.Time
    41  }
    42  
    43  func (r *resource) Context() context.Context {
    44  	return context.TODO()
    45  }
    46  
    47  // TODO Expire content after a defined period (to force resync)
    48  func (r *resource) isSynced() bool {
    49  	return !r.updated.IsZero()
    50  }
    51  
    52  // implements storage.LazySectionReader
    53  func (r *resource) Size(ctx context.Context, _ chan bool) (int64, error) {
    54  	if !r.isSynced() {
    55  		return 0, NewError(ErrNotSynced, "Not synced")
    56  	}
    57  	return int64(len(r.resourceUpdate.data)), nil
    58  }
    59  
    60  //returns the resource's human-readable name
    61  func (r *resource) Name() string {
    62  	return r.ResourceMetadata.Name
    63  }
    64  
    65  // Helper function to calculate the next update period number from the current time, start time and frequency
    66  func getNextPeriod(start uint64, current uint64, frequency uint64) (uint32, error) {
    67  	if current < start {
    68  		return 0, NewErrorf(ErrInvalidValue, "given current time value %d < start time %d", current, start)
    69  	}
    70  	if frequency == 0 {
    71  		return 0, NewError(ErrInvalidValue, "frequency is 0")
    72  	}
    73  	timeDiff := current - start
    74  	period := timeDiff / frequency
    75  	return uint32(period + 1), nil
    76  }