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

parser_cache.go 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package parser
  18. import (
  19. "fmt"
  20. "sync"
  21. )
  22. var (
  23. once sync.Once
  24. cache *UndoLogParserCache
  25. )
  26. // The type Undo log parser factory.
  27. type UndoLogParserCache struct {
  28. // serializerName --> UndoLogParser
  29. serializerNameToParser map[string]UndoLogParser
  30. }
  31. func initCache() {
  32. cache = &UndoLogParserCache{
  33. serializerNameToParser: make(map[string]UndoLogParser, 0),
  34. }
  35. cache.store(&JsonParser{})
  36. cache.store(&ProtobufParser{})
  37. }
  38. func GetCache() *UndoLogParserCache {
  39. once.Do(initCache)
  40. return cache
  41. }
  42. // Gets default UndoLogParser instance.
  43. // return the instance
  44. func (ulpc *UndoLogParserCache) GetDefault() (UndoLogParser, error) {
  45. return ulpc.Load(DefaultSerializer)
  46. }
  47. // Gets UndoLogParser by name
  48. // param name parser name
  49. // return the UndoLogParser
  50. func (ulpc *UndoLogParserCache) Load(name string) (UndoLogParser, error) {
  51. if parser, ok := ulpc.serializerNameToParser[name]; ok && parser != nil {
  52. return parser, nil
  53. }
  54. return nil, fmt.Errorf("undo log parser type %v not found", name)
  55. }
  56. func (ulpc *UndoLogParserCache) store(parser UndoLogParser) {
  57. ulpc.serializerNameToParser[parser.GetName()] = parser
  58. }