1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-27 13:48:49 +08:00
Darko Draskovic 340e685d70
MF-1180 - Add redis based twins and states cache (#1184)
* Add twins redis cache

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add connectToRedis to twins main and twinCache to twins service

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add tracing to twins cache

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add twins cache mock and test setup for redis cache

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add TestTwinSave to redis twins cache tests

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add remove twin redis cache test

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add channels param to CreateDefinition helper method in mocks

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add IDs test to redis twins cache

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Simplify senml rec array and attribute creation funcs by removing unnecessary params

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Align cache remove twin method with service remove twin method

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add cache funcs to twins save, update and remove

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add def SaveIDs to redis cache and ref to service SaveStates

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add TwinSaveIDs tests for redis cache

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add cache related env vars desc to README.md

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add twinid bson key constant

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add Update method to cache

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Integrate uuid unification related changes

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Use named arguments in interface method declarations

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Add env vars to docker-compose file

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Make parameter names in interface methods and implementations consistent

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>

* Wrap vars and consts in var and const blocks

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
2020-06-05 11:42:16 +02:00

97 lines
2.9 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package twins
import (
"context"
"time"
)
// Metadata stores arbitrary twin data
type Metadata map[string]interface{}
// Attribute stores individual attribute data
type Attribute struct {
Name string `json:"name"`
Channel string `json:"channel"`
Subtopic string `json:"subtopic"`
PersistState bool `json:"persist_state"`
}
// Definition stores entity's attributes
type Definition struct {
ID int `json:"id"`
Created time.Time `json:"created"`
Attributes []Attribute `json:"attributes"`
Delta int64 `json:"delta"`
}
// Twin is a Mainflux data system representation. Each twin is owned
// by a single user, and is assigned with the unique identifier.
type Twin struct {
Owner string
ID string
Name string
Created time.Time
Updated time.Time
Revision int
Definitions []Definition
Metadata Metadata
}
// PageMetadata contains page metadata that helps navigation.
type PageMetadata struct {
Total uint64
Offset uint64
Limit uint64
}
// Page contains page related metadata as well as a list of twins that
// belong to this page.
type Page struct {
PageMetadata
Twins []Twin
}
// TwinRepository specifies a twin persistence API.
type TwinRepository interface {
// Save persists the twin
Save(ctx context.Context, twin Twin) (string, error)
// Update performs an update to the existing twin. A non-nil error is
// returned to indicate operation failure.
Update(ctx context.Context, twin Twin) error
// RetrieveByID retrieves the twin having the provided identifier.
RetrieveByID(ctx context.Context, twinID string) (Twin, error)
// RetrieveByAttribute retrieves twin ids whose definition contains
// the attribute with given channel and subtopic
RetrieveByAttribute(ctx context.Context, channel, subtopic string) ([]string, error)
// RetrieveAll retrieves the subset of twins owned by the specified user.
RetrieveAll(ctx context.Context, owner string, offset, limit uint64, name string, metadata Metadata) (Page, error)
// Remove removes the twin having the provided identifier.
Remove(ctx context.Context, twinID string) error
}
// TwinCache contains twin caching interface.
type TwinCache interface {
// Save stores twin ID as element of channel-subtopic keyed set and vice versa.
Save(ctx context.Context, twin Twin) error
// SaveIDs stores twin IDs as elements of channel-subtopic keyed set and vice versa.
SaveIDs(ctx context.Context, channel, subtopic string, twinIDs []string) error
// Update updates update twin id and channel-subtopic attributes mapping
Update(ctx context.Context, twin Twin) error
// ID returns twin IDs for given attribute.
IDs(ctx context.Context, channel, subtopic string) ([]string, error)
// Removes twin from cache based on twin id.
Remove(ctx context.Context, twinID string) error
}