-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NodeCommandTest.java
213 lines (198 loc) · 8.58 KB
/
NodeCommandTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.transaction.state;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.NodeLabels;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.format.lowlimit.LowLimitV3_0;
import org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.transaction.command.Command;
import org.neo4j.kernel.impl.transaction.command.PhysicalLogCommandReaderV2_2;
import org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.storageengine.api.CommandReader;
import org.neo4j.test.rule.PageCacheRule;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.neo4j.kernel.impl.store.DynamicNodeLabels.dynamicPointer;
import static org.neo4j.kernel.impl.store.NodeLabelsField.parseLabelsField;
import static org.neo4j.kernel.impl.store.ShortArray.LONG;
import static org.neo4j.kernel.impl.store.record.DynamicRecord.dynamicRecord;
import static org.neo4j.kernel.impl.util.IoPrimitiveUtils.safeCastLongToInt;
public class NodeCommandTest
{
@ClassRule
public static PageCacheRule pageCacheRule = new PageCacheRule();
private NodeStore nodeStore;
InMemoryClosableChannel channel = new InMemoryClosableChannel();
private final CommandReader commandReader = new PhysicalLogCommandReaderV2_2();
@Rule
public EphemeralFileSystemRule fs = new EphemeralFileSystemRule();
private NeoStores neoStores;
@Test
public void shouldSerializeAndDeserializeUnusedRecords() throws Exception
{
// Given
NodeRecord before = new NodeRecord( 12, false, 1, 2 );
NodeRecord after = new NodeRecord( 12, false, 2, 1 );
// When
assertSerializationWorksFor( new Command.NodeCommand( before, after ) );
}
@Test
public void shouldSerializeCreatedRecord() throws Exception
{
// Given
NodeRecord before = new NodeRecord( 12, false, 1, 2 );
NodeRecord after = new NodeRecord( 12, false, 2, 1 );
after.setCreated();
after.setInUse( true );
// When
assertSerializationWorksFor( new Command.NodeCommand( before, after ) );
}
@Test
public void shouldSerializeUpdatedRecord() throws Exception
{
// Given
NodeRecord before = new NodeRecord( 12, false, 1, 2 );
before.setInUse( true );
NodeRecord after = new NodeRecord( 12, false, 2, 1 );
after.setInUse( true );
// When
assertSerializationWorksFor( new Command.NodeCommand( before, after ) );
}
@Test
public void shouldSerializeInlineLabels() throws Exception
{
// Given
NodeRecord before = new NodeRecord( 12, false, 1, 2 );
before.setInUse( true );
NodeRecord after = new NodeRecord( 12, false, 2, 1 );
after.setInUse( true );
NodeLabels nodeLabels = parseLabelsField( after );
nodeLabels.add( 1337, nodeStore, nodeStore.getDynamicLabelStore() );
// When
assertSerializationWorksFor( new Command.NodeCommand( before, after ) );
}
@Test
public void shouldSerializeDynamicRecordLabels() throws Exception
{
// Given
NodeRecord before = new NodeRecord( 12, false, 1, 2 );
before.setInUse( true );
NodeRecord after = new NodeRecord( 12, false, 2, 1 );
after.setInUse( true );
NodeLabels nodeLabels = parseLabelsField( after );
for ( int i = 10; i < 100; i++ )
{
nodeLabels.add( i, nodeStore, nodeStore.getDynamicLabelStore() );
}
// When
assertSerializationWorksFor( new Command.NodeCommand( before, after ) );
}
@Test
public void shouldSerializeDynamicRecordsRemoved() throws Exception
{
channel.reset();
// Given
NodeRecord before = new NodeRecord( 12, false, 1, 2 );
before.setInUse( true );
List<DynamicRecord> beforeDyn = singletonList( dynamicRecord(
0, true, true, -1l, LONG.intValue(), new byte[]{1, 2, 3, 4, 5, 6, 7, 8} ) );
before.setLabelField( dynamicPointer( beforeDyn ), beforeDyn );
NodeRecord after = new NodeRecord( 12, false, 2, 1 );
after.setInUse( true );
List<DynamicRecord> dynamicRecords = singletonList( dynamicRecord(
0, false, true, -1l, LONG.intValue(), new byte[]{ 1, 2, 3, 4, 5, 6, 7, 8} ) );
after.setLabelField( dynamicPointer( dynamicRecords ), dynamicRecords );
// When
Command.NodeCommand cmd = new Command.NodeCommand( before, after );
cmd.serialize( channel );
Command.NodeCommand result = (Command.NodeCommand) commandReader.read( channel );
// Then
assertThat( result, equalTo( cmd ) );
assertThat( result.getMode(), equalTo( cmd.getMode() ) );
assertThat( result.getBefore(), equalTo( cmd.getBefore() ) );
assertThat( result.getAfter(), equalTo( cmd.getAfter() ) );
// And dynamic records should be the same
assertThat( result.getBefore().getDynamicLabelRecords(), equalTo( cmd.getBefore().getDynamicLabelRecords() ) );
assertThat( result.getAfter().getDynamicLabelRecords(), equalTo( cmd.getAfter().getDynamicLabelRecords() ) );
}
private void assertSerializationWorksFor( org.neo4j.kernel.impl.transaction.command.Command.NodeCommand cmd )
throws IOException
{
channel.reset();
cmd.serialize( channel );
Command.NodeCommand result = (Command.NodeCommand) commandReader.read( channel );
// Then
assertThat( result, equalTo( cmd ) );
assertThat( result.getMode(), equalTo( cmd.getMode() ) );
assertThat( result.getBefore(), equalTo( cmd.getBefore() ) );
assertThat( result.getAfter(), equalTo( cmd.getAfter() ) );
// And labels should be the same
assertThat( labels( result.getBefore() ), equalTo( labels( cmd.getBefore() ) ) );
assertThat( labels( result.getAfter() ), equalTo( labels( cmd.getAfter() ) ) );
// And dynamic records should be the same
assertThat( result.getBefore().getDynamicLabelRecords(), equalTo( result.getBefore().getDynamicLabelRecords() ) );
assertThat( result.getAfter().getDynamicLabelRecords(), equalTo( result.getAfter().getDynamicLabelRecords() ) );
}
private Set<Integer> labels( NodeRecord record )
{
long[] rawLabels = parseLabelsField( record ).get( nodeStore );
Set<Integer> labels = new HashSet<>( rawLabels.length );
for ( long label : rawLabels )
{
labels.add( safeCastLongToInt( label ) );
}
return labels;
}
@Before
public void before() throws Exception
{
File dir = new File( "dir" );
fs.get().mkdirs( dir );
@SuppressWarnings("deprecation")
StoreFactory storeFactory = new StoreFactory( dir, Config.empty(), new DefaultIdGeneratorFactory( fs.get() ),
pageCacheRule.getPageCache( fs.get() ), fs.get(), LowLimitV3_0.RECORD_FORMATS,
NullLogProvider.getInstance() );
neoStores = storeFactory.openAllNeoStores( true );
nodeStore = neoStores.getNodeStore();
}
@After
public void after() throws Exception
{
neoStores.close();
}
}