github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/pkg/sorted/sqlite/sqlitekv.go (about) 1 /* 2 Copyright 2012 The Camlistore Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package sqlite provides an implementation of sorted.KeyValue 18 // using an SQLite database file. 19 package sqlite 20 21 import ( 22 "database/sql" 23 "errors" 24 "fmt" 25 "os" 26 27 "camlistore.org/pkg/jsonconfig" 28 "camlistore.org/pkg/sorted" 29 "camlistore.org/pkg/sorted/sqlkv" 30 ) 31 32 func init() { 33 sorted.RegisterKeyValue("sqlite", newKeyValueFromConfig) 34 } 35 36 func newKeyValueFromConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) { 37 file := cfg.RequiredString("file") 38 if err := cfg.Validate(); err != nil { 39 return nil, err 40 } 41 return NewKeyValue(file) 42 } 43 44 // NewKeyValue returns a KeyValue implementation on top of 45 // an SQLite database file. 46 func NewKeyValue(file string) (sorted.KeyValue, error) { 47 if !compiled { 48 return nil, ErrNotCompiled 49 } 50 51 fi, err := os.Stat(file) 52 if os.IsNotExist(err) || (err == nil && fi.Size() == 0) { 53 return nil, fmt.Errorf(`You need to initialize your SQLite database with: camtool dbinit --dbname=%s --dbtype=sqlite`, file) 54 } 55 db, err := sql.Open("sqlite3", file) 56 if err != nil { 57 return nil, err 58 } 59 kv := &keyValue{ 60 file: file, 61 db: db, 62 KeyValue: &sqlkv.KeyValue{ 63 DB: db, 64 Serial: true, 65 }, 66 } 67 68 version, err := kv.SchemaVersion() 69 if err != nil { 70 return nil, fmt.Errorf("error getting schema version (need to init database with 'camtool dbinit %s'?): %v", file, err) 71 } 72 73 if err := kv.ping(); err != nil { 74 return nil, err 75 } 76 77 if version != requiredSchemaVersion { 78 if os.Getenv("CAMLI_DEV_CAMLI_ROOT") != "" { 79 // Good signal that we're using the devcam server, so help out 80 // the user with a more useful tip: 81 return nil, fmt.Errorf("database schema version is %d; expect %d (run \"devcam server --wipe\" to wipe both your blobs and re-populate the database schema)", version, requiredSchemaVersion) 82 } 83 return nil, fmt.Errorf("database schema version is %d; expect %d (need to re-init/upgrade database?)", 84 version, requiredSchemaVersion) 85 } 86 87 return kv, nil 88 89 } 90 91 type keyValue struct { 92 *sqlkv.KeyValue 93 94 file string 95 db *sql.DB 96 } 97 98 var compiled = false 99 100 // CompiledIn returns whether SQLite support is compiled in. 101 // If it returns false, the build tag "with_sqlite" was not specified. 102 func CompiledIn() bool { 103 return compiled 104 } 105 106 var ErrNotCompiled = errors.New("camlistored was not built with SQLite support. If you built with make.go, use go run make.go --sqlite=true. If you used go get or get install, use go {get,install} --tags=with_sqlite" + compileHint()) 107 108 func compileHint() string { 109 if _, err := os.Stat("/etc/apt"); err == nil { 110 return " (Hint: apt-get install libsqlite3-dev)" 111 } 112 return "" 113 } 114 115 func (kv *keyValue) ping() error { 116 // TODO(bradfitz): something more efficient here? 117 _, err := kv.SchemaVersion() 118 return err 119 } 120 121 func (kv *keyValue) SchemaVersion() (version int, err error) { 122 err = kv.db.QueryRow("SELECT value FROM meta WHERE metakey='version'").Scan(&version) 123 return 124 }