| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package seenstate |
| 18 |
|
| 19 |
import ( |
| 20 |
"database/sql" |
| 21 |
"fmt" |
| 22 |
"strings" |
| 23 |
|
| 24 |
_ "github.com/mattn/go-sqlite3" |
| 25 |
|
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/protocol" |
| 27 |
) |
| 28 |
|
| 29 |
type sqliteSeenState struct { |
| 30 |
db *sql.DB |
| 31 |
} |
| 32 |
|
| 33 |
// NewSqliteSeenState returns an implementation of SeenState that |
| 34 |
// keeps and persists the state in an sqlite database. |
| 35 |
func NewSqliteSeenState(filename string) (SeenState, error) { |
| 36 |
db, err := sql.Open("sqlite3", filename) |
| 37 |
if err != nil { |
| 38 |
return nil, fmt.Errorf("cannot open sqlite level map %#v: %v", filename, err) |
| 39 |
} |
| 40 |
_, err = db.Exec("CREATE TABLE IF NOT EXISTS level_map (level text primary key, top integer)") |
| 41 |
if err != nil { |
| 42 |
return nil, fmt.Errorf("cannot (re)create sqlite level map table: %v", err) |
| 43 |
} |
| 44 |
_, err = db.Exec("CREATE TABLE IF NOT EXISTS seen_msgs (id text primary key)") |
| 45 |
if err != nil { |
| 46 |
return nil, fmt.Errorf("cannot (re)create sqlite seen msgs table: %v", err) |
| 47 |
} |
| 48 |
return &sqliteSeenState{db}, nil |
| 49 |
} |
| 50 |
|
| 51 |
// Closes closes the underlying db. |
| 52 |
func (ps *sqliteSeenState) Close() { |
| 53 |
ps.db.Close() |
| 54 |
} |
| 55 |
|
| 56 |
func (ps *sqliteSeenState) SetLevel(level string, top int64) error { |
| 57 |
_, err := ps.db.Exec("REPLACE INTO level_map (level, top) VALUES (?, ?)", level, top) |
| 58 |
if err != nil { |
| 59 |
return fmt.Errorf("cannot set %#v to %#v in level map: %v", level, top, err) |
| 60 |
} |
| 61 |
return nil |
| 62 |
} |
| 63 |
func (ps *sqliteSeenState) GetAllLevels() (map[string]int64, error) { |
| 64 |
rows, err := ps.db.Query("SELECT * FROM level_map") |
| 65 |
if err != nil { |
| 66 |
return nil, fmt.Errorf("cannot retrieve levels from sqlite level map: %v", err) |
| 67 |
} |
| 68 |
m := map[string]int64{} |
| 69 |
for rows.Next() { |
| 70 |
var level string |
| 71 |
var top int64 |
| 72 |
err = rows.Scan(&level, &top) |
| 73 |
if err != nil { |
| 74 |
return nil, fmt.Errorf("cannot read level from sqlite level map: %v", err) |
| 75 |
} |
| 76 |
m[level] = top |
| 77 |
} |
| 78 |
return m, nil |
| 79 |
} |
| 80 |
|
| 81 |
func (ps *sqliteSeenState) dropPrevThan(msgId string) error { |
| 82 |
_, err := ps.db.Exec("DELETE FROM seen_msgs WHERE rowid < (SELECT rowid FROM seen_msgs WHERE id = ?)", msgId) |
| 83 |
return err |
| 84 |
} |
| 85 |
|
| 86 |
func (ps *sqliteSeenState) FilterBySeen(notifs []protocol.Notification) ([]protocol.Notification, error) { |
| 87 |
if len(notifs) == 0 { |
| 88 |
return nil, nil |
| 89 |
} |
| 90 |
acc := make([]protocol.Notification, 0, len(notifs)) |
| 91 |
for _, notif := range notifs { |
| 92 |
_, err := ps.db.Exec("INSERT INTO seen_msgs (id) VALUES (?)", notif.MsgId) |
| 93 |
if err != nil { |
| 94 |
if strings.HasSuffix(err.Error(), "UNIQUE constraint failed: seen_msgs.id") { |
| 95 |
continue |
| 96 |
} |
| 97 |
return nil, fmt.Errorf("cannot insert %#v in seen msgs: %v", notif.MsgId, err) |
| 98 |
} |
| 99 |
acc = append(acc, notif) |
| 100 |
} |
| 101 |
err := ps.dropPrevThan(notifs[0].MsgId) |
| 102 |
if err != nil { |
| 103 |
return nil, fmt.Errorf("cannot delete obsolete seen msgs: %v", err) |
| 104 |
} |
| 105 |
return acc, nil |
| 106 |
} |