Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
src/modules/teodb.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "config.h"
#include <pbl.h>
#include "teodb.h"
#include "conf.h"
#include "utils/utils.h"

Macros

#define get_file_path(namespace)
 

Functions

ksnTDBClassksnTDBinit (void *ke)
 Initialize PBL KeyFile module. More...
 
void ksnTDBdestroy (ksnTDBClass *kf)
 Destroy PBL KeyFile module. More...
 
void ksnTDBnamespaceSet (ksnTDBClass *kf, const char *namespace)
 Set current namespace. More...
 
char * ksnTDBnamespaceGet (ksnTDBClass *kf)
 Get current namespace. More...
 
void ksnTDBnamespaceRemove (ksnTDBClass *kf, const char *namespace)
 Remove namespace and all it contains. More...
 
int ksnTDBflush (ksnTDBClass *kf)
 Flush a key file. More...
 
void * ksnTDBgetStr (ksnTDBClass *kf, const char *key, size_t *data_len)
 Get data by string key from default namespace. More...
 
void * ksnTDBget (ksnTDBClass *kf, const void *key, size_t key_len, size_t *data_len)
 Get data by string key from default namespace. More...
 
int ksnTDBsetStr (ksnTDBClass *kf, const char *key, void *data, size_t data_len)
 Add (insert or update) data by string key to default namespace. More...
 
int ksnTDBset (ksnTDBClass *kf, const void *key, size_t key_len, void *data, size_t data_len)
 Add (insert or update) data by key to default namespace. More...
 
int ksnTDBdeleteStr (ksnTDBClass *kf, const char *key)
 Delete all records with string key. More...
 
int ksnTDBdelete (ksnTDBClass *kf, const void *key, size_t key_len)
 Delete all records with key. More...
 
void * ksnTDBgetNsStr (ksnTDBClass *kf, const char *namespace, const char *key, size_t *data_len)
 Get data by string key from namespace. More...
 
void * ksnTDBgetNs (ksnTDBClass *kf, const char *namespace, const void *key, size_t key_len, size_t *data_len)
 Get data by key from namespace. More...
 
int ksnTDBsetNsStr (ksnTDBClass *kf, const char *namespace, const char *key, void *data, size_t data_len)
 Add (insert or update) data by string key to namespace. More...
 
int ksnTDBsetNs (ksnTDBClass *kf, const char *namespace, const void *key, size_t key_len, void *data, size_t data_len)
 Add (insert or update) data by key to namespace. More...
 
int ksnTDBdeleteNsStr (ksnTDBClass *kf, const char *namespace, const char *key)
 Delete all records with key. More...
 
int ksnTDBdeleteNs (ksnTDBClass *kf, const char *namespace, const void *key, size_t key_len)
 Delete all records with key. More...
 
int ksnTDBkeyList (ksnTDBClass *kf, const char *key, ksnet_stringArr *argv)
 Get list of keys //. More...
 

Detailed Description

Author
Kirill Scherba kiril.nosp@m.l@sc.nosp@m.herba.nosp@m..ru

Teonet database module based at PBL KeyFile

Created on August 20, 2015, 4:33 PM

Macro Definition Documentation

#define get_file_path (   namespace)
Value:
char path[PATH_MAX], *data_path = (char*)getDataPath(); \
mkdir(data_path, 0755); \
strncpy(path, data_path, PATH_MAX); \
strncat(path, "/", PATH_MAX - strlen(path) - 1); \
strncat(path, namespace, PATH_MAX - strlen(path) - 1)
char * getDataPath(void)
Get path to teonet data folder.
Definition: utils.c:416

Referenced by ksnTDBnamespaceRemove(), and ksnTDBnamespaceSet().

Function Documentation

int ksnTDBdelete ( ksnTDBClass kf,
const void *  key,
size_t  key_len 
)

Delete all records with key.

Parameters
kfPointer to ksnTDBClass
keyBinary key
key_lenKey length
Returns
0: call went OK; != 0 if key not found or some error occurred

