github.imxd.top/hashicorp/consul@v1.4.5/agent/consul/state/schema.go (about) 1 package state 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/go-memdb" 7 ) 8 9 // schemaFn is an interface function used to create and return 10 // new memdb schema structs for constructing an in-memory db. 11 type schemaFn func() *memdb.TableSchema 12 13 // schemas is used to register schemas with the state store. 14 var schemas []schemaFn 15 16 // registerSchema registers a new schema with the state store. This should 17 // get called at package init() time. 18 func registerSchema(fn schemaFn) { 19 schemas = append(schemas, fn) 20 } 21 22 // stateStoreSchema is used to return the combined schema for 23 // the state store. 24 func stateStoreSchema() *memdb.DBSchema { 25 // Create the root DB schema 26 db := &memdb.DBSchema{ 27 Tables: make(map[string]*memdb.TableSchema), 28 } 29 30 // Add the tables to the root schema 31 for _, fn := range schemas { 32 schema := fn() 33 if _, ok := db.Tables[schema.Name]; ok { 34 panic(fmt.Sprintf("duplicate table name: %s", schema.Name)) 35 } 36 db.Tables[schema.Name] = schema 37 } 38 return db 39 } 40 41 // indexTableSchema returns a new table schema used for tracking various indexes 42 // for the Raft log. 43 func indexTableSchema() *memdb.TableSchema { 44 return &memdb.TableSchema{ 45 Name: "index", 46 Indexes: map[string]*memdb.IndexSchema{ 47 "id": &memdb.IndexSchema{ 48 Name: "id", 49 AllowMissing: false, 50 Unique: true, 51 Indexer: &memdb.StringFieldIndex{ 52 Field: "Key", 53 Lowercase: true, 54 }, 55 }, 56 }, 57 } 58 } 59 60 func init() { 61 registerSchema(indexTableSchema) 62 }