Keep fragments in the blog index file
This commit is contained in:
parent
95fdc4baff
commit
92887a6472
|
@ -2,7 +2,6 @@ package posts
|
|||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UpdateIndex updates a post's metadata in the blog index.
|
||||
|
@ -16,7 +15,8 @@ func UpdateIndex(p *Post) error {
|
|||
|
||||
// Index caches high level metadata about the blog's contents for fast access.
|
||||
type Index struct {
|
||||
Posts map[int]Post `json:"posts"`
|
||||
Posts map[int]Post `json:"posts"`
|
||||
Fragments map[string]int `json:"fragments"`
|
||||
}
|
||||
|
||||
// GetIndex loads the index, or rebuilds it first if it doesn't exist.
|
||||
|
@ -33,7 +33,8 @@ func GetIndex() (*Index, error) {
|
|||
// RebuildIndex builds the index from scratch.
|
||||
func RebuildIndex() (*Index, error) {
|
||||
idx := &Index{
|
||||
Posts: map[int]Post{},
|
||||
Posts: map[int]Post{},
|
||||
Fragments: map[string]int{},
|
||||
}
|
||||
entries, _ := DB.List("blog/posts")
|
||||
for _, doc := range entries {
|
||||
|
@ -61,6 +62,7 @@ func (idx *Index) Update(p *Post) error {
|
|||
Created: p.Created,
|
||||
Updated: p.Updated,
|
||||
}
|
||||
idx.Fragments[p.Fragment] = p.ID
|
||||
err := DB.Commit("blog/index", idx)
|
||||
return err
|
||||
}
|
||||
|
@ -77,6 +79,7 @@ type Tag struct {
|
|||
Count int
|
||||
}
|
||||
|
||||
// ByPopularity sort type.
|
||||
type ByPopularity []Tag
|
||||
|
||||
func (s ByPopularity) Len() int {
|
||||
|
@ -123,34 +126,3 @@ func (idx *Index) Tags() ([]Tag, error) {
|
|||
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
// CleanupFragments to clean up old URL fragments.
|
||||
func CleanupFragments() error {
|
||||
idx, err := GetIndex()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return idx.CleanupFragments()
|
||||
}
|
||||
|
||||
// CleanupFragments to clean up old URL fragments.
|
||||
func (idx *Index) CleanupFragments() error {
|
||||
// Keep track of the active URL fragments so we can clean up orphans.
|
||||
fragments := map[string]struct{}{}
|
||||
for _, p := range idx.Posts {
|
||||
fragments[p.Fragment] = struct{}{}
|
||||
}
|
||||
|
||||
// Clean up unused fragments.
|
||||
byFragment, err := DB.List("blog/fragments")
|
||||
for _, doc := range byFragment {
|
||||
parts := strings.Split(doc, "/")
|
||||
fragment := parts[len(parts)-1]
|
||||
if _, ok := fragments[fragment]; !ok {
|
||||
log.Debug("RebuildIndex() clean up old fragment '%s'", fragment)
|
||||
DB.Delete(doc)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -38,11 +38,6 @@ type Post struct {
|
|||
Updated time.Time `json:"updated"`
|
||||
}
|
||||
|
||||
// ByFragment maps a blog post by its URL fragment.
|
||||
type ByFragment struct {
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
// New creates a blank post with sensible defaults.
|
||||
func New() *Post {
|
||||
return &Post{
|
||||
|
@ -97,14 +92,16 @@ func Load(id int) (*Post, error) {
|
|||
|
||||
// LoadFragment loads a blog entry by its URL fragment.
|
||||
func LoadFragment(fragment string) (*Post, error) {
|
||||
f := ByFragment{}
|
||||
err := DB.Get("blog/fragments/"+fragment, &f)
|
||||
idx, err := GetIndex()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p, err := Load(f.ID)
|
||||
return p, err
|
||||
if postID, ok := idx.Fragments[fragment]; ok {
|
||||
return Load(postID)
|
||||
}
|
||||
|
||||
return nil, errors.New("no such fragment found")
|
||||
}
|
||||
|
||||
// Save the blog post.
|
||||
|
@ -166,7 +163,6 @@ func (p *Post) Save() error {
|
|||
|
||||
// Write the post.
|
||||
DB.Commit(fmt.Sprintf("blog/posts/%d", p.ID), p)
|
||||
DB.Commit(fmt.Sprintf("blog/fragments/%s", p.Fragment), ByFragment{p.ID})
|
||||
|
||||
// Update the index cache.
|
||||
err := UpdateIndex(p)
|
||||
|
@ -174,9 +170,6 @@ func (p *Post) Save() error {
|
|||
return fmt.Errorf("RebuildIndex() error: %v", err)
|
||||
}
|
||||
|
||||
// Clean up fragments.
|
||||
CleanupFragments()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user