search: change BlobMeta to have a CamliType, not a misleading MIMEType.

Change-Id: I1c6e111ef27aafd5a15f9b38746361093a78e9e6
This commit is contained in:
Brad Fitzpatrick 2013-11-16 10:35:18 -08:00
parent 131976f165
commit 1e627ce7c3
2 changed files with 31 additions and 12 deletions

View File

@ -944,9 +944,9 @@ func (x *Index) EnumerateBlobMeta(ch chan<- search.BlobMeta) (err error) {
continue
}
ch <- search.BlobMeta{
Ref: br,
Size: size,
MIMEType: v[pipe+1:],
Ref: br,
Size: size,
CamliType: camliTypeFromMIME(v[pipe+1:]),
}
}
return err
@ -964,3 +964,12 @@ func (x *Index) Close() error {
}
return nil
}
// "application/json; camliType=file" => "file"
// "image/gif" => ""
func camliTypeFromMIME(mime string) string {
if v := strings.TrimPrefix(mime, "application/json; camliType="); v != mime {
return v
}
return ""
}

View File

@ -45,9 +45,9 @@ const (
// TODO: extend/merge/delete this type? probably dups in this package.
type BlobMeta struct {
Ref blob.Ref
Size int
MIMEType string
Ref blob.Ref
Size int
CamliType string // or empty if unknown
}
type SearchQuery struct {
@ -252,9 +252,19 @@ type search struct {
matches map[blob.Ref]bool
}
// "application/json; camliType=file" => "file"
// "image/gif" => ""
func camliTypeFromMIME(mime string) string {
if v := strings.TrimPrefix(mime, camliTypeMIME); v != mime {
return v
}
return ""
}
func (s *search) blobMeta(br blob.Ref) (BlobMeta, error) {
mime, size, err := s.h.index.GetBlobMIMEType(br)
return BlobMeta{Ref: br, Size: int(size), MIMEType: mime}, err
camliType := camliTypeFromMIME(mime)
return BlobMeta{Ref: br, Size: int(size), CamliType: camliType}, err
}
// optimizePlan returns an optimized version of c which will hopefully
@ -314,7 +324,7 @@ func alwaysMatch(*search, blob.Ref, BlobMeta) (bool, error) {
}
func anyCamliType(s *search, br blob.Ref, bm BlobMeta) (bool, error) {
return strings.HasPrefix(bm.MIMEType, camliTypeMIME), nil
return bm.CamliType != "", nil
}
func (c *Constraint) blobMatches(s *search, br blob.Ref, blobMeta BlobMeta) (bool, error) {
@ -330,7 +340,7 @@ func (c *Constraint) blobMatches(s *search, br blob.Ref, blobMeta BlobMeta) (boo
}
if c.CamliType != "" {
addCond(func(s *search, br blob.Ref, bm BlobMeta) (bool, error) {
return strings.TrimPrefix(bm.MIMEType, camliTypeMIME) == c.CamliType, nil
return bm.CamliType == c.CamliType, nil
})
}
if c.AnyCamliType {
@ -428,7 +438,7 @@ func (c *LogicalConstraint) blobMatches(s *search, br blob.Ref, bm BlobMeta) (bo
}
func (c *PermanodeConstraint) blobMatches(s *search, br blob.Ref, bm BlobMeta) (bool, error) {
if bm.MIMEType != "application/json; camliType=permanode" {
if bm.CamliType != "permanode" {
return false, nil
}
dr, err := s.h.Describe(&DescribeRequest{
@ -508,7 +518,7 @@ func (c *PermanodeConstraint) permanodeMatchesAttr(s *search, dp *DescribedPerma
}
func (c *FileConstraint) blobMatches(s *search, br blob.Ref, bm BlobMeta) (bool, error) {
if bm.MIMEType != "application/json; camliType=file" {
if bm.CamliType != "file" {
return false, nil
}
fi, err := s.h.index.GetFileInfo(br)
@ -569,7 +579,7 @@ func (c *TimeConstraint) timeMatches(t time.Time) bool {
}
func (c *DirConstraint) blobMatches(s *search, br blob.Ref, bm BlobMeta) (bool, error) {
if bm.MIMEType != "application/json; camliType=directory" {
if bm.CamliType != "directory" {
return false, nil
}