| OLD | NEW |
| 1 package expstorage | 1 package expstorage |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "database/sql" |
| 4 "encoding/json" | 5 "encoding/json" |
| 5 | 6 |
| 6 "skia.googlesource.com/buildbot.git/go/database" | 7 "skia.googlesource.com/buildbot.git/go/database" |
| 7 "skia.googlesource.com/buildbot.git/go/util" | 8 "skia.googlesource.com/buildbot.git/go/util" |
| 8 ) | 9 ) |
| 9 | 10 |
| 10 // Stores expectations in an SQL database without any caching. | 11 // Stores expectations in an SQL database without any caching. |
| 11 type SQLExpectationsStore struct { | 12 type SQLExpectationsStore struct { |
| 12 vdb *database.VersionedDB | 13 vdb *database.VersionedDB |
| 13 } | 14 } |
| 14 | 15 |
| 15 func NewSQLExpectationStore(vdb *database.VersionedDB) ExpectationsStore { | 16 func NewSQLExpectationStore(vdb *database.VersionedDB) ExpectationsStore { |
| 16 return &SQLExpectationsStore{ | 17 return &SQLExpectationsStore{ |
| 17 vdb: vdb, | 18 vdb: vdb, |
| 18 } | 19 } |
| 19 } | 20 } |
| 20 | 21 |
| 21 // See ExpectationsStore interface. | 22 // See ExpectationsStore interface. |
| 22 func (e *SQLExpectationsStore) Get(modifiable bool) (exp *Expectations, err erro
r) { | 23 func (e *SQLExpectationsStore) Get(modifiable bool) (exp *Expectations, err erro
r) { |
| 23 // Load the newest record from the database. | 24 // Load the newest record from the database. |
| 24 stmt := `SELECT expectations | 25 stmt := `SELECT expectations |
| 25 FROM expectations | 26 FROM expectations |
| 26 ORDER BY ts DESC | 27 ORDER BY ts DESC |
| 27 LIMIT 1` | 28 LIMIT 1` |
| 28 | 29 |
| 30 // Read the expectations. If there are no rows, that means we have no |
| 31 // expectations yet. |
| 29 var expJSON string | 32 var expJSON string |
| 30 » if err := e.vdb.DB.QueryRow(stmt).Scan(&expJSON); err != nil { | 33 » switch err := e.vdb.DB.QueryRow(stmt).Scan(&expJSON); { |
| 34 » case err == sql.ErrNoRows: |
| 35 » » return NewExpectations(modifiable), nil |
| 36 » case err != nil: |
| 31 return nil, err | 37 return nil, err |
| 32 } | 38 } |
| 33 | 39 |
| 34 var result Expectations | 40 var result Expectations |
| 35 err = json.Unmarshal([]byte(expJSON), &result) | 41 err = json.Unmarshal([]byte(expJSON), &result) |
| 36 | 42 |
| 37 // Since it's freshly allocated we can just set it to the requested mode
. | 43 // Since it's freshly allocated we can just set it to the requested mode
. |
| 38 result.Modifiable = modifiable | 44 result.Modifiable = modifiable |
| 39 | 45 |
| 40 return &result, nil | 46 return &result, nil |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 // See ExpectationsStore interface. | 95 // See ExpectationsStore interface. |
| 90 func (c *CachingExpectationStore) Put(exp *Expectations, userId string) error { | 96 func (c *CachingExpectationStore) Put(exp *Expectations, userId string) error { |
| 91 exp.checkModifiable() | 97 exp.checkModifiable() |
| 92 | 98 |
| 93 if err := c.store.Put(exp, userId); err != nil { | 99 if err := c.store.Put(exp, userId); err != nil { |
| 94 return err | 100 return err |
| 95 } | 101 } |
| 96 | 102 |
| 97 return c.cache.Put(exp, userId) | 103 return c.cache.Put(exp, userId) |
| 98 } | 104 } |
| OLD | NEW |