2012-04-09 16:53:49 +00:00
|
|
|
/*
|
2018-01-04 00:52:49 +00:00
|
|
|
Copyright 2012 The Perkeep Authors
|
2012-04-09 16:53:49 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-11-23 07:24:54 +00:00
|
|
|
// Package sqlindex implements the sorted.KeyValue interface using an *sql.DB.
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package sqlindex // import "perkeep.org/pkg/index/sqlindex"
|
2012-04-09 16:53:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2013-02-18 06:10:28 +00:00
|
|
|
"sync"
|
2012-04-09 16:53:49 +00:00
|
|
|
|
2018-01-02 04:48:34 +00:00
|
|
|
"perkeep.org/internal/leak"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/sorted"
|
2012-04-09 16:53:49 +00:00
|
|
|
)
|
|
|
|
|
2013-11-23 07:24:54 +00:00
|
|
|
// Storage implements the sorted.KeyValue interface using an *sql.DB.
|
2012-04-09 16:53:49 +00:00
|
|
|
type Storage struct {
|
|
|
|
DB *sql.DB
|
2012-11-03 18:58:50 +00:00
|
|
|
|
|
|
|
// SetFunc is an optional func to use when REPLACE INTO does not exist
|
|
|
|
SetFunc func(*sql.DB, string, string) error
|
|
|
|
BatchSetFunc func(*sql.Tx, string, string) error
|
|
|
|
|
|
|
|
// PlaceHolderFunc optionally replaces ? placeholders with the right ones for the rdbms
|
|
|
|
// in use
|
|
|
|
PlaceHolderFunc func(string) string
|
2013-02-18 06:10:28 +00:00
|
|
|
|
|
|
|
// Serial determines whether a Go-level mutex protects DB from
|
|
|
|
// concurrent access. This isn't perfect and exists just for
|
|
|
|
// SQLite, whose driver likes to return "the database is
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
// locked" (perkeep.org/issue/114), so this keeps some
|
2013-02-18 06:10:28 +00:00
|
|
|
// pressure off. But we still trust SQLite to deal with
|
|
|
|
// concurrency in most cases.
|
|
|
|
Serial bool
|
|
|
|
|
|
|
|
mu sync.Mutex // the mutex used, if Serial is set
|
2012-11-03 18:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) sql(v string) string {
|
|
|
|
if f := s.PlaceHolderFunc; f != nil {
|
|
|
|
return f(v)
|
|
|
|
}
|
|
|
|
return v
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type batchTx struct {
|
|
|
|
tx *sql.Tx
|
|
|
|
err error // sticky
|
2012-11-03 18:58:50 +00:00
|
|
|
|
|
|
|
// SetFunc is an optional func to use when REPLACE INTO does not exist
|
|
|
|
SetFunc func(*sql.Tx, string, string) error
|
|
|
|
|
|
|
|
// PlaceHolderFunc optionally replaces ? placeholders with the right ones for the rdbms
|
|
|
|
// in use
|
|
|
|
PlaceHolderFunc func(string) string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batchTx) sql(v string) string {
|
|
|
|
if f := b.PlaceHolderFunc; f != nil {
|
|
|
|
return f(v)
|
|
|
|
}
|
|
|
|
return v
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batchTx) Set(key, value string) {
|
|
|
|
if b.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-11-03 18:58:50 +00:00
|
|
|
if b.SetFunc != nil {
|
|
|
|
b.err = b.SetFunc(b.tx, key, value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, b.err = b.tx.Exec(b.sql("REPLACE INTO rows (k, v) VALUES (?, ?)"), key, value)
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *batchTx) Delete(key string) {
|
|
|
|
if b.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-11-03 18:58:50 +00:00
|
|
|
_, b.err = b.tx.Exec(b.sql("DELETE FROM rows WHERE k=?"), key)
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-23 07:24:54 +00:00
|
|
|
func (s *Storage) BeginBatch() sorted.BatchMutation {
|
2013-02-18 06:10:28 +00:00
|
|
|
if s.Serial {
|
|
|
|
s.mu.Lock()
|
|
|
|
}
|
2012-04-09 16:53:49 +00:00
|
|
|
tx, err := s.DB.Begin()
|
|
|
|
return &batchTx{
|
2012-11-03 18:58:50 +00:00
|
|
|
tx: tx,
|
|
|
|
err: err,
|
|
|
|
SetFunc: s.BatchSetFunc,
|
|
|
|
PlaceHolderFunc: s.PlaceHolderFunc,
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-23 07:24:54 +00:00
|
|
|
func (s *Storage) CommitBatch(b sorted.BatchMutation) error {
|
2013-02-18 06:10:28 +00:00
|
|
|
if s.Serial {
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
}
|
2012-04-09 16:53:49 +00:00
|
|
|
bt, ok := b.(*batchTx)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("wrong BatchMutation type %T", b)
|
|
|
|
}
|
|
|
|
if bt.err != nil {
|
|
|
|
return bt.err
|
|
|
|
}
|
|
|
|
return bt.tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Get(key string) (value string, err error) {
|
2013-02-18 06:10:28 +00:00
|
|
|
if s.Serial {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
}
|
2012-11-03 18:58:50 +00:00
|
|
|
err = s.DB.QueryRow(s.sql("SELECT v FROM rows WHERE k=?"), key).Scan(&value)
|
2012-04-09 16:53:49 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2013-11-23 07:24:54 +00:00
|
|
|
err = sorted.ErrNotFound
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Set(key, value string) error {
|
2013-02-18 06:10:28 +00:00
|
|
|
if s.Serial {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
}
|
2012-11-03 18:58:50 +00:00
|
|
|
if s.SetFunc != nil {
|
|
|
|
return s.SetFunc(s.DB, key, value)
|
|
|
|
}
|
|
|
|
_, err := s.DB.Exec(s.sql("REPLACE INTO rows (k, v) VALUES (?, ?)"), key, value)
|
2012-04-09 16:53:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Delete(key string) error {
|
2013-02-18 06:10:28 +00:00
|
|
|
if s.Serial {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
}
|
2012-11-03 18:58:50 +00:00
|
|
|
_, err := s.DB.Exec(s.sql("DELETE FROM rows WHERE k=?"), key)
|
2012-04-09 16:53:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-24 05:07:03 +00:00
|
|
|
func (s *Storage) Close() error { return s.DB.Close() }
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
func (s *Storage) Find(start, end string) sorted.Iterator {
|
|
|
|
if s.Serial {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
}
|
|
|
|
var rows *sql.Rows
|
|
|
|
var err error
|
|
|
|
if end == "" {
|
|
|
|
rows, err = s.DB.Query(s.sql("SELECT k, v FROM rows WHERE k >= ? ORDER BY k "), start)
|
|
|
|
} else {
|
|
|
|
rows, err = s.DB.Query(s.sql("SELECT k, v FROM rows WHERE k >= ? AND k < ? ORDER BY k "), start, end)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("unexpected query error: %v", err)
|
|
|
|
return &iter{err: err}
|
|
|
|
}
|
|
|
|
|
2013-09-12 17:13:29 +00:00
|
|
|
it := &iter{
|
|
|
|
s: s,
|
2013-12-07 16:43:18 +00:00
|
|
|
rows: rows,
|
2013-09-12 17:13:29 +00:00
|
|
|
closeCheck: leak.NewChecker(),
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
2013-09-12 17:13:29 +00:00
|
|
|
return it
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// iter is a iterator over sorted key/value pairs in rows.
|
|
|
|
type iter struct {
|
|
|
|
s *Storage
|
2016-04-04 16:12:50 +00:00
|
|
|
err error // accumulated error, returned at Close
|
2012-04-09 16:53:49 +00:00
|
|
|
|
2013-09-12 17:13:29 +00:00
|
|
|
closeCheck *leak.Checker
|
|
|
|
|
2012-04-09 16:53:49 +00:00
|
|
|
rows *sql.Rows // if non-nil, the rows we're reading from
|
|
|
|
|
2013-12-04 13:18:52 +00:00
|
|
|
key sql.RawBytes
|
|
|
|
val sql.RawBytes
|
|
|
|
skey, sval *string // if non-nil, it's been stringified
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
var errClosed = errors.New("sqlkv: Iterator already closed")
|
2012-04-09 16:53:49 +00:00
|
|
|
|
2013-12-04 13:18:52 +00:00
|
|
|
func (t *iter) KeyBytes() []byte { return t.key }
|
|
|
|
func (t *iter) Key() string {
|
|
|
|
if t.skey != nil {
|
|
|
|
return *t.skey
|
|
|
|
}
|
|
|
|
str := string(t.key)
|
|
|
|
t.skey = &str
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *iter) ValueBytes() []byte { return t.val }
|
|
|
|
func (t *iter) Value() string {
|
|
|
|
if t.sval != nil {
|
|
|
|
return *t.sval
|
|
|
|
}
|
|
|
|
str := string(t.val)
|
|
|
|
t.sval = &str
|
|
|
|
return str
|
|
|
|
}
|
2012-04-09 16:53:49 +00:00
|
|
|
|
|
|
|
func (t *iter) Close() error {
|
2013-09-12 17:13:29 +00:00
|
|
|
t.closeCheck.Close()
|
2012-04-09 16:53:49 +00:00
|
|
|
if t.rows != nil {
|
|
|
|
t.rows.Close()
|
2013-12-07 16:43:18 +00:00
|
|
|
t.rows = nil
|
2012-04-09 16:53:49 +00:00
|
|
|
}
|
|
|
|
err := t.err
|
|
|
|
t.err = errClosed
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *iter) Next() bool {
|
|
|
|
if t.err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2013-12-04 13:18:52 +00:00
|
|
|
t.skey, t.sval = nil, nil
|
2012-04-09 16:53:49 +00:00
|
|
|
if !t.rows.Next() {
|
|
|
|
return false
|
|
|
|
}
|
2013-12-04 13:18:52 +00:00
|
|
|
t.err = t.rows.Scan(&t.key, &t.val)
|
2012-04-09 16:53:49 +00:00
|
|
|
if t.err != nil {
|
|
|
|
log.Printf("unexpected Scan error: %v", t.err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|