github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/namespace/map.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package namespace 22 23 import ( 24 "errors" 25 "fmt" 26 27 xerrors "github.com/m3db/m3/src/x/errors" 28 "github.com/m3db/m3/src/x/ident" 29 ) 30 31 var ( 32 errEmptyMetadatas = errors.New("no namespace metadata provided") 33 ) 34 35 type nsMap struct { 36 namespaces *metadataMap 37 ids []ident.ID 38 metadatas []Metadata 39 } 40 41 // NewMap returns a new registry containing provided metadatas, 42 // providing a consistent order. 43 func NewMap(metadatas []Metadata) (Map, error) { 44 if len(metadatas) == 0 { 45 return nil, errEmptyMetadatas 46 } 47 48 var ( 49 ns = newMetadataMap(metadataMapOptions{}) 50 ids = make([]ident.ID, 0, len(metadatas)) 51 nsMetadatas = make([]Metadata, 0, len(metadatas)) 52 multiErr xerrors.MultiError 53 ) 54 for _, m := range metadatas { 55 id := m.ID() 56 ids = append(ids, id) 57 nsMetadatas = append(nsMetadatas, m) 58 59 if _, ok := ns.Get(id); ok { 60 multiErr = multiErr.Add(fmt.Errorf( 61 "namespace ids must be unique, duplicate found: %v", id.String())) 62 } 63 ns.Set(id, m) 64 } 65 66 if err := multiErr.FinalError(); err != nil { 67 return nil, err 68 } 69 70 return &nsMap{ 71 namespaces: ns, 72 ids: ids, 73 metadatas: nsMetadatas, 74 }, nil 75 } 76 77 func (r *nsMap) Get(namespace ident.ID) (Metadata, error) { 78 metadata, ok := r.namespaces.Get(namespace) 79 if !ok { 80 return nil, fmt.Errorf("unable to find namespace (%v) in registry", namespace.String()) 81 } 82 return metadata, nil 83 } 84 85 func (r *nsMap) IDs() []ident.ID { 86 return r.ids 87 } 88 89 func (r *nsMap) Metadatas() []Metadata { 90 return r.metadatas 91 } 92 93 func (r *nsMap) Equal(value Map) bool { 94 // short circuit ptr equals 95 if value == r { 96 return true 97 } 98 99 ourMds := r.Metadatas() 100 theirMds := value.Metadatas() 101 if len(ourMds) != len(theirMds) { 102 return false 103 } 104 105 // O(n**2) test, not a big deal because this is only 3-5 elements 106 for _, om := range ourMds { 107 found := false 108 for _, tm := range theirMds { 109 if om.Equal(tm) { 110 found = true 111 break 112 } 113 } 114 if !found { 115 return false 116 } 117 } 118 119 return true 120 }