Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADDED] kvStore_GetRevision() API #522

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/kv.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The NATS Authors
// Copyright 2021-2022 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -381,7 +381,7 @@ _getKVOp(natsMsg *msg)
}

static natsStatus
_getEntry(kvEntry **new_entry, bool *deleted, kvStore *kv, const char *key)
_getEntry(kvEntry **new_entry, bool *deleted, kvStore *kv, const char *key, uint64_t revision)
{
natsStatus s = NATS_OK;
natsMsg *msg = NULL;
Expand All @@ -395,7 +395,15 @@ _getEntry(kvEntry **new_entry, bool *deleted, kvStore *kv, const char *key)
return nats_setError(NATS_INVALID_ARG, "%s", kvErrInvalidKey);

BUILD_SUBJECT(KEY_NAME_ONLY);
IFOK(s, js_GetLastMsg(&msg, kv->js, kv->stream, natsBuf_Data(&buf), NULL, NULL));
if (revision == 0)
{
IFOK(s, js_GetLastMsg(&msg, kv->js, kv->stream, natsBuf_Data(&buf), NULL, NULL));
}
else
{
IFOK(s, js_GetMsg(&msg, kv->js, kv->stream, revision, NULL, NULL));
IFOK(s, (strcmp(natsMsg_GetSubject(msg), natsBuf_Data(&buf)) == 0 ? NATS_OK : NATS_NOT_FOUND));
}
IFOK(s, _createEntry(&e, kv, &msg));
if (s == NATS_OK)
e->op = _getKVOp(e->msg);
Expand Down Expand Up @@ -423,15 +431,15 @@ _getEntry(kvEntry **new_entry, bool *deleted, kvStore *kv, const char *key)
}

natsStatus
kvStore_Get(kvEntry **new_entry, kvStore *kv, const char *key)
_get(kvEntry **new_entry, kvStore *kv, const char *key, uint64_t revision)
{
natsStatus s;
bool deleted = false;

if ((new_entry == NULL) || (kv == NULL))
return nats_setDefaultError(NATS_INVALID_ARG);

s = _getEntry(new_entry, &deleted, kv, key);
s = _getEntry(new_entry, &deleted, kv, key, revision);
if (s == NATS_OK)
{
if (deleted)
Expand All @@ -447,6 +455,31 @@ kvStore_Get(kvEntry **new_entry, kvStore *kv, const char *key)
return NATS_UPDATE_ERR_STACK(s);
}

natsStatus
kvStore_Get(kvEntry **new_entry, kvStore *kv, const char *key)
{
natsStatus s = _get(new_entry, kv, key, 0);
// We don't want stack trace for this error
if (s == NATS_NOT_FOUND)
return s;
return NATS_UPDATE_ERR_STACK(s);
}

natsStatus
kvStore_GetRevision(kvEntry **new_entry, kvStore *kv, const char *key, uint64_t revision)
{
natsStatus s;

if (revision <= 0)
return nats_setError(NATS_INVALID_ARG, "%s", kvErrInvalidRevision);

s = _get(new_entry, kv, key, revision);
// We don't want stack trace for this error
if (s == NATS_NOT_FOUND)
return s;
return NATS_UPDATE_ERR_STACK(s);
}

static natsStatus
_putEntry(uint64_t *rev, kvStore *kv, jsPubOptions *po, const char *key, const void *data, int len)
{
Expand Down Expand Up @@ -515,7 +548,7 @@ kvStore_Create(uint64_t *rev, kvStore *kv, const char *key, const void *data, in

// Since we have tombstones for DEL ops for watchers, this could be from that
// so we need to double check.
ls = _getEntry(&e, &deleted, kv, key);
ls = _getEntry(&e, &deleted, kv, key, 0);
if (ls == NATS_OK)
{
if (deleted)
Expand Down
3 changes: 2 additions & 1 deletion src/kv.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The NATS Authors
// Copyright 2021-2022 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -22,3 +22,4 @@
#define kvErrBadBucket "bucket not valid key-value store"
#define kvErrBucketNotFound "bucket not found"
#define kvErrInvalidKey "invalid key"
#define kvErrInvalidRevision "invalid revision"
15 changes: 15 additions & 0 deletions src/nats.h
Original file line number Diff line number Diff line change
Expand Up @@ -6082,6 +6082,21 @@ kvEntry_Destroy(kvEntry *e);
NATS_EXTERN natsStatus
kvStore_Get(kvEntry **new_entry, kvStore *kv, const char *key);

/** \brief Returns the entry at the specific revision for the key.
*
* Returns the entry at the specific revision for the key, or #NATS_NOT_FOUND if there is no
* entry for that key and revision.
*
* \note The entry should be destroyed to release memory using #kvEntry_Destroy.
*
* @param new_entry the location where to store the pointer to the entry associated with the `key`.
* @param kv the pointer to the #kvStore object.
* @param key the name of the key.
* @param revision the revision of the entry (must be > 0).
*/
NATS_EXTERN natsStatus
kvStore_GetRevision(kvEntry **new_entry, kvStore *kv, const char *key, uint64_t revision);

/** \brief Places the new value for the key into the store.
*
* Places the new value for the key into the store.
Expand Down
40 changes: 40 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -27539,6 +27539,23 @@ test_KeyValueBasics(void)
e = NULL;
testCond(true);

test("Get revision (bad args): ");
s = kvStore_GetRevision(&e, kv, "key", 0);
testCond((s == NATS_INVALID_ARG) && (e == NULL)
&& (strstr(nats_GetLastError(NULL), kvErrInvalidRevision) != NULL));
nats_clearLastError();

test("Get revision: ");
s = kvStore_GetRevision(&e, kv, "key", 1);
testCond((s == NATS_OK) && (e != NULL)
&& (strcmp(kvEntry_ValueString(e), "value") == 0)
&& (kvEntry_Revision(e) == 1));

test("Destroy entry: ");
kvEntry_Destroy(e);
e = NULL;
testCond(true);

test("Delete key (bad args): ");
s = kvStore_Delete(NULL, "key");
testCond(s == NATS_INVALID_ARG);
Expand Down Expand Up @@ -27682,6 +27699,29 @@ test_KeyValueBasics(void)
kvStatus_Destroy(NULL);
testCond(true);

test("Put for revision check: ");
s = kvStore_PutString(&rev, kv, "test.rev.one", "val1");
IFOK(s, kvStore_PutString(NULL, kv, "test.rev.two", "val2"));
testCond(s == NATS_OK);

test("Get revision (bad args): ");
s = kvStore_GetRevision(&e, kv, "test.rev.one", 0);
testCond((s == NATS_INVALID_ARG) && (e == NULL)
&& (strstr(nats_GetLastError(NULL), kvErrInvalidRevision) != NULL));
nats_clearLastError();

test("Get revision: ");
s = kvStore_GetRevision(&e, kv, "test.rev.one", rev);
testCond((s == NATS_OK) && (e != NULL)
&& (strcmp(kvEntry_ValueString(e), "val1") == 0)
&& (kvEntry_Revision(e) == rev));
kvEntry_Destroy(e);
e = NULL;

test("Get wrong revision for the key: ");
s = kvStore_GetRevision(&e, kv, "test.rev.two", rev);
testCond((s == NATS_NOT_FOUND) && (e == NULL));

test("Destroy kv store: ");
kvStore_Destroy(kv);
testCond(true);
Expand Down