From 81f126dd34fa804650e5f7b1fbf26c52776d9a67 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Thu, 6 Sep 2018 17:04:07 +0300 Subject: [PATCH] Added field options to pilosa import --- cmd/import.go | 9 +++++++-- cmd/import_test.go | 16 ++++++++++++++++ ctl/import.go | 26 +++++++++++--------------- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/cmd/import.go b/cmd/import.go index 3d63586ce..7cb6c34c3 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -51,8 +51,13 @@ 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.Field, "field", "f", "", "Field to import into.") - flags.BoolVar(&Importer.IndexKeys, "index-keys", false, "use keys=true when creating an index") - flags.BoolVar(&Importer.FieldKeys, "field-keys", false, "use keys=true when creating a field") + flags.BoolVar(&Importer.IndexOptions.Keys, "index-keys", false, "specify keys=true when creating an index") + flags.BoolVar(&Importer.FieldOptions.Keys, "field-keys", false, "specify keys=true when creating a field") + flags.Int64Var(&Importer.FieldOptions.Min, "field-min", 0, "specify the minimum for an int field on creation") + flags.Int64Var(&Importer.FieldOptions.Max, "field-max", 0, "specify the maximum for an int field on creation") + flags.StringVar(&Importer.FieldOptions.CacheType, "field-cache-type", "", "specify the cache type for a set field on creation") + flags.Uint32Var(&Importer.FieldOptions.CacheSize, "field-cache-size", 0, "specify the cache size for a set field on creation") + flags.Var(&Importer.FieldOptions.TimeQuantum, "field-time-quantum", "specify the time quantum for a time field on creation") 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.") flags.BoolVarP(&Importer.CreateSchema, "create", "e", false, "Create the schema if it does not exist before import.") diff --git a/cmd/import_test.go b/cmd/import_test.go index e8e5d61f4..9a68a1218 100644 --- a/cmd/import_test.go +++ b/cmd/import_test.go @@ -18,6 +18,8 @@ import ( "strings" "testing" + "github.com/pilosa/pilosa" + "github.com/pilosa/pilosa/cmd" ) @@ -47,6 +49,20 @@ field = "f1" return v.Error() }, }, + { + args: []string{"import", "--index", "i1", "--field", "f1", "--field-keys", "--field-max", "100"}, + env: map[string]string{}, + validation: func() error { + v := validator{} + v.Check(cmd.Importer.Index, "i1") + v.Check(cmd.Importer.Field, "f1") + v.Check(cmd.Importer.FieldOptions, pilosa.FieldOptions{ + Keys: true, + Max: 100, + }) + return v.Error() + }, + }, } executeDry(t, tests) } diff --git a/ctl/import.go b/ctl/import.go index 1c000dd73..d2694a64f 100644 --- a/ctl/import.go +++ b/ctl/import.go @@ -41,20 +41,14 @@ type ImportCommand struct { // nolint: maligned Field string `json:"field"` // Options for the index to be created if it doesn't exist - indexOptions pilosa.IndexOptions + IndexOptions pilosa.IndexOptions // Options for the field to be created if it doesn't exist - fieldOptions pilosa.FieldOptions + FieldOptions pilosa.FieldOptions // CreateSchema ensures the schema exists before import CreateSchema bool - // IndexKeys makes the import command use keys=true when creating an index - IndexKeys bool `json:"indexKeys"` - - // FieldKeys makes the import command use keys=true when creating a field - FieldKeys bool `json:"fieldKeys"` - // Filenames to import from. Paths []string `json:"paths"` @@ -102,11 +96,13 @@ func (cmd *ImportCommand) Run(ctx context.Context) error { cmd.client = client if cmd.CreateSchema { - cmd.indexOptions = pilosa.IndexOptions{ - Keys: cmd.IndexKeys, - } - cmd.fieldOptions = pilosa.FieldOptions{ - Keys: cmd.FieldKeys, + // set the correct type for the field + if cmd.FieldOptions.TimeQuantum != "" { + cmd.FieldOptions.Type = "time" + } else if cmd.FieldOptions.Min != 0 || cmd.FieldOptions.Max != 0 { + cmd.FieldOptions.Type = "int" + } else { + cmd.FieldOptions.Type = "set" } err := cmd.ensureSchema(ctx) if err != nil { @@ -148,11 +144,11 @@ func (cmd *ImportCommand) Run(ctx context.Context) error { } func (cmd *ImportCommand) ensureSchema(ctx context.Context) error { - err := cmd.client.EnsureIndex(ctx, cmd.Index, cmd.indexOptions) + err := cmd.client.EnsureIndex(ctx, cmd.Index, cmd.IndexOptions) if err != nil { return fmt.Errorf("Error Creating Index: %s", err) } - err = cmd.client.EnsureFieldWithOptions(ctx, cmd.Index, cmd.Field, cmd.fieldOptions) + err = cmd.client.EnsureFieldWithOptions(ctx, cmd.Index, cmd.Field, cmd.FieldOptions) if err != nil { return fmt.Errorf("Error Creating Field: %s", err) }