Rename functions for their purpose

This commit is contained in:
Harshavardhana 2015-06-27 12:39:04 -07:00
parent 9c2e861470
commit ae66ae42c4
2 changed files with 38 additions and 26 deletions

View File

@ -160,36 +160,36 @@ func (b bucket) ListObjects(prefix, marker, delimiter string, maxkeys int) ([]st
for objectName := range bucketMetadata.Buckets[b.getBucketName()].BucketObjects { for objectName := range bucketMetadata.Buckets[b.getBucketName()].BucketObjects {
if strings.HasPrefix(objectName, strings.TrimSpace(prefix)) { if strings.HasPrefix(objectName, strings.TrimSpace(prefix)) {
if objectName > marker { if objectName > marker {
objects = appendUniq(objects, objectName) objects = AppendU(objects, objectName)
} }
} }
} }
if strings.TrimSpace(prefix) != "" { if strings.TrimSpace(prefix) != "" {
objects = removePrefix(objects, prefix) objects = TrimPrefix(objects, prefix)
} }
var prefixes []string var prefixes []string
var filteredObjects []string var filteredObjects []string
if strings.TrimSpace(delimiter) != "" { if strings.TrimSpace(delimiter) != "" {
filteredObjects = filterDelimited(objects, delimiter) filteredObjects = HasNoDelimiter(objects, delimiter)
prefixes = filterNotDelimited(objects, delimiter) prefixes = HasDelimiter(objects, delimiter)
prefixes = extractDelimited(prefixes, delimiter) prefixes = SplitDelimiter(prefixes, delimiter)
prefixes = uniqueObjects(prefixes) prefixes = SortU(prefixes)
} else { } else {
filteredObjects = objects filteredObjects = objects
} }
var results []string var results []string
var commonPrefixes []string var commonPrefixes []string
for _, commonPrefix := range prefixes {
commonPrefixes = AppendU(commonPrefixes, prefix+commonPrefix)
}
sort.Strings(filteredObjects) sort.Strings(filteredObjects)
for _, objectName := range filteredObjects { for _, objectName := range filteredObjects {
if len(results) >= maxkeys { if len(results) >= maxkeys {
isTruncated = true isTruncated = true
break break
} }
results = appendUniq(results, prefix+objectName) results = AppendU(results, prefix+objectName)
}
for _, commonPrefix := range prefixes {
commonPrefixes = appendUniq(commonPrefixes, prefix+commonPrefix)
} }
sort.Strings(results) sort.Strings(results)
sort.Strings(commonPrefixes) sort.Strings(commonPrefixes)

View File

@ -1,3 +1,19 @@
/*
* Minimalist Object Storage, (C) 2015 Minio, Inc.
*
* 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 donut package donut
import ( import (
@ -5,7 +21,8 @@ import (
"strings" "strings"
) )
func appendUniq(slice []string, i string) []string { // AppendU append to an input slice if the element is unique and provides a new slice
func AppendU(slice []string, i string) []string {
for _, ele := range slice { for _, ele := range slice {
if ele == i { if ele == i {
return slice return slice
@ -14,17 +31,8 @@ func appendUniq(slice []string, i string) []string {
return append(slice, i) return append(slice, i)
} }
func filterPrefix(objects []string, prefix string) []string { // TrimPrefix trims off a prefix string from all the elements in a given slice
var results []string func TrimPrefix(objects []string, prefix string) []string {
for _, object := range objects {
if strings.HasPrefix(object, prefix) {
results = append(results, object)
}
}
return results
}
func removePrefix(objects []string, prefix string) []string {
var results []string var results []string
for _, object := range objects { for _, object := range objects {
results = append(results, strings.TrimPrefix(object, prefix)) results = append(results, strings.TrimPrefix(object, prefix))
@ -32,7 +40,8 @@ func removePrefix(objects []string, prefix string) []string {
return results return results
} }
func filterDelimited(objects []string, delim string) []string { // HasNoDelimiter provides a new slice from an input slice which has elements without delimiter
func HasNoDelimiter(objects []string, delim string) []string {
var results []string var results []string
for _, object := range objects { for _, object := range objects {
if !strings.Contains(object, delim) { if !strings.Contains(object, delim) {
@ -42,7 +51,8 @@ func filterDelimited(objects []string, delim string) []string {
return results return results
} }
func filterNotDelimited(objects []string, delim string) []string { // HasDelimiter provides a new slice from an input slice which has elements with a delimiter
func HasDelimiter(objects []string, delim string) []string {
var results []string var results []string
for _, object := range objects { for _, object := range objects {
if strings.Contains(object, delim) { if strings.Contains(object, delim) {
@ -52,7 +62,8 @@ func filterNotDelimited(objects []string, delim string) []string {
return results return results
} }
func extractDelimited(objects []string, delim string) []string { // SplitDelimiter provides a new slice from an input slice by splitting a delimiter
func SplitDelimiter(objects []string, delim string) []string {
var results []string var results []string
for _, object := range objects { for _, object := range objects {
parts := strings.Split(object, delim) parts := strings.Split(object, delim)
@ -61,7 +72,8 @@ func extractDelimited(objects []string, delim string) []string {
return results return results
} }
func uniqueObjects(objects []string) []string { // SortU sort a slice in lexical order, removing duplicate elements
func SortU(objects []string) []string {
objectMap := make(map[string]string) objectMap := make(map[string]string)
for _, v := range objects { for _, v := range objects {
objectMap[v] = v objectMap[v] = v