Skip to content

Commit

Permalink
Merge pull request #132 from kobergj/NatjsKVStore
Browse files Browse the repository at this point in the history
Add Natjs KeyValue Store
  • Loading branch information
asim authored Dec 26, 2023
2 parents f7f8d32 + 729fa0b commit 94a49ba
Show file tree
Hide file tree
Showing 12 changed files with 1,757 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ linters-settings:
min-occurrences: 3
depguard:
rules:
main:
deny:
- pkg: "github.com/golang/protobuf/proto"
desc: not allowed
main:
deny:
- pkg: "github.com/golang/protobuf/proto"
desc: not allowed
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
Expand Down
3 changes: 2 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.18
go 1.21

use (
./v4/acme/certmagic
Expand Down Expand Up @@ -84,6 +84,7 @@ use (
./v4/store/memory
./v4/store/mysql
./v4/store/nats-js
./v4/store/nats-js-kv
./v4/store/redis
./v4/sync/consul
./v4/sync/etcd
Expand Down
79 changes: 79 additions & 0 deletions v4/store/nats-js-kv/README.md
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()
```

18 changes: 18 additions & 0 deletions v4/store/nats-js-kv/context.go
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)
}
}
66 changes: 66 additions & 0 deletions v4/store/nats-js-kv/go.mod
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
)
Loading

0 comments on commit 94a49ba

Please sign in to comment.