github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/posting/mvcc_test.go (about)

     1  /*
     2   * Copyright 2018 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package posting
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/dgraph-io/dgraph/protos/pb"
    23  	"github.com/dgraph-io/dgraph/x"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestRollupTimestamp(t *testing.T) {
    28  	key := x.DataKey("rollup", 1)
    29  	// 3 Delta commits.
    30  	addEdgeToUID(t, "rollup", 1, 2, 1, 2)
    31  	addEdgeToUID(t, "rollup", 1, 3, 3, 4)
    32  	addEdgeToUID(t, "rollup", 1, 4, 5, 6)
    33  
    34  	l, err := GetNoStore(key)
    35  	require.NoError(t, err)
    36  
    37  	uidList, err := l.Uids(ListOptions{ReadTs: 7})
    38  	require.NoError(t, err)
    39  	require.Equal(t, 3, len(uidList.Uids))
    40  
    41  	edge := &pb.DirectedEdge{
    42  		Entity: 1,
    43  		Attr:   "rollup",
    44  		Value:  []byte(x.Star),
    45  		Op:     pb.DirectedEdge_DEL,
    46  	}
    47  	addMutation(t, l, edge, Del, 9, 10, false)
    48  
    49  	nl, err := getNew(key, pstore)
    50  	require.NoError(t, err)
    51  
    52  	uidList, err = nl.Uids(ListOptions{ReadTs: 11})
    53  	require.NoError(t, err)
    54  	require.Equal(t, 0, len(uidList.Uids))
    55  
    56  	// Now check that we don't lost the highest version during a rollup operation, despite the STAR
    57  	// delete marker being the most recent update.
    58  	kvs, err := nl.Rollup()
    59  	require.NoError(t, err)
    60  	require.Equal(t, uint64(10), kvs[0].Version)
    61  }
    62  
    63  func TestPostingListRead(t *testing.T) {
    64  	key := x.DataKey("emptypl", 1)
    65  
    66  	assertLength := func(readTs, sz int) {
    67  		nl, err := getNew(key, pstore)
    68  		require.NoError(t, err)
    69  		uidList, err := nl.Uids(ListOptions{ReadTs: uint64(readTs)})
    70  		require.NoError(t, err)
    71  		require.Equal(t, sz, len(uidList.Uids))
    72  	}
    73  
    74  	addEdgeToUID(t, "emptypl", 1, 2, 1, 2)
    75  	addEdgeToUID(t, "emptypl", 1, 3, 3, 4)
    76  
    77  	writer := NewTxnWriter(pstore)
    78  	require.NoError(t, writer.SetAt(key, []byte{}, BitEmptyPosting, 6))
    79  	require.NoError(t, writer.Flush())
    80  	assertLength(7, 0)
    81  
    82  	addEdgeToUID(t, "emptypl", 1, 4, 7, 8)
    83  	assertLength(9, 1)
    84  
    85  	var empty pb.PostingList
    86  	data, err := empty.Marshal()
    87  	require.NoError(t, err)
    88  
    89  	writer = NewTxnWriter(pstore)
    90  	require.NoError(t, writer.SetAt(key, data, BitCompletePosting, 10))
    91  	require.NoError(t, writer.Flush())
    92  	assertLength(10, 0)
    93  
    94  	addEdgeToUID(t, "emptypl", 1, 5, 11, 12)
    95  	addEdgeToUID(t, "emptypl", 1, 6, 13, 14)
    96  	addEdgeToUID(t, "emptypl", 1, 7, 15, 16)
    97  	assertLength(17, 3)
    98  }