MCPcopy
hub / github.com/canopy-network/canopy / TestNestedTxnMergedIteration

Function TestNestedTxnMergedIteration

store/txn_test.go:78–118  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

76}
77
78func TestNestedTxnMergedIteration(t *testing.T) {
79 baseTxn, db, batch := newTxn(t, []byte(nil))
80 defer func() { baseTxn.Close(); db.Close(); baseTxn.Discard() }()
81 // create a nested transaction
82 nested := NewTxn(baseTxn, baseTxn, nil, false, true, true, baseTxn.writeVersion)
83 // set and and flush a value in the parent transaction
84 require.NoError(t, nested.Set(lib.JoinLenPrefix([]byte("a")), []byte("a")))
85 require.NoError(t, baseTxn.Commit())
86 // set and flush a value in the nested transaction
87 require.NoError(t, nested.Set(lib.JoinLenPrefix([]byte("b")), []byte("b")))
88 require.NoError(t, nested.Commit())
89 // flush the batch
90 require.NoError(t, batch.Commit(&pebble.WriteOptions{Sync: false}))
91 // set a value in the parent transaction to not be flushed
92 require.NoError(t, baseTxn.Set(lib.JoinLenPrefix([]byte("c")), []byte("c")))
93 // set a value in the nested transaction to not be flushed
94 require.NoError(t, nested.Set(lib.JoinLenPrefix([]byte("d")), []byte("d")))
95
96 // create a new iterator on the nested transaction
97 iter, err := nested.NewIterator(nil, false, false)
98 require.NoError(t, err)
99 expected := []string{"a", "b", "c", "d"}
100 got := []string{}
101 for ; iter.Valid(); iter.Next() {
102 got = append(got, string(iter.Value()))
103 }
104 iter.Close()
105 // confirm the iterator returns the expected values
106 require.Equal(t, expected, got)
107
108 // create a new reverse iterator on the nested transaction
109 iter, err = nested.NewIterator(nil, true, false)
110 require.NoError(t, err)
111 expected = []string{"d", "c", "b", "a"}
112 got = []string{}
113 for ; iter.Valid(); iter.Next() {
114 got = append(got, string(iter.Value()))
115 }
116 iter.Close()
117 require.Equal(t, expected, got)
118}
119
120func TestTxnWriteSetGet(t *testing.T) {
121 prefix := lib.JoinLenPrefix([]byte("1/"))

Callers

nothing calls this directly

Calls 14

SetMethod · 0.95
CommitMethod · 0.95
NewIteratorMethod · 0.95
JoinLenPrefixFunction · 0.92
newTxnFunction · 0.85
NewTxnFunction · 0.85
EqualMethod · 0.80
CloseMethod · 0.65
DiscardMethod · 0.65
CommitMethod · 0.65
SetMethod · 0.65
ValidMethod · 0.65

Tested by

no test coverage detected