github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/stats/statreceiver.go (about) 1 /* 2 Copyright 2013 The Camlistore AUTHORS 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package stats contains an in-memory StatReceiver that only stores sizes 18 // of received blobs but not their contents. 19 package stats 20 21 import ( 22 "io" 23 "io/ioutil" 24 "sort" 25 "sync" 26 27 "camlistore.org/pkg/blob" 28 ) 29 30 // Receiver is a dummy blobserver.StatReceiver that doesn't 31 // store anything; it just collects statistics. 32 // 33 // TODO: we have another copy of this same type in 34 // camput/files.go. move them to a common place? well, the camput one 35 // is probably going away at some point. 36 type Receiver struct { 37 sync.Mutex // guards Have 38 Have map[blob.Ref]int64 39 } 40 41 func (sr *Receiver) NumBlobs() int { 42 sr.Lock() 43 defer sr.Unlock() 44 return len(sr.Have) 45 } 46 47 // Sizes returns the sorted blob sizes. 48 func (sr *Receiver) Sizes() []int { 49 sr.Lock() 50 defer sr.Unlock() 51 sizes := make([]int, 0, len(sr.Have)) 52 for _, size := range sr.Have { 53 sizes = append(sizes, int(size)) 54 } 55 sort.Ints(sizes) 56 return sizes 57 } 58 59 func (sr *Receiver) SumBlobSize() int64 { 60 sr.Lock() 61 defer sr.Unlock() 62 var sum int64 63 for _, v := range sr.Have { 64 sum += v 65 } 66 return sum 67 } 68 69 func (sr *Receiver) ReceiveBlob(br blob.Ref, source io.Reader) (sb blob.SizedRef, err error) { 70 n, err := io.Copy(ioutil.Discard, source) 71 if err != nil { 72 return 73 } 74 sr.Lock() 75 defer sr.Unlock() 76 if sr.Have == nil { 77 sr.Have = make(map[blob.Ref]int64) 78 } 79 sr.Have[br] = n 80 return blob.SizedRef{br, uint32(n)}, nil 81 } 82 83 func (sr *Receiver) StatBlobs(dest chan<- blob.SizedRef, blobs []blob.Ref) error { 84 sr.Lock() 85 defer sr.Unlock() 86 for _, br := range blobs { 87 if size, ok := sr.Have[br]; ok { 88 dest <- blob.SizedRef{br, uint32(size)} 89 } 90 } 91 return nil 92 }