2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-08-06 17:06:55 +02:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2019-03-15 18:38:07 +01:00
|
|
|
"fmt"
|
2019-04-20 14:09:11 +02:00
|
|
|
|
2018-08-06 17:06:55 +02:00
|
|
|
"github.com/gocql/gocql"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
"github.com/mainflux/mainflux/readers"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ readers.MessageRepository = (*cassandraRepository)(nil)
|
|
|
|
|
|
|
|
type cassandraRepository struct {
|
|
|
|
session *gocql.Session
|
|
|
|
}
|
|
|
|
|
|
|
|
// New instantiates Cassandra message repository.
|
|
|
|
func New(session *gocql.Session) readers.MessageRepository {
|
2019-05-07 15:59:18 +02:00
|
|
|
return cassandraRepository{
|
|
|
|
session: session,
|
|
|
|
}
|
2018-08-06 17:06:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 15:59:18 +02:00
|
|
|
func (cr cassandraRepository) ReadAll(chanID string, offset, limit uint64, query map[string]string) (readers.MessagesPage, error) {
|
2019-04-25 00:18:43 +02:00
|
|
|
names := []string{}
|
|
|
|
vals := []interface{}{chanID}
|
|
|
|
for name, val := range query {
|
|
|
|
names = append(names, name)
|
|
|
|
vals = append(vals, val)
|
|
|
|
}
|
|
|
|
vals = append(vals, offset+limit)
|
|
|
|
|
|
|
|
selectCQL := buildSelectQuery(chanID, offset, limit, names)
|
|
|
|
countCQL := buildCountQuery(chanID, names)
|
2018-08-06 17:06:55 +02:00
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
iter := cr.session.Query(selectCQL, vals...).Iter()
|
2019-05-07 15:59:18 +02:00
|
|
|
defer iter.Close()
|
2018-08-06 17:06:55 +02:00
|
|
|
scanner := iter.Scanner()
|
|
|
|
|
|
|
|
// skip first OFFSET rows
|
|
|
|
for i := uint64(0); i < offset; i++ {
|
|
|
|
if !scanner.Next() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
var floatVal, valueSum *float64
|
|
|
|
var strVal, dataVal *string
|
|
|
|
var boolVal *bool
|
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
page := readers.MessagesPage{
|
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
|
|
|
Messages: []mainflux.Message{},
|
|
|
|
}
|
2018-08-06 17:06:55 +02:00
|
|
|
for scanner.Next() {
|
|
|
|
var msg mainflux.Message
|
2019-04-25 00:18:43 +02:00
|
|
|
err := scanner.Scan(&msg.Channel, &msg.Subtopic, &msg.Publisher, &msg.Protocol,
|
2018-11-05 19:18:51 +01:00
|
|
|
&msg.Name, &msg.Unit, &floatVal, &strVal, &boolVal,
|
|
|
|
&dataVal, &valueSum, &msg.Time, &msg.UpdateTime, &msg.Link)
|
2019-04-25 00:18:43 +02:00
|
|
|
if err != nil {
|
2019-05-07 15:59:18 +02:00
|
|
|
return readers.MessagesPage{}, err
|
2019-04-25 00:18:43 +02:00
|
|
|
}
|
2018-11-05 19:18:51 +01:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case floatVal != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_FloatValue{FloatValue: *floatVal}
|
2018-11-05 19:18:51 +01:00
|
|
|
case strVal != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_StringValue{StringValue: *strVal}
|
2018-11-05 19:18:51 +01:00
|
|
|
case boolVal != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_BoolValue{BoolValue: *boolVal}
|
2018-11-05 19:18:51 +01:00
|
|
|
case dataVal != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_DataValue{DataValue: *dataVal}
|
2018-11-05 19:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if valueSum != nil {
|
|
|
|
msg.ValueSum = &mainflux.SumValue{Value: *valueSum}
|
|
|
|
}
|
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
page.Messages = append(page.Messages, msg)
|
2018-08-06 17:06:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
if err := cr.session.Query(countCQL, vals[:len(vals)-1]...).Scan(&page.Total); err != nil {
|
2019-05-07 15:59:18 +02:00
|
|
|
return readers.MessagesPage{}, err
|
2018-08-06 17:06:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 15:59:18 +02:00
|
|
|
return page, nil
|
2018-08-06 17:06:55 +02:00
|
|
|
}
|
2019-03-15 18:38:07 +01:00
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
func buildSelectQuery(chanID string, offset, limit uint64, names []string) string {
|
|
|
|
var condCQL string
|
2019-03-15 18:38:07 +01:00
|
|
|
cql := `SELECT channel, subtopic, publisher, protocol, name, unit,
|
2019-04-25 00:18:43 +02:00
|
|
|
value, string_value, bool_value, data_value, value_sum, time,
|
2019-05-07 15:59:18 +02:00
|
|
|
update_time, link FROM messages WHERE channel = ? %s LIMIT ?
|
2019-03-15 18:38:07 +01:00
|
|
|
ALLOW FILTERING`
|
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
for _, name := range names {
|
|
|
|
switch name {
|
|
|
|
case
|
|
|
|
"channel",
|
|
|
|
"subtopic",
|
|
|
|
"publisher",
|
|
|
|
"name",
|
|
|
|
"protocol":
|
|
|
|
condCQL = fmt.Sprintf(`%s AND %s = ?`, condCQL, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(cql, condCQL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildCountQuery(chanID string, names []string) string {
|
|
|
|
var condCQL string
|
|
|
|
cql := `SELECT COUNT(*) FROM messages WHERE channel = ? %s ALLOW FILTERING`
|
2019-03-15 18:38:07 +01:00
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
for _, name := range names {
|
2019-03-15 18:38:07 +01:00
|
|
|
switch name {
|
|
|
|
case
|
|
|
|
"channel",
|
|
|
|
"subtopic",
|
|
|
|
"publisher",
|
|
|
|
"name",
|
|
|
|
"protocol":
|
2019-04-25 00:18:43 +02:00
|
|
|
condCQL = fmt.Sprintf(`%s AND %s = ?`, condCQL, name)
|
2019-03-15 18:38:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
return fmt.Sprintf(cql, condCQL)
|
2019-03-15 18:38:07 +01:00
|
|
|
}
|