Skip to content

Commit

Permalink
Merge pull request #110 from fastly/athomason/kvstore-delete
Browse files Browse the repository at this point in the history
Implement KVStore delete method
  • Loading branch information
athomason authored Apr 25, 2024
2 parents 1a69131 + b29313d commit 1b68cae
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Integration Tests
on: [push]
env:
VICEROY_VERSION: 0.9.3
VICEROY_VERSION: 0.9.6
jobs:
integration-tests-tinygo:
strategy:
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 1.3.1 (2024-04-25)

### Added

- `kvstore`: add Store.Delete method

### Changed

- Update Viceroy requirement to 0.9.6

## 1.3.0 (2024-02-21)

### Added
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/kvstore/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,20 @@ func TestKVStore(t *testing.T) {
if got, want := animal.String(), "cat"; got != want {
t.Errorf("Insert: got %q, want %q", got, want)
}

if err = store.Delete("animal"); err != nil {
t.Fatal(err)
}

_, err = store.Lookup("animal")
if err == nil {
t.Error("expected Lookup failure")
}

/*
// TODO(athomason) address inconsistent behavior in viceroy and production
if err = store.Delete("nonexistent"); err != nil {
t.Fatal(err)
}
*/
}
46 changes: 46 additions & 0 deletions internal/abi/fastly/hostcalls_guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,52 @@ func (o *KVStore) Insert(key string, value io.Reader) error {
return nil
}

// witx:
//
// (@interface func (export "delete_async")
// (param $store $object_store_handle)
// (param $key string)
// (param $pending_handle_out (@witx pointer $pending_kv_delete_handle))
// (result $err (expected (error $fastly_status)))
// )

//go:wasmimport fastly_object_store delete_async
//go:noescape
func fastlyObjectStoreDeleteAsync(
h objectStoreHandle,
keyData prim.Pointer[prim.U8], keyLen prim.Usize,
pendingReq prim.Pointer[pendingRequestHandle],
) FastlyStatus

// witx:
//
// (@interface func (export "pending_delete_wait")
// (param $pending_handle $pending_kv_delete_handle)
// (result $err (expected (error $fastly_status)))
// )

//go:wasmimport fastly_object_store pending_delete_wait
//go:noescape
func fastlyObjectStorePendingDeleteWait(
pendingReq pendingRequestHandle,
) FastlyStatus

// Delete removes a key from the kv store.
func (o *KVStore) Delete(key string) error {
keyBuffer := prim.NewReadBufferFromString(key).Wstring()

var handle pendingRequestHandle
if err := fastlyObjectStoreDeleteAsync(
o.h,
keyBuffer.Data, keyBuffer.Len,
prim.ToPointer(&handle),
).toError(); err != nil {
return err
}

return fastlyObjectStorePendingDeleteWait(handle).toError()
}

// SecretStore represents a Fastly secret store, a collection of
// key/value pairs for storing sensitive data.
type SecretStore struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/abi/fastly/hostcalls_noguest.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ func (o *KVStore) Insert(key string, value io.Reader) error {
return fmt.Errorf("not implemented")
}

func (o *KVStore) Delete(key string) error {
return fmt.Errorf("not implemented")
}

type (
SecretStore struct{}
Secret struct{}
Expand Down
19 changes: 19 additions & 0 deletions kvstore/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,22 @@ func (s *Store) Insert(key string, value io.Reader) error {
}
return nil
}

// Delete removes a key from the associated KV store.
func (s *Store) Delete(key string) error {
err := s.kvstore.Delete(key)
if err != nil {
status, ok := fastly.IsFastlyError(err)
switch {
case ok && status == fastly.FastlyStatusNone:
return ErrKeyNotFound
case ok && status == fastly.FastlyStatusInval:
return ErrInvalidKey
case ok:
return fmt.Errorf("%w (%s)", ErrUnexpected, status)
default:
return err
}
}
return nil
}

0 comments on commit 1b68cae

Please sign in to comment.