-
Notifications
You must be signed in to change notification settings - Fork 8
/
buckets.go
123 lines (104 loc) · 2.97 KB
/
buckets.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
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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package main
// #include "uplink_definitions.h"
import "C"
import (
"unsafe"
"storj.io/uplink"
)
// BucketIterator is an iterator over buckets.
type BucketIterator struct {
scope
iterator *uplink.BucketIterator
initialError error
}
// uplink_list_buckets lists buckets.
//
//export uplink_list_buckets
func uplink_list_buckets(project *C.UplinkProject, options *C.UplinkListBucketsOptions) *C.UplinkBucketIterator {
if project == nil {
return (*C.UplinkBucketIterator)(mallocHandle(universe.Add(&BucketIterator{
initialError: ErrNull.New("project"),
})))
}
proj, ok := universe.Get(project._handle).(*Project)
if !ok {
return (*C.UplinkBucketIterator)(mallocHandle(universe.Add(&BucketIterator{
initialError: ErrInvalidHandle.New("project"),
})))
}
opts := &uplink.ListBucketsOptions{}
if options != nil {
opts.Cursor = C.GoString(options.cursor)
}
scope := proj.scope.child()
iterator := proj.ListBuckets(scope.ctx, opts)
return (*C.UplinkBucketIterator)(mallocHandle(universe.Add(&BucketIterator{
scope: scope,
iterator: iterator,
})))
}
// uplink_bucket_iterator_next prepares next Bucket for reading.
//
// It returns false if the end of the iteration is reached and there are no more buckets, or if there is an error.
//
//export uplink_bucket_iterator_next
func uplink_bucket_iterator_next(iterator *C.UplinkBucketIterator) C.bool {
if iterator == nil {
return C.bool(false)
}
iter, ok := universe.Get(iterator._handle).(*BucketIterator)
if !ok {
return C.bool(false)
}
if iter.initialError != nil {
return C.bool(false)
}
return C.bool(iter.iterator.Next())
}
// uplink_bucket_iterator_err returns error, if one happened during iteration.
//
//export uplink_bucket_iterator_err
func uplink_bucket_iterator_err(iterator *C.UplinkBucketIterator) *C.UplinkError {
if iterator == nil {
return mallocError(ErrNull.New("iterator"))
}
iter, ok := universe.Get(iterator._handle).(*BucketIterator)
if !ok {
return mallocError(ErrInvalidHandle.New("iterator"))
}
if iter.initialError != nil {
return mallocError(iter.initialError)
}
return mallocError(iter.iterator.Err())
}
// uplink_bucket_iterator_item returns the current bucket in the iterator.
//
//export uplink_bucket_iterator_item
func uplink_bucket_iterator_item(iterator *C.UplinkBucketIterator) *C.UplinkBucket {
if iterator == nil {
return nil
}
iter, ok := universe.Get(iterator._handle).(*BucketIterator)
if !ok {
return nil
}
return mallocBucket(iter.iterator.Item())
}
// uplink_free_bucket_iterator frees memory associated with the BucketIterator.
//
//export uplink_free_bucket_iterator
func uplink_free_bucket_iterator(iterator *C.UplinkBucketIterator) {
if iterator == nil {
return
}
defer C.free(unsafe.Pointer(iterator))
defer universe.Del(iterator._handle)
iter, ok := universe.Get(iterator._handle).(*BucketIterator)
if ok {
if iter.scope.cancel != nil {
iter.scope.cancel()
}
}
}