mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
return an error if the definition does not exist
This commit is contained in:
parent
3e2bbc3717
commit
56f8705407
2 changed files with 21 additions and 24 deletions
20
index.go
20
index.go
|
|
@ -349,13 +349,13 @@ func (i *Index) Frame(name string) *Frame {
|
|||
}
|
||||
|
||||
// InputDefinition returns an input definition in the index by name.
|
||||
func (i *Index) InputDefinition(name string) *InputDefinition {
|
||||
func (i *Index) InputDefinition(name string) (*InputDefinition, error) {
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
if inputDef, ok := i.inputDefinitions[name]; ok {
|
||||
return inputDef
|
||||
return inputDef, nil
|
||||
}
|
||||
return nil
|
||||
return nil, ErrInputDefinitionNotFound
|
||||
}
|
||||
|
||||
func (i *Index) frame(name string) *Frame { return i.frames[name] }
|
||||
|
|
@ -657,8 +657,6 @@ func (i *Index) CreateInputDefinition(pb *internal.InputDefinition) (*InputDefin
|
|||
func (i *Index) createInputDefinition(pb *internal.InputDefinition) (*InputDefinition, error) {
|
||||
if pb.Name == "" {
|
||||
return nil, ErrInputDefinitionNameRequired
|
||||
} else if len(pb.Frames) == 0 || len(pb.Fields) == 0 {
|
||||
return nil, ErrInputDefinitionAttrsRequired
|
||||
}
|
||||
|
||||
for _, fr := range pb.Frames {
|
||||
|
|
@ -704,15 +702,15 @@ func (i *Index) newInputDefinition(name string) (*InputDefinition, error) {
|
|||
|
||||
// DeleteInputDefinition removes an input definition from the index.
|
||||
func (i *Index) DeleteInputDefinition(name string) error {
|
||||
// Fail if input definition doesn't exist.
|
||||
_, err := i.InputDefinition(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
|
||||
// Ignore if input definition doesn't exist.
|
||||
inputDef := i.inputDefinition(name)
|
||||
if inputDef == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete input definition file.
|
||||
if err := os.Remove(filepath.Join(i.InputDefinitionPath(), name)); err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -384,16 +384,9 @@ func TestIndex_CreateExistingInputDefinition(t *testing.T) {
|
|||
index := MustOpenIndex()
|
||||
defer index.Close()
|
||||
|
||||
// Test frames and fields are required
|
||||
def := internal.InputDefinition{Name: "test", Frames: []*internal.Frame{}, Fields: []*internal.InputDefinitionField{}}
|
||||
_, err := index.CreateInputDefinition(&def)
|
||||
if err != pilosa.ErrInputDefinitionAttrsRequired {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
//Test input definition name is required
|
||||
def = internal.InputDefinition{Name: "", Frames: []*internal.Frame{}, Fields: []*internal.InputDefinitionField{}}
|
||||
_, err = index.CreateInputDefinition(&def)
|
||||
def := internal.InputDefinition{Name: "", Frames: []*internal.Frame{}, Fields: []*internal.InputDefinitionField{}}
|
||||
_, err := index.CreateInputDefinition(&def)
|
||||
if err != pilosa.ErrInputDefinitionNameRequired {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -426,15 +419,21 @@ func TestIndex_DeleteInputDefinition(t *testing.T) {
|
|||
_, err := index.CreateInputDefinition(&def)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if index.InputDefinition("test") == nil {
|
||||
t.Fatal("No input definition created")
|
||||
}
|
||||
|
||||
_, err = index.InputDefinition("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = index.DeleteInputDefinition("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if index.InputDefinition("test") != nil {
|
||||
t.Fatal("input definition isn't deleted")
|
||||
}
|
||||
|
||||
_, err = index.InputDefinition("test")
|
||||
if err != pilosa.ErrInputDefinitionNotFound {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue