Skip to content

Commit

Permalink
KV Minor Changes (#1264)
Browse files Browse the repository at this point in the history
No functional changes
  • Loading branch information
scottf authored Jan 6, 2025
1 parent 9c10628 commit e18c04e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
6 changes: 2 additions & 4 deletions src/main/java/io/nats/client/impl/NatsKeyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public long put(String key, String value) throws IOException, JetStreamApiExcept
*/
@Override
public long put(String key, Number value) throws IOException, JetStreamApiException {
return put(key, value.toString().getBytes(StandardCharsets.US_ASCII));
return _write(key, value.toString().getBytes(StandardCharsets.US_ASCII), null).getSeqno();
}

/**
Expand Down Expand Up @@ -202,7 +202,6 @@ public long update(String key, String value, long expectedRevision) throws IOExc
*/
@Override
public void delete(String key) throws IOException, JetStreamApiException {
validateNonWildcardKvKeyRequired(key);
_write(key, null, getDeleteHeaders());
}

Expand All @@ -211,9 +210,8 @@ public void delete(String key) throws IOException, JetStreamApiException {
*/
@Override
public void delete(String key, long expectedRevision) throws IOException, JetStreamApiException {
validateNonWildcardKvKeyRequired(key);
Headers h = getDeleteHeaders().put(EXPECTED_LAST_SUB_SEQ_HDR, Long.toString(expectedRevision));
_write(key, null, h).getSeqno();
_write(key, null, h);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public NatsKeyValueWatchSubscription(NatsKeyValue kv, String keyPattern, KeyValu

public NatsKeyValueWatchSubscription(NatsKeyValue kv, List<String> keyPatterns, KeyValueWatcher watcher, long fromRevision, KeyValueWatchOption... watchOptions) throws IOException, JetStreamApiException {
super(kv.js);
kwWatchInit(kv, keyPatterns, watcher, fromRevision, watchOptions);
kvWatchInit(kv, keyPatterns, watcher, fromRevision, watchOptions);
}

private void kwWatchInit(NatsKeyValue kv, List<String> keyPatterns, KeyValueWatcher watcher, long fromRevision, KeyValueWatchOption[] watchOptions) throws IOException, JetStreamApiException {
private void kvWatchInit(NatsKeyValue kv, List<String> keyPatterns, KeyValueWatcher watcher, long fromRevision, KeyValueWatchOption[] watchOptions) throws IOException, JetStreamApiException {
// figure out the result options
boolean headersOnly = false;
boolean ignoreDeletes = false;
Expand Down
28 changes: 15 additions & 13 deletions src/test/java/io/nats/client/impl/KeyValueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ public void testWorkflow() throws Exception {
.build();

KeyValueStatus status = kvm.create(kvc);
assertInitialStatus(status, bucket, desc, metadata);
assertInitialStatus(status, bucket, desc);

// get the kv context for the specific bucket
KeyValue kv = nc.keyValue(bucket);
assertEquals(bucket, kv.getBucketName());
status = kv.getStatus();
assertInitialStatus(status, bucket, desc, metadata);
assertInitialStatus(status, bucket, desc);

KeyValue kv2 = nc.keyValue(bucket, KeyValueOptions.builder(DEFAULT_JS_OPTIONS).build()); // coverage
assertEquals(bucket, kv2.getBucketName());
assertInitialStatus(kv2.getStatus(), bucket, desc, metadata);
assertInitialStatus(kv2.getStatus(), bucket, desc);

// Put some keys. Each key is put in a subject in the bucket (stream)
// The put returns the sequence number in the bucket (stream)
Expand Down Expand Up @@ -143,7 +143,7 @@ public void testWorkflow() throws Exception {
status = kvm.getStatus(bucket);
assertState(status, 4, 4);

// if the key has been deleted no etnry is returned
// if the key has been deleted no entry is returned
assertNull(kv.get(byteKey));

// if the key does not exist (no history) there is no entry
Expand Down Expand Up @@ -306,9 +306,8 @@ private static void assertState(KeyValueStatus status, int entryCount, int lastS
assertEquals(status.getByteCount(), status.getBackingStreamInfo().getStreamState().getByteCount());
}

private void assertInitialStatus(KeyValueStatus status, String bucket, String desc, Map<String, String> metadata) {
KeyValueConfiguration kvc;
kvc = status.getConfiguration();
private void assertInitialStatus(KeyValueStatus status, String bucket, String desc) {
KeyValueConfiguration kvc = status.getConfiguration();
assertEquals(bucket, status.getBucketName());
assertEquals(bucket, kvc.getBucketName());
assertEquals(desc, status.getDescription());
Expand Down Expand Up @@ -483,10 +482,11 @@ public void testMaxHistoryPerKey() throws Exception {
.build());

KeyValue kv = nc.keyValue(bucket1);
kv.put(KEY, 1);
kv.put(KEY, 2);
String key = key();
kv.put(key, 1);
kv.put(key, 2);

List<KeyValueEntry> history = kv.history(KEY);
List<KeyValueEntry> history = kv.history(key);
assertEquals(1, history.size());
assertEquals(2, history.get(0).getValueAsLong());

Expand All @@ -496,7 +496,7 @@ public void testMaxHistoryPerKey() throws Exception {
.storageType(StorageType.Memory)
.build());

String key = key();
key = key();
kv = nc.keyValue(bucket2);
kv.put(key, 1);
kv.put(key, 2);
Expand All @@ -514,9 +514,11 @@ public void testCreateUpdate() throws Exception {
jsServer.run(nc -> {
KeyValueManagement kvm = nc.keyValueManagement();

assertThrows(JetStreamApiException.class, () -> kvm.getStatus(BUCKET));

String bucket = bucket();

// doesn't exist yet
assertThrows(JetStreamApiException.class, () -> kvm.getStatus(bucket));

KeyValueStatus kvs = kvm.create(KeyValueConfiguration.builder()
.name(bucket)
.storageType(StorageType.Memory)
Expand Down

0 comments on commit e18c04e

Please sign in to comment.