github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/database/ffldb/driver.go (about) 1 // Copyright (c) 2015-2016 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package ffldb 7 8 import ( 9 "fmt" 10 11 "github.com/btcsuite/btclog" 12 "github.com/BlockABC/godash/database" 13 "github.com/BlockABC/godash/wire" 14 ) 15 16 var log = btclog.Disabled 17 18 const ( 19 dbType = "ffldb" 20 ) 21 22 // parseArgs parses the arguments from the database Open/Create methods. 23 func parseArgs(funcName string, args ...interface{}) (string, wire.BitcoinNet, error) { 24 if len(args) != 2 { 25 return "", 0, fmt.Errorf("invalid arguments to %s.%s -- "+ 26 "expected database path and block network", dbType, 27 funcName) 28 } 29 30 dbPath, ok := args[0].(string) 31 if !ok { 32 return "", 0, fmt.Errorf("first argument to %s.%s is invalid -- "+ 33 "expected database path string", dbType, funcName) 34 } 35 36 network, ok := args[1].(wire.BitcoinNet) 37 if !ok { 38 return "", 0, fmt.Errorf("second argument to %s.%s is invalid -- "+ 39 "expected block network", dbType, funcName) 40 } 41 42 return dbPath, network, nil 43 } 44 45 // openDBDriver is the callback provided during driver registration that opens 46 // an existing database for use. 47 func openDBDriver(args ...interface{}) (database.DB, error) { 48 dbPath, network, err := parseArgs("Open", args...) 49 if err != nil { 50 return nil, err 51 } 52 53 return openDB(dbPath, network, false) 54 } 55 56 // createDBDriver is the callback provided during driver registration that 57 // creates, initializes, and opens a database for use. 58 func createDBDriver(args ...interface{}) (database.DB, error) { 59 dbPath, network, err := parseArgs("Create", args...) 60 if err != nil { 61 return nil, err 62 } 63 64 return openDB(dbPath, network, true) 65 } 66 67 // useLogger is the callback provided during driver registration that sets the 68 // current logger to the provided one. 69 func useLogger(logger btclog.Logger) { 70 log = logger 71 } 72 73 func init() { 74 // Register the driver. 75 driver := database.Driver{ 76 DbType: dbType, 77 Create: createDBDriver, 78 Open: openDBDriver, 79 UseLogger: useLogger, 80 } 81 if err := database.RegisterDriver(driver); err != nil { 82 panic(fmt.Sprintf("Failed to regiser database driver '%s': %v", 83 dbType, err)) 84 } 85 }