github.com/m3db/m3@v1.5.0/src/dbnode/ts/writes/types.go (about)

     1  // Copyright (c) 2020 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 writes
    22  
    23  import (
    24  	"github.com/m3db/m3/src/dbnode/storage/index"
    25  	"github.com/m3db/m3/src/dbnode/ts"
    26  	"github.com/m3db/m3/src/m3ninx/doc"
    27  	"github.com/m3db/m3/src/x/ident"
    28  	xtime "github.com/m3db/m3/src/x/time"
    29  )
    30  
    31  // FinalizeEncodedTagsFn is a function that will be called for each encoded tags once
    32  // the WriteBatch itself is finalized.
    33  type FinalizeEncodedTagsFn func(b []byte)
    34  
    35  // FinalizeAnnotationFn is a function that will be called for each annotation once
    36  // the WriteBatch itself is finalized.
    37  type FinalizeAnnotationFn func(b []byte)
    38  
    39  // Write is a write for the commitlog.
    40  type Write struct {
    41  	Series     ts.Series
    42  	Datapoint  ts.Datapoint
    43  	Unit       xtime.Unit
    44  	Annotation ts.Annotation
    45  }
    46  
    47  // PendingIndexInsert is a pending index insert.
    48  type PendingIndexInsert struct {
    49  	Entry    index.WriteBatchEntry
    50  	Document doc.Metadata
    51  }
    52  
    53  // BatchWrite represents a write that was added to the
    54  // BatchWriter.
    55  type BatchWrite struct {
    56  	// Used by the commitlog. If this is false, the commitlog should not write
    57  	// the series at this index.
    58  	SkipWrite bool
    59  	// PendingIndex returns whether a write has a pending index.
    60  	PendingIndex bool
    61  	// Used by the commitlog (series needed to be updated by the shard
    62  	// object first, cannot use the Series provided by the caller as it
    63  	// is missing important fields like Tags.)
    64  	Write Write
    65  	// EncodedTags is used by the commit log, but also held onto as a reference
    66  	// here so that it can be returned to the pool after the write to commit log
    67  	// completes (since the Write.Series gets overwritten in SetOutcome so can't
    68  	// use the reference there for returning to the pool).
    69  	EncodedTags ts.EncodedTags
    70  	// Used to help the caller tie errors back to an index in their
    71  	// own collection.
    72  	OriginalIndex int
    73  	// Used by the commitlog.
    74  	Err error
    75  }
    76  
    77  // WriteBatch is the interface that supports adding writes to the batch,
    78  // as well as iterating through the batched writes and resetting the
    79  // struct (for pooling).
    80  type WriteBatch interface {
    81  	BatchWriter
    82  	// Can't use a real iterator pattern here as it slows things down.
    83  	Iter() []BatchWrite
    84  	SetPendingIndex(idx int, pending PendingIndexInsert)
    85  	PendingIndex() []PendingIndexInsert
    86  	SetError(idx int, err error)
    87  	SetSeries(idx int, series ts.Series)
    88  	SetSkipWrite(idx int)
    89  	Reset(batchSize int, ns ident.ID)
    90  	Finalize()
    91  
    92  	// Returns the WriteBatch's internal capacity. Used by the pool to throw
    93  	// away batches that have grown too large.
    94  	cap() int
    95  }
    96  
    97  // BatchWriter is the interface that is used for preparing a batch of
    98  // writes.
    99  type BatchWriter interface {
   100  	Add(
   101  		originalIndex int,
   102  		id ident.ID,
   103  		timestamp xtime.UnixNano,
   104  		value float64,
   105  		unit xtime.Unit,
   106  		annotation []byte,
   107  	) error
   108  
   109  	AddTagged(
   110  		originalIndex int,
   111  		id ident.ID,
   112  		encodedTags ts.EncodedTags,
   113  		timestamp xtime.UnixNano,
   114  		value float64,
   115  		unit xtime.Unit,
   116  		annotation []byte,
   117  	) error
   118  
   119  	SetFinalizeEncodedTagsFn(f FinalizeEncodedTagsFn)
   120  
   121  	SetFinalizeAnnotationFn(f FinalizeAnnotationFn)
   122  }