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 {
dry bool
state int
content []*videoContainer
par int
}
// listUpdateChunk concurrently runs the lists update function and
......@@ -18,9 +19,19 @@ func (l *listLazy) listUpdateChunk(state int) chan []*videoContainer {
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 {
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
......@@ -28,9 +39,8 @@ func listNew(update func(int) []*videoContainer) *listLazy {
// the requested element is available. If the element could not be acquired
// nil is returned.
func (l *listLazy) listGet(index int) *videoContainer {
par := 5 // parallel update count
for index >= len(l.content) && !l.dry {
temp := make([]chan []*videoContainer, par)
temp := make([]chan []*videoContainer, l.par)
for i, _ := range temp {
temp[i] = l.listUpdateChunk(l.state + i)
......@@ -53,7 +63,12 @@ func (l *listLazy) listGet(index int) *videoContainer {
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
// the length is sufficient. If the list is too small, a smaller slice
// then requested may be returned.
......
......@@ -9,7 +9,6 @@ import (
"sort"
"strconv"
"strings"
"sync"
)
// maps user input to events
......@@ -266,43 +265,25 @@ func logicUserPage(user string, page int) {
func logicNew(none string) {
logicVarWindow = logicWindowSearch
logicVarSearch = ""
videos := []*videoContainer{}
users := logicGetUsers()
lock := &sync.Mutex{}
barrier := &sync.WaitGroup{}
barrier.Add(len(users))
for i, user := range users {
// put into scope
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()
}()
upd := func(state int) []*videoContainer {
if state < len(users) {
return videoListUser(users[state])
}
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()
fmt.Println()
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)
}
......
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