Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]Add prefix to vid when importing #200

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ schema:
vid:
index: 1
function: hash
prefix: abc
tags:
- name: student
props:
Expand All @@ -208,6 +209,7 @@ schema:

* `index`: **Optional**. The column number in the CSV file. Started with 0. The default value is 0.
* `function`: **Optional**. Functions to generate the VIDs. Currently, we only support function `hash` and `uuid`.
* `prefix`: **Optional**. Add prefix to the original vid. When `function` is specified also, `prefix` is applied to the original vid before `function`.

##### `schema.vertex.tags`

Expand Down
2 changes: 2 additions & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ schema:
vid:
index: 1
function: hash
prefix: abc
tags:
- name: student
props:
Expand All @@ -182,6 +183,7 @@ schema:

- `index`:**可选**。在 CSV 文件中的列标,从 0 开始计数。默认值 0。
- `function`:**可选**。用来生成 VID 时的函数,有 `hash` 和 `uuid` 两种函数可选。
- `prefix`: **可选**。给 原始vid 添加的前缀,当同时指定了 `function` 时, 生成 VID 的方法是先添加 `prefix` 前缀, 再用 `function`生成 VID。

##### `schema.vertex.tags`

Expand Down
3 changes: 2 additions & 1 deletion examples/v2/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ clientSettings:
UPDATE CONFIGS storage:wal_ttl=3600;
UPDATE CONFIGS storage:rocksdb_column_family_options = { disable_auto_compactions = true };
DROP SPACE IF EXISTS importer_test;
CREATE SPACE IF NOT EXISTS importer_test(partition_num=5, replica_factor=1, vid_type=FIXED_STRING(10));USE importer_test;
CREATE SPACE IF NOT EXISTS importer_test(partition_num=5, replica_factor=1, vid_type=FIXED_STRING(32));USE importer_test;
CREATE TAG course(name string, credits int);
CREATE TAG building(name string);
CREATE TAG student(name string, age int, gender string);
Expand Down Expand Up @@ -384,6 +384,7 @@ files:
vertex:
vid:
index: 0
prefix: wxyz
tags:
- name: building
props:
Expand Down
26 changes: 20 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type VID struct {
Index *int `json:"index" yaml:"index"`
Function *string `json:"function" yaml:"function"`
Type *string `json:"type" yaml:"type"`
Prefix *string `json:"prefix" yaml:"prefix"`
}

type Rank struct {
Expand Down Expand Up @@ -482,12 +483,18 @@ func (v *VID) ParseFunction(str string) (err error) {
} else if i > 0 && j > i {
strs := strings.ToLower(str[i+1 : j])
fnType := strings.Split(strs, "+")
if len(fnType) > 1 {
if len(fnType) == 2 {
v.Function = &fnType[0]
v.Type = &fnType[1]
v.Prefix = nil
} else if len(fnType) == 3 {
v.Function = &fnType[0]
v.Type = &fnType[1]
v.Prefix = &fnType[2]
} else {
v.Function = nil
v.Type = &fnType[0]
v.Prefix = nil
}
} else {
err = fmt.Errorf("Invalid function format: %s", str)
Expand All @@ -496,19 +503,26 @@ func (v *VID) ParseFunction(str string) (err error) {
}

func (v *VID) String(vid string) string {
if v.Function == nil || *v.Function == "" {
return fmt.Sprintf("%s(%s)", vid, *v.Type)
} else {
if (v.Function != nil && *v.Function != "") && (v.Prefix != nil && *v.Prefix != "") {
return fmt.Sprintf("%s(%s+%s+%s)", vid, *v.Function, *v.Type, *v.Prefix)
} else if (v.Function == nil || *v.Function == "") && (v.Prefix != nil && *v.Prefix != "") {
return fmt.Sprintf("%s(%s+%s+%s)", vid, "", *v.Type, *v.Prefix)
} else if (v.Function != nil && *v.Function != "") && (v.Prefix == nil || *v.Prefix == "") {
return fmt.Sprintf("%s(%s+%s)", vid, *v.Function, *v.Type)
} else {
return fmt.Sprintf("%s(%s)", vid, *v.Type)
}
}

func (v *VID) FormatValue(record base.Record) (string, error) {
if len(record) <= *v.Index {
return "", fmt.Errorf("vid index(%d) out of range record length(%d)", *v.Index, len(record))
}
vid := record[*v.Index]
if v.Prefix != nil {
vid = *v.Prefix + vid
}
if v.Function == nil || *v.Function == "" {
vid := record[*v.Index]
if err := checkVidFormat(vid, *v.Type == "int"); err != nil {
return "", err
}
Expand All @@ -518,7 +532,7 @@ func (v *VID) FormatValue(record base.Record) (string, error) {
return vid, nil
}
} else {
return fmt.Sprintf("%s(%q)", *v.Function, record[*v.Index]), nil
return fmt.Sprintf("%s(%q)", *v.Function, vid), nil
}
}

Expand Down