Merge lp:~chipaca/ubuntu-push/introducing-sqlevelmap into lp:ubuntu-push

Proposed by John Lenton
Status: Merged
Approved by: Samuele Pedroni
Approved revision: 66
Merged at revision: 65
Proposed branch: lp:~chipaca/ubuntu-push/introducing-sqlevelmap
Merge into: lp:ubuntu-push
Prerequisite: lp:~chipaca/ubuntu-push/prepersistance
Diff against target: 219 lines (+165/-8)
5 files modified
Makefile (+1/-0)
client/session/levelmap/levelmap.go (+3/-6)
client/session/levelmap/sqlevelmap.go (+66/-0)
client/session/levelmap/sqlevelmap_test.go (+92/-0)
dependencies.tsv (+3/-2)
To merge this branch: bzr merge lp:~chipaca/ubuntu-push/introducing-sqlevelmap
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+205482@code.launchpad.net

Commit message

this is sqlevelmap

Description of the change

Here's sqliteLevelMap. Guess what it does.

To post a comment you must log in.
Revision history for this message
Samuele Pedroni (pedronis) wrote :

there is need to update the Makefile to grab the dependency or specify in the README which package to install,

there is still GOPATH tweaking to be done then though

review: Needs Fixing
Revision history for this message
John Lenton (chipaca) wrote :

Updated Makefile and dependencies.tsv

Revision history for this message
Samuele Pedroni (pedronis) wrote :

I needed this variant of the Makefile change, otherwise it explodes in the build bit: GODEPS += code.google.com/p/gosqlite/sqlite3

Revision history for this message
Samuele Pedroni (pedronis) wrote :

PACKAGE DOCUMENTATION

package levelmap
    import "/home/pedronis/canonical/go-ws/src/launchpad.net/ubuntu-push/client/session/levelmap"

    levelmap holds an implementation of the LevelMap that the client session

^^^ implementations

    uses to keep track of what messages it has seen.

TYPES

type LevelMap interface {
    // Set() (re)sets the given level to the given value.
    Set(level string, top int64) error
    // GetAll() returns a "simple" map of the current levels.
    GetAll() (map[string]int64, error)
}

func NewLevelMap() (LevelMap, error)
    default constructor

^^^ in-memory something something

func NewSqliteLevelMap(filename string) (LevelMap, error)

^^^ sqlite-backed something something

review: Approve
Revision history for this message
Ubuntu One Auto Pilot (otto-pilot) wrote :

The attempt to merge lp:~chipaca/ubuntu-push/introducing-sqlevelmap into lp:ubuntu-push failed. Below is the output from the failed tests.

mkdir -p /mnt/tarmac/cache/ubuntu-push/go-ws/bin
mkdir -p /mnt/tarmac/cache/ubuntu-push/go-ws/pkg
go get -u launchpad.net/godeps
go get -d -u launchpad.net/gocheck launchpad.net/go-dbus/v1 code.google.com/p/gosqlite/sqlite3

go: missing Mercurial command. See http://golang.org/s/gogetcmd
package launchpad.net/gocheck
 imports launchpad.net/go-dbus/v1
 imports code.google.com/p/gosqlite/sqlite3: exec: "hg": executable file not found in $PATH
