-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrados_io.go
236 lines (201 loc) · 5.9 KB
/
rados_io.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package gorados
/*
#cgo LDFLAGS: -lrados
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "rados/librados.h"
*/
import "C"
import (
"errors"
"unsafe"
"io"
"log"
)
type RadosIoCtx struct{
ctx *C.rados_ioctx_t
}
func (r *Rados) IoCtxCreate(poolname string) (*RadosIoCtx, error) {
cpoolname := C.CString(poolname)
defer func(){
C.free(unsafe.Pointer(cpoolname))
}()
var ctx C.rados_ioctx_t
cerr := C.rados_ioctx_create(*r.cluster, cpoolname, &ctx)
if cerr < 0 {
return nil, errors.New("create io contxt failed:" + C.GoString(C.strerror(-cerr)))
}
return &RadosIoCtx{&ctx}, nil
}
func (r *RadosIoCtx) Destroy() {
C.rados_ioctx_destroy(*r.ctx)
}
type RadosObjectStat struct{
Size uint64
Timestamp uint64
}
func (r *RadosIoCtx) Stat(oid string) (*RadosObjectStat, error){
var size C.uint64_t
var time_t C.time_t
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(coid))
}()
cerr := C.rados_stat(*r.ctx, coid, &size, &time_t)
if cerr < 0 {
//log.Println("Get object stat failed, error is", C.GoString(C.strerror(-cerr)))
return nil, errors.New("Get object stat failed")
}
return &RadosObjectStat{uint64(size), uint64(C.uint64_t(time_t))}, nil
}
func (r *RadosIoCtx) PoolSetAuid(uid uint64) error {
cerr := C.rados_ioctx_pool_set_auid(*r.ctx, C.uint64_t(uid))
if cerr < 0 {
return errors.New("set auid failed")
}
return nil
}
func (r *RadosIoCtx) PoolGetAuid() (uint64, error) {
var uid C.uint64_t
cerr := C.rados_ioctx_pool_get_auid(*r.ctx, &uid)
if cerr < 0 {
return 0, errors.New("get auid failed")
}
return uint64(uid), nil
}
func (r *RadosIoCtx) GetId() uint64 {
id := C.rados_ioctx_get_id(*r.ctx)
return uint64(id)
}
func (r *RadosIoCtx) GetPoolName() (string, error) {
var buf [MAX_NAME_LEN]C.char
cerr := C.rados_ioctx_get_pool_name(*r.ctx, &buf[0], MAX_NAME_LEN-1)
if cerr < 0 {
return "", errors.New("get pool name failed")
}
return C.GoString(&buf[0]), nil
}
func (r *RadosIoCtx) Write(oid string, bin []byte, offset uint64) error {
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(coid))
}()
originLen := len(bin)
if originLen == 0 {
bin = []byte{0}
}
cerr := C.rados_write(*r.ctx, coid, (*C.char)(unsafe.Pointer(&bin[0])), C.size_t(originLen), C.uint64_t(offset))
if cerr < 0 {
return errors.New("write data failed")
}
return nil
}
func (r *RadosIoCtx) WriteFull(oid string, bin []byte) error {
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(coid))
}()
cerr := C.rados_write_full(*r.ctx, coid, (*C.char)(unsafe.Pointer(&bin[0])), C.size_t(len(bin)))
if cerr < 0 {
return errors.New("write full data failed")
}
return nil
}
func (r *RadosIoCtx) Append(oid string, bin []byte) error {
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(coid))
}()
cerr := C.rados_append(*r.ctx, coid, (*C.char)(unsafe.Pointer(&bin[0])), C.size_t(len(bin)))
if cerr < 0 {
return errors.New("append data failed")
}
return nil
}
func (r *RadosIoCtx) Read(oid string, length, offset uint64) ([]byte, error) {
var buf = make([]byte, length)
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(coid))
}()
cerr := C.rados_read(*r.ctx, coid, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(length), C.uint64_t(offset))
if cerr < 0 {
return nil, errors.New("read data failed")
}
return buf, nil
}
func (r *RadosIoCtx) ReadRaw(oid string, length, offset uint64, buf unsafe.Pointer) int {
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(coid))
}()
cerr := C.rados_read(*r.ctx, coid, (*C.char)(buf), C.size_t(length), C.uint64_t(offset))
return int(cerr)
}
func (r *RadosIoCtx) Remove(oid string) error {
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(coid))
}()
cerr := C.rados_remove(*r.ctx, coid)
if cerr < 0 {
return errors.New("remove object failed")
}
return nil
}
func (r *RadosIoCtx) Trunc(oid string, length uint64) error {
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(coid))
}()
cerr := C.rados_trunc(*r.ctx, coid, C.uint64_t(length))
if cerr < 0 {
return errors.New("resize object failed")
}
return nil
}
type RadosPoolStat struct {
num_bytes, num_kb, num_objects, num_object_clones, num_object_copies uint64
}
func (r *RadosIoCtx) PoolStat() (*RadosPoolStat, error) {
var pool_stat C.struct_rados_pool_stat_t
cerr := C.rados_ioctx_pool_stat(*r.ctx, &pool_stat)
if cerr < 0 {
return nil, errors.New("Get pool status failed")
}
return &RadosPoolStat{
num_bytes: uint64(pool_stat.num_bytes),
num_kb: uint64(pool_stat.num_kb),
num_objects: uint64(pool_stat.num_objects),
num_object_clones: uint64(pool_stat.num_object_clones),
num_object_copies: uint64(pool_stat.num_object_copies)}, nil
}
func (r *RadosIoCtx) ObjectsListOpen() (*RadosListCtx, error){
var list_ctx C.rados_list_ctx_t
cerr := C.rados_objects_list_open(*r.ctx, &list_ctx)
if cerr < 0 {
return nil, errors.New("list object failed")
}
return &RadosListCtx{&list_ctx}, nil
}
type RadosListCtx struct{
list_ctx *C.rados_list_ctx_t
}
func (ctx *RadosListCtx) Next() (string, error) {
var buf *C.char
cerr := C.rados_objects_list_next(*ctx.list_ctx, &buf, nil)
if cerr == -C.ENOENT {
log.Println("Next failed")
return "", io.EOF
}
if cerr < 0 {
log.Println("Next failed")
return "", errors.New("next failed")
}
//log.Println(C.GoString(buf))
return C.GoString(buf), nil
}
func (ctx *RadosListCtx) Close() {
C.rados_objects_list_close(*ctx.list_ctx)
}