本文整理匯總了Golang中camlistore/org/pkg/blob.Ref類的典型用法代碼示例。如果您正苦於以下問題:Golang Ref類的具體用法?Golang Ref怎麽用?Golang Ref使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Ref類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Fetch
func (s *Storage) Fetch(br blob.Ref) (rc io.ReadCloser, size uint32, err error) {
if s.cache != nil {
if rc, size, err = s.cache.Fetch(br); err == nil {
return
}
}
// TODO(mpl): use context from caller, once one is available (issue 733)
r, err := s.client.Bucket(s.bucket).Object(s.dirPrefix + br.String()).NewReader(context.TODO())
if err == storage.ErrObjectNotExist {
return nil, 0, os.ErrNotExist
}
if err != nil {
return nil, 0, err
}
if r.Size() >= 1<<32 {
r.Close()
return nil, 0, errors.New("object larger than a uint32")
}
size = uint32(r.Size())
if size > constants.MaxBlobSize {
r.Close()
return nil, size, errors.New("object too big")
}
return r, size, nil
}
示例2: populateMutationMap
// populateMutationMap populates keys & values that will be committed
// into the returned map.
//
// the blobref can be trusted at this point (it's been fully consumed
// and verified to match), and the sniffer has been populated.
func (ix *Index) populateMutationMap(fetcher *missTrackFetcher, br blob.Ref, sniffer *BlobSniffer) (*mutationMap, error) {
// TODO(mpl): shouldn't we remove these two from the map (so they don't get committed) when
// e.g in populateClaim we detect a bogus claim (which does not yield an error)?
mm := &mutationMap{
kv: map[string]string{
"have:" + br.String(): fmt.Sprintf("%d", sniffer.Size()),
"meta:" + br.String(): fmt.Sprintf("%d|%s", sniffer.Size(), sniffer.MIMEType()),
},
}
if blob, ok := sniffer.SchemaBlob(); ok {
switch blob.Type() {
case "claim":
if err := ix.populateClaim(blob, mm); err != nil {
return nil, err
}
case "file":
if err := ix.populateFile(fetcher, blob, mm); err != nil {
return nil, err
}
case "directory":
if err := ix.populateDir(fetcher, blob, mm); err != nil {
return nil, err
}
}
}
return mm, nil
}
示例3: scaledCached
// ScaledCached reads the scaled version of the image in file,
// if it is in cache and writes it to buf.
//
// On successful read and population of buf, the returned format is non-empty.
// Almost all errors are not interesting. Real errors will be logged.
func (ih *ImageHandler) scaledCached(buf *bytes.Buffer, file blob.Ref) (format string) {
key := cacheKey(file.String(), ih.MaxWidth, ih.MaxHeight)
br, err := ih.thumbMeta.Get(key)
if err == errCacheMiss {
return
}
if err != nil {
log.Printf("Warning: thumbnail cachekey(%q)->meta lookup error: %v", key, err)
return
}
fr, err := ih.cached(br)
if err != nil {
return
}
defer fr.Close()
_, err = io.Copy(buf, fr)
if err != nil {
return
}
mime := magic.MIMEType(buf.Bytes())
if format = strings.TrimPrefix(mime, "image/"); format == mime {
log.Printf("Warning: unescaped MIME type %q of %v file for thumbnail %q", mime, br, key)
return
}
return format
}
示例4: NewFileReader
// NewFileReader returns a new FileReader reading the contents of fileBlobRef,
// fetching blobs from fetcher. The fileBlobRef must be of a "bytes" or "file"
// schema blob.
//
// The caller should call Close on the FileReader when done reading.
func NewFileReader(fetcher blob.Fetcher, fileBlobRef blob.Ref) (*FileReader, error) {
// TODO(bradfitz): rename this into bytes reader? but for now it's still
// named FileReader, but can also read a "bytes" schema.
if !fileBlobRef.Valid() {
return nil, errors.New("schema/filereader: NewFileReader blobref invalid")
}
rc, _, err := fetcher.Fetch(fileBlobRef)
if err != nil {
return nil, fmt.Errorf("schema/filereader: fetching file schema blob: %v", err)
}
defer rc.Close()
ss, err := parseSuperset(rc)
if err != nil {
return nil, fmt.Errorf("schema/filereader: decoding file schema blob: %v", err)
}
ss.BlobRef = fileBlobRef
if ss.Type != "file" && ss.Type != "bytes" {
return nil, fmt.Errorf("schema/filereader: expected \"file\" or \"bytes\" schema blob, got %q", ss.Type)
}
fr, err := ss.NewFileReader(fetcher)
if err != nil {
return nil, fmt.Errorf("schema/filereader: creating FileReader for %s: %v", fileBlobRef, err)
}
return fr, nil
}
示例5: Fetch
func (s *Storage) Fetch(ref blob.Ref) (file io.ReadCloser, size uint32, err error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.lru != nil {
s.lru.Get(ref.String()) // force to head
}
if s.m == nil {
err = os.ErrNotExist
return
}
b, ok := s.m[ref]
if !ok {
err = os.ErrNotExist
return
}
size = uint32(len(b))
atomic.AddInt64(&s.blobsFetched, 1)
atomic.AddInt64(&s.bytesFetched, int64(len(b)))
return struct {
*io.SectionReader
io.Closer
}{
io.NewSectionReader(bytes.NewReader(b), 0, int64(size)),
types.NopCloser,
}, size, nil
}
示例6: NotifyBlobReceived
func (h *SimpleBlobHub) NotifyBlobReceived(br blob.Ref) {
h.l.Lock()
defer h.l.Unlock()
// Callback channels to notify, nil until non-empty
var notify []chan blob.Ref
// Append global listeners
for ch, _ := range h.listeners {
notify = append(notify, ch)
}
// Append blob-specific listeners
if h.blobListeners != nil {
blobstr := br.String()
if set, ok := h.blobListeners[blobstr]; ok {
for ch, _ := range set {
notify = append(notify, ch)
}
}
}
// Run in a separate Goroutine so NotifyBlobReceived doesn't block
// callers if callbacks are slow.
go func() {
for _, ch := range notify {
ch <- br
}
}()
}
示例7: scaledCached
// ScaledCached reads the scaled version of the image in file,
// if it is in cache. On success, the image format is returned.
func (ih *ImageHandler) scaledCached(buf *bytes.Buffer, file blob.Ref) (format string, err error) {
name := cacheKey(file.String(), ih.MaxWidth, ih.MaxHeight)
br, err := ih.sc.Get(name)
if err != nil {
return format, fmt.Errorf("%v: %v", name, err)
}
fr, err := ih.cached(br)
if err != nil {
return format, fmt.Errorf("No cache hit for %v: %v", br, err)
}
defer fr.Close()
_, err = io.Copy(buf, fr)
if err != nil {
return format, fmt.Errorf("error reading cached thumbnail %v: %v", name, err)
}
mime := magic.MIMEType(buf.Bytes())
if mime == "" {
return format, fmt.Errorf("error with cached thumbnail %v: unknown mime type", name)
}
pieces := strings.Split(mime, "/")
if len(pieces) < 2 {
return format, fmt.Errorf("error with cached thumbnail %v: bogus mime type", name)
}
if pieces[0] != "image" {
return format, fmt.Errorf("error with cached thumbnail %v: not an image", name)
}
return pieces[1], nil
}
示例8: GetImageInfo
func (x *Index) GetImageInfo(fileRef blob.Ref) (*search.ImageInfo, error) {
// it might be that the key does not exist because image.DecodeConfig failed earlier
// (because of unsupported JPEG features like progressive mode).
key := keyImageSize.Key(fileRef.String())
dim, err := x.s.Get(key)
if err == ErrNotFound {
err = os.ErrNotExist
}
if err != nil {
return nil, err
}
valPart := strings.Split(dim, "|")
if len(valPart) != 2 {
return nil, fmt.Errorf("index: bogus key %q = %q", key, dim)
}
width, err := strconv.Atoi(valPart[0])
if err != nil {
return nil, fmt.Errorf("index: bogus integer at position 0 in key %q: %q", key, valPart[0])
}
height, err := strconv.Atoi(valPart[1])
if err != nil {
return nil, fmt.Errorf("index: bogus integer at position 1 in key %q: %q", key, valPart[1])
}
imgInfo := &search.ImageInfo{
Width: width,
Height: height,
}
return imgInfo, nil
}
示例9: deepDescribe
func (ph *publishHandler) deepDescribe(br blob.Ref) (*search.DescribeResponse, error) {
res, err := ph.cl.Query(&search.SearchQuery{
Constraint: &search.Constraint{
BlobRefPrefix: br.String(),
CamliType: "permanode",
},
Describe: &search.DescribeRequest{
ThumbnailSize: 1000,
Depth: 1,
Rules: []*search.DescribeRule{
{
Attrs: []string{"camliContent", "camliContentImage", "camliMember", "camliPath:*"},
},
},
},
Limit: -1,
})
if err != nil {
return nil, fmt.Errorf("Could not deep describe %v: %v", br, err)
}
if res == nil || res.Describe == nil {
return nil, fmt.Errorf("no describe result for %v", br)
}
return res.Describe, nil
}
示例10: NoteBlobExists
func (c *FlatHaveCache) NoteBlobExists(br blob.Ref, size int64) {
c.mu.Lock()
defer c.mu.Unlock()
if size < 0 {
panic("negative size")
}
k := br.String()
if c.m[k] == size {
// dup
return
}
c.m[k] = size
if c.af == nil {
var err error
c.af, err = os.OpenFile(c.filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Printf("opening have-cache for append: %v", err)
return
}
}
// TODO: flocking. see leveldb-go.
c.af.Seek(0, os.SEEK_END)
c.af.Write([]byte(fmt.Sprintf("%s %d\n", k, size)))
}
示例11: unmarshalBinary
func (scv *statCacheValue) unmarshalBinary(data []byte) error {
if scv == nil {
return errors.New("Can't unmarshalBinary into a nil stat cache value")
}
if scv.Fingerprint != "" {
return errors.New("Can't unmarshalBinary into a non empty stat cache value")
}
parts := bytes.SplitN(data, pipe, 3)
if len(parts) != 3 {
return fmt.Errorf("Bogus stat cache value; was expecting fingerprint|blobSize|blobRef, got %q", data)
}
fingerprint := string(parts[0])
buf := bytes.NewReader(parts[1])
var size int32
err := binary.Read(buf, binary.BigEndian, &size)
if err != nil {
return fmt.Errorf("Could not decode blob size from stat cache value part %q: %v", parts[1], err)
}
br := new(blob.Ref)
if err := br.UnmarshalBinary(parts[2]); err != nil {
return fmt.Errorf("Could not unmarshalBinary for %q: %v", parts[2], err)
}
scv.Fingerprint = statFingerprint(fingerprint)
scv.Result = client.PutResult{
BlobRef: *br,
Size: uint32(size),
Skipped: true,
}
return nil
}
示例12: Fetch
func (tf *Fetcher) Fetch(ref blob.Ref) (file io.ReadCloser, size uint32, err error) {
if tf.FetchErr != nil {
if err = tf.FetchErr(); err != nil {
return
}
}
tf.mu.RLock()
defer tf.mu.RUnlock()
if tf.m == nil {
err = os.ErrNotExist
return
}
tb, ok := tf.m[ref.String()]
if !ok {
err = os.ErrNotExist
return
}
size = uint32(len(tb.Contents))
return struct {
*io.SectionReader
io.Closer
}{
io.NewSectionReader(strings.NewReader(tb.Contents), 0, int64(size)),
types.NopCloser,
}, size, nil
}
示例13: getMetaRow
// if not found, err == nil.
func (s *storage) getMetaRow(br blob.Ref) (meta, error) {
v, err := s.meta.Get(blobMetaPrefix + br.String())
if err == sorted.ErrNotFound {
return meta{}, nil
}
return parseMetaRow([]byte(v))
}
示例14: blobDirectory
func (ds *DiskStorage) blobDirectory(partition string, b blob.Ref) string {
d := b.Digest()
if len(d) < 6 {
d = d + "______"
}
return filepath.Join(ds.PartitionRoot(partition), b.HashName(), d[0:3], d[3:6])
}
示例15: SetShareTarget
// SetShareTarget sets the target of share claim.
// It panics if bb isn't a "share" claim type.
func (bb *Builder) SetShareTarget(t blob.Ref) *Builder {
if bb.Type() != "claim" || bb.ClaimType() != ShareClaim {
panic("called SetShareTarget on non-share")
}
bb.m["target"] = t.String()
return bb
}