Home Download Docs Code Community
     1	/*
     2	Copyright 2012 The Perkeep 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 // import "perkeep.org/pkg/sorted/sqlite"
    20	
    21	import (
    22		"database/sql"
    23		"fmt"
    24		"os"
    25	
    26		_ "modernc.org/sqlite"
    27	
    28		"go4.org/jsonconfig"
    29		"go4.org/syncutil"
    30		"perkeep.org/pkg/env"
    31		"perkeep.org/pkg/sorted"
    32		"perkeep.org/pkg/sorted/sqlkv"
    33	)
    34	
    35	func init() {
    36		sorted.RegisterKeyValue("sqlite", newKeyValueFromConfig)
    37	}
    38	
    39	// NewStorage is a convenience that calls newKeyValueFromConfig
    40	// with file as the sqlite storage file.
    41	func NewStorage(file string) (sorted.KeyValue, error) {
    42		return newKeyValueFromConfig(jsonconfig.Obj{"file": file})
    43	}
    44	
    45	func newKeyValueFromConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) {
    46		file := cfg.RequiredString("file")
    47		if err := cfg.Validate(); err != nil {
    48			return nil, err
    49		}
    50	
    51		fi, err := os.Stat(file)
    52		if os.IsNotExist(err) || (err == nil && fi.Size() == 0) {
    53			if err := InitDB(file); err != nil {
    54				return nil, fmt.Errorf("could not initialize sqlite DB at %s: %v", file, err)
    55			}
    56		}
    57		db, err := sql.Open("sqlite", file)
    58		if err != nil {
    59			return nil, err
    60		}
    61		kv := &keyValue{
    62			file: file,
    63			db:   db,
    64			KeyValue: &sqlkv.KeyValue{
    65				DB:   db,
    66				Gate: syncutil.NewGate(1),
    67			},
    68		}
    69	
    70		version, err := kv.SchemaVersion()
    71		if err != nil {
    72			return nil, fmt.Errorf("error getting schema version (need to init database with 'camtool dbinit %s'?): %v", file, err)
    73		}
    74	
    75		if err := kv.ping(); err != nil {
    76			return nil, err
    77		}
    78	
    79		if version != requiredSchemaVersion {
    80			if env.IsDev() {
    81				// Good signal that we're using the devcam server, so help out
    82				// the user with a more useful tip:
    83				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)
    84			}
    85			return nil, fmt.Errorf("database schema version is %d; expect %d (need to re-init/upgrade database?)",
    86				version, requiredSchemaVersion)
    87		}
    88	
    89		return kv, nil
    90	
    91	}
    92	
    93	type keyValue struct {
    94		*sqlkv.KeyValue
    95	
    96		file string
    97		db   *sql.DB
    98	}
    99	
   100	func (kv *keyValue) ping() error {
   101		// TODO(bradfitz): something more efficient here?
   102		_, err := kv.SchemaVersion()
   103		return err
   104	}
   105	
   106	func (kv *keyValue) SchemaVersion() (version int, err error) {
   107		err = kv.db.QueryRow("SELECT value FROM meta WHERE metakey='version'").Scan(&version)
   108		return
   109	}
Website layout inspired by memcached.
Content by the authors.