aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/storage/localstore/mode_get.go
blob: 3a69f6e9d4e2de3f79de16dd34e5d01d491f5be8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package localstore

import (
    "github.com/ethereum/go-ethereum/log"
    "github.com/ethereum/go-ethereum/swarm/shed"
    "github.com/ethereum/go-ethereum/swarm/storage"
    "github.com/syndtr/goleveldb/leveldb"
)

// ModeGet enumerates different Getter modes.
type ModeGet int

// Getter modes.
const (
    // ModeGetRequest: when accessed for retrieval
    ModeGetRequest ModeGet = iota
    // ModeGetSync: when accessed for syncing or proof of custody request
    ModeGetSync
)

// Getter provides Get method to retrieve Chunks
// from database.
type Getter struct {
    db   *DB
    mode ModeGet
}

// NewGetter returns a new Getter on database
// with a specific Mode.
func (db *DB) NewGetter(mode ModeGet) *Getter {
    return &Getter{
        mode: mode,
        db:   db,
    }
}

// Get returns a chunk from the database. If the chunk is
// not found storage.ErrChunkNotFound will be returned.
// All required indexes will be updated required by the
// Getter Mode.
func (g *Getter) Get(addr storage.Address) (chunk storage.Chunk, err error) {
    out, err := g.db.get(g.mode, addr)
    if err != nil {
        if err == leveldb.ErrNotFound {
            return nil, storage.ErrChunkNotFound
        }
        return nil, err
    }
    return storage.NewChunk(out.Address, out.Data), nil
}

// get returns Item from the retrieval index
// and updates other indexes.
func (db *DB) get(mode ModeGet, addr storage.Address) (out shed.Item, err error) {
    item := addressToItem(addr)

    out, err = db.retrievalDataIndex.Get(item)
    if err != nil {
        return out, err
    }
    switch mode {
    // update the access timestamp and gc index
    case ModeGetRequest:
        if db.updateGCSem != nil {
            // wait before creating new goroutines
            // if updateGCSem buffer id full
            db.updateGCSem <- struct{}{}
        }
        db.updateGCWG.Add(1)
        go func() {
            defer db.updateGCWG.Done()
            if db.updateGCSem != nil {
                // free a spot in updateGCSem buffer
                // for a new goroutine
                defer func() { <-db.updateGCSem }()
            }
            err := db.updateGC(out)
            if err != nil {
                log.Error("localstore update gc", "err", err)
            }
            // if gc update hook is defined, call it
            if testHookUpdateGC != nil {
                testHookUpdateGC()
            }
        }()

    // no updates to indexes
    case ModeGetSync:
    default:
        return out, ErrInvalidMode
    }
    return out, nil
}

// updateGC updates garbage collection index for
// a single item. Provided item is expected to have
// only Address and Data fields with non zero values,
// which is ensured by the get function.
func (db *DB) updateGC(item shed.Item) (err error) {
    unlock, err := db.lockAddr(item.Address)
    if err != nil {
        return err
    }
    defer unlock()

    batch := new(leveldb.Batch)

    // update accessTimeStamp in retrieve, gc

    i, err := db.retrievalAccessIndex.Get(item)
    switch err {
    case nil:
        item.AccessTimestamp = i.AccessTimestamp
    case leveldb.ErrNotFound:
        // no chunk accesses
    default:
        return err
    }
    if item.AccessTimestamp == 0 {
        // chunk is not yet synced
        // do not add it to the gc index
        return nil
    }
    // delete current entry from the gc index
    db.gcIndex.DeleteInBatch(batch, item)
    // update access timestamp
    item.AccessTimestamp = now()
    // update retrieve access index
    db.retrievalAccessIndex.PutInBatch(batch, item)
    // add new entry to gc index
    db.gcIndex.PutInBatch(batch, item)

    return db.shed.WriteBatch(batch)
}

// testHookUpdateGC is a hook that can provide
// information when a garbage collection index is updated.
var testHookUpdateGC func()