summaryrefslogtreecommitdiff
path: root/app/bitcask_store.go
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2020-03-25 16:01:29 +1000
committerJames Mills <prologic@shortcircuit.net.au>2020-03-25 16:01:29 +1000
commit2c8af031648d110148dfe672c4fc3063f1743374 (patch)
treeb474bfb91fa8dcc524940f779757422f44efafd1 /app/bitcask_store.go
parent7113a7bd05c72406fc971fa720b05bb934732c04 (diff)
Refactored the way views are stored with a mgiration off the old data (to be removed)
Diffstat (limited to 'app/bitcask_store.go')
-rw-r--r--app/bitcask_store.go72
1 files changed, 67 insertions, 5 deletions
diff --git a/app/bitcask_store.go b/app/bitcask_store.go
index 7289b35..d79706e 100644
--- a/app/bitcask_store.go
+++ b/app/bitcask_store.go
@@ -23,13 +23,38 @@ func NewBitcaskStore(path string, options ...bitcask.Option) (Store, error) {
return &BitcaskStore{db: db}, nil
}
+// Migrate ...
+func (s *BitcaskStore) Migrate(collection, id string) error {
+ if s.db.Has([]byte(fmt.Sprintf("/views/%s/%s", collection, id))) {
+ oldViews, err := s.GetViews_(collection, id)
+ if err != nil {
+ err := fmt.Errorf("error getting old views for %s %s: %w", collection, id, err)
+ return err
+ }
+
+ buf := make([]byte, 8)
+ binary.BigEndian.PutUint64(buf, uint64(oldViews))
+ err = s.db.Put([]byte(fmt.Sprintf("/views/%s/%s", id)), buf)
+ if err != nil {
+ err := fmt.Errorf("error storing new views for %s: %w", id, err)
+ return err
+ }
+
+ if err := s.db.Delete([]byte(fmt.Sprintf("/views/%s/%s", collection, id))); err != nil {
+ err := fmt.Errorf("error deleting old views for %s %s: %w", collection, id, err)
+ return err
+ }
+ }
+ return nil
+}
+
// Close ...
func (s *BitcaskStore) Close() error {
return s.db.Close()
}
-// GetViews ...
-func (s *BitcaskStore) GetViews(collection, id string) (int64, error) {
+// GetViews_ ...
+func (s *BitcaskStore) GetViews_(collection, id string) (int64, error) {
var views uint64
rawViews, err := s.db.Get([]byte(fmt.Sprintf("/views/%s/%s", collection, id)))
if err != nil {
@@ -45,9 +70,9 @@ func (s *BitcaskStore) GetViews(collection, id string) (int64, error) {
return int64(views), nil
}
-// IncView ...
-func (s *BitcaskStore) IncView(collection, id string) error {
- views, err := s.GetViews(collection, id)
+// IncView_ ...
+func (s *BitcaskStore) IncView_(collection, id string) error {
+ views, err := s.GetViews_(collection, id)
if err != nil {
err := fmt.Errorf("error getting existing views for %s %s: %w", collection, id, err)
return err
@@ -64,3 +89,40 @@ func (s *BitcaskStore) IncView(collection, id string) error {
return nil
}
+
+// GetViews ...
+func (s *BitcaskStore) GetViews(id string) (int64, error) {
+ var views uint64
+ rawViews, err := s.db.Get([]byte(fmt.Sprintf("/views/%s", id)))
+ if err != nil {
+ if err != bitcask.ErrKeyNotFound {
+ err := fmt.Errorf("error getting views for %s: %w", id, err)
+ log.Error(err)
+ return 0, err
+ }
+ } else {
+ views = binary.BigEndian.Uint64(rawViews)
+ }
+
+ return int64(views), nil
+}
+
+// IncViews ...
+func (s *BitcaskStore) IncViews(id string) error {
+ views, err := s.GetViews(id)
+ if err != nil {
+ err := fmt.Errorf("error getting existing views for %s: %w", id, err)
+ return err
+ }
+
+ buf := make([]byte, 8)
+ views++
+ binary.BigEndian.PutUint64(buf, uint64(views))
+ err = s.db.Put([]byte(fmt.Sprintf("/views/%s", id)), buf)
+ if err != nil {
+ err := fmt.Errorf("error storing updated views for %s: %w", id, err)
+ return err
+ }
+
+ return nil
+}