0
0
Fork 0

document Reset behavior as its non-obvious

This commit is contained in:
Marty Schoch 2016-08-03 17:16:15 -04:00
parent d7405a4d79
commit b857769217
2 changed files with 8 additions and 0 deletions

View File

@ -122,9 +122,13 @@ type TermFieldDoc struct {
Vectors []*TermFieldVector
}
// Reset allows an already allocated TermFieldDoc to be reused
func (tfd *TermFieldDoc) Reset() *TermFieldDoc {
// remember the []byte used for the ID
id := tfd.ID
// idiom to copy over from empty TermFieldDoc (0 allocations)
*tfd = TermFieldDoc{}
// reuse the []byte already allocated (and reset len to 0)
tfd.ID = id[:0]
return tfd
}

View File

@ -88,9 +88,13 @@ func (dm *DocumentMatch) AddFieldValue(name string, value interface{}) {
dm.Fields[name] = valSlice
}
// Reset allows an already allocated DocumentMatch to be reused
func (dm *DocumentMatch) Reset() *DocumentMatch {
// remember the []byte used for the IndexInternalID
indexInternalId := dm.IndexInternalID
// idiom to copy over from empty DocumentMatch (0 allocations)
*dm = DocumentMatch{}
// reuse the []byte already allocated (and reset len to 0)
dm.IndexInternalID = indexInternalId[:0]
return dm
}