-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
big speed improvements and remove redis dependency
- Loading branch information
Showing
14 changed files
with
427 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
*.yml | ||
articles | ||
Dockerfile | ||
Dockerfile | ||
content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ go.work | |
.env | ||
articles | ||
TODO.md | ||
style.css | ||
style.css | ||
content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
|
||
badger "github.com/dgraph-io/badger/v4" | ||
) | ||
|
||
var Badger Database | ||
|
||
type Database struct { | ||
*badger.DB | ||
} | ||
|
||
func InitBadger() { | ||
opts := badger.DefaultOptions("").WithInMemory(true) | ||
opts.Logger = nil // Disable logging | ||
var err error | ||
Badger.DB, err = badger.Open(opts) | ||
if err != nil { | ||
log.Fatal().Err(err) | ||
} | ||
} | ||
|
||
// BLOGO SPECIFIC FUNCTIONS | ||
|
||
func (d *Database) GetAllArticleSlugs() ([]string, error) { | ||
var keys []string | ||
err := d.View(func(txn *badger.Txn) error { | ||
opts := badger.DefaultIteratorOptions | ||
opts.Prefix = []byte("post_") | ||
it := txn.NewIterator(opts) | ||
defer it.Close() | ||
|
||
for it.Rewind(); it.Valid(); it.Next() { | ||
item := it.Item() | ||
key := item.Key() | ||
keys = append(keys, strings.Trim(string(key), "post_")) | ||
} | ||
return nil | ||
}) | ||
return keys, err | ||
} | ||
|
||
func (d *Database) GetPostBySlug(slug string) (ArticleData, error) { | ||
var value []byte | ||
var aData ArticleData | ||
err := d.View(func(txn *badger.Txn) error { | ||
item, err := txn.Get([]byte("post_" + slug)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = item.Value(func(val []byte) error { | ||
value = val | ||
return nil | ||
}) | ||
return err | ||
}) | ||
if err != nil { | ||
return aData, err | ||
} | ||
|
||
err = json.Unmarshal(value, &aData) | ||
if err != nil { | ||
return aData, err | ||
} | ||
return aData, err | ||
} | ||
|
||
func (d *Database) GetAllArticles() []ArticleData { | ||
var articles []ArticleData | ||
articleBytes := d.GetValuesWithPrefix("post_") | ||
for _, ab := range articleBytes { | ||
var article ArticleData | ||
err := json.Unmarshal(ab, &article) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Error unmarshalling article from Badger:") | ||
continue | ||
} | ||
articles = append(articles, article) | ||
} | ||
return articles | ||
} | ||
|
||
func (d *Database) DeleteArticle(key string) error { | ||
return d.Delete("post_" + key) | ||
} | ||
|
||
// GENERIC FUNCTIONS | ||
|
||
func (d *Database) Set(key string, value []byte) error { | ||
return d.Update(func(txn *badger.Txn) error { | ||
return txn.Set([]byte(key), value) | ||
}) | ||
} | ||
|
||
func (d *Database) Get(key string) ([]byte, error) { | ||
var value []byte | ||
err := d.View(func(txn *badger.Txn) error { | ||
item, err := txn.Get([]byte(key)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = item.Value(func(val []byte) error { | ||
value = val | ||
return nil | ||
}) | ||
return err | ||
}) | ||
return value, err | ||
} | ||
|
||
func (d *Database) Delete(key string) error { | ||
return d.Update(func(txn *badger.Txn) error { | ||
return txn.Delete([]byte(key)) | ||
}) | ||
} | ||
|
||
func (d *Database) GetAllKeys() ([]string, error) { | ||
var keys []string | ||
err := d.View(func(txn *badger.Txn) error { | ||
opts := badger.DefaultIteratorOptions | ||
it := txn.NewIterator(opts) | ||
defer it.Close() | ||
|
||
for it.Rewind(); it.Valid(); it.Next() { | ||
item := it.Item() | ||
key := item.Key() | ||
keys = append(keys, string(key)) | ||
} | ||
return nil | ||
}) | ||
return keys, err | ||
} | ||
|
||
func (d *Database) GetValues() ([][]byte, error) { | ||
var values [][]byte | ||
err := d.View(func(txn *badger.Txn) error { | ||
opts := badger.DefaultIteratorOptions | ||
it := txn.NewIterator(opts) | ||
defer it.Close() | ||
|
||
for it.Rewind(); it.ValidForPrefix(opts.Prefix); it.Next() { | ||
item := it.Item() | ||
err := item.Value(func(val []byte) error { | ||
values = append(values, val) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
return values, err | ||
} | ||
|
||
func (d *Database) GetValuesWithPrefix(prefix string) [][]byte { | ||
var values [][]byte | ||
d.View(func(txn *badger.Txn) error { | ||
opts := badger.DefaultIteratorOptions | ||
opts.Prefix = []byte(prefix) | ||
it := txn.NewIterator(opts) | ||
defer it.Close() | ||
|
||
for it.Rewind(); it.ValidForPrefix(opts.Prefix); it.Next() { | ||
item := it.Item() | ||
err := item.Value(func(val []byte) error { | ||
values = append(values, val) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
return values | ||
} |
Oops, something went wrong.