From 063491c01991ba97da44c485152e4cb3469473ab Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 7 Jun 2015 13:42:03 -0700 Subject: [PATCH] memdb: initialize and more testing --- nomad/memdb/memdb.go | 16 ++++++++++ nomad/memdb/schema.go | 14 ++++++--- nomad/memdb/schema_test.go | 64 +++++++++++++++++++++++++++++++++----- 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/nomad/memdb/memdb.go b/nomad/memdb/memdb.go index 199732f2c..a8c46b0c0 100644 --- a/nomad/memdb/memdb.go +++ b/nomad/memdb/memdb.go @@ -30,6 +30,9 @@ func NewMemDB(schema *DBSchema) (*MemDB, error) { schema: schema, root: iradix.New(), } + if err := db.initialize(); err != nil { + return nil, err + } return db, nil } @@ -46,3 +49,16 @@ func (db *MemDB) Txn(write bool) *Txn { } return txn } + +// initialize is used to setup the DB for use after creation +func (db *MemDB) initialize() error { + for _, tableSchema := range db.schema.Tables { + table := iradix.New() + for _, indexSchema := range tableSchema.Indexes { + index := iradix.New() + table, _, _ = table.Insert([]byte(indexSchema.Name), index) + } + db.root, _, _ = db.root.Insert([]byte(tableSchema.Name), table) + } + return nil +} diff --git a/nomad/memdb/schema.go b/nomad/memdb/schema.go index b732129ed..cd8ec313d 100644 --- a/nomad/memdb/schema.go +++ b/nomad/memdb/schema.go @@ -4,7 +4,7 @@ import "fmt" // DBSchema contains the full database schema used for MemDB type DBSchema struct { - Tables []*TableSchema + Tables map[string]*TableSchema } // Validate is used to validate the database schema @@ -15,7 +15,10 @@ func (s *DBSchema) Validate() error { if len(s.Tables) == 0 { return fmt.Errorf("no tables defined") } - for _, table := range s.Tables { + for name, table := range s.Tables { + if name != table.Name { + return fmt.Errorf("table name mis-match for '%s'", name) + } if err := table.Validate(); err != nil { return err } @@ -26,7 +29,7 @@ func (s *DBSchema) Validate() error { // TableSchema contains the schema for a single table type TableSchema struct { Name string - Indexes []*IndexSchema + Indexes map[string]*IndexSchema } // Validate is used to validate the table schema @@ -37,7 +40,10 @@ func (s *TableSchema) Validate() error { if len(s.Indexes) == 0 { return fmt.Errorf("missing table schemas for '%s'", s.Name) } - for _, index := range s.Indexes { + for name, index := range s.Indexes { + if name != index.Name { + return fmt.Errorf("index name mis-match for '%s'", name) + } if err := index.Validate(); err != nil { return err } diff --git a/nomad/memdb/schema_test.go b/nomad/memdb/schema_test.go index d33a74141..73e13e36d 100644 --- a/nomad/memdb/schema_test.go +++ b/nomad/memdb/schema_test.go @@ -4,11 +4,11 @@ import "testing" func testValidSchema() *DBSchema { return &DBSchema{ - Tables: []*TableSchema{ - &TableSchema{ + Tables: map[string]*TableSchema{ + "main": &TableSchema{ Name: "main", - Indexes: []*IndexSchema{ - &IndexSchema{ + Indexes: map[string]*IndexSchema{ + "id": &IndexSchema{ Name: "id", Indexer: StringFieldIndex("ID", false), }, @@ -25,12 +25,12 @@ func TestDBSchema_Validate(t *testing.T) { t.Fatalf("should not validate, empty") } - s.Tables = []*TableSchema{ - &TableSchema{}, + s.Tables = map[string]*TableSchema{ + "foo": &TableSchema{Name: "foo"}, } err = s.Validate() if err == nil { - t.Fatalf("should not validate, no table name") + t.Fatalf("should not validate, no indexes") } valid := testValidSchema() @@ -39,3 +39,53 @@ func TestDBSchema_Validate(t *testing.T) { t.Fatalf("should validate: %v", err) } } + +func TestTableSchema_Validate(t *testing.T) { + s := &TableSchema{} + err := s.Validate() + if err == nil { + t.Fatalf("should not validate, empty") + } + + s.Indexes = map[string]*IndexSchema{ + "foo": &IndexSchema{Name: "foo"}, + } + err = s.Validate() + if err == nil { + t.Fatalf("should not validate, no indexes") + } + + valid := &TableSchema{ + Name: "main", + Indexes: map[string]*IndexSchema{ + "id": &IndexSchema{ + Name: "id", + Indexer: StringFieldIndex("ID", true), + }, + }, + } + err = valid.Validate() + if err != nil { + t.Fatalf("should validate: %v", err) + } +} + +func TestIndexSchema_Validate(t *testing.T) { + s := &IndexSchema{} + err := s.Validate() + if err == nil { + t.Fatalf("should not validate, empty") + } + + s.Name = "foo" + err = s.Validate() + if err == nil { + t.Fatalf("should not validate, no indexer") + } + + s.Indexer = StringFieldIndex("Foo", false) + err = s.Validate() + if err != nil { + t.Fatalf("should validate: %v", err) + } +}