Skip to content

Commit e4c71ff

Browse files
committed
refactor some of the input defintion logic to better utilize Encode()
1 parent f68110b commit e4c71ff

5 files changed

+57
-69
lines changed

handler.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -1520,12 +1520,16 @@ func (h *Handler) handlePostInputDefinition(w http.ResponseWriter, r *http.Reque
15201520
return
15211521
}
15221522

1523+
// TODO: validation before/after encode?
1524+
15231525
// Encode InputDefinition to its internal representation.
1524-
def, err := req.Encode()
1525-
if err != nil {
1526-
http.Error(w, err.Error(), http.StatusInternalServerError)
1527-
return
1528-
}
1526+
def := req.Encode()
1527+
/*
1528+
if err != nil {
1529+
http.Error(w, err.Error(), http.StatusInternalServerError)
1530+
return
1531+
}
1532+
*/
15291533
def.Name = inputDefName
15301534

15311535
// Validate columnLabel and duplicate primaryKey.

handler_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ func EncodeInputDef(name string, body []byte) (*internal.InputDefinition, error)
14851485
if err != nil {
14861486
return nil, err
14871487
}
1488-
def, err := req.Encode()
1488+
def := req.Encode()
14891489
def.Name = name
1490-
return def, err
1490+
return def, nil
14911491
}

index.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (i *Index) Open() error {
155155
return err
156156
}
157157

