// getFiles returns a sorted list of files in the directory // in which fname exists starting with fname (i.e. fname*). func getFiles(fname string) ([]string, error) { dn := path.Dir(fname) bn := path.Base(fname) files, err := ioutil.ReadDir(dn) if err != nil { return nil, err } match := make([]os.FileInfo, len(files)) count := 0 for _, f := range files { if m, _ := path.Match(bn+".*", f.Name()); m { match[count] = f count++ } } if count > 0 { res := make([]string, count) for i, f := range match[:count] { res[i] = fmt.Sprintf("%s/%s", dn, f.Name()) } sort.Sort(handysort.Strings(res)) return res, nil } return nil, nil }
// Returns elastic search types func (u *User) GetLogTypes() []string { t, err := GetTypes(u.GetIndexName()) if err != nil { sort.Sort(handysort.Strings(t)) } return t }
// UsersHandle channel index func UsersHandle(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) f, err := os.Open(common.GetConfig().LogPath + "/" + vars["channel"] + "/" + vars["month"]) if err != nil { serveError(w, ErrNotFound) return } files, err := f.Readdir(0) if err != nil { serveError(w, err) return } nicks := common.NickList{} for _, file := range files { if NicksExtension.MatchString(file.Name()) { common.ReadNickList(nicks, common.GetConfig().LogPath+"/"+vars["channel"]+"/"+vars["month"]+"/"+file.Name()) } } names := make([]string, 0, len(nicks)) for nick := range nicks { names = append(names, nick+".txt") } sort.Sort(handysort.Strings(names)) serveDirIndex(w, []string{vars["channel"], vars["month"], "userlogs"}, names) }
func BenchmarkHandyStringSort(b *testing.B) { set := testSet(300) arr := make([]string, len(set[0])) b.ResetTimer() for i := 0; i < b.N; i++ { for _, list := range set { copy(arr, list) sort.Sort(handysort.Strings(arr)) } } }
func readDirIndex(path string) ([]string, error) { f, err := os.Open(path) if err != nil { return nil, ErrNotFound } names, err := f.Readdirnames(0) if err != nil { return nil, err } sort.Sort(handysort.Strings(names)) return names, nil }
func readLogDir(path string) ([]string, error) { f, err := os.Open(path) if err != nil { return nil, ErrNotFound } files, err := f.Readdir(0) if err != nil { return nil, err } var names []string for _, file := range files { if LogExtension.MatchString(file.Name()) { names = append(names, file.Name()) } } sort.Sort(handysort.Strings(names)) return names, nil }