1//  Copyright (c) 2014 Couchbase, Inc.
2//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
3//  except in compliance with the License. You may obtain a copy of the License at
4//    http://www.apache.org/licenses/LICENSE-2.0
5//  Unless required by applicable law or agreed to in writing, software distributed under the
6//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
7//  either express or implied. See the License for the specific language governing permissions
8//  and limitations under the License.
9
10package expression
11
12import (
13	"sync"
14)
15
16type StringExpressionPool struct {
17	pool *sync.Pool
18	size int
19}
20
21func NewStringExpressionPool(size int) *StringExpressionPool {
22	rv := &StringExpressionPool{
23		pool: &sync.Pool{
24			New: func() interface{} {
25				return make(map[string]Expression, size)
26			},
27		},
28		size: size,
29	}
30
31	return rv
32}
33
34func (this *StringExpressionPool) Get() map[string]Expression {
35	return this.pool.Get().(map[string]Expression)
36}
37
38func (this *StringExpressionPool) Put(s map[string]Expression) {
39	if s == nil || len(s) > this.size {
40		return
41	}
42
43	for k, _ := range s {
44		s[k] = nil
45		delete(s, k)
46	}
47
48	this.pool.Put(s)
49}
50