158-
if err := i.openInputDefinition(); err != nil {
158+
if err := i.openInputDefinitions(); err != nil {
159159
return err
160160
}
161161

@@ -336,7 +336,7 @@ func (i *Index) SetTimeQuantum(q TimeQuantum) error {
336336
// FramePath returns the path to a frame in the index.
337337
func (i *Index) FramePath(name string) string { return filepath.Join(i.path, name) }
338338

339-
// InputDefinitionPath returns the path to an input definition in the index.
339+
// InputDefinitionPath returns the path to the input definition directory for the index.
340340
func (i *Index) InputDefinitionPath() string {
341341
return filepath.Join(i.path, InputDefinitionDir)
342342
}
@@ -723,8 +723,8 @@ func (i *Index) DeleteInputDefinition(name string) error {
723723
return nil
724724
}
725725

726-
// openInputDefinition opens and initializes the input definitions inside the index.
727-
func (i *Index) openInputDefinition() error {
726+
// openInputDefinitions opens and initializes the input definitions inside the index.
727+
func (i *Index) openInputDefinitions() error {
728728
inputDef, err := os.Open(i.InputDefinitionPath())
729729
if os.IsNotExist(err) {
730730
return nil

input_definition.go

+38-54
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/pilosa/pilosa/internal"
2727
)
2828

29-
// Action Mapping types
29+
// Action types.
3030
const (
3131
InputMapping = "mapping"
3232
InputValueToRow = "value-to-row"
@@ -101,19 +101,19 @@ func (i *InputDefinition) LoadDefinition(pb *internal.InputDefinition) error {
101101
i.frames = append(i.frames, inputFrame)
102102
}
103103

104-
countRowID := make(map[string]uint64)
104+
accountRowID := make(map[string]uint64)
105105
for _, field := range pb.Fields {
106106
var actions []Action
107107
for _, action := range field.InputDefinitionActions {
108108
if err := i.ValidateAction(action); err != nil {
109109
return err
110110
}
111111
if action.ValueDestination == InputSingleRowBool && action.Frame != "" {
112-
val, ok := countRowID[action.Frame]
112+
val, ok := accountRowID[action.Frame]
113113
if ok && val == action.RowID {
114114
return fmt.Errorf("duplicate rowID with other field: %v", action.RowID)
115115
}
116-
countRowID[action.Frame] = action.RowID
116+
accountRowID[action.Frame] = action.RowID
117117
}
118118
actions = append(actions, Action{
119119
Frame: action.Frame,
@@ -152,40 +152,18 @@ func (i *InputDefinition) saveMeta() error {
152152
if err := os.MkdirAll(i.path, 0777); err != nil {
153153
return err
154154
}
155-
// Marshal metadata.
155+
156156
var frames []*internal.Frame
157157
for _, fr := range i.frames {
158-
frameMeta := &internal.FrameMeta{
159-
RowLabel: fr.Options.RowLabel,
160-
InverseEnabled: fr.Options.InverseEnabled,
161-
CacheType: fr.Options.CacheType,
162-
CacheSize: fr.Options.CacheSize,
163-
TimeQuantum: string(fr.Options.TimeQuantum),
164-
}
165-
frame := &internal.Frame{Name: fr.Name, Meta: frameMeta}
166-
frames = append(frames, frame)
158+
frames = append(frames, fr.Encode())
167159
}
168160

169161
var fields []*internal.InputDefinitionField
170162
for _, field := range i.fields {
171-
var actions []*internal.InputDefinitionAction
172-
for _, action := range field.Actions {
173-
actionMeta := &internal.InputDefinitionAction{
174-
Frame: action.Frame,
175-
ValueDestination: action.ValueDestination,
176-
ValueMap: action.ValueMap,
177-
RowID: convert(action.RowID),
178-
}
179-
actions = append(actions, actionMeta)
180-
}
181-
182-
fieldMeta := &internal.InputDefinitionField{
183-
Name: field.Name,
184-
PrimaryKey: field.PrimaryKey,
185-
InputDefinitionActions: actions,
186-
}
187-
fields = append(fields, fieldMeta)
163+
fields = append(fields, field.Encode())
188164
}
165+
166+
// Marshal input definition.
189167
buf, err := proto.Marshal(&internal.InputDefinition{
190168
Name: i.name,
191169
Frames: frames,
@@ -211,17 +189,16 @@ type InputDefinitionField struct {
211189
}
212190

213191
// Encode converts InputDefinitionField into its internal representation.
214-
func (o *InputDefinitionField) Encode() (*internal.InputDefinitionField, error) {
215-
field := internal.InputDefinitionField{Name: o.Name, PrimaryKey: o.PrimaryKey}
216-
192+
func (o *InputDefinitionField) Encode() *internal.InputDefinitionField {
193+
var actions []*internal.InputDefinitionAction
217194
for _, action := range o.Actions {
218-
actionEncode, err := action.Encode()
219-
if err != nil {
220-
return nil, err
221-
}
222-
field.InputDefinitionActions = append(field.InputDefinitionActions, actionEncode)
195+
actions = append(actions, action.Encode())
196+
}
197+
return &internal.InputDefinitionField{
198+
Name: o.Name,
199+
PrimaryKey: o.PrimaryKey,
200+
InputDefinitionActions: actions,
223201
}
224-
return &field, nil
225202
}
226203

227204
// Action describes the mapping method for the field in the InputDefinition.
@@ -233,16 +210,19 @@ type Action struct {
233210
}
234211

235212
// Encode converts Action into its internal representation.
236-
func (o *Action) Encode() (*internal.InputDefinitionAction, error) {
237-
if o.RowID == nil && o.ValueDestination == "single-row-boolean" {
238-
return nil, errors.New("rowID required for single-row-boolean")
239-
}
213+
func (o *Action) Encode() *internal.InputDefinitionAction {
214+
// TODO: this check needs to happen somewhere other than Encode()
215+
/*
216+
if o.RowID == nil && o.ValueDestination == InputSingleRowBool {
217+
return nil, errors.New("rowID required for single-row-boolean")
218+
}
219+
*/
240220
return &internal.InputDefinitionAction{
241221
Frame: o.Frame,
242222
ValueDestination: o.ValueDestination,
243223
ValueMap: o.ValueMap,
244224
RowID: convert(o.RowID),
245-
}, nil
225+
}
246226
}
247227

248228
// convert pointer to uint64
@@ -259,27 +239,31 @@ type InputFrame struct {
259239
Options FrameOptions `json:"options,omitempty"`
260240
}
261241

242+
// Encode converts InputFrame into its internal representation.
243+
func (f *InputFrame) Encode() *internal.Frame {
244+
return &internal.Frame{
245+
Name: f.Name,
246+
Meta: f.Options.Encode(),
247+
}
248+
249+
}
250+
262251
// InputDefinitionInfo represents the json message format needed to create an InputDefinition.
263252
type InputDefinitionInfo struct {
264253
Frames []InputFrame `json:"frames"`
265254
Fields []InputDefinitionField `json:"fields"`
266255
}
267256

268257
// Encode converts InputDefinitionInfo into its internal representation.
269-
func (i *InputDefinitionInfo) Encode() (*internal.InputDefinition, error) {
258+
func (i *InputDefinitionInfo) Encode() *internal.InputDefinition {
270259
var def internal.InputDefinition
271260
for _, f := range i.Frames {
272-
def.Frames = append(def.Frames, &internal.Frame{Name: f.Name, Meta: f.Options.Encode()})
261+
def.Frames = append(def.Frames, f.Encode())
273262
}
274263
for _, f := range i.Fields {
275-
fEncode, err := f.Encode()
276-
if err != nil {
277-
return nil, err
278-
}
279-
def.Fields = append(def.Fields, fEncode)
264+
def.Fields = append(def.Fields, f.Encode())
280265
}
281-
282-
return &def, nil
266+
return &def
283267
}
284268

285269
// AddFrame manually add frame to input definition.

input_definition_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ func TestInputDefinition_Encoding(t *testing.T) {
8787
t.Fatal(err)
8888
}
8989

90-
internalDef, err := def.Encode()
91-
if err != nil {
92-
t.Fatal(err)
93-
}
90+
internalDef := def.Encode()
9491

9592
if internalDef.Frames[0].Name != "event-time" {
9693
t.Fatalf("unexpected frame: %v", internalDef)
@@ -145,6 +142,8 @@ func TestInputDefinition_LoadDefinition(t *testing.T) {
145142
}
146143
}
147144

145+
/*
146+
// TODO: handle validation outside of the Encode()
148147
func TestActionEncoding(t *testing.T) {
149148
action := pilosa.Action{Frame: "f", ValueDestination: pilosa.InputSingleRowBool, ValueMap: map[string]uint64{"Green": 1}}
150149
_, err := action.Encode()
@@ -159,6 +158,7 @@ func TestActionEncoding(t *testing.T) {
159158
t.Fatalf("Expected rowID required for single-row-boolean error, actual error: %s", err)
160159
}
161160
}
161+
*/
162162

163163
func TestHandleAction(t *testing.T) {
164164
var value interface{}

0 commit comments

Comments
 (0)