github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/mem/merge_test.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 mem
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/m3ninx/doc"
    27  	"github.com/m3db/m3/src/m3ninx/index"
    28  
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestMemSegmentMerge(t *testing.T) {
    33  	docs := []doc.Metadata{
    34  		{
    35  			ID: []byte("abc"),
    36  			Fields: []doc.Field{
    37  				{
    38  					Name:  []byte("fruit"),
    39  					Value: []byte("banana"),
    40  				},
    41  				{
    42  					Name:  []byte("color"),
    43  					Value: []byte("yellow"),
    44  				},
    45  			},
    46  		},
    47  		{
    48  			ID: []byte("cde"),
    49  			Fields: []doc.Field{
    50  				{
    51  					Name:  []byte("fruit"),
    52  					Value: []byte("apple"),
    53  				},
    54  				{
    55  					Name:  []byte("color"),
    56  					Value: []byte("red"),
    57  				},
    58  			},
    59  		},
    60  		{
    61  			ID: []byte("dfg"),
    62  			Fields: []doc.Field{
    63  				{
    64  					Name:  []byte("fruit"),
    65  					Value: []byte("pineapple"),
    66  				},
    67  				{
    68  					Name:  []byte("color"),
    69  					Value: []byte("yellow"),
    70  				},
    71  			},
    72  		},
    73  	}
    74  	d := docs[0]
    75  	rest := docs[1:]
    76  
    77  	opts := NewOptions()
    78  	m1, err := NewSegment(opts)
    79  	require.NoError(t, err)
    80  	_, err = m1.Insert(d)
    81  	require.NoError(t, err)
    82  
    83  	m2, err := NewSegment(opts)
    84  	require.NoError(t, err)
    85  	for _, d := range rest {
    86  		_, err = m2.Insert(d)
    87  		require.NoError(t, err)
    88  	}
    89  
    90  	m3, err := NewSegment(opts)
    91  	require.NoError(t, err)
    92  
    93  	require.NoError(t, Merge(m3, m1, m2))
    94  
    95  	reader, err := m3.Reader()
    96  	require.NoError(t, err)
    97  
    98  	for _, d := range docs {
    99  		assertReaderHasDoc(t, reader, d)
   100  	}
   101  
   102  	require.NoError(t, reader.Close())
   103  }
   104  
   105  func assertReaderHasDoc(t *testing.T, r index.Reader, d doc.Metadata) {
   106  	iter, err := r.AllDocs()
   107  	require.NoError(t, err)
   108  	found := false
   109  	for iter.Next() {
   110  		di := iter.Current()
   111  		if di.Equal(d) {
   112  			found = true
   113  			break
   114  		}
   115  	}
   116  	require.True(t, found)
   117  	require.NoError(t, iter.Err())
   118  	require.NoError(t, iter.Close())
   119  }