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