From 57d035260dd4fe351ee27de712c14347919dec44 Mon Sep 17 00:00:00 2001 From: Linh Vo Date: Fri, 23 Jun 2017 11:17:36 -0500 Subject: [PATCH] update JSONparser --- handler.go | 89 +++++++++++++++++++++--------------------------------- index.go | 70 ------------------------------------------ 2 files changed, 34 insertions(+), 125 deletions(-) diff --git a/handler.go b/handler.go index f3af90305..f7849efe9 100644 --- a/handler.go +++ b/handler.go @@ -1644,7 +1644,7 @@ func (h *Handler) handlePostInput(w http.ResponseWriter, r *http.Request) { return } for _, req := range reqs { - err = index.JSONParser(req.(map[string]interface{}), inputDefName) + err = h.JSONParser(req.(map[string]interface{}), index, inputDefName) if err == ErrInputDefinitionNotFound { http.Error(w, err.Error(), http.StatusNotFound) return @@ -1655,64 +1655,43 @@ func (h *Handler) handlePostInput(w http.ResponseWriter, r *http.Request) { } } -// MapAction Process the input data and set a bit -func (h *Handler) MapAction(a *Action, value string, colID uint64) (*Bit, error) { - var bit Bit - var ok bool - bit.ColumnID = colID - bit.RowID, ok = a.ValueMap[value] - if !ok { - return nil, fmt.Errorf("Value %s does not exist in definition map", value) +// JSONParser validate input json file and execute SetBit +func (h *Handler) JSONParser(req map[string]interface{}, index *Index, name string) error { + inputDef := index.inputDefinition(name) + if inputDef == nil { + return ErrInputDefinitionNotFound } - - // _, err := i.Frame(f).SetBit(ViewStandard, rowID, colID, nil) - return &bit, nil -} - -// ValueToRow Sets a bitmap with rowID from the input value -func (h *Handler) ValueToRow(a *Action, value string, colID uint64) (*Bit, error) { - var bit Bit - var err error - bit.ColumnID = colID - bit.RowID, err = strconv.ParseUint(value, 10, 64) - if err != nil { - return nil, err + // if field in input data is not in defined definition, return error + validFields := make(map[string]bool) + for _, field := range inputDef.Fields() { + validFields[field.Name] = true } - // _, err = i.Frame(f).SetBit(ViewStandard, rowID, colID, nil) - return &bit, err -} - -// SingleRowBoolean Sets a bitmap with rowID from the Action defintion -func (h *Handler) SingleRowBoolean(a *Action, value string, colID uint64) (*Bit, error) { - var bit Bit - var err error - bit.ColumnID = colID - bit.RowID, err = strconv.ParseUint(value, 10, 64) - if err != nil { - return nil, err - } - // _, err = i.Frame(f).SetBit(ViewStandard, rowID, colID, nil) - return &bit, err -} - -// InputBits Process and Sort the Input Bits and route to appropriate nodes. -func (h *Handler) InputBits(index, frame string, bits []Bit) error { - client, err := NewClient(h.Host) - if err != nil { - return err - } - - bitsBySlice := Bits(bits).GroupBySlice() - - // Parse path into bits. - for slice, bits := range bitsBySlice { - sort.Sort(BitsByPos(bits)) - - h.logger().Printf("inputing slice: %d, n=%d", slice, len(bits)) - if err := client.Import(context.Background(), index, frame, slice, bits); err != nil { - return err + for key, _ := range req { + _, ok := validFields[key] + if !ok { + fmt.Errorf("field not found", key) } } + for _, field := range inputDef.Fields() { + // skip field that defined in definition but not in input data + var colValue uint64 + if _, ok := req[field.Name]; !ok { + continue + } else if field.PrimaryKey { + colValue, ok := req[field.Name].(float64) + if !ok { + return fmt.Errorf("float type required, got %s:%s", field.Name, colValue) + } else { + val, ok := req[DefaultColumnLabel] + if !ok { + return errors.New("column ID not provided") + } + colValue = val.(float64) + } + } + + } + return nil } diff --git a/index.go b/index.go index 01705bb44..ca5428e06 100644 --- a/index.go +++ b/index.go @@ -755,73 +755,3 @@ func (i *Index) openInputDefinition() error { } return nil } - -func (i *Index) JSONParser(req map[string]interface{}, name string) error { - inputDef := i.inputDefinition(name) - if inputDef == nil { - return ErrInputDefinitionNotFound - } - // if field in input data is not in defined definition, return error - validFields := make(map[string]bool) - for _, field := range inputDef.Fields() { - validFields[field.Name] = true - } - for key, _ := range req { - _, ok := validFields[key] - if !ok { - fmt.Errorf("field not found", key) - } - } - - for _, field := range inputDef.Fields() { - // skip field that defined in definition but not in input data - if _, ok := req[field.Name]; !ok { - continue - } - - for _, action := range field.Actions { - switch action.ValueDestination { - case "mapping": - value, ok := req[field.Name].(string) - if !ok { - return fmt.Errorf("String type required, got %s:%s", field.Name, value) - } - err := i.MapAction(action.Frame, action.ValueMap, req[field.Name].(string)) - if err != nil { - return fmt.Errorf("Error action mapping : %s", err) - } - case "single-row-boolean": - value, ok := req[field.Name].(bool) - if !ok { - return fmt.Errorf("Bool type required, got %s:%s", field.Name, req[field.Name]) - } - err := i.StringRowBoolean(action.Frame, action.RowID, value) - if err != nil { - return fmt.Errorf("Error action single-row-boolean : %s", err) - } - case "value-to-row": - value, ok := req[field.Name].(float64) - if !ok { - return fmt.Errorf("Float type required, got %s:%s", field.Name, req[field.Name]) - } - err := i.ValueToRow(action.Frame, field.Name, uint64(value)) - if err != nil { - return fmt.Errorf("Error action value-to-row: %s", err) - } - } - } - } - - return nil -} -func (i *Index) MapAction(frame string, valueMap map[string]uint64, value string) error { - return nil -} - -func (i *Index) StringRowBoolean(frame string, rowID *uint64, value bool) error { - return nil -} - -func (i *Index) ValueToRow(frame, name string, value uint64) error { - return nil -}