diff --git a/handler.go b/handler.go index c183f70a4..e3049f3df 100644 --- a/handler.go +++ b/handler.go @@ -1537,6 +1537,10 @@ func (h *Handler) handlePostDefinition(w http.ResponseWriter, r *http.Request) { if err != nil { h.logger().Printf("problem sending CreateInputDefinition message: %s", err) } + + if err := json.NewEncoder(w).Encode(postInputDefinitionResponse{}); err != nil { + h.logger().Printf("response encoding error: %s", err) + } } func (h *Handler) handleGetDefinition(w http.ResponseWriter, r *http.Request) { @@ -1599,3 +1603,5 @@ type InputDefinitionInfo struct { Frames []InputFrame `json:"frames"` Fields []Field `json:"fields"` } + +type postInputDefinitionResponse struct{} diff --git a/handler_test.go b/handler_test.go index ab65894fb..8c7f3a7e2 100644 --- a/handler_test.go +++ b/handler_test.go @@ -1066,3 +1066,51 @@ func MustReadAll(r io.Reader) []byte { } return buf } + +// Ensure handler can delete a frame. +func TestHandler_CreateInputDefinition(t *testing.T) { + hldr := MustOpenHolder() + defer hldr.Close() + hldr.MustCreateIndexIfNotExists("i0", pilosa.IndexOptions{}) + + h := NewHandler() + h.Holder = hldr.Holder + h.Cluster = NewCluster(1) + w := httptest.NewRecorder() + inputBody := []byte(` + { + "frames":[{ + "name":"event-time", + "options":{ + "timeQuantum": "YMD", + "inverseEnabled": false, + "cacheType": "ranked" + } + }], + "fields": [ + { + "name": "id", + "primaryKey": true + }, + { + "name": "cabType", + "actions": [ + { + "frame": "cab-type", + "valueDestination": "mapping", + "valueMap": { + "Green": 1, + "Yellow": 2 + } + } + ] + } + ] + }`) + h.ServeHTTP(w, MustNewHTTPRequest("POST", "/index/i0/input-definition/input1", bytes.NewBuffer(inputBody))) + if w.Code != http.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } else if body := w.Body.String(); body != `{}`+"\n" { + t.Fatalf("unexpected body: %s", body) + } +} diff --git a/index.go b/index.go index b0ebc1c40..58bb8adaf 100644 --- a/index.go +++ b/index.go @@ -348,6 +348,16 @@ func (i *Index) Frame(name string) *Frame { return i.frame(name) } +// InputDefinition returns an input definition in the index by name. +func (i *Index) InputDefinition(name string) *InputDefinition { + i.mu.Lock() + defer i.mu.Unlock() + if inputDef, ok := i.inputDefinitions[name]; ok { + return inputDef + } + return nil +} + func (i *Index) frame(name string) *Frame { return i.frames[name] } func (i *Index) inputDefinition(name string) *InputDefinition { return i.inputDefinitions[name] } diff --git a/index_test.go b/index_test.go index 43a064967..125be84c4 100644 --- a/index_test.go +++ b/index_test.go @@ -267,7 +267,7 @@ func TestIndex_CreateExistingInputDefinition(t *testing.T) { frames := pilosa.InputFrame{Name: "f", Options: pilosa.FrameOptions{RowLabel: "row"}} action := pilosa.Action{Frame: "f", ValueDestination: "map", ValueMap: map[string]uint64{"Green": 1}} fields := pilosa.Field{Name: "id", PrimaryKey: true, Actions: []pilosa.Action{action}} - _ , err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields}) + _, err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields}) if err != nil { t.Fatal(err) } @@ -282,8 +282,32 @@ func TestIndex_CreateEmptyInputDefinition(t *testing.T) { defer index.Close() // Create Input Definition. - _ , err := index.CreateInputDefinition("test", []pilosa.InputFrame{}, []pilosa.Field{}) + _, err := index.CreateInputDefinition("test", []pilosa.InputFrame{}, []pilosa.Field{}) if err.Error() != "frames and fields are required" { t.Fatal(err) } } + +func TestIndex_DeleteInputDefinition(t *testing.T) { + index := MustOpenIndex() + defer index.Close() + + // Create Input Definition. + frames := pilosa.InputFrame{Name: "f", Options: pilosa.FrameOptions{RowLabel: "row"}} + action := pilosa.Action{Frame: "f", ValueDestination: "map", ValueMap: map[string]uint64{"Green": 1}} + fields := pilosa.Field{Name: "id", PrimaryKey: true, Actions: []pilosa.Action{action}} + _, err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields}) + if err != nil { + t.Fatal(err) + } else if index.InputDefinition("test") == nil { + t.Fatal("No input definition created") + } + + err = index.DeleteInputDefinition("test") + if err != nil { + t.Fatal(err) + } else if index.InputDefinition("test") != nil { + t.Fatal("input definition isn't deleted") + } + +}