コード例 #1
0
ファイル: posting.go プロジェクト: pietergvw/superfastmatch
func (p *Posting) search(doc *document.Document, results *document.SearchMap) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	stats := &Stats{
		doc:    doc,
		start:  time.Now(),
		length: doc.HashLength(p.hashKey),
	}
	l := NewPostingLine()
	*results = make(document.SearchMap)
	searchFunc := func(i int, hash uint64) {
		pos := hash - p.offset
		if pos >= p.size {
			return
		}
		stats.count++
		if err := p.table.Get(pos, l); err != nil {
			glog.Fatalln(newPostingError("Search Document: Sparsetable Get:", err))
		}
		stats.ops++
		l.FillMap(results, uint32(i))
	}
	doc.ApplyHasher(p.hashKey, searchFunc)
	glog.Infoln("Searched Document: ", stats.String())
	return nil
}
コード例 #2
0
ファイル: posting.go プロジェクト: pietergvw/superfastmatch
func (p *Posting) alter(operation int, doc *document.Document) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	l := NewPostingLine()
	stats := &Stats{
		doc:    doc,
		start:  time.Now(),
		length: doc.HashLength(p.hashKey),
	}
	alterFunc := func(i int, hash uint64) {
		pos := hash - p.offset
		if pos >= p.size {
			return
		}
		stats.count++
		if err := p.table.Get(pos, l); err != nil {
			glog.Fatalln(newPostingError("Alter Document: Sparsetable Get:", err))
		}
		var err error
		switch operation {
		case Add:
			if !l.AddDocumentId(&doc.Id) {
				stats.dupes++
				return
			}
			err = p.table.Set(pos, l, l.Length)
		case Delete:
			if !l.RemoveDocumentId(&doc.Id) {
				stats.dupes++
				return
			}
			buf := make([]byte, l.Length)
			if _, err := l.Read(buf); err != nil && err != io.EOF {
				glog.Fatalln(newPostingError("Alter Document: Buffered Delete:", err))
			}
			err = p.table.Set(pos, bytes.NewReader(buf), l.Length)
		}
		if err != nil {
			if serr, ok := err.(*sparsetable.Error); ok {
				switch {
				case serr.Full:
					stats.saturated++
				case serr.ShortRead:
					glog.Errorf("Short Read for Document: %v Length: %v\n%v", doc.Id.String(), l.Length, l.String(true))
				default:
					glog.Fatalln(newPostingError("Add Document: Sparsetable Set:", err))
				}
			}
		}
		stats.ops++
	}
	doc.ApplyHasher(p.hashKey, alterFunc)
	switch operation {
	case Add:
		glog.V(2).Infoln("Added Document:", stats.String())
		p.documents++
	case Delete:
		glog.V(2).Infoln("Deleted Document:", stats.String())
		p.documents--
	}
	return nil
}