You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package search
  15. // DocumentMatchPoolTooSmall is a callback function that can be executed
  16. // when the DocumentMatchPool does not have sufficient capacity
  17. // By default we just perform just-in-time allocation, but you could log
  18. // a message, or panic, etc.
  19. type DocumentMatchPoolTooSmall func(p *DocumentMatchPool) *DocumentMatch
  20. // DocumentMatchPool manages use/re-use of DocumentMatch instances
  21. // it pre-allocates space from a single large block with the expected
  22. // number of instances. It is not thread-safe as currently all
  23. // aspects of search take place in a single goroutine.
  24. type DocumentMatchPool struct {
  25. avail DocumentMatchCollection
  26. TooSmall DocumentMatchPoolTooSmall
  27. }
  28. func defaultDocumentMatchPoolTooSmall(p *DocumentMatchPool) *DocumentMatch {
  29. return &DocumentMatch{}
  30. }
  31. // NewDocumentMatchPool will build a DocumentMatchPool with memory
  32. // pre-allocated to accommodate the requested number of DocumentMatch
  33. // instances
  34. func NewDocumentMatchPool(size, sortsize int) *DocumentMatchPool {
  35. avail := make(DocumentMatchCollection, size)
  36. // pre-allocate the expected number of instances
  37. startBlock := make([]DocumentMatch, size)
  38. startSorts := make([]string, size*sortsize)
  39. // make these initial instances available
  40. i, j := 0, 0
  41. for i < size {
  42. avail[i] = &startBlock[i]
  43. avail[i].Sort = startSorts[j:j]
  44. i += 1
  45. j += sortsize
  46. }
  47. return &DocumentMatchPool{
  48. avail: avail,
  49. TooSmall: defaultDocumentMatchPoolTooSmall,
  50. }
  51. }
  52. // Get returns an available DocumentMatch from the pool
  53. // if the pool was not allocated with sufficient size, an allocation will
  54. // occur to satisfy this request. As a side-effect this will grow the size
  55. // of the pool.
  56. func (p *DocumentMatchPool) Get() *DocumentMatch {
  57. var rv *DocumentMatch
  58. if len(p.avail) > 0 {
  59. rv, p.avail = p.avail[len(p.avail)-1], p.avail[:len(p.avail)-1]
  60. } else {
  61. rv = p.TooSmall(p)
  62. }
  63. return rv
  64. }
  65. // Put returns a DocumentMatch to the pool
  66. func (p *DocumentMatchPool) Put(d *DocumentMatch) {
  67. if d == nil {
  68. return
  69. }
  70. // reset DocumentMatch before returning it to available pool
  71. d.Reset()
  72. p.avail = append(p.avail, d)
  73. }