summaryrefslogtreecommitdiff
path: root/sort.go
diff options
context:
space:
mode:
Diffstat (limited to 'sort.go')
-rw-r--r--sort.go54
1 files changed, 24 insertions, 30 deletions
diff --git a/sort.go b/sort.go
index 871632b..ff7026b 100644
--- a/sort.go
+++ b/sort.go
@@ -14,31 +14,21 @@ func makeSortfile() {
syncLock(f, sortmap)
iterTop(f, sortmap)
iterNext(f, sortmap)
- // setup Sort() functions
- if len(argv.Sort) == 0 {
- // don't do any sorting
- // setup Append() functions
- if argv.Append == "" {
- iterAppend(f, sortmap) // Append() enforce no unique keys
- } else {
- iterAppend(f, sortmap) // Append() enforce no unique keys
- sortmap["append"] = argv.Append
+ iterAppend(f, sortmap) // Append() enforce no unique keys
+ iterSortAll(f, sortmap)
+
+ if argv.Append != "" {
+ sortmap["append"] = string(argv.Append)
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
- }
- } else {
- sortparts := strings.Split(argv.Sort[0], ",")
+ }
+
+ for _, s := range argv.Sort {
+ sortparts := strings.Split(s, ",")
sortmap["sortBy"] = sortparts[0]
sortmap["sortKey"] = sortparts[1]
- iterSort(f, sortmap)
+ iterSortBy(f, sortmap)
- if argv.Append == "" {
- iterAppend(f, sortmap) // Append() enforce no unique keys
- } else {
- iterAppend(f, sortmap) // Append() enforce no unique keys
- sortmap["append"] = argv.Append
- iterAppend(f, sortmap) // Append() enforce unique key argv.Append
- }
sortmap["append"] = sortmap["sortKey"]
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
@@ -125,7 +115,7 @@ func iterNext(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "")
}
-func iterSort(w io.Writer, names map[string]string) {
+func iterSortAll(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") All() *"+names["Base"]+"Iterator {")
fmt.Fprintln(w, " "+names["base"]+"Pointers := all.selectAll"+names["Base"]+"()")
fmt.Fprintln(w, "")
@@ -133,6 +123,16 @@ func iterSort(w io.Writer, names map[string]string) {
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "func (all *"+names["Bases"]+") Len() int {")
+ fmt.Fprintln(w, " "+names["lock"]+".RLock()")
+ fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " return len(all."+names["Bases"]+")")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+}
+
+func iterSortBy(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Sort"+names["sortBy"]+"() *"+names["Base"]+"Iterator {")
fmt.Fprintln(w, " packs := all.selectAll"+names["Base"]+"()")
fmt.Fprintln(w, "")
@@ -142,22 +142,16 @@ func iterSort(w io.Writer, names map[string]string) {
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
- fmt.Fprintln(w, "func (all *"+names["Bases"]+") Len() int {")
- fmt.Fprintln(w, " "+names["lock"]+".RLock()")
- fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
- fmt.Fprintln(w, "")
- fmt.Fprintln(w, " return len(all."+names["Bases"]+")")
- fmt.Fprintln(w, "}")
- fmt.Fprintln(w, "")
-}
-func iterEnd(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "type "+names["Base"]+""+names["sortBy"]+" []*"+names["Base"]+"")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (a "+names["Base"]+""+names["sortBy"]+") Len() int { return len(a) }")
fmt.Fprintln(w, "func (a "+names["Base"]+""+names["sortBy"]+") Less(i, j int) bool { return a[i]."+names["sortKey"]+" < a[j]."+names["sortKey"]+" }")
fmt.Fprintln(w, "func (a "+names["Base"]+""+names["sortBy"]+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
fmt.Fprintln(w, "")
+}
+
+func iterEnd(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// safely returns a slice of pointers to the "+names["Base"]+" protobufs")
fmt.Fprintln(w, "func (all *"+names["Bases"]+") selectAll"+names["Base"]+"() []*"+names["Base"]+" {")
fmt.Fprintln(w, " "+names["lock"]+".RLock()")