Skip to content

Commit

Permalink
📦 updates MDK for v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed May 8, 2023
1 parent 96ffa07 commit 64dbc47
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Capsule Module SDK

!!! info "What's new?"
- `v0.0.2`: ✨ Redis support
- `v0.0.1`: 🎉 first release

## What is the Capsule Module SDK alias **Capsule MDK**?
Expand Down
1 change: 0 additions & 1 deletion handle.json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package capsule
import "github.com/valyala/fastjson"



var handleJSONFunction func(param *fastjson.Value) ([]byte, error)

// SetHandleJSON sets the handle function
Expand Down
184 changes: 184 additions & 0 deletions hostfunc.redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package capsule

import "github.com/valyala/fastjson"

// hostRedisSet sets the value of a redis key.
//
// keyPosition: the position of the key in memory.
// keyLength: the length of the key.
// valueStringPosition: the position of the value string in memory.
// valueStringLength: the length of the value string.
// returnValuePosition: the position of the pointer to the return value in memory.
// returnValueLength: the length of the return value.
//
// Returns the new uint32 value.
//export hostRedisSet
func hostRedisSet(
keyPosition, keyLength uint32,
valueStringPosition, valueStringLength uint32,
returnValuePosition **uint32, returnValueLength *uint32) uint32


// RedisSet sends a message to the host to cache a key and its value.
//
// key: the string value of the key to cache.
// value: the byte slice value to cache.
// []byte: a byte slice that contains the response from the host.
func RedisSet(key string, value []byte) ([]byte, error) {

keyPosition, keyLength := getBufferPosSize([]byte(key))
valueStringPosition, valueStringLength := getBufferPosSize(value)

// This will be use to get the response from the host
var responseBufferPtr *uint32
var responseBufferSize uint32

// Send the lessage to the host
hostCacheSet(
keyPosition, keyLength,
valueStringPosition, valueStringLength,
&responseBufferPtr, &responseBufferSize)

bufferResponseFromHost := readBufferFromMemory(responseBufferPtr, responseBufferSize)

// check if success or failure
data, err := Result(bufferResponseFromHost)

return data, err
}



// hostRedisGet retrieves a Redis key-value pair from the host.
//
// keyPosition: the position of the key in memory.
// keyLength: the length of the key in memory.
// returnValuePosition: a pointer to the position of the return value in memory.
// returnValueLength: a pointer to the length of the return value in memory.
//
// Returns the new function.
//export hostRedisGet
func hostRedisGet(
keyPosition, keyLength uint32,
returnValuePosition **uint32, returnValueLength *uint32) uint32

// RedisGet retrieves the value for the given key from Redis.
//
// It takes a single string parameter, `key`, which is used to identify the value
// to retrieve from Redis.
//
// RedisGet returns a slice of bytes containing the retrieved value, and an error
// if the retrieval failed or the key was not found.
func RedisGet(key string) ([]byte, error) {

keyPosition, keyLength := getBufferPosSize([]byte(key))

// This will be use to get the response from the host
var responseBufferPtr *uint32
var responseBufferSize uint32

// Send the lessage to the host
hostCacheGet(
keyPosition, keyLength,
&responseBufferPtr, &responseBufferSize)

bufferResponseFromHost := readBufferFromMemory(responseBufferPtr, responseBufferSize)

// check if success or failure
data, err := Result(bufferResponseFromHost)
if err != nil {
return nil, err // "key not found"
}
return data, nil
}




// hostRedisDel deletes the Redis key stored at the specified position and returns the length of the deleted key.
//
// keyPosition: the position of the key in Redis.
// keyLength: the length of the key.
// returnValuePosition: a pointer to the position of the return value.
// returnValueLength: a pointer to the length of the return value.
//
// Returns: the length of the deleted key.
//export hostRedisDel
func hostRedisDel(
keyPosition, keyLength uint32,
returnValuePosition **uint32, returnValueLength *uint32) uint32

// RedisDel deletes a Redis key and returns the result as a slice of bytes.
// The key parameter is a string representing the key to be deleted.
// The function returns a slice of bytes and an error.
func RedisDel(key string) ([]byte, error) {

keyPosition, keyLength := getBufferPosSize([]byte(key))

// This will be use to get the response from the host
var responseBufferPtr *uint32
var responseBufferSize uint32

// Send the lessage to the host
hostCacheDel(
keyPosition, keyLength,
&responseBufferPtr, &responseBufferSize)

bufferResponseFromHost := readBufferFromMemory(responseBufferPtr, responseBufferSize)

// check if success or failure
data, err := Result(bufferResponseFromHost)
return data, err
}

// hostRedisKeys returns a uint32 representing the new function.
//
// filterPosition: uint32 representing the filter position.
// filterLength: uint32 representing the filter length.
// returnValuePosition: pointer to uint32 representing the return value position.
// returnValueLength: pointer to uint32 representing the return value length.
//
// Returns a uint32 representing the new function.
//export hostRedisKeys
func hostRedisKeys(
filterPosition, filterLength uint32,
returnValuePosition **uint32, returnValueLength *uint32) uint32

// RedisKeys returns an array of Redis keys that match the given filter.
//
// filter: A string used to filter Redis keys.
// Returns an array of strings and an error.
func RedisKeys(filter string) ([]string, error) {

filterPosition, filterLength := getBufferPosSize([]byte(filter))

// This will be use to get the response from the host
var responseBufferPtr *uint32
var responseBufferSize uint32

// Send the lessage to the host
hostCacheKeys(
filterPosition, filterLength,
&responseBufferPtr, &responseBufferSize)

bufferResponseFromHost := readBufferFromMemory(responseBufferPtr, responseBufferSize)
//! 🤚 this is a json string (array json string: `["Hello", "World"]`)

// check if success or failure
data, err := Result(bufferResponseFromHost)
if err != nil {
return nil, err
}
var jsonParser fastjson.Parser
keysArray, err := jsonParser.Parse(string(data))
if err != nil {
return nil, err
}
var keys []string
for _, key := range keysArray.GetArray("keys") {
keys = append(keys, string(key.GetStringBytes()))
//! if it doesn't work, implement my own simple parser
}
return keys, nil

}
2 changes: 1 addition & 1 deletion samples/simple-hello/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module simple-hello

go 1.20

require github.com/bots-garden/capsule-module-sdk v0.0.1
require github.com/bots-garden/capsule-module-sdk v0.0.2

require github.com/valyala/fastjson v1.6.4 // indirect

Expand Down
2 changes: 1 addition & 1 deletion samples/simple-talk/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module simple-talk

go 1.20

require github.com/bots-garden/capsule-module-sdk v0.0.1
require github.com/bots-garden/capsule-module-sdk v0.0.2

require github.com/valyala/fastjson v1.6.4 // indirect

Expand Down
2 changes: 1 addition & 1 deletion samples/simple.json/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/bots-garden/capsule-module-sdk/samples/simplejson

go 1.20

require github.com/bots-garden/capsule-module-sdk v0.0.0
require github.com/bots-garden/capsule-module-sdk v0.0.2

require github.com/valyala/fastjson v1.6.4

Expand Down

0 comments on commit 64dbc47

Please sign in to comment.