/* Copyright 2011 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package search import ( "bytes" "fmt" "os" "strings" "time" "camli/blobref" ) type Result struct { BlobRef *blobref.BlobRef Signer *blobref.BlobRef // may be nil LastModTime int64 // seconds since epoch } // Results exists mostly for debugging, to provide a String method on // a slice of Result. type Results []*Result func (s Results) String() string { var buf bytes.Buffer fmt.Fprintf(&buf, "[%d search results: ", len(s)) for _, r := range s { fmt.Fprintf(&buf, "{BlobRef: %s, Signer: %s, LastModTime: %d}", r.BlobRef, r.Signer, r.LastModTime) } buf.WriteString("]") return buf.String() } // TODO: move this to schema or something? type Claim struct { BlobRef, Signer, Permanode *blobref.BlobRef Date *time.Time Type string // "set-attribute", "add-attribute", etc // If an attribute modification Attr, Value string } func (c *Claim) String() string { return fmt.Sprintf( "search.Claim{BlobRef: %s, Signer: %s, Permanode: %s, Date: %s, Type: %s, Attr: %s, Value: %s}", c.BlobRef, c.Signer, c.Permanode, c.Date, c.Type, c.Attr, c.Value) } type ClaimList []*Claim func (cl ClaimList) Len() int { return len(cl) } func (cl ClaimList) Less(i, j int) bool { // TODO: memoize Seconds in unexported Claim field return cl[i].Date.Seconds() < cl[j].Date.Seconds() } func (cl ClaimList) Swap(i, j int) { cl[i], cl[j] = cl[j], cl[i] } func (cl ClaimList) String() string { var buf bytes.Buffer fmt.Fprintf(&buf, "[%d claims: ", len(cl)) for _, r := range cl { buf.WriteString(r.String()) } buf.WriteString("]") return buf.String() } type FileInfo struct { Size int64 `json:"size"` FileName string `json:"fileName"` MimeType string `json:"mimeType"` } func (fi *FileInfo) IsImage() bool { return strings.HasPrefix(fi.MimeType, "image/") } type Path struct { Claim, Base, Target *blobref.BlobRef ClaimDate string Suffix string } func (p *Path) String() string { return fmt.Sprintf("Path{Claim: %v, %v; Base: %v + Suffix %q => Target %v}", p.Claim, p.ClaimDate, p.Base, p.Suffix, p.Target) } type PermanodeByAttrRequest struct { Signer *blobref.BlobRef // Attribute to search. currently supported: "tag", "title" // If FuzzyMatch is set, this can be blank to search all // attributes. Attribute string // The attribute value to find exactly (or roughly, if // FuzzyMatch is set) Query string FuzzyMatch bool // by default, an exact match is required MaxResults int // optional max results } type Index interface { // dest must be closed, even when returning an error. // limit is <= 0 for default. smallest possible default is 0 GetRecentPermanodes(dest chan *Result, owner *blobref.BlobRef, limit int) os.Error // SearchPermanodes finds permanodes matching the provided // request and sends unique permanode blobrefs to dest. // In particular, if request.FuzzyMatch is true, a fulltext // search is performed (if supported by the attribute(s)) // instead of an exact match search. // Additionally, if request.Attribute is blank, all attributes // are searched (as fulltext), otherwise the search is // restricted to the named attribute. // // dest is always closed, regardless of the error return value. SearchPermanodesWithAttr(dest chan<- *blobref.BlobRef, request *PermanodeByAttrRequest) os.Error GetOwnerClaims(permaNode, owner *blobref.BlobRef) (ClaimList, os.Error) // os.ENOENT should be returned if the blob isn't known GetBlobMimeType(blob *blobref.BlobRef) (mime string, size int64, err os.Error) // ExistingFileSchemas returns 0 or more blobrefs of "bytes" // (TODO(bradfitz): or file?) schema blobs that represent the // bytes of a file given in bytesRef. The file schema blobs // returned are not guaranteed to reference chunks that still // exist on the blobservers, though. It's purely a hint for // clients to avoid uploads if possible. Before re-using any // returned blobref they should be checked. // // Use case: a user drag & drops a large file onto their // browser to upload. (imagine that "large" means anything // larger than a blobserver's max blob size) JavaScript can // first SHA-1 the large file locally, then send the // wholeFileRef to this call and see if they'd previously // uploaded the same file in the past. If so, the upload // can be avoided if at least one of the returned schemaRefs // can be validated (with a validating HEAD request) to still // all exist on the blob server. ExistingFileSchemas(wholeFileRef *blobref.BlobRef) (schemaRefs []*blobref.BlobRef, err os.Error) // Should return os.ENOENT if not found. GetFileInfo(fileRef *blobref.BlobRef) (*FileInfo, os.Error) // Given an owner key, a camliType 'claim', 'attribute' name, // and specific 'value', find the most recent permanode that has // a corresponding 'set-attribute' claim attached. // Returns os.ENOENT if none is found. // Only attributes white-listed by IsIndexedAttribute are valid. PermanodeOfSignerAttrValue(signer *blobref.BlobRef, attr, val string) (*blobref.BlobRef, os.Error) // PathsOfSignerTarget queries the index about "camliPath:" // URL-dispatch attributes. // // It returns a list of all the path claims that have been signed // by the provided signer and point at the given target. // // This is used when editing a permanode, to figure work up // the name resolution tree backwards ultimately to a // camliRoot permanode (which should know its base URL), and // then the complete URL(s) of a target can be found. PathsOfSignerTarget(signer, target *blobref.BlobRef) ([]*Path, os.Error) // All Path claims for (signer, base, suffix) PathsLookup(signer, base *blobref.BlobRef, suffix string) ([]*Path, os.Error) // Most recent Path claim for (signer, base, suffix) as of // provided time 'at', or most recent if 'at' is nil. PathLookup(signer, base *blobref.BlobRef, suffix string, at *time.Time) (*Path, os.Error) } // TODO(bradfitz): rename this? This is really about signer-attr-value // (PermanodeOfSignerAttrValue), and not about indexed attributes in general. func IsIndexedAttribute(attr string) bool { switch attr { case "camliRoot", "tag", "title": return true } return false } func IsFulltextAttribute(attr string) bool { switch attr { case "tag", "title": return true } return false }