commit f677c57f351b982ac757af9f0094902645053580
parent 6ff2ef57992ca1c8d19c4bbb1fa05d5035c5bf4f
Author: Peter Kosyh <p.kosyh@gmail.com>
Date: Fri, 5 Feb 2021 12:48:23 +0000
Merge branch 'master' of https://github.com/gl00my/ii-go
Diffstat:
2 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/ii-node/web.go b/ii-node/web.go
@@ -501,18 +501,11 @@ func www_topic(ctx *WebContext, w http.ResponseWriter, r *http.Request, page int
ctx.Echo = mi.Echo
mis := db.LookupIDS(Select(ctx, ii.Query{Echo: mi.Echo}))
- topic := mi.Id
- for p := mi; p != nil; p = db.LookupFast(p.Repto, false) {
- if p.Repto == p.Id {
- break
- }
- if p.Echo != mi.Echo {
- continue
- }
- topic = p.Id
- }
+ topics := db.GetTopics(mis)
+ topic := mi.Topic
ctx.Topic = topic
- ids := db.GetTopics(mis)[topic]
+ ids := topics[topic]
+
if len(ids) == 0 {
ids = append(ids, id)
} else if topic != mi.Id {
diff --git a/ii/db.go b/ii/db.go
@@ -36,13 +36,14 @@ type MsgInfo struct {
Off int64
Repto string
From string
+ Topic string
}
// Index object. Holds List and Hash for all MsgInfo entries
// FileSize is used to auto reread new entries if it has changed by
// someone.
type Index struct {
- Hash map[string]MsgInfo
+ Hash map[string]*MsgInfo
List []string
FileSize int64
}
@@ -253,6 +254,10 @@ func (db *DB) LoadIndex() error {
return err
}
Idx = db.Idx
+ // rebuild topics
+ for _, v := range Idx.Hash {
+ v.Topic = ""
+ }
} else if info.Size() < db.Idx.FileSize {
Info.Printf("Index file truncated, rebuild inndex...")
file, err = db._ReopenIndex()
@@ -265,7 +270,7 @@ func (db *DB) LoadIndex() error {
return nil
}
} else {
- Idx.Hash = make(map[string]MsgInfo)
+ Idx.Hash = make(map[string]*MsgInfo)
}
var err2 error
linenr := 0
@@ -289,7 +294,7 @@ func (db *DB) LoadIndex() error {
} else {
mi.Num = mm.Num
}
- Idx.Hash[mi.Id] = mi
+ Idx.Hash[mi.Id] = &mi
// Trace.Printf("Adding %s to index", mi.Id)
return true
})
@@ -323,7 +328,7 @@ func (db *DB) _Lookup(Id string, bl bool, idx bool) *MsgInfo {
if !ok || (!bl && info.Off < 0) {
return nil
}
- return &info
+ return info
}
// Lookup variant, but without locking.
@@ -468,7 +473,7 @@ type Query struct {
Lim int
Blacklisted bool
User User
- Match func(mi MsgInfo, q Query) bool
+ Match func(mi *MsgInfo, q Query) bool
}
// utility function to add string in front of slice
@@ -480,7 +485,7 @@ func prependStr(x []string, y string) []string {
}
// Default match function for queries.
-func (db *DB) Match(info MsgInfo, r Query) bool {
+func (db *DB) Match(info *MsgInfo, r Query) bool {
if r.Blacklisted {
if info.Off >= 0 {
return false
@@ -527,7 +532,7 @@ type Echo struct {
Name string
Count int
Topics int
- Last MsgInfo
+ Last *MsgInfo
Msg *Msg
}
@@ -682,13 +687,27 @@ func (db *DB) GetTopics(mi []*MsgInfo) map[string][]string {
continue
}
var l []*MsgInfo
+ if m.Topic != "" { // fast path
+ if len(topics[m.Topic]) == 0 {
+ topics[m.Topic] = append(topics[m.Topic], m.Topic)
+ }
+ if m.Id != m.Topic {
+ topics[m.Topic] = append(topics[m.Topic], m.Id)
+ intopic[m.Id] = m.Topic
+ }
+ continue
+ }
for p := m; p != nil; p = db.LookupFast(p.Repto, false) {
- if p.Repto == p.Id { // self answer?
+ if p.Repto == p.Id || p.Topic == "visited" { // loop?
+ p.Topic = ""
break
}
if m.Echo != p.Echo {
continue
}
+ if p.Topic == "" {
+ p.Topic = "visited"
+ }
l = append(l, p)
}
if len(l) == 0 {
@@ -703,6 +722,7 @@ func (db *DB) GetTopics(mi []*MsgInfo) map[string][]string {
})
for _, i := range l {
if i.Id == t.Id {
+ i.Topic = t.Id
continue
}
if _, ok := intopic[i.Id]; ok {
@@ -710,8 +730,10 @@ func (db *DB) GetTopics(mi []*MsgInfo) map[string][]string {
}
topics[t.Id] = append(topics[t.Id], i.Id)
intopic[i.Id] = t.Id
+ i.Topic = t.Id
}
}
+
return topics
}