Commit bd104fc3 authored by Jonny Schäfer's avatar Jonny Schäfer
Browse files

implements new video update using lazy lists

parent 05503158
...@@ -6,6 +6,7 @@ type listLazy struct { ...@@ -6,6 +6,7 @@ type listLazy struct {
dry bool dry bool
state int state int
content []*videoContainer content []*videoContainer
par int
} }
// listUpdateChunk concurrently runs the lists update function and // listUpdateChunk concurrently runs the lists update function and
...@@ -18,9 +19,19 @@ func (l *listLazy) listUpdateChunk(state int) chan []*videoContainer { ...@@ -18,9 +19,19 @@ func (l *listLazy) listUpdateChunk(state int) chan []*videoContainer {
return chn return chn
} }
// listNew creates a new lazy list with the given update function. // listNew creates a new lazy list with the given update function and a
// parallel update count of 5.
func listNew(update func(int) []*videoContainer) *listLazy { func listNew(update func(int) []*videoContainer) *listLazy {
return &listLazy{update, false, 0, []*videoContainer{}} return &listLazy{update, false, 0, []*videoContainer{}, 5}
}
// listPar sets the number of parallel update functions to be executed on each
// triggered list update. The count is always >= 1.
func (l *listLazy) listPar(count int) {
if count < 1 {
count = 1
}
l.par = count
} }
// listGet returns the element with the given index from the list. If // listGet returns the element with the given index from the list. If
...@@ -28,9 +39,8 @@ func listNew(update func(int) []*videoContainer) *listLazy { ...@@ -28,9 +39,8 @@ func listNew(update func(int) []*videoContainer) *listLazy {
// the requested element is available. If the element could not be acquired // the requested element is available. If the element could not be acquired
// nil is returned. // nil is returned.
func (l *listLazy) listGet(index int) *videoContainer { func (l *listLazy) listGet(index int) *videoContainer {
par := 5 // parallel update count
for index >= len(l.content) && !l.dry { for index >= len(l.content) && !l.dry {
temp := make([]chan []*videoContainer, par) temp := make([]chan []*videoContainer, l.par)
for i, _ := range temp { for i, _ := range temp {
temp[i] = l.listUpdateChunk(l.state + i) temp[i] = l.listUpdateChunk(l.state + i)
...@@ -53,7 +63,12 @@ func (l *listLazy) listGet(index int) *videoContainer { ...@@ -53,7 +63,12 @@ func (l *listLazy) listGet(index int) *videoContainer {
return nil return nil
} }
// listLazy tries to return the requested slice from the list. If the // listCached returns all cached entries of the underlying list slice.
func (l *listLazy) listCached() []*videoContainer {
return l.content
}
// listSlice tries to return the requested slice from the list. If the
// list is too short the lists update function will be called until // list is too short the lists update function will be called until
// the length is sufficient. If the list is too small, a smaller slice // the length is sufficient. If the list is too small, a smaller slice
// then requested may be returned. // then requested may be returned.
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
) )
// maps user input to events // maps user input to events
...@@ -266,43 +265,25 @@ func logicUserPage(user string, page int) { ...@@ -266,43 +265,25 @@ func logicUserPage(user string, page int) {
func logicNew(none string) { func logicNew(none string) {
logicVarWindow = logicWindowSearch logicVarWindow = logicWindowSearch
logicVarSearch = "" logicVarSearch = ""
videos := []*videoContainer{}
users := logicGetUsers() users := logicGetUsers()
lock := &sync.Mutex{}
barrier := &sync.WaitGroup{}
barrier.Add(len(users))
for i, user := range users { upd := func(state int) []*videoContainer {
if state < len(users) {
// put into scope return videoListUser(users[state])
user := user
i := i
go func() {
list := videoListUser(user)
cliHome()
fmt.Println()
lock.Lock()
cliStatus("updating users (" + strconv.Itoa(i+1) + "/" + strconv.Itoa(len(users)) + ")")
videos = append(videos, list...)
lock.Unlock()
barrier.Done()
}()
} }
barrier.Wait() return nil // all users updated (wont occur in reality)
}
logicVarLazy = listNew(upd)
cliStatus("updating users ...")
logicVarLazy.listPar(len(users))
logicVarLazy.listGet(0) // downloads all user pages as par = len(users)
cliHome() cliHome()
fmt.Println() fmt.Println()
cliStatus("sorting ...") cliStatus("sorting ...")
sort.Sort(&videoSort{videos}) sort.Sort(&videoSort{logicVarLazy.listCached()})
upd := func(state int) []*videoContainer {
all := videos
videos = nil
return all
}
logicVarLazy = listNew(upd)
logicSearchPage("new videos", 0) logicSearchPage("new videos", 0)
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment