memdb: initialize and more testing

This commit is contained in:
Armon Dadgar
2015-06-07 13:42:03 -07:00
parent 284f410378
commit 063491c019
3 changed files with 83 additions and 11 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}
}