github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/fst/fst_writer.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 fst 22 23 import ( 24 "errors" 25 "io" 26 27 "github.com/m3dbx/vellum" 28 ) 29 30 var ( 31 errFSTWriterBuildUnset = errors.New("fst writer builer has not been Reset() before use") 32 33 // NB(r): The registry cache used by vellum is: table size * mru size * cell size 34 // where cell size = 16 bytes (since its an addr and a ptr) 35 // basically MRU size is the size of each bucket for each combination of 36 // a builder node 37 defaultVellumBuilderOpts = vellum.BuilderOpts{ 38 Encoder: 1, 39 RegistryTableSize: 10000, // 10k 40 RegistryMRUSize: 2, // 4 41 UnfinishedNodesStackSize: 4096, 42 BuilderNodePoolingConfig: vellum.BuilderNodePoolingConfig{ 43 MaxSize: 2 << 16, // ~130k 44 MaxTransitionSize: 2 << 7, // 256 45 }, 46 } 47 ) 48 49 // fstWriter is a writer to help construct an FST. 50 type fstWriter struct { 51 bytesWritten uint64 52 writer io.Writer 53 builderOpts *vellum.BuilderOpts 54 builder *vellum.Builder 55 } 56 57 func newFSTWriter(opts WriterOptions) *fstWriter { 58 builderOpts := new(vellum.BuilderOpts) 59 *builderOpts = defaultVellumBuilderOpts 60 if opts.DisableRegistry { 61 builderOpts.RegistryTableSize = 0 62 builderOpts.RegistryMRUSize = 0 63 } 64 return &fstWriter{builderOpts: builderOpts} 65 } 66 67 func (f *fstWriter) Write(p []byte) (int, error) { 68 if f.writer == nil { 69 return 0, errFSTWriterBuildUnset 70 } 71 n, err := f.writer.Write(p) 72 if err != nil { 73 return 0, err 74 } 75 f.bytesWritten += uint64(n) 76 return n, nil 77 } 78 79 func (f *fstWriter) Reset(w io.Writer) error { 80 f.bytesWritten = 0 81 f.writer = w 82 if f.builder == nil { 83 builder, err := vellum.New(f, f.builderOpts) 84 if err != nil { 85 return err 86 } 87 f.builder = builder 88 return nil 89 } 90 return f.builder.Reset(f) 91 } 92 93 func (f *fstWriter) Add(b []byte, v uint64) error { 94 if f.builder == nil { 95 return errFSTWriterBuildUnset 96 } 97 return f.builder.Insert(b, v) 98 } 99 100 func (f *fstWriter) Close() (uint64, error) { 101 if f.builder == nil { 102 return 0, errFSTWriterBuildUnset 103 } 104 err := f.builder.Close() 105 if err != nil { 106 return 0, nil 107 } 108 return f.bytesWritten, nil 109 }