github.com/m3db/m3@v1.5.0/src/m3ninx/persist/types.go (about) 1 // Copyright (c) 2018 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 persist 22 23 import ( 24 "fmt" 25 "io" 26 "regexp" 27 28 "github.com/m3db/m3/src/m3ninx/index/segment" 29 "github.com/m3db/m3/src/x/mmap" 30 ) 31 32 var ( 33 // TypeRegex allows what can be used for a IndexSegmentType or 34 // IndexSegmentFileType, explicitly cannot use "-" as that can be 35 // then used as a separator elsewhere, also it ensures callers must 36 // use lower cased strings. 37 TypeRegex = regexp.MustCompile("^[a-z_]+$") 38 ) 39 40 // IndexFileSetWriter is an index file set writer, it can write out many 41 // segments. 42 type IndexFileSetWriter interface { 43 // WriteSegmentFileSet writes a index segment file set. 44 WriteSegmentFileSet(segmentFileSet IndexSegmentFileSetWriter) error 45 } 46 47 // IndexSegmentFileSetWriter is an index segment file set writer. 48 type IndexSegmentFileSetWriter interface { 49 SegmentType() IndexSegmentType 50 MajorVersion() int 51 MinorVersion() int 52 SegmentMetadata() []byte 53 Files() []IndexSegmentFileType 54 WriteFile(fileType IndexSegmentFileType, writer io.Writer) error 55 } 56 57 // MutableSegmentFileSetWriter is a new IndexSegmentFileSetWriter for writing 58 // out Mutable Segments. 59 type MutableSegmentFileSetWriter interface { 60 IndexSegmentFileSetWriter 61 62 // Reset resets the writer to write the provided mutable segment. 63 Reset(segment.Builder) error 64 } 65 66 // IndexFileSetReader is an index file set reader, it can read many segments. 67 type IndexFileSetReader interface { 68 // SegmentFileSets returns the number of segment file sets. 69 SegmentFileSets() int 70 71 // ReadSegmentFileSet returns the next segment file set or an error. 72 // It will return io.EOF error when no more file sets remain. 73 // The IndexSegmentFileSet will only be valid before it's closed, 74 // after that calls to Read or Bytes on it will have unexpected results. 75 ReadSegmentFileSet() (IndexSegmentFileSet, error) 76 77 IndexVolumeType() IndexVolumeType 78 } 79 80 // IndexSegmentFileSet is an index segment file set. 81 type IndexSegmentFileSet interface { 82 SegmentType() IndexSegmentType 83 MajorVersion() int 84 MinorVersion() int 85 SegmentMetadata() []byte 86 Files() []IndexSegmentFile 87 } 88 89 // IndexSegmentFile is a file in an index segment file set. 90 type IndexSegmentFile interface { 91 io.Reader 92 io.Closer 93 94 // SegmentFileType returns the segment file type. 95 SegmentFileType() IndexSegmentFileType 96 97 // Mmap will be valid until the segment file is closed. 98 Mmap() (mmap.Descriptor, error) 99 } 100 101 // IndexVolumeType is the type of an index volume. 102 type IndexVolumeType string 103 104 const ( 105 // DefaultIndexVolumeType is a default IndexVolumeType. 106 // This is the type if not otherwise specified. 107 DefaultIndexVolumeType IndexVolumeType = "default" 108 ) 109 110 // IndexSegmentType is the type of an index file set. 111 type IndexSegmentType string 112 113 const ( 114 // FSTIndexSegmentType is a FST IndexSegmentType. 115 FSTIndexSegmentType IndexSegmentType = "fst" 116 ) 117 118 // IndexSegmentFileType is the type of a file in an index file set. 119 type IndexSegmentFileType string 120 121 const ( 122 // DocumentDataIndexSegmentFileType is a document data segment file. 123 DocumentDataIndexSegmentFileType IndexSegmentFileType = "docdata" 124 125 // DocumentIndexIndexSegmentFileType is a document index segment file. 126 DocumentIndexIndexSegmentFileType IndexSegmentFileType = "docidx" 127 128 // PostingsIndexSegmentFileType is a postings List data index segment file. 129 PostingsIndexSegmentFileType IndexSegmentFileType = "postingsdata" 130 131 // FSTFieldsIndexSegmentFileType is a FST Fields index segment file. 132 FSTFieldsIndexSegmentFileType IndexSegmentFileType = "fstfields" 133 134 // FSTTermsIndexSegmentFileType is a FST Terms index segment file. 135 FSTTermsIndexSegmentFileType IndexSegmentFileType = "fstterms" 136 ) 137 138 var ( 139 indexSegmentTypes = []IndexSegmentType{ 140 FSTIndexSegmentType, 141 } 142 143 indexSegmentFileTypes = []IndexSegmentFileType{ 144 DocumentDataIndexSegmentFileType, 145 DocumentIndexIndexSegmentFileType, 146 PostingsIndexSegmentFileType, 147 FSTFieldsIndexSegmentFileType, 148 FSTTermsIndexSegmentFileType, 149 } 150 ) 151 152 func init() { 153 for _, f := range indexSegmentTypes { 154 if err := f.Validate(); err != nil { 155 panic(err) 156 } 157 } 158 for _, f := range indexSegmentFileTypes { 159 if err := f.Validate(); err != nil { 160 panic(err) 161 } 162 } 163 } 164 165 // Validate validates whether the string value is a valid segment type 166 // and contains only lowercase a-z and underscore characters. 167 func (t IndexSegmentType) Validate() error { 168 s := string(t) 169 if t == "" || !TypeRegex.MatchString(s) { 170 return fmt.Errorf("invalid segment type must match pattern=%s", 171 TypeRegex.String()) 172 } 173 return nil 174 } 175 176 // Validate validates whether the string value is a valid segment file type 177 // and contains only lowercase a-z and underscore characters. 178 func (t IndexSegmentFileType) Validate() error { 179 s := string(t) 180 if t == "" || !TypeRegex.MatchString(s) { 181 return fmt.Errorf("invalid segment file type must match pattern=%s", 182 TypeRegex.String()) 183 } 184 return nil 185 }