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/readers"
|
2019-11-05 11:57:16 +01:00
|
|
|
"github.com/mainflux/mainflux/transformers/senml"
|
2018-08-06 17:06:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 00:18:43 +02:00
|
|
|
page := readers.MessagesPage{
|
|
|
|
Offset: offset,
|
|
|
|
Limit: limit,
|
2019-11-05 11:57:16 +01:00
|
|
|
Messages: []senml.Message{},
|
2019-04-25 00:18:43 +02:00
|
|
|
}
|
2019-11-05 11:57:16 +01:00
|
|
|
|
2018-08-06 17:06:55 +02:00
|
|
|
for scanner.Next() {
|
2019-11-05 11:57:16 +01:00
|
|
|
var msg senml.Message
|
2019-04-25 00:18:43 +02:00
|
|
|
err := scanner.Scan(&msg.Channel, &msg.Subtopic, &msg.Publisher, &msg.Protocol,
|
2019-11-05 11:57:16 +01:00
|
|
|
&msg.Name, &msg.Unit, &msg.Value, &msg.StringValue, &msg.BoolValue,
|
2019-11-29 20:47:28 +01:00
|
|
|
&msg.DataValue, &msg.Sum, &msg.Time, &msg.UpdateTime)
|
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
|
|
|
}
|
|
|
|
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-11-05 11:57:16 +01:00
|
|
|
value, string_value, bool_value, data_value, sum, time,
|
2019-11-29 20:47:28 +01:00
|
|
|
update_time 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
|
|
|
}
|