storer.go (1305B)
1 package db 2 3 import ( 4 "github.com/dgraph-io/badger" 5 ) 6 7 // Storer is the interface which needs to be conformed to in order to persist Go-NEB data 8 type Storer interface { 9 UpdateNextBatch(userID, nextBatch string) (err error) 10 LoadNextBatch(userID string) (nextBatch string, err error) 11 } 12 13 type MorpheusStorage struct { 14 Database *badger.DB 15 } 16 17 // UpdateNextBatch updates the next_batch token for the given user. 18 func (m *MorpheusStorage) UpdateNextBatch(userID, nextBatch string) (err error) { 19 DBerr := m.Database.Update(func(txn *badger.Txn) error { 20 21 DBSetNextBatchErr := txn.Set([]byte("matrix|"+userID+"|nextBatch|"), []byte(nextBatch)) 22 return DBSetNextBatchErr 23 }) 24 if DBerr != nil { 25 err = DBerr 26 return 27 } 28 return 29 } 30 31 // LoadNextBatch loads the next_batch token for the given user. 32 func (m *MorpheusStorage) LoadNextBatch(userID string) (nextBatch string, err error) { 33 DBerr := m.Database.View(func(txn *badger.Txn) error { 34 35 nextBatchItem, NextBatchErr := txn.Get([]byte("matrix|" + userID + "|nextBatch|")) 36 if NextBatchErr != nil { 37 return NextBatchErr 38 } 39 40 nextBatchByte, nextBatchByteErr := nextBatchItem.Value() 41 if nextBatchByteErr != nil { 42 return nextBatchByteErr 43 } 44 45 nextBatch = string(nextBatchByte) 46 return nil 47 }) 48 if DBerr != nil { 49 err = DBerr 50 return 51 } 52 return 53 }