本站源代码
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.

92 lines
2.8KB

  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. import (
  16. "reflect"
  17. )
  18. var reflectStaticSizeDocumentMatchPool int
  19. func init() {
  20. var dmp DocumentMatchPool
  21. reflectStaticSizeDocumentMatchPool = int(reflect.TypeOf(dmp).Size())
  22. }
  23. // DocumentMatchPoolTooSmall is a callback function that can be executed
  24. // when the DocumentMatchPool does not have sufficient capacity
  25. // By default we just perform just-in-time allocation, but you could log
  26. // a message, or panic, etc.
  27. type DocumentMatchPoolTooSmall func(p *DocumentMatchPool) *DocumentMatch
  28. // DocumentMatchPool manages use/re-use of DocumentMatch instances
  29. // it pre-allocates space from a single large block with the expected
  30. // number of instances. It is not thread-safe as currently all
  31. // aspects of search take place in a single goroutine.
  32. type DocumentMatchPool struct {
  33. avail DocumentMatchCollection
  34. TooSmall DocumentMatchPoolTooSmall
  35. }
  36. func defaultDocumentMatchPoolTooSmall(p *DocumentMatchPool) *DocumentMatch {
  37. return &DocumentMatch{}
  38. }
  39. // NewDocumentMatchPool will build a DocumentMatchPool with memory
  40. // pre-allocated to accommodate the requested number of DocumentMatch
  41. // instances
  42. func NewDocumentMatchPool(size, sortsize int) *DocumentMatchPool {
  43. avail := make(DocumentMatchCollection, size)
  44. // pre-allocate the expected number of instances
  45. startBlock := make([]DocumentMatch, size)
  46. startSorts := make([]string, size*sortsize)
  47. // make these initial instances available
  48. i, j := 0, 0
  49. for i < size {
  50. avail[i] = &startBlock[i]
  51. avail[i].Sort = startSorts[j:j]
  52. i += 1
  53. j += sortsize
  54. }
  55. return &DocumentMatchPool{
  56. avail: avail,
  57. TooSmall: defaultDocumentMatchPoolTooSmall,
  58. }
  59. }
  60. // Get returns an available DocumentMatch from the pool
  61. // if the pool was not allocated with sufficient size, an allocation will
  62. // occur to satisfy this request. As a side-effect this will grow the size
  63. // of the pool.
  64. func (p *DocumentMatchPool) Get() *DocumentMatch {
  65. var rv *DocumentMatch
  66. if len(p.avail) > 0 {
  67. rv, p.avail = p.avail[len(p.avail)-1], p.avail[:len(p.avail)-1]
  68. } else {
  69. rv = p.TooSmall(p)
  70. }
  71. return rv
  72. }
  73. // Put returns a DocumentMatch to the pool
  74. func (p *DocumentMatchPool) Put(d *DocumentMatch) {
  75. if d == nil {
  76. return
  77. }
  78. // reset DocumentMatch before returning it to available pool
  79. d.Reset()
  80. p.avail = append(p.avail, d)
  81. }
上海开阖软件有限公司 沪ICP备12045867号-1