中文API手册请参考这里
nexus has a few quite easy-to-go interfaces for beginners to use. The system is based on a Raft protocol key-value storage. So although some functions may behave operating on a tree-like data structure, their are NOT.
Now all the interfaces will be listed below. All the functions are in a class named InsSDK
in the namespace galaxy::ins
.
Functions | Interfaces |
---|---|
Cluster Status | bool ShowCluster(members[OUT]) |
Put k/v Data | bool Put(key[IN], value[IN], error[OUT]) |
Get k/v Data | bool Get(key[IN], value[OUT], error[OUT]) |
Delete k/v Data | bool Delete(key[IN], error[OUT]) |
Get Range of Data | ScanResult Scan(start_key[IN], end_key[IN]) |
Value Event Listener | bool Watch(key[IN], callback[IN], context[IN], error[OUT]) |
Get Exclusive Priority on A Key | bool Lock(key[IN], error[OUT]) |
Try to Get Lock | bool TryLock(key[IN], error[OUT]) |
Release the Lock | bool UnLock(key[IN], error[OUT]) |
Login to New Namespace | bool Login(username[IN], password[IN], error[OUT]) |
Logout to Default Namespace | bool Logout(error[OUT]) |
Register a User Namespace | bool Register(username[IN], password[IN], error[OUT]) |
Get Sessin ID | string GetSessionID() |
Get User ID | string GetCurrentUserID() |
Check If Logged In | bool IsLoggedIn() |
Get Event When Session Fails | void RegisterSessionTimeout(handle_session_timeout[IN], ctx[IN]) |
Get RPC Statistics of Cluster | bool ShowStatistics(statistics[OUT]) |
String of Cluster Status | string StatusToString(status[IN]) |
String of Error Code | string ErrorToString(error[IN]) |
- Session
A session means a heartbeat kept communication between client and cluster. WhenLock
,Watch
, and user-related functions are used, a session will be automatically created. A client will send heartbeat package to cluster every 2 seconds to declare the existence of itself. And the leader of cluster will abondone the session after certain seconds without receiving any heartbeat packages. Meanwhile, the client will detect if the heartbeat succeeded, and will create another session ID and mark the session timeout if after certain seconds without sending any heartbeat packages.
The session is essential toLock
,Watch
, and user-related functions. These operations will be considered invalid after session timeout. - User
User is designed to separate data of different user in case of collision. UseRegister
to create a user. And after authentication by login, all the operations are in a unique namespace and will not affect by others. Operations will be preformed in a public default namespace without logging in.
Logged user will get a unique user ID and this ID is tied with survival of session. If session timeout, the operations will keep returning ankUnknownUser
error code. At that time a maunalLogout
operation is needed to clean the login status. - Unique User IDentifier
UUID is used to avoid cheating. All logged users get a universal unique user ID representing its current session, which will be considered invalid after logout or timeout. WatchCallback
This is a function pointer accepts(const WatchParam&, SDKError)
parameters and returns void. When the value of watched key changes, whether is modified or deleted, the user definedWatchCallback
type callback function will be called.WatchParam
stores the detail of the change, and SDKError suggests the status of operation.
NOTICE: the callback here is synchronized and should not contains time consuming operations like disk operations.WatchParam
This is a struct representing the result of a watch event. It has following members:key(string)
- watched keyvalue(string)
- changed valuedeleted(bool)
- suggests if the k/v is deletedcontext(void*)
- user defined parameter, passed whenWatch
was called
ScanResult
This is a iterator styled class returned byScan
function. Use the object as an iterator:bool Done()
- suggests if the result is exhaustedSDKError Error()
- status of current operationconst std::string Key()
- key of current dataconst std::string Value()
- value of current datavoid Next()
- moves to next data, similiar to++
operation in iterator
-
bool ShowCluster(std::vector<ClusterNodeInfo>* cluster)
Get status of every node in the cluster and stores in defined vector. Returnstrue
when success, orfalse
otherwise- Parameter:
cluster
- pointer to a vector which will be filled with node status information
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
bool Put(const std::string& key, const std::string& value, SDKError* error)
Put a key-value data to the cluster. Returnstrue
when success, orfalse
otherwise anderror
will be set- Parameter:
key
- key of the datavalue
- value of the dataerror
- status of the operation, would bekOk, kUnknownUser, kClusterDown
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
bool Get(const std::string& key, const std::string* value, SDKError* error)
Get the value of the key. Returnstrue
when success or no such key, orfalse
otherwise anderror
will be set- Parameter:
key
- key of the datavalue
- pointer to the value of the dataerror
- status of the operation, would bekOk, kNoSuchKey, kUnknownUser, kClusterDown
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
bool Delete(const std::string& key, SDKError* error)
Delete the k/v data. Returnstrue
when success, orfalse
otherwise anderror
will be set- Parameter:
key
- key of the dataerror
- status of the operation, would bekOk, kUnknownUser, kClusterDown
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
ScanResult* Scan(const std::string& start_key, const std::string& end_key)
Get a sorted range of [start_key
,end_key
) data. Returns pointer to an object ofScanResult
NOTICE: Caller should be responsible for releasing the result pointer- Parameter:
start_key
- start key of the rangeend_key
- end key of the range
- Return value: pointer to an object of
ScanResult
- stores the result of this scan operation
- Parameter:
-
bool Watch(const std::string& key, WatchCallback callback, void* context, SDKError* error)
Register callback function and get notification when key is touched. Returnstrue
when success, orfalse
otherwise anderror
will be set
NOTICE: Consider that nexus can be used to manage large scale services, the caller will get notification when the children path is touched. e.g.:/product/instance
is touched and watcher to/product
will alse get notification- Parameter:
key
- key which needs to be watchedcallback
-WatchCallback
type pointercontext
- user defined parameter and will be passed to callback functionerror
- status of the operation, would bekOk, kUnknownUser, kClusterDown
, and the callback function would getkOk, kUnknownUser
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
bool Lock(const std::string& key, SDKError* error)
Declare ownership of a key and same operation of others will be blocked until the first one release the key. Returnstrue
when success, orfalse
otherwise anderror
will be set
NOTICE: Do not write/delete a locked key, or lock an unempty key, which may lead to lock or data loss, since the cluster will actually occupy the value to record the session ID of caller.Get
can be used to check this result- Parameter:
key
- key which needs to be lockederror
- status of the operation, would bekOk, kUnknownUser, kLockFail
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
bool TryLock(const std::string& key, SDKError* error)
Try to declare ownership of a key. Will return instantly if the key is already locked. Returnstrue
when success, orfalse
otherwise anderror
will be set- Parameter:
key
- key which needs to be lockederror
- status of the operation, would bekOk, kUnknownUser, kLockFail
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
bool UnLock(const std::string& key, SDKError* error)
Release the ownership of a key. Returnstrue
when success, orfalse
otherwise anderror
will be set
NOTICE: This operation should be used by the same session with locking, and others will get failure. To deprive a lock, tryDelete
it- Parameter:
key
- key which needs to be unlockederror
- status of the operation, would bekOk, kUnknownUser, kClusterDown
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
bool Login(const std::string& username, const std::string& password, SDKError* error)
Login and act as new user. Returnstrue
when success, orfalse
otherwise anderror
will be set- Parameter:
username
- username of the new userpassword
- password of the new usererror
- status of the operation, would bekOk, kUnknownUser, kUserExists, kPasswordError, kClusterDown
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
bool Logout(SDKError* error)
Logout and return to default user. Should be called after user session timeout. Returnstrue
when success, orfalse
otherwise anderror
will be set- Parameter:
error
- status of the operation, would bekOk, kUnknownUser, kClusterDown
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
bool Register(const std::string& username, const std::string& password, SDKError* error)
Register a new user and can be use in mulitple session. Returnstrue
when success, orfalse
otherwise anderror
will be set- Parameter:
username
- username of the new user, should not be emptypassword
- password of the new user, should not be emptyerror
- status of the operation, would bekOk, kUserExists, kPasswordError, kClusterDown
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
std::string GetSessionID()
Get current session ID generated when session started. ID will changed if the session times out. Returns the ID as string- Parameter: None
- Return value:
string
- ID of current session
-
std::string GetCurrentUserID()
Get current user ID generated by the cluster at login time. Returns the ID as string- Parameter: None
- Return value:
string
- current user ID, would be empty if user is not logged in
-
bool IsLoggedIn()
Returns if the user is logged in- Parameter: None
- Return value:
bool
- suggests if the user is logged in
-
void RegisterSessionTimeout(void (*handle_session_timeout)(void*), void* ctx)
Register a callback function and get notification when session timeout. Returns void- Parameter:
handle_session_timeout
- pointer to a function which receivesvoid*
as parameter and returns voidctx
- user defined parameter and will be passed to callback function
- Return value: None
- Parameter:
-
bool ShowStatistics(std::vector<NodeStatInfo* statistics)
Get RPC statistics of every node in the cluster and stores in defined vector. Returnstrue
when success, orfalse
otherwise- Parameter:
cluster
- pointer to a vector which will be filled with RPC statistics
- Return value:
bool
- suggests if the operation is succeeded
- Parameter:
-
static std::string StatusToString(int32_t status)
Static function to convertint
type status to human readable string: Follower, Candidate, Leader, and Offline. Returns status as string- Parameter:
status
- int type status
- Return value:
string
- status string
- Parameter:
-
static std::string ErrorToString(SDKError error)
Static function to convertSDKError
type error to human readable string. Returns error as string- Parameter:
error
- error returned by functions above
- Return value:
string
- error string
- Parameter: