2015-08-25 20:52:42 +02:00
|
|
|
// Copyright (c) 2015 Couchbase, Inc.
|
2014-09-24 14:13:14 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
// except in compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the
|
|
|
|
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
// either express or implied. See the License for the specific language governing permissions
|
|
|
|
// and limitations under the License.
|
|
|
|
|
2015-08-25 20:52:42 +02:00
|
|
|
package index
|
2014-09-24 14:13:14 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2015-08-25 20:52:42 +02:00
|
|
|
type FieldCache struct {
|
2014-09-24 14:13:14 +02:00
|
|
|
fieldIndexes map[string]uint16
|
|
|
|
lastFieldIndex int
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2015-08-25 20:52:42 +02:00
|
|
|
func NewFieldCache() *FieldCache {
|
|
|
|
return &FieldCache{
|
|
|
|
fieldIndexes: make(map[string]uint16),
|
|
|
|
lastFieldIndex: -1,
|
2014-09-24 14:13:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 20:52:42 +02:00
|
|
|
func (f *FieldCache) AddExisting(field string, index uint16) {
|
2014-09-24 14:13:14 +02:00
|
|
|
f.mutex.Lock()
|
|
|
|
defer f.mutex.Unlock()
|
|
|
|
f.fieldIndexes[field] = index
|
|
|
|
if int(index) > f.lastFieldIndex {
|
|
|
|
f.lastFieldIndex = int(index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 20:52:42 +02:00
|
|
|
// FieldNamed returns the index of the field, and whether or not it existed
|
|
|
|
// before this call. if createIfMissing is true, and new field index is assigned
|
|
|
|
// but the second return value will still be false
|
|
|
|
func (f *FieldCache) FieldNamed(field string, createIfMissing bool) (uint16, bool) {
|
2014-09-24 14:13:14 +02:00
|
|
|
f.mutex.RLock()
|
|
|
|
if index, ok := f.fieldIndexes[field]; ok {
|
2015-08-25 20:52:42 +02:00
|
|
|
f.mutex.RUnlock()
|
2014-09-24 14:13:14 +02:00
|
|
|
return index, true
|
2015-08-25 20:52:42 +02:00
|
|
|
} else if !createIfMissing {
|
|
|
|
f.mutex.RUnlock()
|
|
|
|
return 0, false
|
2014-09-24 14:13:14 +02:00
|
|
|
}
|
2015-08-25 20:52:42 +02:00
|
|
|
// trade read lock for write lock
|
|
|
|
f.mutex.RUnlock()
|
2014-09-24 14:13:14 +02:00
|
|
|
f.mutex.Lock()
|
|
|
|
// assign next field id
|
2015-08-25 20:52:42 +02:00
|
|
|
index := uint16(f.lastFieldIndex + 1)
|
2014-09-24 14:13:14 +02:00
|
|
|
f.fieldIndexes[field] = index
|
|
|
|
f.lastFieldIndex = int(index)
|
2015-08-25 20:52:42 +02:00
|
|
|
f.mutex.Unlock()
|
|
|
|
return index, false
|
2014-09-24 14:13:14 +02:00
|
|
|
}
|
|
|
|
|
2015-08-25 20:52:42 +02:00
|
|
|
func (f *FieldCache) FieldIndexed(index uint16) string {
|
2014-09-24 14:13:14 +02:00
|
|
|
f.mutex.RLock()
|
|
|
|
defer f.mutex.RUnlock()
|
|
|
|
for fieldName, fieldIndex := range f.fieldIndexes {
|
|
|
|
if index == fieldIndex {
|
|
|
|
return fieldName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|