diff --git a/handler.go b/handler.go index 299343dc5..464506ae8 100644 --- a/handler.go +++ b/handler.go @@ -1550,9 +1550,10 @@ func (h *Handler) handleGetDefinition(w http.ResponseWriter, r *http.Request) { return } inputDef, _ := index.inputDefinitions[inputDefName] - inputInfo := InputDefinitionInfo{Frames: inputDef.frames, Fields: inputDef.fields} - if err := json.NewEncoder(w).Encode(getInputDefinitionResponse{ - InputDefinition: inputInfo, + //inputInfo := InputDefinitionInfo{Frames: inputDef.frames, Fields: inputDef.fields} + if err := json.NewEncoder(w).Encode(InputDefinitionInfo{ + Frames: inputDef.frames, + Fields: inputDef.fields, }); err != nil { h.logger().Printf("write status response error: %s", err) } @@ -1577,6 +1578,10 @@ func (h *Handler) handleDeleteDefinition(w http.ResponseWriter, r *http.Request) http.Error(w, err.Error(), http.StatusInternalServerError) return } + + if err := json.NewEncoder(w).Encode(postInputDefinitionResponse{}); err != nil { + h.logger().Printf("response encoding error: %s", err) + } } type InputFrame struct { @@ -1584,10 +1589,6 @@ type InputFrame struct { Options FrameOptions `json:"options,omitempty"` } -type getInputDefinitionResponse struct { - InputDefinition InputDefinitionInfo `json:"input-definition"` -} - type InputDefinitionInfo struct { Frames []InputFrame `json:"frames"` Fields []Field `json:"fields"` diff --git a/handler_test.go b/handler_test.go index 8c7f3a7e2..a89244d49 100644 --- a/handler_test.go +++ b/handler_test.go @@ -1067,16 +1067,11 @@ func MustReadAll(r io.Reader) []byte { return buf } -// Ensure handler can delete a frame. +// Ensure handler can create a input definition. 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":[{ @@ -1107,6 +1102,10 @@ func TestHandler_CreateInputDefinition(t *testing.T) { } ] }`) + h := NewHandler() + h.Holder = hldr.Holder + h.Cluster = NewCluster(1) + w := httptest.NewRecorder() 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) @@ -1114,3 +1113,62 @@ func TestHandler_CreateInputDefinition(t *testing.T) { t.Fatalf("unexpected body: %s", body) } } + +// Ensure handler can delete a input definition. +func TestHandler_DeleteInputDefinition(t *testing.T) { + hldr := MustOpenHolder() + defer hldr.Close() + index := hldr.MustCreateIndexIfNotExists("i0", pilosa.IndexOptions{}) + + 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) + } + + h := NewHandler() + h.Holder = hldr.Holder + h.Cluster = NewCluster(1) + w := httptest.NewRecorder() + h.ServeHTTP(w, MustNewHTTPRequest("DELETE", "/index/i0/input-definition/test", strings.NewReader(""))) + 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) + } else if index.InputDefinition("test") != nil { + t.Fatalf("unexpected result: %s", index.InputDefinition("test")) + } +} + +// Return existing input definition +func TestHandler_GetInputDefinition(t *testing.T) { + hldr := MustOpenHolder() + defer hldr.Close() + index := hldr.MustCreateIndexIfNotExists("i0", pilosa.IndexOptions{}) + + 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}} + inputDef, err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields}) + if err != nil { + t.Fatal(err) + } + + response := &pilosa.InputDefinitionInfo{Frames: inputDef.Frames(), Fields: inputDef.Fields()} + expect, err := json.Marshal(response) + if err != nil { + t.Fatal(err) + } + h := NewHandler() + h.Holder = hldr.Holder + h.Cluster = NewCluster(1) + w := httptest.NewRecorder() + h.ServeHTTP(w, MustNewHTTPRequest("GET", "/index/i0/input-definition/test", strings.NewReader(""))) + if w.Code != http.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } else if body := w.Body.String(); body != string(expect)+"\n" { + t.Fatalf("unexpected body: %s, expect: %s", body, string(expect)) + } +} diff --git a/index.go b/index.go index 58bb8adaf..4dc40501b 100644 --- a/index.go +++ b/index.go @@ -662,14 +662,14 @@ func (i *Index) newInputDefinition(path, name string) (*InputDefinition, error) return f, nil } -// DeleteFrame removes a frame from the index. +// DeleteInputDefinition removes a input definition from the index. func (i *Index) DeleteInputDefinition(name string) error { i.mu.Lock() defer i.mu.Unlock() // Ignore if input definition doesn't exist. - f := i.inputDefinition(name) - if f == nil { + inputDef := i.inputDefinition(name) + if inputDef == nil { return nil } diff --git a/input_definition.go b/input_definition.go index 31a354a3b..baa8bf215 100644 --- a/input_definition.go +++ b/input_definition.go @@ -31,19 +31,10 @@ func NewInputDefinition(path, index, name string) (*InputDefinition, error) { }, nil } -// Name returns the name of input definition was initialized with. -func (i *InputDefinition) Name() string { return i.name } - -// Index returns the index name of the input definition was initialized with. -func (i *InputDefinition) Index() string { return i.index } - -// Path returns the path of the input definition was initialized with. -func (i *InputDefinition) Path() string { return i.path } - // Frames returns frames of the input definition was initialized with. func (i *InputDefinition) Frames() []InputFrame { return i.frames } -// Fields returns frames of the input definition was initialized with. +// Fields returns fields of the input definition was initialized with. func (i *InputDefinition) Fields() []Field { return i.fields } func (i *InputDefinition) Open() error { diff --git a/input_definition_test.go b/input_definition_test.go index 191b5d182..5fd78c60c 100644 --- a/input_definition_test.go +++ b/input_definition_test.go @@ -1 +1,38 @@ -package pilosa +// Copyright 2017 Pilosa Corp. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package pilosa_test + +import ( + "github.com/pilosa/pilosa" + "testing" +) + +func TestInputDefinition_Open(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}} + inputDef, err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields}) + if err != nil { + t.Fatal(err) + } + err = inputDef.Open() + if err != nil { + t.Fatal(err) + } +}