zeroshade commented on code in PR #13924: URL: https://github.com/apache/arrow/pull/13924#discussion_r951559020
########## go/arrow/compute/registry.go: ########## @@ -0,0 +1,194 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +package compute + +import ( + "sync" + + "github.com/apache/arrow/go/v10/arrow/internal/debug" + "golang.org/x/exp/maps" + "golang.org/x/exp/slices" +) + +type FunctionRegistry interface { + CanAddFunction(fn Function, allowOverwrite bool) bool + AddFunction(fn Function, allowOverwrite bool) bool + CanAddAlias(target, source string) bool + AddAlias(target, source string) bool + GetFunction(name string) (Function, bool) + GetFunctionNames() []string + NumFunctions() int + + canAddFuncName(string, bool) bool +} + +var ( + registry FunctionRegistry + once sync.Once +) + +func GetFunctionRegistry() FunctionRegistry { + once.Do(func() { + registry = NewRegistry() + // initialize the others + }) + return registry +} + +func NewRegistry() FunctionRegistry { + return &funcRegistry{ + nameToFunction: make(map[string]Function)} +} + +func NewChildRegistry(parent FunctionRegistry) FunctionRegistry { + return &funcRegistry{ + parent: parent.(*funcRegistry), + nameToFunction: make(map[string]Function)} +} + +type funcRegistry struct { + parent *funcRegistry + + mx sync.RWMutex + nameToFunction map[string]Function +} + +func (reg *funcRegistry) getLocker(add bool) sync.Locker { + if add { + return ®.mx + } + return reg.mx.RLocker() +} + +func (reg *funcRegistry) CanAddFunction(fn Function, allowOverwrite bool) bool { + if reg.parent != nil && !reg.parent.CanAddFunction(fn, allowOverwrite) { + return false + } + + return reg.doAddFunction(fn, allowOverwrite, false) +} + +func (reg *funcRegistry) AddFunction(fn Function, allowOverwrite bool) bool { + if reg.parent != nil && !reg.parent.CanAddFunction(fn, allowOverwrite) { + return false + } + + return reg.doAddFunction(fn, allowOverwrite, true) +} + +func (reg *funcRegistry) CanAddAlias(target, source string) bool { + if reg.parent != nil && !reg.parent.canAddFuncName(target, false) { + return false + } + return reg.doAddAlias(target, source, false) +} + +func (reg *funcRegistry) AddAlias(target, source string) bool { + if reg.parent != nil && !reg.parent.canAddFuncName(target, false) { + return false + } + + return reg.doAddAlias(target, source, true) +} + +func (reg *funcRegistry) GetFunction(name string) (Function, bool) { + reg.mx.RLock() + defer reg.mx.RUnlock() + + if fn, ok := reg.nameToFunction[name]; ok { + return fn, ok + } + + if reg.parent != nil { + return reg.parent.GetFunction(name) + } + + return nil, false +} + +func (reg *funcRegistry) GetFunctionNames() (out []string) { + if reg.parent != nil { + out = reg.parent.GetFunctionNames() + } else { + out = make([]string, 0, len(reg.nameToFunction)) + } + reg.mx.RLock() + defer reg.mx.RUnlock() + + out = append(out, maps.Keys(reg.nameToFunction)...) + slices.Sort(out) + return +} + +func (reg *funcRegistry) NumFunctions() (n int) { + if reg.parent != nil { + n = reg.parent.NumFunctions() + } + return n + len(reg.nameToFunction) +} + +func (reg *funcRegistry) canAddFuncName(name string, allowOverwrite bool) bool { + if reg.parent != nil && !reg.parent.canAddFuncName(name, allowOverwrite) { + return false + } + if !allowOverwrite { + _, ok := reg.nameToFunction[name] Review Comment: good catch! i'll add a lock of `reg.parent.mx` here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
