mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00

* Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
156 lines
3.0 KiB
Go
156 lines
3.0 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package mocks
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/mainflux/mainflux/things"
|
|
)
|
|
|
|
var _ things.ThingRepository = (*thingRepositoryMock)(nil)
|
|
|
|
type thingRepositoryMock struct {
|
|
mu sync.Mutex
|
|
counter uint64
|
|
things map[string]things.Thing
|
|
}
|
|
|
|
// NewThingRepository creates in-memory thing repository.
|
|
func NewThingRepository() things.ThingRepository {
|
|
return &thingRepositoryMock{
|
|
things: make(map[string]things.Thing),
|
|
}
|
|
}
|
|
|
|
func (trm *thingRepositoryMock) Save(thing things.Thing) (string, error) {
|
|
trm.mu.Lock()
|
|
defer trm.mu.Unlock()
|
|
|
|
trm.counter++
|
|
thing.ID = strconv.FormatUint(trm.counter, 10)
|
|
trm.things[key(thing.Owner, thing.ID)] = thing
|
|
|
|
return thing.ID, nil
|
|
}
|
|
|
|
func (trm *thingRepositoryMock) Update(thing things.Thing) error {
|
|
trm.mu.Lock()
|
|
defer trm.mu.Unlock()
|
|
|
|
dbKey := key(thing.Owner, thing.ID)
|
|
|
|
if _, ok := trm.things[dbKey]; !ok {
|
|
return things.ErrNotFound
|
|
}
|
|
|
|
trm.things[dbKey] = thing
|
|
|
|
return nil
|
|
}
|
|
|
|
func (trm *thingRepositoryMock) RetrieveByID(owner, id string) (things.Thing, error) {
|
|
if c, ok := trm.things[key(owner, id)]; ok {
|
|
return c, nil
|
|
}
|
|
|
|
return things.Thing{}, things.ErrNotFound
|
|
}
|
|
|
|
func (trm *thingRepositoryMock) RetrieveAll(owner string, offset, limit uint64) []things.Thing {
|
|
things := make([]things.Thing, 0)
|
|
|
|
if offset < 0 || limit <= 0 {
|
|
return things
|
|
}
|
|
|
|
first := uint64(offset) + 1
|
|
last := first + uint64(limit)
|
|
|
|
// This obscure way to examine map keys is enforced by the key structure
|
|
// itself (see mocks/commons.go).
|
|
prefix := fmt.Sprintf("%s-", owner)
|
|
for k, v := range trm.things {
|
|
id, _ := strconv.ParseUint(v.ID, 10, 64)
|
|
if strings.HasPrefix(k, prefix) && id >= first && id < last {
|
|
things = append(things, v)
|
|
}
|
|
}
|
|
|
|
sort.SliceStable(things, func(i, j int) bool {
|
|
return things[i].ID < things[j].ID
|
|
})
|
|
|
|
return things
|
|
}
|
|
|
|
func (trm *thingRepositoryMock) Remove(owner, id string) error {
|
|
delete(trm.things, key(owner, id))
|
|
return nil
|
|
}
|
|
|
|
func (trm *thingRepositoryMock) RetrieveByKey(key string) (string, error) {
|
|
for _, thing := range trm.things {
|
|
if thing.Key == key {
|
|
return thing.ID, nil
|
|
}
|
|
}
|
|
|
|
return "", things.ErrNotFound
|
|
}
|
|
|
|
type thingCacheMock struct {
|
|
mu sync.Mutex
|
|
things map[string]string
|
|
}
|
|
|
|
// NewThingCache returns mock cache instance.
|
|
func NewThingCache() things.ThingCache {
|
|
return &thingCacheMock{
|
|
things: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
func (tcm *thingCacheMock) Save(key, id string) error {
|
|
tcm.mu.Lock()
|
|
defer tcm.mu.Unlock()
|
|
|
|
tcm.things[key] = id
|
|
return nil
|
|
}
|
|
|
|
func (tcm *thingCacheMock) ID(key string) (string, error) {
|
|
tcm.mu.Lock()
|
|
defer tcm.mu.Unlock()
|
|
|
|
id, ok := tcm.things[key]
|
|
if !ok {
|
|
return "", things.ErrNotFound
|
|
}
|
|
|
|
return id, nil
|
|
}
|
|
|
|
func (tcm *thingCacheMock) Remove(id string) error {
|
|
tcm.mu.Lock()
|
|
defer tcm.mu.Unlock()
|
|
|
|
for key, val := range tcm.things {
|
|
if val == id {
|
|
delete(tcm.things, key)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return things.ErrNotFound
|
|
}
|