diff --git a/cmd/import.go b/cmd/import.go index 8dd31181c..ea489f59d 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -55,7 +55,6 @@ omitted. If it is present then its format should be YYYY-MM-DDTHH:MM. flags.StringVarP(&Importer.Host, "host", "", "localhost:10101", "host:port of Pilosa.") flags.StringVarP(&Importer.Index, "index", "i", "", "Pilosa index to import into.") flags.StringVarP(&Importer.Frame, "frame", "f", "", "Frame to import into.") - flags.StringVarP(&Importer.Field, "field", "", "", "Field to import into.") flags.BoolVar(&Importer.StringKeys, "string-keys", false, "Treat payload as string keys.") flags.IntVarP(&Importer.BufferSize, "buffer-size", "s", 10000000, "Number of bits to buffer/sort before importing.") flags.BoolVarP(&Importer.Sort, "sort", "", false, "Enables sorting before import.") diff --git a/ctl/import.go b/ctl/import.go index 4fdfba444..493e1429b 100644 --- a/ctl/import.go +++ b/ctl/import.go @@ -46,9 +46,6 @@ type ImportCommand struct { // CreateSchema ensures the schema exists before import CreateSchema bool - // For Range-Encoded fields, name of the Field to import into. - Field string `json:"field"` - // Indicates that the payload should be treated as string keys. StringKeys bool `json:"StringKeys"` @@ -105,10 +102,26 @@ func (cmd *ImportCommand) Run(ctx context.Context) error { } } + // Determine the frame type in order to correctly handle the input data. + frameType := pilosa.DefaultFrameType + schema, err := cmd.Client.Schema(ctx) + if err != nil { + return errors.Wrap(err, "getting schema") + } + for _, index := range schema { + if index.Name == cmd.Index { + for _, frame := range index.Frames { + if frame.Name == cmd.Frame { + frameType = frame.Options.Type + } + } + } + } + // Import each path and import by slice. for _, path := range cmd.Paths { logger.Printf("parsing: %s", path) - if err := cmd.importPath(ctx, path); err != nil { + if err := cmd.importPath(ctx, frameType, path); err != nil { return err } } @@ -129,9 +142,9 @@ func (cmd *ImportCommand) ensureSchema(ctx context.Context) error { } // importPath parses a path into bits and imports it to the server. -func (cmd *ImportCommand) importPath(ctx context.Context, path string) error { - // If a field is provided, treat the import data as values to be range-encoded. - if cmd.Field != "" { +func (cmd *ImportCommand) importPath(ctx context.Context, frameType, path string) error { + // If frameType is `int`, treat the import data as values to be range-encoded. + if frameType == pilosa.FrameTypeInt { return cmd.bufferFieldValues(ctx, path) } else { if cmd.StringKeys { diff --git a/ctl/import_test.go b/ctl/import_test.go index 7c6c02721..7c64944f6 100644 --- a/ctl/import_test.go +++ b/ctl/import_test.go @@ -82,9 +82,7 @@ func TestImportCommand_Run(t *testing.T) { } } -// TODO: revisit this test once Frame is renamed Field -// Ensure that the ImportValue path runs (note: we have specified a value -// for cm.Field.) +// Ensure that the ImportValue path runs. func TestImportCommand_RunValue(t *testing.T) { buf := bytes.Buffer{} @@ -112,7 +110,6 @@ func TestImportCommand_RunValue(t *testing.T) { cm.Index = "i" cm.Frame = "f" - cm.Field = "f" cm.Paths = []string{file.Name()} err = cm.Run(ctx) if err != nil { @@ -122,10 +119,19 @@ func TestImportCommand_RunValue(t *testing.T) { func TestImportCommand_InvalidFile(t *testing.T) { + hldr := test.MustOpenHolder() + defer hldr.Close() + s := test.NewServer() + defer s.Close() + + s.Handler.API.Cluster = test.NewCluster(1) + s.Handler.API.Cluster.Nodes[0].URI = s.HostURI() + s.Handler.API.Holder = hldr.Holder + buf := bytes.Buffer{} stdin, stdout, stderr := GetIO(buf) cm := NewImportCommand(stdin, stdout, stderr) - cm.Host = "anyhost" + cm.Host = s.Host() cm.Index = "i" cm.Frame = "f" file, err := ioutil.TempFile("", "import.csv")