2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-11-20 19:46:33 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2019-07-18 15:01:09 +02:00
|
|
|
"context"
|
|
|
|
|
2021-05-20 20:53:56 +02:00
|
|
|
"github.com/go-redis/redis/v8"
|
2018-11-20 19:46:33 +01:00
|
|
|
"github.com/mainflux/mainflux/things"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
streamID = "mainflux.things"
|
|
|
|
streamLen = 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ things.Service = (*eventStore)(nil)
|
|
|
|
|
|
|
|
type eventStore struct {
|
|
|
|
svc things.Service
|
|
|
|
client *redis.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEventStoreMiddleware returns wrapper around things service that sends
|
|
|
|
// events to event store.
|
|
|
|
func NewEventStoreMiddleware(svc things.Service, client *redis.Client) things.Service {
|
|
|
|
return eventStore{
|
|
|
|
svc: svc,
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
func (es eventStore) CreateThings(ctx context.Context, token string, ths ...things.Thing) ([]things.Thing, error) {
|
|
|
|
sths, err := es.svc.CreateThings(ctx, token, ths...)
|
2019-10-29 05:59:54 -06:00
|
|
|
if err != nil {
|
|
|
|
return sths, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, thing := range sths {
|
|
|
|
event := createThingEvent{
|
|
|
|
id: thing.ID,
|
|
|
|
owner: thing.Owner,
|
|
|
|
name: thing.Name,
|
|
|
|
metadata: thing.Metadata,
|
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
2021-05-20 20:53:56 +02:00
|
|
|
es.client.XAdd(ctx, record).Err()
|
2019-10-29 05:59:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return sths, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) UpdateThing(ctx context.Context, token string, thing things.Thing) error {
|
|
|
|
if err := es.svc.UpdateThing(ctx, token, thing); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := updateThingEvent{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: thing.ID,
|
2018-11-20 19:46:33 +01:00
|
|
|
name: thing.Name,
|
|
|
|
metadata: thing.Metadata,
|
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
2021-05-20 20:53:56 +02:00
|
|
|
es.client.XAdd(ctx, record).Err()
|
2018-11-20 19:46:33 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
// UpdateKey doesn't send event because key shouldn't be sent over stream.
|
|
|
|
// Maybe we can start publishing this event at some point, without key value
|
|
|
|
// in order to notify adapters to disconnect connected things after key update.
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) UpdateKey(ctx context.Context, token, id, key string) error {
|
|
|
|
return es.svc.UpdateKey(ctx, token, id, key)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 00:38:28 +02:00
|
|
|
func (es eventStore) ShareThing(ctx context.Context, token, thingID string, actions, userIDs []string) error {
|
|
|
|
return es.svc.ShareThing(ctx, token, thingID, actions, userIDs)
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) ViewThing(ctx context.Context, token, id string) (things.Thing, error) {
|
|
|
|
return es.svc.ViewThing(ctx, token, id)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
func (es eventStore) ListThings(ctx context.Context, token string, pm things.PageMetadata) (things.Page, error) {
|
|
|
|
return es.svc.ListThings(ctx, token, pm)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
func (es eventStore) ListThingsByChannel(ctx context.Context, token, chID string, pm things.PageMetadata) (things.Page, error) {
|
|
|
|
return es.svc.ListThingsByChannel(ctx, token, chID, pm)
|
2019-04-25 14:37:51 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) RemoveThing(ctx context.Context, token, id string) error {
|
|
|
|
if err := es.svc.RemoveThing(ctx, token, id); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := removeThingEvent{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: id,
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
2021-05-20 20:53:56 +02:00
|
|
|
es.client.XAdd(ctx, record).Err()
|
2018-11-20 19:46:33 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
func (es eventStore) CreateChannels(ctx context.Context, token string, channels ...things.Channel) ([]things.Channel, error) {
|
|
|
|
schs, err := es.svc.CreateChannels(ctx, token, channels...)
|
2019-10-29 05:59:54 -06:00
|
|
|
if err != nil {
|
|
|
|
return schs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range schs {
|
|
|
|
event := createChannelEvent{
|
|
|
|
id: channel.ID,
|
|
|
|
owner: channel.Owner,
|
|
|
|
name: channel.Name,
|
|
|
|
metadata: channel.Metadata,
|
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
2021-05-20 20:53:56 +02:00
|
|
|
es.client.XAdd(ctx, record).Err()
|
2019-10-29 05:59:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return schs, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) UpdateChannel(ctx context.Context, token string, channel things.Channel) error {
|
|
|
|
if err := es.svc.UpdateChannel(ctx, token, channel); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := updateChannelEvent{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: channel.ID,
|
2018-11-28 15:58:48 +01:00
|
|
|
name: channel.Name,
|
|
|
|
metadata: channel.Metadata,
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
2021-05-20 20:53:56 +02:00
|
|
|
es.client.XAdd(ctx, record).Err()
|
2018-11-20 19:46:33 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) ViewChannel(ctx context.Context, token, id string) (things.Channel, error) {
|
|
|
|
return es.svc.ViewChannel(ctx, token, id)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
func (es eventStore) ListChannels(ctx context.Context, token string, pm things.PageMetadata) (things.ChannelsPage, error) {
|
|
|
|
return es.svc.ListChannels(ctx, token, pm)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
func (es eventStore) ListChannelsByThing(ctx context.Context, token, thID string, pm things.PageMetadata) (things.ChannelsPage, error) {
|
|
|
|
return es.svc.ListChannelsByThing(ctx, token, thID, pm)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) RemoveChannel(ctx context.Context, token, id string) error {
|
|
|
|
if err := es.svc.RemoveChannel(ctx, token, id); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := removeChannelEvent{
|
2018-12-05 13:09:25 +01:00
|
|
|
id: id,
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
2021-05-20 20:53:56 +02:00
|
|
|
es.client.XAdd(ctx, record).Err()
|
2018-11-20 19:46:33 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:44:31 -07:00
|
|
|
func (es eventStore) Connect(ctx context.Context, token string, chIDs, thIDs []string) error {
|
|
|
|
if err := es.svc.Connect(ctx, token, chIDs, thIDs); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:44:31 -07:00
|
|
|
for _, chID := range chIDs {
|
|
|
|
for _, thID := range thIDs {
|
|
|
|
event := connectThingEvent{
|
|
|
|
chanID: chID,
|
|
|
|
thingID: thID,
|
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
2021-05-20 20:53:56 +02:00
|
|
|
es.client.XAdd(ctx, record).Err()
|
2019-11-11 17:24:29 -07:00
|
|
|
}
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-10 01:59:12 +03:00
|
|
|
func (es eventStore) Disconnect(ctx context.Context, token string, chIDs, thIDs []string) error {
|
|
|
|
if err := es.svc.Disconnect(ctx, token, chIDs, thIDs); err != nil {
|
2018-11-20 19:46:33 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-10 01:59:12 +03:00
|
|
|
for _, chID := range chIDs {
|
|
|
|
for _, thID := range thIDs {
|
|
|
|
event := disconnectThingEvent{
|
|
|
|
chanID: chID,
|
|
|
|
thingID: thID,
|
|
|
|
}
|
|
|
|
record := &redis.XAddArgs{
|
|
|
|
Stream: streamID,
|
|
|
|
MaxLenApprox: streamLen,
|
|
|
|
Values: event.Encode(),
|
|
|
|
}
|
|
|
|
es.client.XAdd(ctx, record).Err()
|
|
|
|
}
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
func (es eventStore) CanAccessByKey(ctx context.Context, chanID string, key string) (string, error) {
|
|
|
|
return es.svc.CanAccessByKey(ctx, chanID, key)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) CanAccessByID(ctx context.Context, chanID string, thingID string) error {
|
|
|
|
return es.svc.CanAccessByID(ctx, chanID, thingID)
|
2019-07-15 18:28:15 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 19:41:59 +01:00
|
|
|
func (es eventStore) IsChannelOwner(ctx context.Context, owner, chanID string) error {
|
|
|
|
return es.svc.IsChannelOwner(ctx, owner, chanID)
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (es eventStore) Identify(ctx context.Context, key string) (string, error) {
|
|
|
|
return es.svc.Identify(ctx, key)
|
2018-11-20 19:46:33 +01:00
|
|
|
}
|
2020-11-23 11:34:29 +01:00
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (es eventStore) ListMembers(ctx context.Context, token, groupID string, pm things.PageMetadata) (things.Page, error) {
|
|
|
|
return es.svc.ListMembers(ctx, token, groupID, pm)
|
2020-11-23 11:34:29 +01:00
|
|
|
}
|