github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/util/docs.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 util 22 23 import ( 24 "bufio" 25 "encoding/json" 26 "fmt" 27 "os" 28 29 "github.com/m3db/m3/src/m3ninx/doc" 30 ) 31 32 // ReadDocs reads up to n documents from a JSON formatted file at the provided path. 33 // It is useful for getting a set of documents to run tests with. 34 func ReadDocs(path string, n int) ([]doc.Metadata, error) { 35 f, err := os.Open(path) 36 if err != nil { 37 return nil, err 38 } 39 defer f.Close() 40 41 var ( 42 docs []doc.Metadata 43 scanner = bufio.NewScanner(f) 44 ) 45 for scanner.Scan() && len(docs) < n { 46 var fieldsMap map[string]string 47 if err := json.Unmarshal(scanner.Bytes(), &fieldsMap); err != nil { 48 return nil, err 49 } 50 51 // Generate a new random UUID for the document. 52 id, err := NewUUID() 53 if err != nil { 54 return nil, err 55 } 56 57 fields := make([]doc.Field, 0, len(fieldsMap)) 58 for k, v := range fieldsMap { 59 if len(k) == 0 || len(v) == 0 { 60 continue 61 } 62 fields = append(fields, doc.Field{ 63 Name: []byte(k), 64 Value: []byte(v), 65 }) 66 } 67 docs = append(docs, doc.Metadata{ 68 ID: id, 69 Fields: fields, 70 }) 71 } 72 73 if len(docs) != n { 74 return nil, fmt.Errorf("requested %d metrics but found %d", n, len(docs)) 75 } 76 77 return docs, nil 78 } 79 80 // MustReadDocs calls ReadDocs and panics if there is an error. 81 func MustReadDocs(path string, n int) []doc.Metadata { 82 docs, err := ReadDocs(path, n) 83 if err != nil { 84 panic(err) 85 } 86 return docs 87 }