-
Notifications
You must be signed in to change notification settings - Fork 99
/
iterator.go
91 lines (77 loc) · 2.73 KB
/
iterator.go
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
package api
import (
"fmt"
"sync"
"github.com/CosmWasm/wasmvm/v2/types"
)
// frame stores all Iterators for one contract call
type frame []types.Iterator
// iteratorFrames contains one frame for each contract call, indexed by contract call ID.
var (
iteratorFrames = make(map[uint64]frame)
iteratorFramesMutex sync.Mutex
)
// this is a global counter for creating call IDs
var (
latestCallID uint64
latestCallIDMutex sync.Mutex
)
// startCall is called at the beginning of a contract call to create a new frame in iteratorFrames.
// It updates latestCallID for generating a new call ID.
func startCall() uint64 {
latestCallIDMutex.Lock()
defer latestCallIDMutex.Unlock()
latestCallID += 1
return latestCallID
}
// removeFrame removes the frame with for the given call ID.
// The result can be nil when the frame is not initialized,
// i.e. when startCall() is called but no iterator is stored.
func removeFrame(callID uint64) frame {
iteratorFramesMutex.Lock()
defer iteratorFramesMutex.Unlock()
remove := iteratorFrames[callID]
delete(iteratorFrames, callID)
return remove
}
// endCall is called at the end of a contract call to remove one item the iteratorFrames
func endCall(callID uint64) {
// we pull removeFrame in another function so we don't hold the mutex while cleaning up the removed frame
remove := removeFrame(callID)
// free all iterators in the frame when we release it
for _, iter := range remove {
iter.Close()
}
}
// storeIterator will add this to the end of the frame for the given ID and return a reference to it.
// We start counting with 1, so the 0 value is flagged as an error. This means we must
// remember to do idx-1 when retrieving
func storeIterator(callID uint64, it types.Iterator, frameLenLimit int) (uint64, error) {
iteratorFramesMutex.Lock()
defer iteratorFramesMutex.Unlock()
old_frame_len := len(iteratorFrames[callID])
if old_frame_len >= frameLenLimit {
return 0, fmt.Errorf("Reached iterator limit (%d)", frameLenLimit)
}
// store at array position `old_frame_len`
iteratorFrames[callID] = append(iteratorFrames[callID], it)
new_index := old_frame_len + 1
return uint64(new_index), nil
}
// retrieveIterator will recover an iterator based on index. This ensures it will not be garbage collected.
// We start counting with 1, in storeIterator so the 0 value is flagged as an error. This means we must
// remember to do idx-1 when retrieving
func retrieveIterator(callID uint64, index uint64) types.Iterator {
iteratorFramesMutex.Lock()
defer iteratorFramesMutex.Unlock()
myFrame := iteratorFrames[callID]
if myFrame == nil {
return nil
}
posInFrame := int(index) - 1
if posInFrame < 0 || posInFrame >= len(myFrame) {
// index out of range
return nil
}
return myFrame[posInFrame]
}