2021-12-20 21:08:21 +00:00
|
|
|
// Package backlinks maintains the index of backlinks and lets you update it and query it.
|
|
|
|
package backlinks
|
2021-08-31 17:27:52 +00:00
|
|
|
|
|
|
|
import (
|
2022-02-19 08:26:38 +00:00
|
|
|
"log"
|
2021-12-20 21:08:21 +00:00
|
|
|
"os"
|
2021-08-31 17:27:52 +00:00
|
|
|
|
2021-12-30 19:59:28 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2021-08-31 17:27:52 +00:00
|
|
|
)
|
|
|
|
|
2022-04-02 16:58:57 +00:00
|
|
|
// yieldHyphaBacklinks gets backlinks for the desired hypha, sorts and yields them one by one.
|
|
|
|
func yieldHyphaBacklinks(hyphaName string) <-chan string {
|
2021-12-30 19:59:28 +00:00
|
|
|
hyphaName = util.CanonicalName(hyphaName)
|
|
|
|
out := make(chan string)
|
|
|
|
sorted := hyphae.PathographicSort(out)
|
|
|
|
go func() {
|
|
|
|
backlinks, exists := backlinkIndex[hyphaName]
|
|
|
|
if exists {
|
|
|
|
for link := range backlinks {
|
|
|
|
out <- link
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(out)
|
|
|
|
}()
|
|
|
|
return sorted
|
|
|
|
}
|
|
|
|
|
|
|
|
var backlinkConveyor = make(chan backlinkIndexOperation) // No need to buffer because these operations are rare.
|
|
|
|
|
|
|
|
// RunBacklinksConveyor runs an index operation processing loop. Call it somewhere in main.
|
|
|
|
func RunBacklinksConveyor() {
|
|
|
|
// It is supposed to run as a goroutine for all the time. So, don't blame the infinite loop.
|
|
|
|
defer close(backlinkConveyor)
|
|
|
|
for {
|
|
|
|
(<-backlinkConveyor).apply()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var backlinkIndex = make(map[string]linkSet)
|
|
|
|
|
|
|
|
// IndexBacklinks traverses all text hyphae, extracts links from them and forms an initial index. Call it when indexing and reindexing hyphae.
|
|
|
|
func IndexBacklinks() {
|
|
|
|
// It is safe to ignore the mutex, because there is only one worker.
|
2022-01-30 21:34:52 +00:00
|
|
|
for h := range hyphae.FilterHyphaeWithText(hyphae.YieldExistingHyphae()) {
|
2022-02-03 21:47:36 +00:00
|
|
|
foundLinks := extractHyphaLinksFromContent(h.CanonicalName(), fetchText(h))
|
2021-12-30 19:59:28 +00:00
|
|
|
for _, link := range foundLinks {
|
|
|
|
if _, exists := backlinkIndex[link]; !exists {
|
|
|
|
backlinkIndex[link] = make(linkSet)
|
|
|
|
}
|
2022-02-03 21:47:36 +00:00
|
|
|
backlinkIndex[link][h.CanonicalName()] = struct{}{}
|
2021-12-30 19:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BacklinksCount returns the amount of backlinks to the hypha.
|
2022-02-19 08:26:38 +00:00
|
|
|
func BacklinksCount(h hyphae.Hypha) int {
|
2022-02-03 22:29:01 +00:00
|
|
|
if links, exists := backlinkIndex[h.CanonicalName()]; exists {
|
|
|
|
return len(links)
|
2021-12-30 19:59:28 +00:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-08-31 17:27:52 +00:00
|
|
|
// Using set here seems like the most appropriate solution
|
|
|
|
type linkSet map[string]struct{}
|
|
|
|
|
2021-08-31 19:28:12 +00:00
|
|
|
func toLinkSet(xs []string) linkSet {
|
|
|
|
result := make(linkSet)
|
|
|
|
for _, x := range xs {
|
|
|
|
result[x] = struct{}{}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-02-19 08:26:38 +00:00
|
|
|
func fetchText(h hyphae.Hypha) string {
|
|
|
|
var path string
|
|
|
|
switch h := h.(type) {
|
|
|
|
case *hyphae.EmptyHypha:
|
2021-08-31 19:28:12 +00:00
|
|
|
return ""
|
2022-02-19 08:26:38 +00:00
|
|
|
case *hyphae.TextualHypha:
|
|
|
|
path = h.TextFilePath()
|
|
|
|
case *hyphae.MediaHypha:
|
|
|
|
if !h.HasTextFile() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
path = h.TextFilePath()
|
2021-08-31 19:28:12 +00:00
|
|
|
}
|
2022-02-19 08:26:38 +00:00
|
|
|
|
|
|
|
text, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return ""
|
2021-08-31 19:28:12 +00:00
|
|
|
}
|
2022-02-19 08:26:38 +00:00
|
|
|
return string(text)
|
2021-08-31 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 20:59:23 +00:00
|
|
|
// backlinkIndexOperation is an operation for the backlink index. This operation is executed async-safe.
|
|
|
|
type backlinkIndexOperation interface {
|
|
|
|
apply()
|
2021-09-01 06:28:21 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 20:59:23 +00:00
|
|
|
// backlinkIndexEdit contains data for backlink index update after a hypha edit
|
|
|
|
type backlinkIndexEdit struct {
|
2021-12-30 19:59:28 +00:00
|
|
|
name string
|
|
|
|
oldLinks []string
|
|
|
|
newLinks []string
|
2021-09-01 06:28:21 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 19:59:28 +00:00
|
|
|
// apply changes backlink index respective to the operation data
|
2021-12-20 20:59:23 +00:00
|
|
|
func (op backlinkIndexEdit) apply() {
|
2021-12-30 19:59:28 +00:00
|
|
|
oldLinks := toLinkSet(op.oldLinks)
|
|
|
|
newLinks := toLinkSet(op.newLinks)
|
2021-09-01 06:28:21 +00:00
|
|
|
for link := range oldLinks {
|
|
|
|
if _, exists := newLinks[link]; !exists {
|
2021-12-30 19:59:28 +00:00
|
|
|
delete(backlinkIndex[link], op.name)
|
2021-09-01 06:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for link := range newLinks {
|
|
|
|
if _, exists := oldLinks[link]; !exists {
|
|
|
|
if _, exists := backlinkIndex[link]; !exists {
|
|
|
|
backlinkIndex[link] = make(linkSet)
|
|
|
|
}
|
2021-12-30 19:59:28 +00:00
|
|
|
backlinkIndex[link][op.name] = struct{}{}
|
2021-09-01 06:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 20:59:23 +00:00
|
|
|
// backlinkIndexDeletion contains data for backlink index update after a hypha deletion
|
|
|
|
type backlinkIndexDeletion struct {
|
2021-12-30 19:59:28 +00:00
|
|
|
name string
|
|
|
|
links []string
|
2021-09-01 06:28:21 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 21:08:21 +00:00
|
|
|
// apply changes backlink index respective to the operation data
|
2021-12-20 20:59:23 +00:00
|
|
|
func (op backlinkIndexDeletion) apply() {
|
2021-12-30 19:59:28 +00:00
|
|
|
for _, link := range op.links {
|
2021-09-01 06:28:21 +00:00
|
|
|
if lSet, exists := backlinkIndex[link]; exists {
|
2021-12-30 19:59:28 +00:00
|
|
|
delete(lSet, op.name)
|
2021-09-01 06:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 20:59:23 +00:00
|
|
|
// backlinkIndexRenaming contains data for backlink index update after a hypha renaming
|
|
|
|
type backlinkIndexRenaming struct {
|
2021-12-30 19:59:28 +00:00
|
|
|
oldName string
|
|
|
|
newName string
|
|
|
|
links []string
|
2021-09-01 06:28:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 16:58:57 +00:00
|
|
|
// apply changes backlink index respective to the operation data
|
2021-12-20 20:59:23 +00:00
|
|
|
func (op backlinkIndexRenaming) apply() {
|
2021-12-30 19:59:28 +00:00
|
|
|
for _, link := range op.links {
|
2021-09-01 06:28:21 +00:00
|
|
|
if lSet, exists := backlinkIndex[link]; exists {
|
2021-12-30 19:59:28 +00:00
|
|
|
delete(lSet, op.oldName)
|
|
|
|
backlinkIndex[link][op.newName] = struct{}{}
|
2021-09-01 06:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|