mdbx: change mdbx_dkey().

This commit is contained in:
Leo Yuriev 2017-03-31 16:20:38 +03:00
parent f8eb858ef3
commit 5c5ef256b3
3 changed files with 25 additions and 17 deletions

2
mdbx.h
View File

@ -1625,7 +1625,7 @@ LIBMDBX_API int mdbx_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx);
*/ */
LIBMDBX_API int mdbx_reader_check(MDB_env *env, int *dead); LIBMDBX_API int mdbx_reader_check(MDB_env *env, int *dead);
LIBMDBX_API char *mdbx_dkey(MDB_val *key, char *buf); LIBMDBX_API char *mdbx_dkey(MDB_val *key, char *buf, const size_t bufsize);
LIBMDBX_API int mdbx_env_close_ex(MDB_env *env, int dont_sync); LIBMDBX_API int mdbx_env_close_ex(MDB_env *env, int dont_sync);

View File

@ -332,7 +332,7 @@ txnid_t mdbx_debug_edge;
* @ingroup debug * @ingroup debug
* Invoke a function to display a key in hex. * Invoke a function to display a key in hex.
*/ */
#define DKEY(x) mdbx_dkey(x, kbuf) #define DKEY(x) mdbx_dkey(x, kbuf, sizeof(kbuf))
/** An invalid page number. /** An invalid page number.
* Mainly used to denote an empty tree. * Mainly used to denote an empty tree.
@ -821,7 +821,7 @@ static __inline pgno_t mdbx_dbg_pgno(MDB_page *mp) {
* @param[in] buf the buffer to write into. Should always be #DKBUF. * @param[in] buf the buffer to write into. Should always be #DKBUF.
* @return The key in hexadecimal form. * @return The key in hexadecimal form.
*/ */
char *mdbx_dkey(MDB_val *key, char *buf) { char *mdbx_dkey(MDB_val *key, char *buf, const size_t bufsize) {
#ifdef _MSC_VER #ifdef _MSC_VER
(void)key; (void)key;
(void)buf; (void)buf;
@ -833,17 +833,24 @@ char *mdbx_dkey(MDB_val *key, char *buf) {
if (!key) if (!key)
return ""; return "";
if (key->mv_size > DKBUF_MAXKEYSIZE) const uint8_t *const data = key->mv_data;
return "MDB_MAXKEYSIZE"; bool is_ascii = true;
/* may want to make this a dynamic check: if the key is mostly for (i = 0; is_ascii && i < key->mv_size; i++)
* printable characters, print it as-is instead of converting to hex. */ if (data[i] < ' ' || data[i] > 127)
#if 1 is_ascii = false;
buf[0] = '\0';
for (i = 0; i < key->mv_size; i++) if (is_ascii)
ptr += sprintf(ptr, "%02x", ((unsigned char *)key->mv_data)[i]); snprintf(buf, bufsize, "%.*s",
#else (key->mv_size > INT_MAX) ? INT_MAX : (int)key->mv_size, data);
sprintf(buf, "%.*s", key->mv_size, key->mv_data); else {
#endif buf[0] = '\0';
for (i = 0; i < key->mv_size; i++) {
int len = snprintf(ptr, bufsize - (ptr - buf), "%02x", data[i]);
if (len < 1)
break;
ptr += len;
}
}
return buf; return buf;
#endif /* _MSC_VER */ #endif /* _MSC_VER */
} }
@ -6833,7 +6840,7 @@ static int mdbx_update_key(MDB_cursor *mc, MDB_val *key) {
k2.mv_data = NODEKEY(node); k2.mv_data = NODEKEY(node);
k2.mv_size = node->mn_ksize; k2.mv_size = node->mn_ksize;
mdbx_debug("update key %u (ofs %u) [%s] to [%s] on page %zu", indx, ptr, mdbx_debug("update key %u (ofs %u) [%s] to [%s] on page %zu", indx, ptr,
mdbx_dkey(&k2, kbuf2), DKEY(key), mp->mp_pgno); mdbx_dkey(&k2, kbuf2, sizeof(kbuf2)), DKEY(key), mp->mp_pgno);
} }
/* Sizes must be 2-byte aligned. */ /* Sizes must be 2-byte aligned. */

View File

@ -113,8 +113,9 @@ int main(int argc, char *argv[]) {
E(mdbx_cursor_get(cursor, &key, &data, MDB_FIRST)); E(mdbx_cursor_get(cursor, &key, &data, MDB_FIRST));
do { do {
printf("key: %p %s, data: %p %.*s\n", key.mv_data, mdbx_dkey(&key, dkbuf), printf("key: %p %s, data: %p %.*s\n", key.mv_data,
data.mv_data, (int)data.mv_size, (char *)data.mv_data); mdbx_dkey(&key, dkbuf, sizeof(dkbuf)), data.mv_data,
(int)data.mv_size, (char *)data.mv_data);
} while ((rc = mdbx_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0); } while ((rc = mdbx_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0);
CHECK(rc == MDB_NOTFOUND, "mdbx_cursor_get"); CHECK(rc == MDB_NOTFOUND, "mdbx_cursor_get");
mdbx_cursor_close(cursor); mdbx_cursor_close(cursor);