From 53adfb38f4f4aea191c08e9a0a6992ff6d159a74 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 23 Oct 2015 15:54:47 -0700 Subject: [PATCH] Leverage sort Interface to provide sortUnique function --- pkg/fs/fs-bucket-listobjects.go | 3 ++- pkg/fs/fs-common.go | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pkg/fs/fs-bucket-listobjects.go b/pkg/fs/fs-bucket-listobjects.go index e29a47753..78836fed2 100644 --- a/pkg/fs/fs-bucket-listobjects.go +++ b/pkg/fs/fs-bucket-listobjects.go @@ -21,6 +21,7 @@ import ( "os" "path/filepath" "runtime" + "sort" "strings" "github.com/minio/minio-xl/pkg/probe" @@ -356,6 +357,6 @@ func (fs Filesystem) filterObjects(bucket string, content contentInfo, resources return ObjectMetadata{}, resources, err.Trace() } } - sortUnique(resources.CommonPrefixes) + sortUnique(sort.StringSlice(resources.CommonPrefixes)) return metadata, resources, nil } diff --git a/pkg/fs/fs-common.go b/pkg/fs/fs-common.go index 63ba3cbf4..172cf23f0 100644 --- a/pkg/fs/fs-common.go +++ b/pkg/fs/fs-common.go @@ -45,18 +45,23 @@ func sanitizeWindowsPaths(paths ...string) []string { return results } -// sortUnique sort a slice in lexical order, removing duplicate elements -func sortUnique(objects []string) []string { - results := []string{} - seen := make(map[string]string) - for _, val := range objects { - if _, ok := seen[val]; !ok { - results = append(results, val) - seen[val] = val - } +// sortUnique returns n, the number of distinct elements in data in sorted order. +func sortUnique(data sort.Interface) (n int) { + if n = data.Len(); n < 2 { + return n } - sort.Strings(results) - return results + sort.Sort(data) + a, b := 0, 1 + for b < n { + if data.Less(a, b) { + a++ + if a != b { + data.Swap(a, b) + } + } + b++ + } + return a + 1 } type contentInfo struct {