KVS
The KVS (Key-Value Store) service provides a basic persistent storage of key-value pairs. Keys are strings and values can be any valid JSON value. The service is accessed thorugh RPC and the follwoing methods are provided: KVS.Set, KVS.Delete, KVS.Get, KVS.List and KVS.GetMany. Users of the API can use etag-based mechanism to ensure atomicity of updates. The following limits are enforced:
- Maximum Key Length = 42
- Maximum Value Length = 253
- Maximum Number of Keys Stored = 50
Additionally, a global revision number of the store is maintained which is incremented on every update.
KVS.Set
This method adds a new key-value pair to the store or updates an existing one.
Request
Parameters:
| Property | Type | Description |
|---|---|---|
| string | The key to be added or updated. Required |
| any JSON value | Value for the key. Required |
| string | Generated hash uniquely identifying the key-value pair. Optional |
- When
etagis present it should specify the value for existing pair and is checked against the one in the store so that and update is made only if they match. - Otherwise the update is rejected meaning that someone has changed the value after the last access.
- If
etagis not present and thekeydoes not exist it is added or if it exists it is updated unconditionally. - If
etagis present and thekeydoes not exist error is returned.
Up to and including version 2.0.0 the KVStore was flawed due to a limitation in the JSON parser ignoring JSON keys containing dots, resulting in such keys being silently dropped on reboot. With version 2.0.1 there is a partial remedy of the situation not allowing KVStore keys with embedded dot altogether.
So currently keys with dot are not supported in the KVStore. Updated version supporting them is planned for a future release.
Response
Attributes in the result:
| Property | Type | Description |
|---|---|---|
| string | Generated hash of the newly created or updated item |
| number | Revision of the store (after update) |
KVS.Get
This method returns the value stored and etag for a given key. If the key does not exist error is returned.
Request
Parameters:
| Property | Type | Description |
|---|---|---|
| string | The key to be looked up. Required |
Response
Attributes in the result:
| Property | Type | Description |
|---|---|---|
| string | Generated hash of the requested item |
| any JSON value | The value for the key |
KVS.GetMany
This method returns the full information stored for items in the store based on an optinal key matching parameter.
Request
Parameters:
| Property | Type | Description |
|---|---|---|
| string | Pattern against which keys are matched. default is |
| number | Index of the list from which to start generating the result Optional |
If match is specified the keys are matched according to the following rules:
*matches zero or more characters?Matches exactly one character- ',' divides alternative patterns
- any other character matches itself
Match is case insensitive.
Response
Attributes in the result:
| Property | Type | Description |
|---|---|---|
| object | JSON object containing a set of JSON objects whose keys are the keys matched against the requested pattern with properties |
| number | Index of the first entry in the result |
| number | Total number of available entries |
KVS.List
This method returns a list of existing keys and etags based on an optional match parameter.
Request
Parameters:
| Property | Type | Description |
|---|---|---|
| string | Pattern against which keys are matched. default is |
Same pattern matching rules as for KVS.GetMany.
Response
Attributes in the result:
| Property | Type | Description |
|---|---|---|
| object | Whose keys are the keys which matched against the requested pattern and the only property of the corresponding |
| number | Current revision of the store |
KVS.Delete
This methods deletes existing key from the store.
Request
Parameters:
| Property | Type | Description |
|---|---|---|
| string | The key to be deleted. Required |
| string | Generated hash uniquely identifying the key-value pair. Optional |
- When
etagis present it should specify the value for existing pair and is checked against the one in the store so that the key is deleted only if they match. - Otherwise error is returned.
- If the
keydoes not exist error is returned.
Response
Attributes in the result:
| Property | Type | Description |
|---|---|---|
| number | Revision of the store (after delete) |
Examples
KVS.Set example
- KVS.Set HTTP GET Request
- KVS.Set Curl Request
- KVS.Set Mos Request
http://192.168.33.1/rpc/KVS.Set?key="item1"&value="value1"
curl -X POST -d '{"id":1,"method":"KVS.Set","params":{"key":"item1","value":"value1"}}' http://${SHELLY}/rpc
mos --port ${PORT} call KVS.Set '{"key":"item1","value":"value1"}'
Response
- KVS.Set HTTP GET Response
- KVS.Set Curl Response
- KVS.Set Mos Response
{
"etag": "08UD6EJ+gr/TQr/qou+aYFng==",
"rev": 1
}
{
"id": 1,
"src": "shellyplus1-a8032abe54dc",
"params": {
"etag": "08UD6EJ+gr/TQr/qou+aYFng==",
"rev": 1
}
}
{
"etag": "08UD6EJ+gr/TQr/qou+aYFng==",
"rev": 1
}
KVS.Get example
- KVS.Get HTTP GET Request
- KVS.Get Curl Request
- KVS.Get Mos Request
http://192.168.33.1/rpc/KVS.Get?key="item1"
curl -X POST -d '{"id":1,"method":"KVS.Get","params":{"key":"item1"}}' http://${SHELLY}/rpc
mos --port ${PORT} call KVS.Get '{"key":"item1"}'
Response
- KVS.Get HTTP GET Response
- KVS.Get Curl Response
- KVS.Get Mos Response
{
"etag": "08UD6EJ+gr/TQr/qou+aYFng==",
"value": "value1"
}
{
"id": 1,
"src": "shellyplus1-a8032abe54dc",
"params": {
"etag": "08UD6EJ+gr/TQr/qou+aYFng==",
"value": "value1"
}
}
{
"etag": "08UD6EJ+gr/TQr/qou+aYFng==",
"value": "value1"
}
KVS.GetMany example
- KVS.GetMany HTTP GET Request
- KVS.GetMany Curl Request
- KVS.GetMany Mos Request
http://192.168.33.1/rpc/KVS.GetMany?match="item*"
curl -X POST -d '{"id":1,"method":"KVS.GetMany","params":{"match":"item*"}}' http://${SHELLY}/rpc
mos --port ${PORT} call KVS.GetMany '{"match":"item*"}'
Response
- KVS.GetMany HTTP GET Response
- KVS.GetMany Curl Response
- KVS.GetMany Mos Response
{
"items": [
{
"key": "item1",
"etag": "08UD6EJ+gr/TQr/qou+aYFng==",
"value": "value1"
},
{
"key": "item2",
"etag": "08+jWymZ24aYJvhgIHUpLP0g==",
"value": "value2"
}
],
"offset": 0,
"total": 2
}
{
"id": 1,
"src": "shellyplus1-a8032abe54dc",
"params": {
"items": [
{
"key": "item1",
"etag": "08UD6EJ+gr/TQr/qou+aYFng==",
"value": "value1"
},
{
"key": "item2",
"etag": "08+jWymZ24aYJvhgIHUpLP0g==",
"value": "value2"
}
],
"offset": 0,
"total": 2
}
}
{
"items": [
{
"key": "item1",
"etag": "08UD6EJ+gr/TQr/qou+aYFng==",
"value": "value1"
},
{
"key": "item2",
"etag": "08+jWymZ24aYJvhgIHUpLP0g==",
"value": "value2"
}
],
"offset": 0,
"total": 2
}
KVS.List example
- KVS.List HTTP GET Request
- KVS.List Curl Request
- KVS.List Mos Request
http://192.168.33.1/rpc/KVS.List?match="item?"
curl -X POST -d '{"id":1,"method":"KVS.List","params":{"match":"item?"}}' http://${SHELLY}/rpc
mos --port ${PORT} call KVS.List '{"match":"item?"}'
Response
- KVS.List HTTP GET Response
- KVS.List Curl Response
- KVS.List Mos Response
{
"keys": {
"item1": {
"etag": "08UD6EJ+gr/TQr/qou+aYFng=="
},
"item2": {
"etag": "08+jWymZ24aYJvhgIHUpLP0g=="
}
},
"rev": 3
}
{
"id": 1,
"src": "shellyplus1-a8032abe54dc",
"params": {
"keys": {
"item1": {
"etag": "08UD6EJ+gr/TQr/qou+aYFng=="
},
"item2": {
"etag": "08+jWymZ24aYJvhgIHUpLP0g=="
}
},
"rev": 3
}
}
{
"keys": {
"item1": {
"etag": "08UD6EJ+gr/TQr/qou+aYFng=="
},
"item2": {
"etag": "08+jWymZ24aYJvhgIHUpLP0g=="
}
},
"rev": 3
}
KVS.Delete example
- KVS.Delete HTTP GET Request
- KVS.Delete Curl Request
- KVS.Delete Mos Request
http://192.168.33.1/rpc/KVS.Delete?key="item1"
curl -X POST -d '{"id":1,"method":"KVS.Delete","params":{"key":"item1"}}' http://${SHELLY}/rpc
mos --port ${PORT} call KVS.Delete '{"key":"item1"}'
Response
- KVS.Delete HTTP GET Response
- KVS.Delete Curl Response
- KVS.Delete Mos Response
{
"rev": 4
}
{
"id": 1,
"src": "shellyplus1-a8032abe54dc",
"params": {
"rev": 4
}
}
{
"rev": 4
}