github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/builder/multi_segments_field_iter_test.go (about)

     1  // Copyright (c) 2019 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 builder
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/m3ninx/doc"
    27  	"github.com/m3db/m3/src/m3ninx/index"
    28  	"github.com/m3db/m3/src/m3ninx/index/segment"
    29  	"github.com/m3db/m3/src/m3ninx/index/segment/mem"
    30  	xerrors "github.com/m3db/m3/src/x/errors"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  var (
    36  	testMemOptions = mem.NewOptions()
    37  )
    38  
    39  func TestFieldIterFromSegmentsDeduplicates(t *testing.T) {
    40  	segments := []segmentMetadata{
    41  		{segment: newTestSegmentWithDocs(t, []doc.Metadata{
    42  			{
    43  				ID: []byte("foo"),
    44  				Fields: []doc.Field{
    45  					{Name: []byte("fruit"), Value: []byte("apple")},
    46  					{Name: []byte("vegetable"), Value: []byte("carrot")},
    47  				},
    48  			},
    49  		})},
    50  		{segment: newTestSegmentWithDocs(t, []doc.Metadata{
    51  			{
    52  				ID: []byte("bar"),
    53  				Fields: []doc.Field{
    54  					{Name: []byte("fruit"), Value: []byte("banana")},
    55  					{Name: []byte("meat"), Value: []byte("beef")},
    56  				},
    57  			},
    58  			{
    59  				ID: []byte("baz"),
    60  				Fields: []doc.Field{
    61  					{Name: []byte("color"), Value: []byte("blue")},
    62  					{Name: []byte("alpha"), Value: []byte("1.0")},
    63  				},
    64  			},
    65  		})},
    66  	}
    67  
    68  	iter, err := newFieldIterFromSegments(segments)
    69  	require.NoError(t, err)
    70  
    71  	assertIterValues(t, iter, []string{
    72  		string(doc.IDReservedFieldName),
    73  		"alpha",
    74  		"color",
    75  		"fruit",
    76  		"meat",
    77  		"vegetable",
    78  	})
    79  }
    80  
    81  func TestFieldIterFromSegmentsSomeEmpty(t *testing.T) {
    82  	segments := []segmentMetadata{
    83  		{segment: newTestSegmentWithDocs(t, []doc.Metadata{
    84  			{
    85  				ID: []byte("foo"),
    86  				Fields: []doc.Field{
    87  					{Name: []byte("fruit"), Value: []byte("apple")},
    88  					{Name: []byte("vegetable"), Value: []byte("carrot")},
    89  				},
    90  			},
    91  		})},
    92  		{segment: newTestSegmentWithDocs(t, []doc.Metadata{})},
    93  	}
    94  
    95  	iter, err := newFieldIterFromSegments(segments)
    96  	require.NoError(t, err)
    97  
    98  	assertIterValues(t, iter, []string{
    99  		string(doc.IDReservedFieldName),
   100  		"fruit",
   101  		"vegetable",
   102  	})
   103  }
   104  
   105  func TestFieldIterFromSegmentsIdentical(t *testing.T) {
   106  	segments := []segmentMetadata{
   107  		{segment: newTestSegmentWithDocs(t, []doc.Metadata{
   108  			{
   109  				ID: []byte("foo"),
   110  				Fields: []doc.Field{
   111  					{Name: []byte("fruit"), Value: []byte("apple")},
   112  					{Name: []byte("vegetable"), Value: []byte("carrot")},
   113  				},
   114  			},
   115  		})},
   116  		{segment: newTestSegmentWithDocs(t, []doc.Metadata{
   117  			{
   118  				ID: []byte("bar"),
   119  				Fields: []doc.Field{
   120  					{Name: []byte("fruit"), Value: []byte("apple")},
   121  					{Name: []byte("vegetable"), Value: []byte("carrot")},
   122  				},
   123  			},
   124  		})},
   125  	}
   126  
   127  	iter, err := newFieldIterFromSegments(segments)
   128  	require.NoError(t, err)
   129  
   130  	assertIterValues(t, iter, []string{
   131  		string(doc.IDReservedFieldName),
   132  		"fruit",
   133  		"vegetable",
   134  	})
   135  }
   136  
   137  func assertIterValues(
   138  	t *testing.T,
   139  	iter segment.FieldsIterator,
   140  	vals []string,
   141  ) {
   142  	var actual []string
   143  	for iter.Next() {
   144  		actual = append(actual, string(iter.Current()))
   145  	}
   146  	require.False(t, iter.Next())
   147  
   148  	require.Equal(t, vals, actual)
   149  	require.NoError(t, xerrors.FirstError(iter.Err(), iter.Close()))
   150  }
   151  
   152  func newTestSegmentWithDocs(
   153  	t *testing.T,
   154  	docs []doc.Metadata,
   155  ) segment.Segment {
   156  	seg, err := mem.NewSegment(testMemOptions)
   157  	require.NoError(t, err)
   158  
   159  	defer func() {
   160  		require.NoError(t, seg.Seal())
   161  	}()
   162  
   163  	if len(docs) == 0 {
   164  		return seg
   165  	}
   166  
   167  	require.NoError(t, seg.InsertBatch(index.Batch{
   168  		Docs: docs,
   169  	}))
   170  
   171  	return seg
   172  }