-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from kobergj/NatjsKVStore
Add Natjs KeyValue Store
- Loading branch information
Showing
12 changed files
with
1,757 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# NATS JetStream Key Value Store Plugin | ||
|
||
This plugin uses the NATS JetStream [KeyValue Store](https://docs.nats.io/nats-concepts/jetstream/key-value-store) to implement the Go-Micro store interface. | ||
|
||
You can use this plugin like any other store plugin. | ||
To start a local NATS JetStream server run `nats-server -js`. | ||
|
||
To manually create a new storage object call: | ||
|
||
```go | ||
natsjskv.NewStore(opts ...store.Option) | ||
``` | ||
|
||
The Go-Micro store interface uses databases and tables to store keys. These translate | ||
to buckets (key value stores) and key prefixes. If no database (bucket name) is provided, "default" will be used. | ||
|
||
You can call `Write` with any arbitrary database name, and if a bucket with that name does not exist yet, | ||
it will be automatically created. | ||
|
||
If a table name is provided, it will use it to prefix the key as `<table>_<key>`. | ||
|
||
To delete a bucket, and all the key/value pairs in it, pass the `DeleteBucket` option to the `Delete` | ||
method, then they key name will be interpreted as a bucket name, and the bucket will be deleted. | ||
|
||
Next to the default store options, a few NATS specific options are available: | ||
|
||
|
||
```go | ||
// NatsOptions accepts nats.Options | ||
NatsOptions(opts nats.Options) | ||
|
||
// JetStreamOptions accepts multiple nats.JSOpt | ||
JetStreamOptions(opts ...nats.JSOpt) | ||
|
||
// KeyValueOptions accepts multiple nats.KeyValueConfig | ||
// This will create buckets with the provided configs at initialization. | ||
// | ||
// type KeyValueConfig struct { | ||
// Bucket string | ||
// Description string | ||
// MaxValueSize int32 | ||
// History uint8 | ||
// TTL time.Duration | ||
// MaxBytes int64 | ||
// Storage StorageType | ||
// Replicas int | ||
// Placement *Placement | ||
// RePublish *RePublish | ||
// Mirror *StreamSource | ||
// Sources []*StreamSource | ||
} | ||
KeyValueOptions(cfg ...*nats.KeyValueConfig) | ||
|
||
// DefaultTTL sets the default TTL to use for new buckets | ||
// By default no TTL is set. | ||
// | ||
// TTL ON INDIVIDUAL WRITE CALLS IS NOT SUPPORTED, only bucket wide TTL. | ||
// Either set a default TTL with this option or provide bucket specific options | ||
// with ObjectStoreOptions | ||
DefaultTTL(ttl time.Duration) | ||
|
||
// DefaultMemory sets the default storage type to memory only. | ||
// | ||
// The default is file storage, persisting storage between service restarts. | ||
// Be aware that the default storage location of NATS the /tmp dir is, and thus | ||
// won't persist reboots. | ||
DefaultMemory() | ||
|
||
// DefaultDescription sets the default description to use when creating new | ||
// buckets. The default is "Store managed by go-micro" | ||
DefaultDescription(text string) | ||
|
||
// DeleteBucket will use the key passed to Delete as a bucket (database) name, | ||
// and delete the bucket. | ||
// This option should not be combined with the store.DeleteFrom option, as | ||
// that will overwrite the delete action. | ||
DeleteBucket() | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package natsjskv | ||
|
||
import ( | ||
"context" | ||
|
||
"go-micro.dev/v4/store" | ||
) | ||
|
||
// setStoreOption returns a function to setup a context with given value. | ||
func setStoreOption(k, v interface{}) store.Option { | ||
return func(o *store.Options) { | ||
if o.Context == nil { | ||
o.Context = context.Background() | ||
} | ||
|
||
o.Context = context.WithValue(o.Context, k, v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module github.com/go-micro/plugins/v4/store/nats-js-kv | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/cornelk/hashmap v1.0.8 | ||
github.com/nats-io/nats-server/v2 v2.8.4 | ||
) | ||
|
||
require ( | ||
github.com/Microsoft/go-winio v0.5.2 // indirect | ||
github.com/ProtonMail/go-crypto v0.0.0-20220824120805-4b6e5c587895 // indirect | ||
github.com/acomagu/bufpipe v1.0.3 // indirect | ||
github.com/bitly/go-simplejson v0.5.0 // indirect | ||
github.com/cloudflare/circl v1.2.0 // indirect | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/emirpasic/gods v1.18.1 // indirect | ||
github.com/fsnotify/fsnotify v1.5.4 // indirect | ||
github.com/go-git/gcfg v1.5.0 // indirect | ||
github.com/go-git/go-billy/v5 v5.3.1 // indirect | ||
github.com/go-git/go-git/v5 v5.4.2 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/go-cmp v0.5.6 // indirect | ||
github.com/imdario/mergo v0.3.13 // indirect | ||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect | ||
github.com/kevinburke/ssh_config v1.2.0 // indirect | ||
github.com/klauspost/compress v1.17.0 // indirect | ||
github.com/minio/highwayhash v1.0.2 // indirect | ||
github.com/mitchellh/go-homedir v1.1.0 // indirect | ||
github.com/nats-io/jwt/v2 v2.2.1-0.20220330180145-442af02fd36a // indirect | ||
github.com/nats-io/nkeys v0.4.5 // indirect | ||
github.com/nats-io/nuid v1.0.1 // indirect | ||
github.com/nxadm/tail v1.4.8 // indirect | ||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect | ||
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/russross/blackfriday/v2 v2.1.0 // indirect | ||
github.com/sergi/go-diff v1.2.0 // indirect | ||
github.com/test-go/testify v1.1.4 // indirect | ||
github.com/urfave/cli/v2 v2.14.0 // indirect | ||
github.com/xanzy/ssh-agent v0.3.2 // indirect | ||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect | ||
golang.org/x/mod v0.8.0 // indirect | ||
golang.org/x/sys v0.5.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect | ||
golang.org/x/tools v0.6.0 // indirect | ||
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect | ||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect | ||
gopkg.in/warnings.v0 v0.1.2 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) | ||
|
||
require ( | ||
github.com/google/uuid v1.3.0 | ||
github.com/miekg/dns v1.1.50 // indirect | ||
github.com/nats-io/nats.go v1.31.0 | ||
github.com/pkg/errors v0.9.1 | ||
github.com/stretchr/testify v1.7.1 | ||
go-micro.dev/v4 v4.9.0 | ||
golang.org/x/crypto v0.6.0 // indirect | ||
golang.org/x/net v0.6.0 // indirect | ||
golang.org/x/sync v0.1.0 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
) |
Oops, something went wrong.