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
1=== modified file 'Makefile'
2--- Makefile 2014-01-20 12:36:21 +0000
3+++ Makefile 2014-02-12 13:52:27 +0000
4@@ -9,6 +9,7 @@
5
6 GODEPS = launchpad.net/gocheck
7 GODEPS += launchpad.net/go-dbus/v1
8+GODEPS += code.google.com/p/gosqlite/sqlite3
9
10 bootstrap:
11 mkdir -p $(GOPATH)/bin
12
13=== modified file 'client/session/levelmap/levelmap.go'
14--- client/session/levelmap/levelmap.go 2014-02-08 13:50:58 +0000
15+++ client/session/levelmap/levelmap.go 2014-02-12 13:52:27 +0000
16@@ -14,14 +14,10 @@
17 with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20-// levelmap holds an implementation of the LevelMap that the client
21+// levelmap holds implementations of the LevelMap that the client
22 // session uses to keep track of what messages it has seen.
23 package levelmap
24
25-// This implementation is memory-based and does not save state. There
26-// is another one that stores the levels in sqlite that is missing a
27-// few dependencies still.
28-
29 type LevelMap interface {
30 // Set() (re)sets the given level to the given value.
31 Set(level string, top int64) error
32@@ -41,7 +37,8 @@
33
34 var _ LevelMap = &mapLevelMap{}
35
36-// default constructor
37+// NewLevelMap returns an implementation of LevelMap that is memory-based and
38+// does not save state.
39 func NewLevelMap() (LevelMap, error) {
40 return &mapLevelMap{}, nil
41 }
42
43=== added file 'client/session/levelmap/sqlevelmap.go'
44--- client/session/levelmap/sqlevelmap.go 1970-01-01 00:00:00 +0000
45+++ client/session/levelmap/sqlevelmap.go 2014-02-12 13:52:27 +0000
46@@ -0,0 +1,66 @@
47+/*
48+ Copyright 2013-2014 Canonical Ltd.
49+
50+ This program is free software: you can redistribute it and/or modify it
51+ under the terms of the GNU General Public License version 3, as published
52+ by the Free Software Foundation.
53+
54+ This program is distributed in the hope that it will be useful, but
55+ WITHOUT ANY WARRANTY; without even the implied warranties of
56+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
57+ PURPOSE. See the GNU General Public License for more details.
58+
59+ You should have received a copy of the GNU General Public License along
60+ with this program. If not, see <http://www.gnu.org/licenses/>.
61+*/
62+
63+package levelmap
64+
65+import (
66+ _ "code.google.com/p/gosqlite/sqlite3"
67+ "database/sql"
68+ "fmt"
69+)
70+
71+type sqliteLevelMap struct {
72+ db *sql.DB
73+}
74+
75+// NewSqliteLevelMap returns an implementation of LevelMap that
76+// persists the map in an sqlite database.
77+func NewSqliteLevelMap(filename string) (LevelMap, error) {
78+ db, err := sql.Open("sqlite3", filename)
79+ if err != nil {
80+ return nil, fmt.Errorf("cannot open sqlite level map %#v: %v", filename, err)
81+ }
82+ _, err = db.Exec("CREATE TABLE IF NOT EXISTS level_map (level text primary key, top integer)")
83+ if err != nil {
84+ return nil, fmt.Errorf("cannot (re)create sqlite level map table: %v", err)
85+ }
86+ return &sqliteLevelMap{db}, nil
87+}
88+
89+func (pm *sqliteLevelMap) Set(level string, top int64) error {
90+ _, err := pm.db.Exec("REPLACE INTO level_map (level, top) VALUES (?, ?)", level, top)
91+ if err != nil {
92+ return fmt.Errorf("cannot set %#v to %#v in level map: %v", level, top, err)
93+ }
94+ return nil
95+}
96+func (pm *sqliteLevelMap) GetAll() (map[string]int64, error) {
97+ rows, err := pm.db.Query("SELECT * FROM level_map")
98+ if err != nil {
99+ return nil, fmt.Errorf("cannot retrieve levels from sqlite level map: %v", err)
100+ }
101+ m := map[string]int64{}
102+ for rows.Next() {
103+ var level string
104+ var top int64
105+ err = rows.Scan(&level, &top)
106+ if err != nil {
107+ return nil, fmt.Errorf("cannot read level from sqlite level map: %v", err)
108+ }
109+ m[level] = top
110+ }
111+ return m, nil
112+}
113
114=== added file 'client/session/levelmap/sqlevelmap_test.go'
115--- client/session/levelmap/sqlevelmap_test.go 1970-01-01 00:00:00 +0000
116+++ client/session/levelmap/sqlevelmap_test.go 2014-02-12 13:52:27 +0000
117@@ -0,0 +1,92 @@
118+/*
119+ Copyright 2013-2014 Canonical Ltd.
120+
121+ This program is free software: you can redistribute it and/or modify it
122+ under the terms of the GNU General Public License version 3, as published
123+ by the Free Software Foundation.
124+
125+ This program is distributed in the hope that it will be useful, but
126+ WITHOUT ANY WARRANTY; without even the implied warranties of
127+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
128+ PURPOSE. See the GNU General Public License for more details.
129+
130+ You should have received a copy of the GNU General Public License along
131+ with this program. If not, see <http://www.gnu.org/licenses/>.
132+*/
133+
134+package levelmap
135+
136+import (
137+ _ "code.google.com/p/gosqlite/sqlite3"
138+ "database/sql"
139+ . "launchpad.net/gocheck"
140+)
141+
142+type sqlmSuite struct{ lmSuite }
143+
144+var _ = Suite(&sqlmSuite{})
145+
146+func (s *sqlmSuite) SetUpSuite(c *C) {
147+ s.constructor = func() (LevelMap, error) { return NewSqliteLevelMap(":memory:") }
148+}
149+
150+func (s *sqlmSuite) TestNewCanFail(c *C) {
151+ m, err := NewSqliteLevelMap("/does/not/exist")
152+ c.Assert(m, IsNil)
153+ c.Check(err, NotNil)
154+}
155+
156+func (s *sqlmSuite) TestSetCanFail(c *C) {
157+ dir := c.MkDir()
158+ filename := dir + "test.db"
159+ db, err := sql.Open("sqlite3", filename)
160+ c.Assert(err, IsNil)
161+ // create the wrong kind of table
162+ _, err = db.Exec("CREATE TABLE level_map (foo)")
163+ c.Assert(err, IsNil)
164+ // <evil laughter>
165+ m, err := NewSqliteLevelMap(filename)
166+ c.Check(err, IsNil)
167+ c.Assert(m, NotNil)
168+ err = m.Set("foo", 42)
169+ c.Check(err, ErrorMatches, "cannot set .*")
170+}
171+
172+func (s *sqlmSuite) TestGetAllCanFail(c *C) {
173+ dir := c.MkDir()
174+ filename := dir + "test.db"
175+ db, err := sql.Open("sqlite3", filename)
176+ c.Assert(err, IsNil)
177+ // create the wrong kind of table
178+ _, err = db.Exec("CREATE TABLE level_map AS SELECT 'what'")
179+ c.Assert(err, IsNil)
180+ // <evil laughter>
181+ m, err := NewSqliteLevelMap(filename)
182+ c.Check(err, IsNil)
183+ c.Assert(m, NotNil)
184+ all, err := m.GetAll()
185+ c.Check(all, IsNil)
186+ c.Check(err, ErrorMatches, "cannot read level .*")
187+}
188+
189+func (s *sqlmSuite) TestGetAllCanFailDifferently(c *C) {
190+ dir := c.MkDir()
191+ filename := dir + "test.db"
192+ db, err := sql.Open("sqlite3", filename)
193+ c.Assert(err, IsNil)
194+ // create a view with the name the table will have
195+ _, err = db.Exec("CREATE TABLE foo (foo)")
196+ c.Assert(err, IsNil)
197+ _, err = db.Exec("CREATE VIEW level_map AS SELECT * FROM foo")
198+ c.Assert(err, IsNil)
199+ // break the view
200+ _, err = db.Exec("DROP TABLE foo")
201+ c.Assert(err, IsNil)
202+ // <evil laughter>
203+ m, err := NewSqliteLevelMap(filename)
204+ c.Check(err, IsNil)
205+ c.Assert(m, NotNil)
206+ all, err := m.GetAll()
207+ c.Check(all, IsNil)
208+ c.Check(err, ErrorMatches, "cannot retrieve levels .*")
209+}
210
211=== modified file 'dependencies.tsv'
212--- dependencies.tsv 2014-01-20 06:23:17 +0000
213+++ dependencies.tsv 2014-02-12 13:52:27 +0000
214@@ -1,2 +1,3 @@
215-launchpad.net/go-dbus/v1 bzr james@jamesh.id.au-20140117100040-mmhdtiz5w9pxqtcr 124
216-launchpad.net/gocheck bzr gustavo@niemeyer.net-20130302024745-6ikofwq2c03h7giu 85
217+code.google.com/p/gosqlite hg 74691fb6f83716190870cde1b658538dd4b18eb0 15
218+launchpad.net/go-dbus/v1 bzr james@jamesh.id.au-20140206110213-pbzcr6ucaz3rqmnw 125
219+launchpad.net/gocheck bzr gustavo@niemeyer.net-20140127131816-zshobk1qqme626xw 86

Subscribers

People subscribed via source and target branches