pkg/index: Rename audiotag to mediatag.

Also fix up keys and values and add tests.

Change-Id: I7e6c5c4315705442e3517456f2ba16419af49f2f
This commit is contained in:
Daniel Erat 2014-01-20 21:05:45 -08:00
parent 5b03c3f8fb
commit 704d3c6bfc
5 changed files with 45 additions and 26 deletions

View File

@ -186,7 +186,7 @@ var corpusMergeFunc = map[string]func(c *Corpus, k, v []byte) error{
"exifgps": (*Corpus).mergeEXIFGPSRow,
"exiftag": nil, // not using any for now
"signerattrvalue": nil, // ignoring for now
"audiotag": (*Corpus).mergeAudioTag,
"mediatag": (*Corpus).mergeMediaTag,
}
func memstats() *runtime.MemStats {
@ -207,7 +207,7 @@ var slurpPrefixes = []string{
"imagesize|",
"wholetofile|",
"exifgps|",
"audiotag|",
"mediatag|",
}
// Key types (without trailing punctuation) that we slurp to memory at start.
@ -490,22 +490,22 @@ func (c *Corpus) mergeWholeToFileRow(k, v []byte) error {
return nil
}
// "audiotag|album|some+album+name|sha1-2b219be9d9691b4f8090e7ee2690098097f59566" = "1"
func (c *Corpus) mergeAudioTag(k, v []byte) error {
// "mediatag|sha1-2b219be9d9691b4f8090e7ee2690098097f59566|album" = "Some+Album+Name"
func (c *Corpus) mergeMediaTag(k, v []byte) error {
f := strings.Split(string(k), "|")
if len(f) != 4 {
if len(f) != 3 {
return fmt.Errorf("unexpected key %q", k)
}
wholeRef, ok := blob.Parse(f[3])
wholeRef, ok := blob.Parse(f[1])
if !ok {
return fmt.Errorf("unexpected key %q", k)
return fmt.Errorf("failed to parse wholeref from key %q", k)
}
tm, ok := c.mediaTag[wholeRef]
if !ok {
tm = make(map[string]string)
c.mediaTag[wholeRef] = tm
}
tm[c.str(f[1])] = c.str(urld(f[2]))
tm[c.str(f[2])] = c.str(urld(string(v)))
return nil
}

BIN
pkg/index/indextest/testdata/0s.mp3 vendored Normal file

Binary file not shown.

View File

@ -24,6 +24,7 @@ import (
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"reflect"
@ -319,31 +320,31 @@ func Index(t *testing.T, initIdx func() *index.Index) {
}
}
// Upload a basic image.
var jpegFileRef blob.Ref
var exifFileRef blob.Ref
// Upload some files.
var jpegFileRef, exifFileRef, mediaFileRef, mediaWholeRef blob.Ref
{
camliRootPath, err := osutil.GoPackagePath("camlistore.org")
if err != nil {
t.Fatal("Package camlistore.org no found in $GOPATH or $GOPATH not defined")
}
uploadFile := func(file string, modTime time.Time) blob.Ref {
uploadFile := func(file string, modTime time.Time) (fileRef, wholeRef blob.Ref) {
fileName := filepath.Join(camliRootPath, "pkg", "index", "indextest", "testdata", file)
contents, err := ioutil.ReadFile(fileName)
if err != nil {
t.Fatal(err)
}
br, _ := id.UploadFile(file, string(contents), modTime)
return br
fileRef, wholeRef = id.UploadFile(file, string(contents), modTime)
return
}
jpegFileRef = uploadFile("dude.jpg", noTime)
exifFileRef = uploadFile("dude-exif.jpg", time.Unix(1361248796, 0))
jpegFileRef, _ = uploadFile("dude.jpg", noTime)
exifFileRef, _ = uploadFile("dude-exif.jpg", time.Unix(1361248796, 0))
mediaFileRef, mediaWholeRef = uploadFile("0s.mp3", noTime)
}
// Upload the dir containing the two previous images
// Upload the dir containing the previous files.
imagesDirRef := id.UploadDir(
"testdata",
[]blob.Ref{jpegFileRef, exifFileRef},
[]blob.Ref{jpegFileRef, exifFileRef, mediaFileRef},
time.Now(),
)
@ -394,6 +395,23 @@ func Index(t *testing.T, initIdx func() *index.Index) {
t.Fatalf("edgeback row %q = %q, want %q", key, g, e)
}
mediaTests := []struct {
prop, exp string
}{
{"title", "Zero Seconds"},
{"artist", "Test Artist"},
{"album", "Test Album"},
{"genre", "(20)Alternative"},
{"year", "1992"},
{"track", "1"},
}
for _, tt := range mediaTests {
key = fmt.Sprintf("mediatag|%s|%s", mediaWholeRef.String(), tt.prop)
if g, _ := url.QueryUnescape(id.Get(key)); g != tt.exp {
t.Errorf("0s.mp3 key %q = %q; want %q", key, g, tt.exp)
}
}
// PermanodeOfSignerAttrValue
{
gotPN, err := id.Index.PermanodeOfSignerAttrValue(id.SignerBlobRef, "camliRoot", "rootval")
@ -554,7 +572,7 @@ func Index(t *testing.T, initIdx func() *index.Index) {
for r := range ch {
got = append(got, r)
}
want := []blob.Ref{jpegFileRef, exifFileRef}
want := []blob.Ref{jpegFileRef, exifFileRef, mediaFileRef}
if len(got) != len(want) {
t.Errorf("GetDirMembers results differ.\n got: %v\nwant: %v",
got, want)

View File

@ -317,16 +317,17 @@ var (
},
}
// Audio attributes (e.g., ID3 tags). Uses generic terms like
// Media attributes (e.g. ID3 tags). Uses generic terms like
// "artist", "title", "album", etc.
keyAudioTag = &keyType{
"audiotag",
keyMediaTag = &keyType{
"mediatag",
[]part{
{"tag", typeStr},
{"value", typeStr},
{"wholeRef", typeBlobRef}, // wholeRef for song
{"tag", typeStr},
},
[]part{
{"value", typeStr},
},
nil,
}
// EXIF tags

View File

@ -460,7 +460,7 @@ func indexMusic(tag taglib.GenericTag, wholeRef blob.Ref, mm *mutationMap) {
for tag, value := range tags {
if value != "" {
mm.Set(keyAudioTag.Key(tag, strings.ToLower(value), wholeRef), "1")
mm.Set(keyMediaTag.Key(wholeRef, tag), keyMediaTag.Val(value))
}
}
}