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

Issue 2925: export schema with special chars #2929

Merged
merged 2 commits into from
Jan 24, 2019
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
20 changes: 19 additions & 1 deletion worker/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"github.com/golang/glog"
"golang.org/x/net/context"
Expand Down Expand Up @@ -54,6 +55,20 @@ var rdfTypeMap = map[types.TypeID]string{
types.PasswordID: "xs:password",
}

// Having '<' and '>' around all predicates makes the exported schema harder
// for humans to look at, so only put them on predicates containing "exotic"
// characters (i.e. ones not in this list).
var predNonSpecialChars = unicode.RangeTable{
R16: []unicode.Range16{
// Ranges must be in order.
unicode.Range16{'.', '.', 1},
unicode.Range16{'0', '9', 1},
unicode.Range16{'A', 'Z', 1},
unicode.Range16{'_', '_', 1},
unicode.Range16{'a', 'z', 1},
},
}

func toRDF(pl *posting.List, prefix string, readTs uint64) (*bpb.KVList, error) {
var buf bytes.Buffer

Expand Down Expand Up @@ -142,7 +157,10 @@ func toRDF(pl *posting.List, prefix string, readTs uint64) (*bpb.KVList, error)
func toSchema(attr string, update pb.SchemaUpdate) (*bpb.KVList, error) {
// bytes.Buffer never returns error for any of the writes. So, we don't need to check them.
var buf bytes.Buffer
if strings.ContainsRune(attr, ':') {
isSpecial := func(r rune) bool {
return !(unicode.In(r, &predNonSpecialChars))
}
if strings.IndexFunc(attr, isSpecial) >= 0 {
buf.WriteRune('<')
buf.WriteString(attr)
buf.WriteRune('>')
Expand Down
75 changes: 75 additions & 0 deletions worker/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,81 @@ func TestToSchema(t *testing.T) {
},
expected: "<Alice:best>:string @reverse @lang . \n",
},
{
skv: &skv{
attr: "username/password",
schema: pb.SchemaUpdate{
Predicate: "",
ValueType: pb.Posting_STRING,
Directive: pb.SchemaUpdate_NONE,
List: false,
Count: false,
Upsert: false,
Lang: false,
},
},
expected: "<username/password>:string . \n",
},
{
skv: &skv{
attr: "B*-tree",
schema: pb.SchemaUpdate{
Predicate: "",
ValueType: pb.Posting_UID,
Directive: pb.SchemaUpdate_REVERSE,
List: true,
Count: false,
Upsert: false,
Lang: false,
},
},
expected: "<B*-tree>:[uid] @reverse . \n",
},
{
skv: &skv{
attr: "base_de_données",
schema: pb.SchemaUpdate{
Predicate: "",
ValueType: pb.Posting_STRING,
Directive: pb.SchemaUpdate_NONE,
List: false,
Count: false,
Upsert: false,
Lang: true,
},
},
expected: "<base_de_données>:string @lang . \n",
},
{
skv: &skv{
attr: "data_base",
schema: pb.SchemaUpdate{
Predicate: "",
ValueType: pb.Posting_STRING,
Directive: pb.SchemaUpdate_NONE,
List: false,
Count: false,
Upsert: false,
Lang: true,
},
},
expected: "data_base:string @lang . \n",
},
{
skv: &skv{
attr: "data.base",
schema: pb.SchemaUpdate{
Predicate: "",
ValueType: pb.Posting_STRING,
Directive: pb.SchemaUpdate_NONE,
List: false,
Count: false,
Upsert: false,
Lang: true,
},
},
expected: "data.base:string @lang . \n",
},
}
for _, testCase := range testCases {
list, err := toSchema(testCase.skv.attr, testCase.skv.schema)
Expand Down