2014-05-24 17:36:12 +00:00
|
|
|
package rados
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -lrados
|
2015-07-24 03:23:00 +00:00
|
|
|
// #include <errno.h>
|
2014-05-24 17:36:12 +00:00
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include <rados/librados.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2019-11-26 09:52:37 +00:00
|
|
|
"runtime"
|
2015-02-22 10:54:06 +00:00
|
|
|
"unsafe"
|
2014-05-24 17:36:12 +00:00
|
|
|
)
|
|
|
|
|
2020-01-28 15:00:52 +00:00
|
|
|
const (
|
|
|
|
// AllNamespaces is used to reset a selected namespace to all
|
|
|
|
// namespaces. See the IOContext SetNamespace function.
|
|
|
|
AllNamespaces = C.LIBRADOS_ALL_NSPACES
|
2020-01-22 19:28:26 +00:00
|
|
|
|
2020-01-28 15:00:52 +00:00
|
|
|
// FIXME: for backwards compatibility
|
|
|
|
|
|
|
|
// RadosAllNamespaces is used to reset a selected namespace to all
|
|
|
|
// namespaces. See the IOContext SetNamespace function.
|
|
|
|
//
|
|
|
|
// Deprecated: use AllNamespaces instead
|
|
|
|
RadosAllNamespaces = AllNamespaces
|
2020-01-22 19:28:26 +00:00
|
|
|
)
|
2015-07-20 04:15:40 +00:00
|
|
|
|
2014-05-24 18:42:18 +00:00
|
|
|
// Version returns the major, minor, and patch components of the version of
|
|
|
|
// the RADOS library linked against.
|
2014-05-24 17:36:12 +00:00
|
|
|
func Version() (int, int, int) {
|
2015-02-11 21:21:05 +00:00
|
|
|
var c_major, c_minor, c_patch C.int
|
|
|
|
C.rados_version(&c_major, &c_minor, &c_patch)
|
|
|
|
return int(c_major), int(c_minor), int(c_patch)
|
2014-05-24 17:36:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:23:18 +00:00
|
|
|
func makeConn() *Conn {
|
|
|
|
return &Conn{connected: false}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConn(user *C.char) (*Conn, error) {
|
|
|
|
conn := makeConn()
|
|
|
|
ret := C.rados_create(&conn.cluster, user)
|
2014-05-24 17:36:12 +00:00
|
|
|
|
2019-11-26 09:52:37 +00:00
|
|
|
if ret != 0 {
|
2020-03-30 21:03:52 +00:00
|
|
|
return nil, getError(ret)
|
2015-02-11 21:21:05 +00:00
|
|
|
}
|
2019-11-26 09:52:37 +00:00
|
|
|
|
|
|
|
runtime.SetFinalizer(conn, freeConn)
|
|
|
|
return conn, nil
|
2014-05-24 17:36:12 +00:00
|
|
|
}
|
2015-02-22 10:54:06 +00:00
|
|
|
|
2018-07-28 21:23:18 +00:00
|
|
|
// NewConn creates a new connection object. It returns the connection and an
|
|
|
|
// error, if any.
|
|
|
|
func NewConn() (*Conn, error) {
|
|
|
|
return newConn(nil)
|
|
|
|
}
|
|
|
|
|
2015-02-22 10:54:06 +00:00
|
|
|
// NewConnWithUser creates a new connection object with a custom username.
|
|
|
|
// It returns the connection and an error, if any.
|
|
|
|
func NewConnWithUser(user string) (*Conn, error) {
|
|
|
|
c_user := C.CString(user)
|
|
|
|
defer C.free(unsafe.Pointer(c_user))
|
2018-07-28 21:23:18 +00:00
|
|
|
return newConn(c_user)
|
2015-02-22 10:54:06 +00:00
|
|
|
}
|
2015-06-16 22:32:09 +00:00
|
|
|
|
2015-07-08 08:34:54 +00:00
|
|
|
// NewConnWithClusterAndUser creates a new connection object for a specific cluster and username.
|
2015-06-16 22:32:09 +00:00
|
|
|
// It returns the connection and an error, if any.
|
|
|
|
func NewConnWithClusterAndUser(clusterName string, userName string) (*Conn, error) {
|
|
|
|
c_cluster_name := C.CString(clusterName)
|
|
|
|
defer C.free(unsafe.Pointer(c_cluster_name))
|
|
|
|
|
|
|
|
c_name := C.CString(userName)
|
|
|
|
defer C.free(unsafe.Pointer(c_name))
|
|
|
|
|
2018-07-28 21:23:18 +00:00
|
|
|
conn := makeConn()
|
2015-06-16 22:32:09 +00:00
|
|
|
ret := C.rados_create2(&conn.cluster, c_cluster_name, c_name, 0)
|
2019-11-26 09:52:37 +00:00
|
|
|
if ret != 0 {
|
2020-03-30 21:03:52 +00:00
|
|
|
return nil, getError(ret)
|
2015-06-16 22:32:09 +00:00
|
|
|
}
|
2019-11-26 09:52:37 +00:00
|
|
|
|
|
|
|
runtime.SetFinalizer(conn, freeConn)
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// freeConn releases resources that are allocated while configuring the
|
|
|
|
// connection to the cluster. rados_shutdown() should only be needed after a
|
|
|
|
// successful call to rados_connect(), however if the connection has been
|
|
|
|
// configured with non-default parameters, some of the parameters may be
|
|
|
|
// allocated before connecting. rados_shutdown() will free the allocated
|
|
|
|
// resources, even if there has not been a connection yet.
|
|
|
|
//
|
|
|
|
// This function is setup as a destructor/finalizer when rados_create() is
|
|
|
|
// called.
|
|
|
|
func freeConn(conn *Conn) {
|
|
|
|
if conn.cluster != nil {
|
|
|
|
C.rados_shutdown(conn.cluster)
|
|
|
|
// prevent calling rados_shutdown() more than once
|
|
|
|
conn.cluster = nil
|
|
|
|
}
|
2015-06-16 22:32:09 +00:00
|
|
|
}
|