commit 90a4006b917a737071fdf3639842d098eaec459a
parent 475018ddc78eef98bc6342cdf8d07a180f0949c3
Author: Peter Kosyh <p.kosyh@gmail.com>
Date: Wed, 2 Sep 2020 23:44:10 +0300
topics speedup
Diffstat:
3 files changed, 62 insertions(+), 44 deletions(-)
diff --git a/ii-node/main.go b/ii-node/main.go
@@ -52,7 +52,15 @@ var listen_opt *string = flag.String("L", ":8080", "Listen address")
var sysname_opt *string = flag.String("sys", "ii-go", "Node name")
var verbose_opt *bool = flag.Bool("v", false, "Verbose")
+type WWW struct {
+ tpl *template.Template
+ db *ii.DB
+ w http.ResponseWriter
+ r *http.Request
+}
+
func main() {
+ var www WWW
ii.OpenLog(ioutil.Discard, os.Stdout, os.Stderr)
db := open_db(*db_opt)
@@ -64,10 +72,10 @@ func main() {
db.Name = *sysname_opt
- t := template.Must(template.ParseGlob("tpl/*.tpl"))
-
+ www.tpl = template.Must(template.ParseGlob("tpl/*.tpl"))
+ www.db = db
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- err := Web(db, t, w, r)
+ err := Web(www, w, r)
if err != nil {
fmt.Fprintf(w, "%s\n", err)
}
diff --git a/ii-node/web.go b/ii-node/web.go
@@ -12,40 +12,45 @@ import (
type WebContext struct {
Echoes []ii.Echo
- Msg []*ii.Msg
- Topics []*Topic
+ Topics []Topic
+ Msg []ii.Msg
Render func(string) template.HTML
}
-func www_index(db *ii.DB, t *template.Template, w http.ResponseWriter, r *http.Request) error {
+func www_index(www WWW, w http.ResponseWriter, r *http.Request) error {
var ctx WebContext
ii.Trace.Printf("www index")
- ctx.Echoes = db.Echoes(nil)
- err := t.ExecuteTemplate(w, "index.tpl", ctx)
+ ctx.Echoes = www.db.Echoes(nil)
+ // ctx.Msg = make([]*ii.Msg, len(ctx.Echoes))
+ // for k, e := range ctx.Echoes {
+ // ctx.Msg[k] = db.Get(e.Last.Id)
+ // }
+ err := www.tpl.ExecuteTemplate(w, "index.tpl", ctx)
return err
}
-func getTopicFor(head *ii.MsgInfo, mi []*ii.MsgInfo) []string {
- var ids []string
- hash := make(map[string]bool)
+func getParent(db *ii.DB, i *ii.MsgInfo) *ii.MsgInfo {
+ return db.Lookup(i.Repto)
+}
- hash[head.Id] = true
- hit := true
- ids = append(ids, head.Id)
- for hit {
- hit = false
- for _, m := range mi {
- if _, ok := hash[m.Id]; ok {
- continue
- }
- if _, ok := hash[m.Repto]; ok {
- hash[m.Id] = true
- hit = true
- ids = append(ids, m.Id)
- }
+func getTopics(db *ii.DB, mi []*ii.MsgInfo) map[string][]string {
+ intopic := make(map[string]string)
+ topics := make(map[string][]string)
+ for _, m := range mi {
+ if _, ok := intopic[m.Id]; ok {
+ continue
+ }
+ var l []string
+ for p := m; p != nil; p = getParent(db, p) {
+ l = append(l, p.Id)
+ }
+ t := l[len(l) - 1]
+ for _, id := range l {
+ topics[t] = append(topics[t], id)
+ intopic[id] = t
}
}
- return ids
+ return topics
}
type Topic struct {
@@ -57,31 +62,35 @@ type Topic struct {
Last *ii.Msg
}
-func www_topics(db *ii.DB, echo string, t *template.Template, w http.ResponseWriter, r *http.Request) error {
+func www_topics(www WWW, w http.ResponseWriter, r *http.Request, echo string) error {
+ db := www.db
var ctx WebContext
mis := db.LookupIDS(db.SelectIDS(ii.Query{Echo: echo}))
ii.Trace.Printf("www topics: %s", echo)
- for _, mi := range mis {
- if mi.Repto != "" || db.Exists(mi.Repto) != nil {
- continue
- }
+ topics := getTopics(db, mis)
+ ii.Trace.Printf("Start to generate topics")
+ for _, t := range topics {
+ // if mi.Repto != "" || db.Exists(mi.Repto) != nil {
+ // continue
+ //}
topic := Topic{}
- topic.Ids = getTopicFor(mi, mis)
+ topic.Ids = t
m := db.Get(topic.Ids[0])
if m == nil {
- ii.Error.Printf("Skip wrong message: %s\n", mi.Id)
+ ii.Error.Printf("Skip wrong message: %s\n", topic.Ids[0])
continue
}
topic.Count = len(topic.Ids) - 1
topic.Head = m
topic.Last = db.Get(topic.Ids[topic.Count])
topic.Date = time.Unix(topic.Last.Date, 0).Format("2006-01-02 15:04:05")
- ctx.Topics = append(ctx.Topics, &topic)
+ ctx.Topics = append(ctx.Topics, topic)
}
sort.SliceStable(ctx.Topics, func(i, j int) bool {
return ctx.Topics[i].Last.Date > ctx.Topics[j].Last.Date
})
- err := t.ExecuteTemplate(w, "topics.tpl", ctx)
+ ii.Trace.Printf("Stop to generate topics")
+ err := www.tpl.ExecuteTemplate(w, "topics.tpl", ctx)
return err
}
func msg_format(txt string) template.HTML {
@@ -90,7 +99,8 @@ func msg_format(txt string) template.HTML {
txt = strings.Replace(txt, ">", ">", -1)
return template.HTML(strings.Replace(txt, "\n", "<br/>", -1))
}
-func www_topic(db *ii.DB, id string, t *template.Template, w http.ResponseWriter, r *http.Request) error {
+func www_topic(www WWW, w http.ResponseWriter, r *http.Request, id string) error {
+ db := www.db
var ctx WebContext
ctx.Render = msg_format
mi := db.Lookup(id)
@@ -98,7 +108,7 @@ func www_topic(db *ii.DB, id string, t *template.Template, w http.ResponseWriter
return errors.New("No such message")
}
mis := db.LookupIDS(db.SelectIDS(ii.Query{Echo: mi.Echo, Repto: ""}))
- ids := getTopicFor(mi, mis)
+ ids := getTopics(db, mis)[id]
ii.Trace.Printf("www topic: %s", id)
for _, i := range ids {
m := db.Get(i)
@@ -106,22 +116,22 @@ func www_topic(db *ii.DB, id string, t *template.Template, w http.ResponseWriter
ii.Error.Printf("Skip wrong message: %s", i)
continue
}
- ctx.Msg = append(ctx.Msg, m)
+ ctx.Msg = append(ctx.Msg, *m)
}
- err := t.ExecuteTemplate(w, "topic.tpl", ctx)
+ err := www.tpl.ExecuteTemplate(w, "topic.tpl", ctx)
return err
}
-func Web(db *ii.DB, t *template.Template, w http.ResponseWriter, r *http.Request) error {
+func Web(www WWW, w http.ResponseWriter, r *http.Request) error {
path := strings.TrimPrefix(r.URL.Path, "/")
if path == "" {
- return www_index(db, t, w, r)
+ return www_index(www, w, r)
}
if ii.IsEcho(path) {
- return www_topics(db, path, t, w, r)
+ return www_topics(www, w, r, path)
}
if ii.IsMsgId(path) {
- return www_topic(db, path, t, w, r)
+ return www_topic(www, w, r, path)
}
return nil
}
diff --git a/ii/db.go b/ii/db.go
@@ -233,7 +233,7 @@ func (db *DB) LoadIndex() error {
Idx.List = append(Idx.List, mi.Id)
}
Idx.Hash[mi.Id] = mi
- Trace.Printf("Adding %s to index", mi.Id)
+ // Trace.Printf("Adding %s to index", mi.Id)
return true
})
if err != nil {