Using checksums to detect modified stats in the iTunes Library
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/deluan/gosonic/scanner"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
)
|
||||
|
||||
var (
|
||||
keyName = []byte("checksums")
|
||||
)
|
||||
|
||||
type checkSumRepository struct {
|
||||
data map[string]string
|
||||
}
|
||||
|
||||
func NewCheckSumRepository() scanner.CheckSumRepository {
|
||||
r := &checkSumRepository{}
|
||||
r.loadData()
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *checkSumRepository) loadData() {
|
||||
r.data = make(map[string]string)
|
||||
|
||||
pairs, err := Db().HGetAll(keyName)
|
||||
if err != nil {
|
||||
beego.Error("Error loading CheckSums:", err)
|
||||
}
|
||||
for _, p := range pairs {
|
||||
r.data[string(p.Field)] = string(p.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *checkSumRepository) Put(id, sum string) error {
|
||||
if id == "" {
|
||||
return errors.New("Id is required")
|
||||
}
|
||||
_, err := Db().HSet(keyName, []byte(id), []byte(sum))
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *checkSumRepository) Get(id string) (string, error) {
|
||||
return r.data[id], nil
|
||||
}
|
||||
|
||||
func (r *checkSumRepository) SetData(newSums map[string]string) error {
|
||||
Db().HClear(keyName)
|
||||
pairs := make([]ledis.FVPair, len(newSums))
|
||||
r.data = make(map[string]string)
|
||||
i := 0
|
||||
for id, sum := range newSums {
|
||||
p := ledis.FVPair{Field: []byte(id), Value: []byte(sum)}
|
||||
pairs[i] = p
|
||||
r.data[id] = sum
|
||||
i++
|
||||
}
|
||||
return Db().HMset(keyName, pairs...)
|
||||
}
|
||||
Reference in New Issue
Block a user