make: *** [bootstrap] Error 1

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'Makefile'
--- Makefile 2014-01-20 12:36:21 +0000
+++ Makefile 2014-02-12 13:52:27 +0000
@@ -9,6 +9,7 @@
99
10GODEPS = launchpad.net/gocheck10GODEPS = launchpad.net/gocheck
11GODEPS += launchpad.net/go-dbus/v111GODEPS += launchpad.net/go-dbus/v1
12GODEPS += code.google.com/p/gosqlite/sqlite3
1213
13bootstrap:14bootstrap:
14 mkdir -p $(GOPATH)/bin15 mkdir -p $(GOPATH)/bin
1516
=== modified file 'client/session/levelmap/levelmap.go'
--- client/session/levelmap/levelmap.go 2014-02-08 13:50:58 +0000
+++ client/session/levelmap/levelmap.go 2014-02-12 13:52:27 +0000
@@ -14,14 +14,10 @@
14 with this program. If not, see <http://www.gnu.org/licenses/>.14 with this program. If not, see <http://www.gnu.org/licenses/>.
15*/15*/
1616
17// levelmap holds an implementation of the LevelMap that the client17// levelmap holds implementations of the LevelMap that the client
18// session uses to keep track of what messages it has seen.18// session uses to keep track of what messages it has seen.
19package levelmap19package levelmap
2020
21// This implementation is memory-based and does not save state. There
22// is another one that stores the levels in sqlite that is missing a
23// few dependencies still.
24
25type LevelMap interface {21type LevelMap interface {
26 // Set() (re)sets the given level to the given value.22 // Set() (re)sets the given level to the given value.
27 Set(level string, top int64) error23 Set(level string, top int64) error
@@ -41,7 +37,8 @@
4137
42var _ LevelMap = &mapLevelMap{}38var _ LevelMap = &mapLevelMap{}
4339
44// default constructor40// NewLevelMap returns an implementation of LevelMap that is memory-based and
41// does not save state.
45func NewLevelMap() (LevelMap, error) {42func NewLevelMap() (LevelMap, error) {
46 return &mapLevelMap{}, nil43 return &mapLevelMap{}, nil
47}44}
4845
=== added file 'client/session/levelmap/sqlevelmap.go'
--- client/session/levelmap/sqlevelmap.go 1970-01-01 00:00:00 +0000
+++ client/session/levelmap/sqlevelmap.go 2014-02-12 13:52:27 +0000
@@ -0,0 +1,66 @@
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
17package levelmap
18
19import (
20 _ "code.google.com/p/gosqlite/sqlite3"
21 "database/sql"
22 "fmt"
23)
24
25type sqliteLevelMap struct {
26 db *sql.DB
27}
28
29// NewSqliteLevelMap returns an implementation of LevelMap that
30// persists the map in an sqlite database.
31func NewSqliteLevelMap(filename string) (LevelMap, error) {
32 db, err := sql.Open("sqlite3", filename)
33 if err != nil {
34 return nil, fmt.Errorf("cannot open sqlite level map %#v: %v", filename, err)
35 }
36 _, err = db.Exec("CREATE TABLE IF NOT EXISTS level_map (level text primary key, top integer)")
37 if err != nil {
38 return nil, fmt.Errorf("cannot (re)create sqlite level map table: %v", err)
39 }
40 return &sqliteLevelMap{db}, nil
41}
42
43func (pm *sqliteLevelMap) Set(level string, top int64) error {
44 _, err := pm.db.Exec("REPLACE INTO level_map (level, top) VALUES (?, ?)", level, top)
45 if err != nil {
46 return fmt.Errorf("cannot set %#v to %#v in level map: %v", level, top, err)
47 }
48 return nil
49}
50func (pm *sqliteLevelMap) GetAll() (map[string]int64, error) {
51 rows, err := pm.db.Query("SELECT * FROM level_map")
52 if err != nil {
53 return nil, fmt.Errorf("cannot retrieve levels from sqlite level map: %v", err)
54 }
55 m := map[string]int64{}
56 for rows.Next() {
57 var level string
58 var top int64
59 err = rows.Scan(&level, &top)
60 if err != nil {
61 return nil, fmt.Errorf("cannot read level from sqlite level map: %v", err)
62 }
63 m[level] = top
64 }
65 return m, nil
66}
067
=== added file 'client/session/levelmap/sqlevelmap_test.go'
--- client/session/levelmap/sqlevelmap_test.go 1970-01-01 00:00:00 +0000
+++ client/session/levelmap/sqlevelmap_test.go 2014-02-12 13:52:27 +0000
@@ -0,0 +1,92 @@
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
17package levelmap
18
19import (
20 _ "code.google.com/p/gosqlite/sqlite3"
21 "database/sql"
22 . "launchpad.net/gocheck"
23)
24
25type sqlmSuite struct{ lmSuite }
26
27var _ = Suite(&sqlmSuite{})
28
29func (s *sqlmSuite) SetUpSuite(c *C) {
30 s.constructor = func() (LevelMap, error) { return NewSqliteLevelMap(":memory:") }
31}
32
33func (s *sqlmSuite) TestNewCanFail(c *C) {
34 m, err := NewSqliteLevelMap("/does/not/exist")
35 c.Assert(m, IsNil)
36 c.Check(err, NotNil)
37}
38
39func (s *sqlmSuite) TestSetCanFail(c *C) {
40 dir := c.MkDir()
41 filename := dir + "test.db"
42 db, err := sql.Open("sqlite3", filename)
43 c.Assert(err, IsNil)
44 // create the wrong kind of table
45 _, err = db.Exec("CREATE TABLE level_map (foo)")
46 c.Assert(err, IsNil)
47 // <evil laughter>
48 m, err := NewSqliteLevelMap(filename)
49 c.Check(err, IsNil)
50 c.Assert(m, NotNil)
51 err = m.Set("foo", 42)
52 c.Check(err, ErrorMatches, "cannot set .*")
53}
54
55func (s *sqlmSuite) TestGetAllCanFail(c *C) {
56 dir := c.MkDir()
57 filename := dir + "test.db"
58 db, err := sql.Open("sqlite3", filename)
59 c.Assert(err, IsNil)
60 // create the wrong kind of table
61 _, err = db.Exec("CREATE TABLE level_map AS SELECT 'what'")
62 c.Assert(err, IsNil)
63 // <evil laughter>
64 m, err := NewSqliteLevelMap(filename)
65 c.Check(err, IsNil)
66 c.Assert(m, NotNil)
67 all, err := m.GetAll()
68 c.Check(all, IsNil)
69 c.Check(err, ErrorMatches, "cannot read level .*")
70}
71
72func (s *sqlmSuite) TestGetAllCanFailDifferently(c *C) {
73 dir := c.MkDir()
74 filename := dir + "test.db"
75 db, err := sql.Open("sqlite3", filename)
76 c.Assert(err, IsNil)
77 // create a view with the name the table will have
78 _, err = db.Exec("CREATE TABLE foo (foo)")
79 c.Assert(err, IsNil)
80 _, err = db.Exec("CREATE VIEW level_map AS SELECT * FROM foo")
81 c.Assert(err, IsNil)
82 // break the view
83 _, err = db.Exec("DROP TABLE foo")
84 c.Assert(err, IsNil)
85 // <evil laughter>
86 m, err := NewSqliteLevelMap(filename)
87 c.Check(err, IsNil)
88 c.Assert(m, NotNil)
89 all, err := m.GetAll()
90 c.Check(all, IsNil)
91 c.Check(err, ErrorMatches, "cannot retrieve levels .*")
92}
093
=== modified file 'dependencies.tsv'
--- dependencies.tsv 2014-01-20 06:23:17 +0000
+++ dependencies.tsv 2014-02-12 13:52:27 +0000
@@ -1,2 +1,3 @@
1launchpad.net/go-dbus/v1 bzr james@jamesh.id.au-20140117100040-mmhdtiz5w9pxqtcr 1241code.google.com/p/gosqlite hg 74691fb6f83716190870cde1b658538dd4b18eb0 15
2launchpad.net/gocheck bzr gustavo@niemeyer.net-20130302024745-6ikofwq2c03h7giu 852launchpad.net/go-dbus/v1 bzr james@jamesh.id.au-20140206110213-pbzcr6ucaz3rqmnw 125
3launchpad.net/gocheck bzr gustavo@niemeyer.net-20140127131816-zshobk1qqme626xw 86

Subscribers

People subscribed via source and target branches