func NewWriter()

in banyand/tsdb/index/writer.go [80:122]


func NewWriter(ctx context.Context, options WriterOptions) *Writer {
	w := new(Writer)
	parentLogger := ctx.Value(logger.ContextKey)
	if parentLogger != nil {
		if pl, ok := parentLogger.(*logger.Logger); ok {
			w.l = pl.Named("index-writer")
		}
	}
	w.shardNum = options.ShardNum
	w.db = options.DB
	w.enableGlobalIndex = options.EnableGlobalIndex
	w.invertRuleIndex = make(map[byte][]*partition.IndexRuleLocator, 4)
	for _, ruleIndex := range partition.ParseIndexRuleLocators(options.Families, options.IndexRules) {
		rule := ruleIndex.Rule
		var key byte
		switch rule.GetLocation() {
		case databasev1.IndexRule_LOCATION_SERIES:
			key |= local
		case databasev1.IndexRule_LOCATION_GLOBAL:
			if !w.enableGlobalIndex {
				w.l.Warn().RawJSON("index-rule", logger.Proto(ruleIndex.Rule)).Msg("global index is disabled")
				continue
			}
			key |= global
		case databasev1.IndexRule_LOCATION_UNSPECIFIED:
			w.l.Warn().RawJSON("index-rule", logger.Proto(ruleIndex.Rule)).Msg("invalid:unspecified location")
			continue
		}
		switch rule.Type {
		case databasev1.IndexRule_TYPE_INVERTED:
			key |= inverted
		case databasev1.IndexRule_TYPE_TREE:
			key |= tree
		case databasev1.IndexRule_TYPE_UNSPECIFIED:
			w.l.Warn().RawJSON("index-rule", logger.Proto(ruleIndex.Rule)).Msg("invalid:unspecified type")
			continue
		}
		rules := w.invertRuleIndex[key]
		rules = append(rules, ruleIndex)
		w.invertRuleIndex[key] = rules
	}
	return w
}