References data, free(), ksnTDBClass::k, and ksnTDBget().

Referenced by event_cb(), ksnTDBdeleteNs(), and ksnTDBdeleteStr().

int ksnTDBdeleteNs ( ksnTDBClass kf,
const char *  namespace,
const void *  key,
size_t  key_len 
)

Delete all records with key.

Parameters
kfPointer to ksnTDBClass
namespaceString with namespace
keyBinary key
key_lenKey length
Returns
0: call went OK; != 0 if key not found or some error occurred

References free(), ksnTDBdelete(), ksnTDBnamespaceGet(), and ksnTDBnamespaceSet().

Referenced by ksnTDBdeleteNsStr().

int ksnTDBdeleteNsStr ( ksnTDBClass kf,
const char *  namespace,
const char *  key 
)
inline

Delete all records with key.

Parameters
kfPointer to ksnTDBClass
namespaceString with namespace
keyString with key
Returns
0: call went OK; != 0 if key not found or some error occurred

References ksnTDBdeleteNs().

Referenced by test_3_4().

int ksnTDBdeleteStr ( ksnTDBClass kf,
const char *  key 
)
inline

Delete all records with string key.

Parameters
kfPointer to ksnTDBClass
keyString with key
Returns
0: call went OK; != 0 if key not found or some error occurred

References ksnTDBdelete().

Referenced by event_cb(), and test_3_3().

void ksnTDBdestroy ( ksnTDBClass kf)
inline

Destroy PBL KeyFile module.

Parameters
kfPointer to ksnTDBClass

References free(), and ksnTDBnamespaceSet().

Referenced by modules_destroy(), test_3_1(), test_3_2(), test_3_3(), test_3_4(), test_3_5(), and test_3_template().

int ksnTDBflush ( ksnTDBClass kf)
inline

Flush a key file.

Parameters
kf
Returns

References ksnTDBClass::k.

void* ksnTDBget ( ksnTDBClass kf,
const void *  key,
size_t  key_len,
size_t *  data_len 
)

Get data by string key from default namespace.

Parameters
kfPointer to ksnTDBClass
keyString with key
key_lenKey length
data_len[out] Length of data
Returns
Pointer to data with data_len length or NULL if not found, should be free after use

References data, ksnTDBClass::k, KSN_BUFFER_SM_SIZE, and malloc().

Referenced by event_cb(), ksnTDBdelete(), ksnTDBgetNs(), and ksnTDBgetStr().

void* ksnTDBgetNs ( ksnTDBClass kf,
const char *  namespace,
const void *  key,
size_t  key_len,
size_t *  data_len 
)

Get data by key from namespace.

Parameters
kfPointer to ksnTDBClass
namespaceString with namespace
keyBinary key
key_lenKey length
data_len[out] Data length
Returns

References data, free(), ksnTDBget(), ksnTDBnamespaceGet(), and ksnTDBnamespaceSet().

Referenced by ksnTDBgetNsStr().

void* ksnTDBgetNsStr ( ksnTDBClass kf,
const char *  namespace,
const char *  key,
size_t *  data_len 
)
inline

Get data by string key from namespace.

Parameters
kfPointer to ksnTDBClass
namespaceString with namespace
keyString with key
data_len[out] Data length
Returns

References ksnTDBgetNs().

Referenced by test_3_4().

void* ksnTDBgetStr ( ksnTDBClass kf,
const char *  key,
size_t *  data_len 
)
inline

Get data by string key from default namespace.

Parameters
kfPointer to ksnTDBClass
keyString with key
data_len[out] Length of data
Returns
Pointer to data with data_len length or NULL if not found, should be free after use

References ksnTDBget().

Referenced by event_cb(), test_3_2(), and test_3_3().

ksnTDBClass* ksnTDBinit ( void *  ke)

Initialize PBL KeyFile module.

Parameters
kePointer to ksnTDBClass
Returns

References ksnTDBClass::defNameSpace, ksnTDBClass::k, ksnTDBClass::ke, ke, and malloc().

