Skip to content

Commit

Permalink
Sort moved into RemoveDumplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Zdybał committed Jul 13, 2017
1 parent db14930 commit 5263706
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
4 changes: 0 additions & 4 deletions gql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,6 @@ func Parse(r Request) (res Result, rerr error) {
for _, v := range res.MutationVars {
varNames = append(varNames, v)
}
sort.Strings(varNames)
varNames = x.RemoveDuplicates(varNames)

allVars = append(allVars, &Vars{Needs: varNames})
Expand All @@ -608,9 +607,6 @@ func flatten(vl []*Vars) (needs []string, defines []string) {
func checkDependency(vl []*Vars) error {
needs, defines := flatten(vl)

sort.Strings(needs)
sort.Strings(defines)

needs = x.RemoveDuplicates(needs)

if len(defines) != len(x.RemoveDuplicates(defines)) {
Expand Down
11 changes: 0 additions & 11 deletions gql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/dgraph-io/dgraph/protos"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/x"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -3590,16 +3589,6 @@ func TestFilterUid(t *testing.T) {
require.Equal(t, []uint64{3, 7}, gql.Query[0].Filter.Func.UID)
}

func TestRemoveDuplicates(t *testing.T) {
set := x.RemoveDuplicates([]string{"a", "a", "a", "b", "b", "c", "c"})
require.EqualValues(t, []string{"a", "b", "c"}, set)
}

func TestRemoveDuplicatesWithoutDuplicates(t *testing.T) {
set := x.RemoveDuplicates([]string{"a", "b", "c", "d"})
require.EqualValues(t, []string{"a", "b", "c", "d"}, set)
}

func TestMultipleSetBlocks(t *testing.T) {
query := `
mutation {
Expand Down
3 changes: 0 additions & 3 deletions tok/tok.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package tok

import (
"encoding/binary"
"sort"
"time"

farm "github.com/dgryski/go-farm"
Expand Down Expand Up @@ -273,7 +272,6 @@ func getBleveTokens(name string, identifier byte, sv types.Val) ([]string, error
for i, token := range tokenStream {
terms[i] = encodeToken(string(token.Term), identifier)
}
sort.Strings(terms)
terms = x.RemoveDuplicates(terms)
return terms, nil
}
Expand Down Expand Up @@ -336,7 +334,6 @@ func (t TrigramTokenizer) Tokens(sv types.Val) ([]string, error) {
trigram := value[i : i+3]
tokens[i] = encodeToken(trigram, t.Identifier())
}
sort.Strings(tokens)
x.RemoveDuplicates(tokens)
return tokens, nil
}
Expand Down
4 changes: 3 additions & 1 deletion x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -194,8 +195,9 @@ func ValidateAddress(addr string) bool {
return regExpHostName.MatchString(host)
}

// removes duplicates from a sorted slice of strings. Changes underylying array.
// sorts the slice of strings and removes duplicates. Changes underylying array.
func RemoveDuplicates(s []string) (out []string) {
sort.Strings(s)
out = s[:0]
for i := range s {
if i > 0 && s[i] == s[i-1] {
Expand Down
32 changes: 32 additions & 0 deletions x/x_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package x

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestRemoveDuplicates(t *testing.T) {
set := RemoveDuplicates([]string{"a", "a", "a", "b", "b", "c", "c"})
require.EqualValues(t, []string{"a", "b", "c"}, set)
}

func TestRemoveDuplicatesWithoutDuplicates(t *testing.T) {
set := RemoveDuplicates([]string{"a", "b", "c", "d"})
require.EqualValues(t, []string{"a", "b", "c", "d"}, set)
}

0 comments on commit 5263706

Please sign in to comment.