author | Alberto Bertogli
<albertito@blitiri.com.ar> 2010-04-18 15:40:04 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2010-04-19 03:35:53 UTC |
parent | b55affe6214f3d91ac9588c8ddf2444a48ad2e21 |
.gitignore | +3 | -1 |
libnmdb/Makefile | +11 | -8 |
libnmdb/doxygen/Doxyfile.base.in | +204 | -0 |
libnmdb/doxygen/Doxyfile.public | +8 | -0 |
libnmdb/doxygen/Makefile | +30 | -0 |
libnmdb/doxygen/groups.doxy | +12 | -0 |
libnmdb/internal.h | +0 | -29 |
libnmdb/internal.h.in | +83 | -0 |
libnmdb/libnmdb.c | +8 | -1 |
libnmdb/nmdb.h | +282 | -0 |
libnmdb/nmdb.skel.h | +0 | -101 |
diff --git a/.gitignore b/.gitignore index 7cad653..0539648 100644 --- a/.gitignore +++ b/.gitignore @@ -13,8 +13,10 @@ /tests/c/*-*-cache /tests/c/*-*-normal /tests/c/*-*-sync -/libnmdb/nmdb.h +/libnmdb/internal.h /libnmdb/libnmdb.pc +/libnmdb/doxygen/doc.internal +/libnmdb/doxygen/Doxyfile.base /tags /bindings/python/build /bindings/python3/build diff --git a/libnmdb/Makefile b/libnmdb/Makefile index f0ff65f..b11f84f 100644 --- a/libnmdb/Makefile +++ b/libnmdb/Makefile @@ -40,14 +40,14 @@ default: all all: libs libnmdb.pc -nmdb.h: nmdb.skel.h - @echo "generating nmdb.h" - @cat nmdb.skel.h | \ +internal.h: internal.h.in + @echo "generating internal.h" + @cat internal.h.in | \ sed 's/++CONFIG_ENABLE_TIPC++/$(ENABLE_TIPC)/g' | \ sed 's/++CONFIG_ENABLE_TCP++/$(ENABLE_TCP)/g' | \ sed 's/++CONFIG_ENABLE_UDP++/$(ENABLE_UDP)/g' | \ sed 's/++CONFIG_ENABLE_SCTP++/$(ENABLE_SCTP)/g' \ - > nmdb.h + > internal.h libnmdb.pc: libnmdb.skel.pc @echo "generating libnmdb.pc" @@ -57,10 +57,10 @@ libnmdb.pc: libnmdb.skel.pc libs: libnmdb.so libnmdb.a -libnmdb.so: nmdb.h $(OBJS) +libnmdb.so: internal.h $(OBJS) $(NICE_CC) $(ALL_CFLAGS) -shared -fPIC $(OBJS) -o libnmdb.so -libnmdb.a: nmdb.h $(OBJS) +libnmdb.a: internal.h $(OBJS) $(AR) cr libnmdb.a $(OBJS) @@ -82,14 +82,17 @@ install-man: install: install-lib install-man +doxygen: + $(MAKE) -C doxygen/ .c.o: $(NICE_CC) $(ALL_CFLAGS) -c $< -o $@ clean: - rm -f nmdb.h libnmdb.pc $(OBJS) libnmdb.so libnmdb.a + rm -f internal.h libnmdb.pc $(OBJS) libnmdb.so libnmdb.a rm -f *.bb *.bbg *.da *.gcov *.gcda *.gcno gmon.out + $(MAKE) -C doxygen $@ -.PHONY: default all libs install-lib install-man install clean +.PHONY: default all libs install-lib install-man install doxygen clean diff --git a/libnmdb/doxygen/Doxyfile.base.in b/libnmdb/doxygen/Doxyfile.base.in new file mode 100644 index 0000000..9a74040 --- /dev/null +++ b/libnmdb/doxygen/Doxyfile.base.in @@ -0,0 +1,204 @@ +DOXYFILE_ENCODING = UTF-8 +PROJECT_NAME = libnmdb +PROJECT_NUMBER = ++VERSION++ +OUTPUT_DIRECTORY = +CREATE_SUBDIRS = NO +OUTPUT_LANGUAGE = English +BRIEF_MEMBER_DESC = YES +REPEAT_BRIEF = YES +ABBREVIATE_BRIEF = +ALWAYS_DETAILED_SEC = NO +INLINE_INHERITED_MEMB = NO +FULL_PATH_NAMES = NO +STRIP_FROM_PATH = +STRIP_FROM_INC_PATH = +SHORT_NAMES = NO +JAVADOC_AUTOBRIEF = YES +QT_AUTOBRIEF = YES +MULTILINE_CPP_IS_BRIEF = NO +INHERIT_DOCS = YES +SEPARATE_MEMBER_PAGES = NO +TAB_SIZE = 8 +ALIASES = +OPTIMIZE_OUTPUT_FOR_C = YES +OPTIMIZE_OUTPUT_JAVA = NO +OPTIMIZE_FOR_FORTRAN = NO +OPTIMIZE_OUTPUT_VHDL = NO +EXTENSION_MAPPING = +BUILTIN_STL_SUPPORT = NO +CPP_CLI_SUPPORT = NO +SIP_SUPPORT = NO +IDL_PROPERTY_SUPPORT = YES +DISTRIBUTE_GROUP_DOC = NO +SUBGROUPING = YES +TYPEDEF_HIDES_STRUCT = NO +SYMBOL_CACHE_SIZE = 0 +EXTRACT_ALL = NO +EXTRACT_PRIVATE = NO +EXTRACT_STATIC = NO +EXTRACT_LOCAL_CLASSES = YES +EXTRACT_LOCAL_METHODS = NO +EXTRACT_ANON_NSPACES = NO +HIDE_UNDOC_MEMBERS = NO +HIDE_UNDOC_CLASSES = NO +HIDE_FRIEND_COMPOUNDS = NO +HIDE_IN_BODY_DOCS = NO +INTERNAL_DOCS = NO +CASE_SENSE_NAMES = YES +HIDE_SCOPE_NAMES = NO +SHOW_INCLUDE_FILES = YES +INLINE_INFO = YES +SORT_MEMBER_DOCS = NO +SORT_BRIEF_DOCS = NO +SORT_GROUP_NAMES = NO +SORT_BY_SCOPE_NAME = NO +GENERATE_TODOLIST = YES +GENERATE_TESTLIST = YES +GENERATE_BUGLIST = YES +GENERATE_DEPRECATEDLIST= YES +ENABLED_SECTIONS = +MAX_INITIALIZER_LINES = 30 +SHOW_USED_FILES = YES +SHOW_DIRECTORIES = YES +SHOW_FILES = YES +SHOW_NAMESPACES = YES +FILE_VERSION_FILTER = +LAYOUT_FILE = +QUIET = YES +WARNINGS = YES +WARN_IF_UNDOCUMENTED = YES +WARN_IF_DOC_ERROR = YES +WARN_NO_PARAMDOC = YES +WARN_FORMAT = "$file:$line: $text" +WARN_LOGFILE = +INPUT = ../ +INPUT_ENCODING = UTF-8 +FILE_PATTERNS = *.c *.h *.doxy +RECURSIVE = YES +EXCLUDE = +EXCLUDE_SYMLINKS = NO +EXCLUDE_PATTERNS = +EXCLUDE_SYMBOLS = +EXAMPLE_PATH = +EXAMPLE_PATTERNS = +EXAMPLE_RECURSIVE = NO +IMAGE_PATH = +INPUT_FILTER = +FILTER_PATTERNS = +FILTER_SOURCE_FILES = NO +SOURCE_BROWSER = YES +INLINE_SOURCES = NO +STRIP_CODE_COMMENTS = YES +REFERENCED_BY_RELATION = YES +REFERENCES_RELATION = NO +REFERENCES_LINK_SOURCE = NO +USE_HTAGS = NO +VERBATIM_HEADERS = YES +ALPHABETICAL_INDEX = NO +COLS_IN_ALPHA_INDEX = 5 +IGNORE_PREFIX = +GENERATE_HTML = YES +HTML_OUTPUT = html +HTML_FILE_EXTENSION = .html +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = +HTML_ALIGN_MEMBERS = YES +HTML_DYNAMIC_SECTIONS = YES +GENERATE_DOCSET = NO +DOCSET_FEEDNAME = "Doxygen generated docs" +DOCSET_BUNDLE_ID = org.doxygen.Project +GENERATE_HTMLHELP = NO +CHM_FILE = +HHC_LOCATION = +GENERATE_CHI = NO +CHM_INDEX_ENCODING = +BINARY_TOC = NO +TOC_EXPAND = YES +GENERATE_QHP = NO +QCH_FILE = +QHP_NAMESPACE = +QHP_VIRTUAL_FOLDER = doc +QHP_CUST_FILTER_NAME = +QHP_CUST_FILTER_ATTRS = +QHP_SECT_FILTER_ATTRS = +QHG_LOCATION = +DISABLE_INDEX = NO +ENUM_VALUES_PER_LINE = 4 +GENERATE_TREEVIEW = NONE +TREEVIEW_WIDTH = 250 +FORMULA_FONTSIZE = 10 +GENERATE_LATEX = NO +LATEX_OUTPUT = latex +LATEX_CMD_NAME = latex +MAKEINDEX_CMD_NAME = makeindex +COMPACT_LATEX = NO +PAPER_TYPE = a4wide +EXTRA_PACKAGES = +LATEX_HEADER = +PDF_HYPERLINKS = YES +USE_PDFLATEX = YES +LATEX_BATCHMODE = NO +LATEX_HIDE_INDICES = NO +GENERATE_RTF = NO +RTF_OUTPUT = rtf +COMPACT_RTF = NO +RTF_HYPERLINKS = NO +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = +GENERATE_MAN = NO +MAN_OUTPUT = man +MAN_EXTENSION = .3 +MAN_LINKS = NO +GENERATE_XML = NO +XML_OUTPUT = xml +XML_SCHEMA = +XML_DTD = +XML_PROGRAMLISTING = YES +GENERATE_AUTOGEN_DEF = NO +GENERATE_PERLMOD = NO +PERLMOD_LATEX = NO +PERLMOD_PRETTY = YES +PERLMOD_MAKEVAR_PREFIX = +ENABLE_PREPROCESSING = YES +MACRO_EXPANSION = NO +EXPAND_ONLY_PREDEF = NO +SEARCH_INCLUDES = YES +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = +EXPAND_AS_DEFINED = +SKIP_FUNCTION_MACROS = YES +TAGFILES = +GENERATE_TAGFILE = +ALLEXTERNALS = NO +EXTERNAL_GROUPS = YES +PERL_PATH = /usr/bin/perl +CLASS_DIAGRAMS = YES +MSCGEN_PATH = +HIDE_UNDOC_RELATIONS = YES +HAVE_DOT = YES +DOT_FONTNAME = FreeSans +DOT_FONTSIZE = 10 +DOT_FONTPATH = +CLASS_GRAPH = YES +COLLABORATION_GRAPH = YES +GROUP_GRAPHS = YES +UML_LOOK = NO +TEMPLATE_RELATIONS = NO +INCLUDE_GRAPH = YES +INCLUDED_BY_GRAPH = YES +CALL_GRAPH = YES +CALLER_GRAPH = YES +GRAPHICAL_HIERARCHY = YES +DIRECTORY_GRAPH = YES +DOT_IMAGE_FORMAT = png +DOT_PATH = +DOTFILE_DIRS = +DOT_GRAPH_MAX_NODES = 50 +MAX_DOT_GRAPH_DEPTH = 0 +DOT_TRANSPARENT = YES +DOT_MULTI_TARGETS = NO +GENERATE_LEGEND = YES +DOT_CLEANUP = YES +SEARCHENGINE = NO diff --git a/libnmdb/doxygen/Doxyfile.public b/libnmdb/doxygen/Doxyfile.public new file mode 100644 index 0000000..efc5aa7 --- /dev/null +++ b/libnmdb/doxygen/Doxyfile.public @@ -0,0 +1,8 @@ +@INCLUDE = Doxyfile.base +PROJECT_NAME = "libnmdb (public)" +OUTPUT_DIRECTORY = doc.public +EXTRACT_ALL = YES +EXTRACT_STATIC = NO +INTERNAL_DOCS = NO +INPUT = ../nmdb.h groups.doxy + diff --git a/libnmdb/doxygen/Makefile b/libnmdb/doxygen/Makefile new file mode 100644 index 0000000..71a2a2f --- /dev/null +++ b/libnmdb/doxygen/Makefile @@ -0,0 +1,30 @@ + +ifneq ($(V), 1) + NICE_DOXYGEN = @echo " DOXYGEN $@"; doxygen +else + NICE_DOXYGEN = doxygen +endif + + +default: all + +all: public + +# $(LIB_VER) must be defined externally if we want the generated docs to +# specify a version number. Usually, this Makefile will be invoked by +# libnmdb's, which has that variable properly defined. +Doxyfile.base: Doxyfile.base.in + @echo "generating Doxyfile.base" + @cat Doxyfile.base.in | \ + sed 's@++VERSION++@$(LIB_VER)@g' \ + > Doxyfile.base + +public: Doxyfile.base + $(NICE_DOXYGEN) Doxyfile.public + +clean: + rm -rf doc.public Doxyfile.base + + +.PHONY: all clean default doxygen public + diff --git a/libnmdb/doxygen/groups.doxy b/libnmdb/doxygen/groups.doxy new file mode 100644 index 0000000..9e1038f --- /dev/null +++ b/libnmdb/doxygen/groups.doxy @@ -0,0 +1,12 @@ + +/** +@addtogroup connection Connection API +Functions used to connect to the servers. + +@addtogroup database Database API +Functions that affect the database and the cache. + +@addtogroup cache Cache API +Functions that affect only the cache. +*/ + diff --git a/libnmdb/internal.h b/libnmdb/internal.h deleted file mode 100644 index d80424c..0000000 --- a/libnmdb/internal.h +++ /dev/null @@ -1,29 +0,0 @@ - -#ifndef _INTERNAL_H -#define _INTERNAL_H - -/* Different connection types. Used internally to differentiate between the - * different protocols in struct nmdb_srv. */ -#define TIPC_CONN 1 -#define TCP_CONN 2 -#define UDP_CONN 3 -#define SCTP_CONN 4 - -/* The ID code for requests is hardcoded for now, until asynchronous requests - * are implemented. */ -#define ID_CODE 1 - -/* For a given buffer, how much into it should the generic library code write - * the message contents. */ -#define TIPC_MSG_OFFSET 0 -#define TCP_MSG_OFFSET 4 -#define UDP_MSG_OFFSET 0 -#define SCTP_MSG_OFFSET 0 - -/* Functions used internally but shared among the different files. */ -int compare_servers(const void *s1, const void *s2); -ssize_t srecv(int fd, unsigned char *buf, size_t count, int flags); -ssize_t ssend(int fd, const unsigned char *buf, size_t count, int flags); - -#endif - diff --git a/libnmdb/internal.h.in b/libnmdb/internal.h.in new file mode 100644 index 0000000..4b78469 --- /dev/null +++ b/libnmdb/internal.h.in @@ -0,0 +1,83 @@ + +#ifndef _INTERNAL_H +#define _INTERNAL_H + +/* Different connection types. Used internally to differentiate between the + * different protocols in struct nmdb_srv. */ +#define TIPC_CONN 1 +#define TCP_CONN 2 +#define UDP_CONN 3 +#define SCTP_CONN 4 + +/* The ID code for requests is hardcoded for now, until asynchronous requests + * are implemented. */ +#define ID_CODE 1 + +/* For a given buffer, how much into it should the generic library code write + * the message contents. */ +#define TIPC_MSG_OFFSET 0 +#define TCP_MSG_OFFSET 4 +#define UDP_MSG_OFFSET 0 +#define SCTP_MSG_OFFSET 0 + +/* Defined to 0 or 1 at libnmdb build time according the build configuration, + * not to be used externally. */ +#define _ENABLE_TIPC ++CONFIG_ENABLE_TIPC++ +#define _ENABLE_TCP ++CONFIG_ENABLE_TCP++ +#define _ENABLE_UDP ++CONFIG_ENABLE_UDP++ +#define _ENABLE_SCTP ++CONFIG_ENABLE_SCTP++ + +/* Functions used internally but shared among the different files. */ +int compare_servers(const void *s1, const void *s2); +ssize_t srecv(int fd, unsigned char *buf, size_t count, int flags); +ssize_t ssend(int fd, const unsigned char *buf, size_t count, int flags); + + +/* + * Server and connection internal types. + */ + +#include <sys/types.h> /* socket defines */ +#include <sys/socket.h> /* socklen_t */ + +#if _ENABLE_TIPC +#include <linux/tipc.h> /* struct sockaddr_tipc */ +#endif + +#if (_ENABLE_TCP || _ENABLE_UDP || _ENABLE_SCTP) +#include <netinet/in.h> /* struct sockaddr_in */ +#endif + +/* A connection to an nmdb server. */ +struct nmdb_srv { + int fd; + int type; + union { + +#if _ENABLE_TIPC + struct { + unsigned int port; + struct sockaddr_tipc srvsa; + socklen_t srvlen; + } tipc; +#endif + +#if (_ENABLE_TCP || _ENABLE_UDP || _ENABLE_SCTP) + struct { + struct sockaddr_in srvsa; + socklen_t srvlen; + } in; +#endif + + } info; +}; + +/* A connection to one or many nmdb servers. */ +struct nmdb_conn { + unsigned int nservers; + struct nmdb_srv *servers; +}; + + +#endif + diff --git a/libnmdb/libnmdb.c b/libnmdb/libnmdb.c index 6cb612d..8f975de 100644 --- a/libnmdb/libnmdb.c +++ b/libnmdb/libnmdb.c @@ -8,15 +8,22 @@ #include <unistd.h> /* close() */ #include "nmdb.h" +#include "internal.h" #include "net-const.h" #include "tipc.h" #include "tcp.h" #include "udp.h" #include "sctp.h" -#include "internal.h" #include "netutils.h" +/* Possible flags, notice it may make no sense to mix them, consult the + * documentation before doing weird things. Values should be kept in sync with + * the internal net-const.h */ +#define NMDB_CACHE_ONLY 1 +#define NMDB_SYNC 2 + + /* Compares two servers by their connection identifiers. It is used internally * to keep the server array sorted with qsort(). */ int compare_servers(const void *s1, const void *s2) diff --git a/libnmdb/nmdb.h b/libnmdb/nmdb.h new file mode 100644 index 0000000..9159d82 --- /dev/null +++ b/libnmdb/nmdb.h @@ -0,0 +1,282 @@ + +/* Header for the libnmdb library. */ + +#ifndef _NMDB_H +#define _NMDB_H + +/** Opaque type representing a connection with one or more servers. */ +typedef struct nmdb_conn nmdb_t; + +/** Create a new nmdb_t pointer, used to talk with the server. + * + * @returns a new connection instance. + * @ingroup connection + */ +nmdb_t *nmdb_init(); + +/** Add a TIPC server. + * + * @param db connection instance. + * @param port TIPC port to connect to (-1 means use the default). + * @returns 1 on success, 0 on failure. + * @ingroup connection + */ +int nmdb_add_tipc_server(nmdb_t *db, int port); + +/** Add a TCP server. + * + * @param db connection instance. + * @param addr host to connect to. + * @param port port to connect to (-1 means use the default). + * @returns 1 on success, 0 on failure. + * @ingroup connection + */ +int nmdb_add_tcp_server(nmdb_t *db, const char *addr, int port); + +/** Add an UDP server. + * + * @param db connection instance. + * @param addr host to connect to. + * @param port port to connect to (-1 means use the default). + * @returns 1 on success, 0 on failure. + * @ingroup connection + */ +int nmdb_add_udp_server(nmdb_t *db, const char *addr, int port); + +/** Add a SCTP server. + * + * @param db connection instance. + * @param addr host to connect to. + * @param port port to connect to (-1 means use the default). + * @returns 1 on success, 0 on failure. + * @ingroup connection + */ +int nmdb_add_sctp_server(nmdb_t *db, const char *addr, int port); + +/** Free a nmdb_t structure created by nmdb_init(). + * It also closes all the connections opened to the servers. + * + * @param db connection instance. + * @returns 1 on success, 0 on failure. + * @ingroup connection + */ +int nmdb_free(nmdb_t *db); + +/** Get the value associated with a key. + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @param[out] val buffer where the value will be stored. + * @param vsize size of the value buffer. + * @returns the size of the value written to the given buffer, -1 if the key + * is not in the database, or -2 if there was an error. + * @ingroup database + */ +ssize_t nmdb_get(nmdb_t *db, const unsigned char *key, size_t ksize, + unsigned char *val, size_t vsize); + +/** Get the value associated with a key from cache. + * This is just like nmdb_get(), except it only queries the caches, and never + * the database. + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @param[out] val buffer where the value will be stored. + * @param vsize size of the value buffer. + * @returns the size of the value written to the given buffer, -1 if the key + * is not in the cache, or -2 if there was an error. + * @ingroup cache + */ +ssize_t nmdb_cache_get(nmdb_t *db, const unsigned char *key, size_t ksize, + unsigned char *val, size_t vsize); + +/** Set the value associated with a key. + * It returns after the command has been acknowledged by the server, but does + * not wait for the database to confirm it. In any case, further GET requests + * will return this value, even if this set has not reached the backend yet. + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @param val the value + * @param vsize size of the value. + * @returns 1 on success, < 0 on error. + * @ingroup database + */ +int nmdb_set(nmdb_t *db, const unsigned char *key, size_t ksize, + const unsigned char *val, size_t vsize); + +/** Set the value associated with a key, synchronously. + * It works just like nmdb_set(), except it returns only after the database + * confirms it has stored the value. + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @param val the value + * @param vsize size of the value. + * @returns 1 on success, < 0 on error. + * @ingroup database + */ +int nmdb_set_sync(nmdb_t *db, const unsigned char *key, size_t ksize, + const unsigned char *val, size_t vsize); + +/** Set the value associated with a key, but only in the cache. + * This command sets the key's value in the cache, but does NOT affect the + * backend database. Combining it with nmdb_set() and/or nmdb_set_sync() is + * not recommended, because consistency issues may arise. + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @param val the value + * @param vsize size of the value. + * @returns 1 on success, < 0 on error. + * @ingroup cache + */ +int nmdb_cache_set(nmdb_t *db, const unsigned char *key, size_t ksize, + const unsigned char *val, size_t vsize); + +/** Delete a key. + * It returns after the command has been acknowledged by the server, but does + * not wait for the database to confirm it. In any case, further GET requests + * will return the key is missing, even if this delete has not reached the + * backend yet. + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @returns 1 on success, < 0 on error. + * @ingroup database + */ +int nmdb_del(nmdb_t *db, const unsigned char *key, size_t ksize); + +/** Delete a key synchronously. + * It works just like nmdb_del(), except it returns only after the database + * confirms it has deleted the key. + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @returns 1 on success, < 0 on error. + * @ingroup database + */ +int nmdb_del_sync(nmdb_t *db, const unsigned char *key, size_t ksize); + +/** Delete a key, but only from the cache. + * This command deletes a key from the cache, but does NOT affect the backend + * database. Combining it with nmdb_del() and/or nmdb_del_sync() is not + * recommended, because consistency issues may arise. + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @returns 1 on success, < 0 on error. + * @ingroup cache + */ +int nmdb_cache_del(nmdb_t *db, const unsigned char *key, size_t ksize); + +/** Perform an atomic compare-and-swap. + * This command will set the value associated to a key to newval, provided it + * currently is oldval, in an atomic way. + * Equivalent to atomically doing: + * + * if get(key) == oldval, then set(key, newval) + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @param oldval the old, expected value. + * @param ovsize size of the expected value. + * @param newval the new value to set. + * @param nvsize size of the new value. + * @returns 2 on success, 1 if the current value does not match oldval, 0 if + * the key is not in the database, or < 0 on error. + * @ingroup database + */ +int nmdb_cas(nmdb_t *db, const unsigned char *key, size_t ksize, + const unsigned char *oldval, size_t ovsize, + const unsigned char *newval, size_t nvsize); + +/** Perform an atomic compare-and-swap only on the cache. + * This command works just like nmdb_cas(), except it affects only the cache, + * and not the backend database. + * Equivalent to atomically doing: + * + * if get_cache(key) == oldval, then set_cache(key, newval) + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @param oldval the old, expected value. + * @param ovsize size of the expected value. + * @param newval the new value to set. + * @param nvsize size of the new value. + * @returns 2 on success, 1 if the current value does not match oldval, 0 if + * the key is not in the database, or < 0 on error. + * @ingroup cache + */ +int nmdb_cache_cas(nmdb_t *db, const unsigned char *key, size_t ksize, + const unsigned char *oldval, size_t ovsize, + const unsigned char *newval, size_t nvsize); + +/** Atomically increment the value associated with a key. + * This command atomically increments the value associated with a key in the + * given increment. However, there are requirements on the current value: it + * must be a NULL-terminated string with only a number in base 10 in it (i.e. + * it must be parseable by strtoll(3)). + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @param increment the value to add to the current one (can be negative). + * @param[out] newval pointer to an integer that will be set to the new + * value. + * @returns 2 if the increment was successful, 1 if the current value was not + * in an appropriate format, 0 if the key is not in the database, or < 0 + * on error. + * @ingroup database + */ +int nmdb_incr(nmdb_t *db, const unsigned char *key, size_t ksize, + int64_t increment, int64_t *newval); + +/** Atomically increment the value associated with a key only in the cache. + * This command works just like nmdb_incr(), except it affects only the cache, + * and not the backend database. + * + * @param db connection instance. + * @param key the key. + * @param ksize the key size. + * @param increment the value to add to the current one (can be negative). + * @param[out] newval pointer to an integer that will be set to the new + * value. + * @returns 2 if the increment was successful, 1 if the current value was not + * in an appropriate format, 0 if the key is not in the database, or < 0 + * on error. + * @ingroup cache + */ +int nmdb_cache_incr(nmdb_t *db, const unsigned char *key, size_t ksize, + int64_t increment, int64_t *newval); + + +/** Request servers' statistics. + * This API is used by nmdb-stats, and likely to change in the future. Do not + * rely on it. + * + * @param db connection instance. + * @param[out] buf buffer used to store the results. + * @param bsize size of the buffer. + * @param[out] nservers number of servers queried. + * @param [out] nstats number of stats per server. + * @returns 1 if success, -1 if there was an error in the server, -2 if there + * was a network error, -3 if the buffer was too small, -4 if the server + * replies were of different size (indicates different server versions, + * not supported at the time) + */ +int nmdb_stats(nmdb_t *db, unsigned char *buf, size_t bsize, + unsigned int *nservers, unsigned int *nstats); + +#endif + diff --git a/libnmdb/nmdb.skel.h b/libnmdb/nmdb.skel.h deleted file mode 100644 index 448fb15..0000000 --- a/libnmdb/nmdb.skel.h +++ /dev/null @@ -1,101 +0,0 @@ - -/* Header for the libnmdb library. */ - -#ifndef _NMDB_H -#define _NMDB_H - -/* Defined to 0 or 1 at libnmdb build time according the build configuration, - * not to be used externally. */ -#define _ENABLE_TIPC ++CONFIG_ENABLE_TIPC++ -#define _ENABLE_TCP ++CONFIG_ENABLE_TCP++ -#define _ENABLE_UDP ++CONFIG_ENABLE_UDP++ -#define _ENABLE_SCTP ++CONFIG_ENABLE_SCTP++ - - -#include <sys/types.h> /* socket defines */ -#include <sys/socket.h> /* socklen_t */ - -#if _ENABLE_TIPC -#include <linux/tipc.h> /* struct sockaddr_tipc */ -#endif - -#if (_ENABLE_TCP || _ENABLE_UDP || _ENABLE_SCTP) -#include <netinet/in.h> /* struct sockaddr_in */ -#endif - -struct nmdb_srv { - int fd; - int type; - union { - -#if _ENABLE_TIPC - struct { - unsigned int port; - struct sockaddr_tipc srvsa; - socklen_t srvlen; - } tipc; -#endif - -#if (_ENABLE_TCP || _ENABLE_UDP || _ENABLE_SCTP) - struct { - struct sockaddr_in srvsa; - socklen_t srvlen; - } in; -#endif - - } info; -}; - -typedef struct nmdb_t { - unsigned int nservers; - struct nmdb_srv *servers; -} nmdb_t; - - -/* Possible flags, notice it may make no sense to mix them, consult the - * documentation before doing weird things. Values should be kept in sync with - * the internal net-const.h */ -#define NMDB_CACHE_ONLY 1 -#define NMDB_SYNC 2 - - -nmdb_t *nmdb_init(); -int nmdb_add_tipc_server(nmdb_t *db, int port); -int nmdb_add_tcp_server(nmdb_t *db, const char *addr, int port); -int nmdb_add_udp_server(nmdb_t *db, const char *addr, int port); -int nmdb_add_sctp_server(nmdb_t *db, const char *addr, int port); -int nmdb_free(nmdb_t *db); - -ssize_t nmdb_get(nmdb_t *db, const unsigned char *key, size_t ksize, - unsigned char *val, size_t vsize); -ssize_t nmdb_cache_get(nmdb_t *db, const unsigned char *key, size_t ksize, - unsigned char *val, size_t vsize); - -int nmdb_set(nmdb_t *db, const unsigned char *key, size_t ksize, - const unsigned char *val, size_t vsize); -int nmdb_set_sync(nmdb_t *db, const unsigned char *key, size_t ksize, - const unsigned char *val, size_t vsize); -int nmdb_cache_set(nmdb_t *db, const unsigned char *key, size_t ksize, - const unsigned char *val, size_t vsize); - -int nmdb_del(nmdb_t *db, const unsigned char *key, size_t ksize); -int nmdb_del_sync(nmdb_t *db, const unsigned char *key, size_t ksize); -int nmdb_cache_del(nmdb_t *db, const unsigned char *key, size_t ksize); - -int nmdb_cas(nmdb_t *db, const unsigned char *key, size_t ksize, - const unsigned char *oldval, size_t ovsize, - const unsigned char *newval, size_t nvsize); -int nmdb_cache_cas(nmdb_t *db, const unsigned char *key, size_t ksize, - const unsigned char *oldval, size_t ovsize, - const unsigned char *newval, size_t nvsize); - -int nmdb_incr(nmdb_t *db, const unsigned char *key, size_t ksize, - int64_t increment, int64_t *newval); -int nmdb_cache_incr(nmdb_t *db, const unsigned char *key, size_t ksize, - int64_t increment, int64_t *newval); - -int nmdb_stats(nmdb_t *db, unsigned char *buf, size_t bsize, - unsigned int *nservers, unsigned int *nstats); - -#endif -