Referenced by modules_init(), test_3_1(), test_3_2(), test_3_3(), test_3_4(), test_3_5(), and test_3_template().

int ksnTDBkeyList ( ksnTDBClass kf,
const char *  key,
ksnet_stringArr argv 
)

Get list of keys //.

Todo:
Add parameters: From - from key number, To - to key number
Parameters
kfPointer to ksnTDBClass
key
argvPointer to ksnet_stringArr
Returns
Number of

References ksnTDBClass::k, KSN_BUFFER_SM_SIZE, and ksnet_stringArrAdd().

Referenced by event_cb(), and test_3_5().

char* ksnTDBnamespaceGet ( ksnTDBClass kf)
inline

Get current namespace.

Parameters
kfPointer to ksnTDBClass
Returns
Return default namespace or NULL if namespace not set, should be free after use

References ksnTDBClass::defNameSpace.

Referenced by ksnTDBdeleteNs(), ksnTDBgetNs(), and ksnTDBsetNs().

void ksnTDBnamespaceRemove ( ksnTDBClass kf,
const char *  namespace 
)

Remove namespace and all it contains.

Parameters
kfPointer to ksnTDBClass
namespaceString with namespace

References get_file_path, and ksnTDBnamespaceSet().

Referenced by test_3_2(), test_3_3(), test_3_4(), and test_3_5().

void ksnTDBnamespaceSet ( ksnTDBClass kf,
const char *  namespace 
)

Set current namespace.

Set namespace to use in ksnTdbGet, ksnTdbSet and ksnTdbDelete functions to get, set or delete data without select namespace.

Parameters
kfPointer to ksnTDBClass
namespaceString with namespace

References ksnTDBClass::defNameSpace, free(), get_file_path, and ksnTDBClass::k.

Referenced by event_cb(), ksnTDBdeleteNs(), ksnTDBdestroy(), ksnTDBgetNs(), ksnTDBnamespaceRemove(), ksnTDBsetNs(), test_3_2(), test_3_3(), and test_3_5().

int ksnTDBset ( ksnTDBClass kf,
const void *  key,
size_t  key_len,
void *  data,
size_t  data_len 
)

Add (insert or update) data by key to default namespace.

Parameters
kfPointer to ksnTDBClass
keyBinary key
key_lenKey length
dataPointer to data
data_lenData length
Returns
0: call went OK; or an error if != 0

References ksnTDBClass::k, and KSN_BUFFER_SM_SIZE.

Referenced by event_cb(), ksnTDBsetNs(), and ksnTDBsetStr().

int ksnTDBsetNs ( ksnTDBClass kf,
const char *  namespace,
const void *  key,
size_t  key_len,
void *  data,
size_t  data_len 
)

Add (insert or update) data by key to namespace.

Parameters
kfPointer to ksnTDBClass
namespaceString with namespace
keyBinary key
key_lenKey length
dataPointer to data
data_len[out] Data length
Returns

References free(), ksnTDBnamespaceGet(), ksnTDBnamespaceSet(), and ksnTDBset().

Referenced by ksnTDBsetNsStr().

int ksnTDBsetNsStr ( ksnTDBClass kf,
const char *  namespace,
const char *  key,
void *  data,
size_t  data_len 
)
inline

Add (insert or update) data by string key to namespace.

Parameters
kfPointer to ksnTDBClass
namespaceString with namespace
keyString with key
dataPointer to data
data_len[out] Data length
Returns

References ksnTDBsetNs().

Referenced by test_3_4().

int ksnTDBsetStr ( ksnTDBClass kf,
const char *  key,
void *  data,
size_t  data_len 
)
inline

Add (insert or update) data by string key to default namespace.

Parameters
kfPointer to ksnTDBClass
keyString with key
dataPointer to data
data_lenData length
Returns
0: call went OK; or an error if != 0

References ksnTDBset().

Referenced by event_cb(), test_3_2(), test_3_3(), and test_3_5().