github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/storage/mru/resource.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:50</date>
    10  //</624342683527155712>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package mru
    29  
    30  import (
    31  	"bytes"
    32  	"context"
    33  	"time"
    34  
    35  	"github.com/ethereum/go-ethereum/swarm/storage"
    36  )
    37  
    38  const (
    39  	defaultStoreTimeout    = 4000 * time.Millisecond
    40  	hasherCount            = 8
    41  	resourceHashAlgorithm  = storage.SHA3Hash
    42  	defaultRetrieveTimeout = 100 * time.Millisecond
    43  )
    44  
    45  //
    46  type resource struct {
    47  	resourceUpdate
    48  	ResourceMetadata
    49  	*bytes.Reader
    50  	lastKey storage.Address
    51  	updated time.Time
    52  }
    53  
    54  func (r *resource) Context() context.Context {
    55  	return context.TODO()
    56  }
    57  
    58  //
    59  func (r *resource) isSynced() bool {
    60  	return !r.updated.IsZero()
    61  }
    62  
    63  //
    64  func (r *resource) Size(ctx context.Context, _ chan bool) (int64, error) {
    65  	if !r.isSynced() {
    66  		return 0, NewError(ErrNotSynced, "Not synced")
    67  	}
    68  	return int64(len(r.resourceUpdate.data)), nil
    69  }
    70  
    71  //
    72  func (r *resource) Name() string {
    73  	return r.ResourceMetadata.Name
    74  }
    75  
    76  //
    77  func getNextPeriod(start uint64, current uint64, frequency uint64) (uint32, error) {
    78  	if current < start {
    79  		return 0, NewErrorf(ErrInvalidValue, "given current time value %d < start time %d", current, start)
    80  	}
    81  	if frequency == 0 {
    82  		return 0, NewError(ErrInvalidValue, "frequency is 0")
    83  	}
    84  	timeDiff := current - start
    85  	period := timeDiff / frequency
    86  	return uint32(period + 1), nil
    87  }
    88