github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/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 if !compiled { 38 return nil, ErrNotCompiled 39 } 40 41 file := cfg.RequiredString("file") 42 if err := cfg.Validate(); err != nil { 43 return nil, err 44 } 45 46 fi, err := os.Stat(file) 47 if os.IsNotExist(err) || (err == nil && fi.Size() == 0) { 48 if err := initDB(file); err != nil { 49 return nil, fmt.Errorf("could not initialize sqlite DB at %s: %v", file, err) 50 } 51 } 52 db, err := sql.Open("sqlite3", file) 53 if err != nil { 54 return nil, err 55 } 56 kv := &keyValue{ 57 file: file, 58 db: db, 59 KeyValue: &sqlkv.KeyValue{ 60 DB: db, 61 Serial: true, 62 }, 63 } 64 65 version, err := kv.SchemaVersion() 66 if err != nil { 67 return nil, fmt.Errorf("error getting schema version (need to init database with 'camtool dbinit %s'?): %v", file, err) 68 } 69 70 if err := kv.ping(); err != nil { 71 return nil, err 72 } 73 74 if version != requiredSchemaVersion { 75 if os.Getenv("CAMLI_DEV_CAMLI_ROOT") != "" { 76 // Good signal that we're using the devcam server, so help out 77 // the user with a more useful tip: 78 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) 79 } 80 return nil, fmt.Errorf("database schema version is %d; expect %d (need to re-init/upgrade database?)", 81 version, requiredSchemaVersion) 82 } 83 84 return kv, nil 85 86 } 87 88 type keyValue struct { 89 *sqlkv.KeyValue 90 91 file string 92 db *sql.DB 93 } 94 95 var compiled = false 96 97 // CompiledIn returns whether SQLite support is compiled in. 98 // If it returns false, the build tag "with_sqlite" was not specified. 99 func CompiledIn() bool { 100 return compiled 101 } 102 103 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()) 104 105 func compileHint() string { 106 if _, err := os.Stat("/etc/apt"); err == nil { 107 return " (Hint: apt-get install libsqlite3-dev)" 108 } 109 return "" 110 } 111 112 func (kv *keyValue) ping() error { 113 // TODO(bradfitz): something more efficient here? 114 _, err := kv.SchemaVersion() 115 return err 116 } 117 118 func (kv *keyValue) SchemaVersion() (version int, err error) { 119 err = kv.db.QueryRow("SELECT value FROM meta WHERE metakey='version'").Scan(&version) 120 return 121 }