-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RelationshipRecordFormatTest.java
361 lines (310 loc) · 16.2 KB
/
RelationshipRecordFormatTest.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* 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 Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.store.format.highlimit;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.neo4j.io.ByteUnit;
import org.neo4j.io.pagecache.PagedFile;
import org.neo4j.io.pagecache.StubPageCursor;
import org.neo4j.kernel.impl.store.format.highlimit.v30.RelationshipRecordFormatV3_0;
import org.neo4j.kernel.impl.store.record.RecordLoad;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.neo4j.kernel.impl.store.NoStoreHeader.NO_STORE_HEADER;
import static org.neo4j.kernel.impl.store.RecordPageLocationCalculator.offsetForId;
import static org.neo4j.kernel.impl.store.format.BaseRecordFormat.IN_USE_BIT;
import static org.neo4j.kernel.impl.store.format.highlimit.BaseHighLimitRecordFormat.NULL;
public class RelationshipRecordFormatTest
{
private final RelationshipRecordFormat format = new RelationshipRecordFormat();
private final int recordSize = format.getRecordSize( NO_STORE_HEADER );
private ConstantIdSequence idSequence = new ConstantIdSequence();
private final FixedLinkedStubPageCursor cursor = new FixedLinkedStubPageCursor( 0, (int) ByteUnit.kibiBytes( 4 ) )
{
@Override
public boolean next( long pageId ) throws IOException
{
// We're going to use this cursor in an environment where in all genericness this cursor
// is one that can be moved around to other pages. That's not possible with this stub cursor,
// however we know that in this test we'll stay on page 0 even if there are calls to next(pageId)
// which are part of the format code.
assertEquals( 0, pageId );
return true;
}
};
@Test
public void writeAndReadRecordWithRelativeReferences() throws IOException
{
long recordId = 0xF1F1F1F1F1F1L;
int recordOffset = cursor.getOffset();
RelationshipRecord record = createRecord( format, recordId, false, false );
RelationshipRecord firstInSecondChain = createRecord( format, recordId, false, true );
RelationshipRecord firstInFirstChain = createRecord( format, recordId, true, false );
RelationshipRecord firstInBothChains = createRecord( format, recordId, true, true );
checkRecord( format, recordSize, cursor, recordId, recordOffset, record );
checkRecord( format, recordSize, cursor, recordId, recordOffset, firstInSecondChain );
checkRecord( format, recordSize, cursor, recordId, recordOffset, firstInFirstChain );
checkRecord( format, recordSize, cursor, recordId, recordOffset, firstInBothChains );
}
/*
* This test acts as a test group for whoever uses BaseHighLimitRecordFormat base class,
* the logic for marking both units as unused when deleting exists there.
*/
@Test
public void shouldMarkBothUnitsAsUnusedWhenDeletingRecordWhichHasSecondaryUnit() throws Exception
{
// GIVEN a record which requires two units
PagedFile storeFile = mock( PagedFile.class );
when( storeFile.pageSize() ).thenReturn( cursor.getCurrentPageSize() );
long hugeValue = 1L << 48;
RelationshipRecord record = new RelationshipRecord( 5 ).initialize( true,
hugeValue + 1, hugeValue + 2, hugeValue + 3, 4,
hugeValue + 5, hugeValue + 6, hugeValue + 7, hugeValue + 8, true, true );
record.setSecondaryUnitId( 17 );
record.setRequiresSecondaryUnit( true );
cursor.setOffset( offsetForId( record.getId(), cursor.getCurrentPageSize(), recordSize ) );
format.write( record, cursor, recordSize );
// WHEN deleting that record
record.setInUse( false );
cursor.setOffset( offsetForId( record.getId(), cursor.getCurrentPageSize(), recordSize ) );
format.write( record, cursor, recordSize );
// THEN both units should have been marked as unused
cursor.setOffset( offsetForId( record.getId(), cursor.getCurrentPageSize(), recordSize ) );
assertFalse( recordInUse( cursor ) );
cursor.setOffset( offsetForId( record.getSecondaryUnitId(), cursor.getCurrentPageSize(), recordSize ) );
assertFalse( recordInUse( cursor ) );
}
@Test
public void readWriteFixedReferencesRecord() throws Exception
{
RelationshipRecord source = new RelationshipRecord( 1 );
RelationshipRecord target = new RelationshipRecord( 1 );
source.initialize( true, randomFixedReference(), randomFixedReference(), randomFixedReference(), 0,
randomFixedReference(), randomFixedReference(), randomFixedReference(), randomFixedReference(),
true, true );
writeReadRecord( source, target );
assertTrue( "Record should use fixed reference format.", target.isUseFixedReferences() );
verifySameReferences( source, target);
}
@Test
public void useFixedRecordFormatWhenAtLeastOneOfTheReferencesIsMissing() throws IOException
{
RelationshipRecord source = new RelationshipRecord( 1 );
RelationshipRecord target = new RelationshipRecord( 1 );
verifyRecordsWithPoisonedReference( source, target, NULL );
}
@Test
public void useVariableLengthFormatWhenAtLeastOneOfTheReferencesIsTooBig() throws IOException
{
RelationshipRecord source = new RelationshipRecord( 1 );
RelationshipRecord target = new RelationshipRecord( 1 );
verifyRecordsWithPoisonedReference( source, target, 1L << Integer.SIZE + 5 );
}
@Test
public void useVariableLengthFormatWhenRecordSizeIsTooSmall() throws IOException
{
RelationshipRecord source = new RelationshipRecord( 1 );
RelationshipRecord target = new RelationshipRecord( 1 );
source.initialize( true, randomFixedReference(), randomFixedReference(), randomFixedReference(), 0,
randomFixedReference(), randomFixedReference(), randomFixedReference(), randomFixedReference(),
true, true );
writeReadRecord( source, target, RelationshipRecordFormat.FIXED_FORMAT_RECORD_SIZE - 1 );
assertFalse( "Record should use variable length reference if format record is too small.", target.isUseFixedReferences() );
verifySameReferences( source, target);
}
@Test
public void useFixedReferenceFormatWhenRecordCanFitInRecordSizeRecord() throws IOException
{
RelationshipRecord source = new RelationshipRecord( 1 );
RelationshipRecord target = new RelationshipRecord( 1 );
source.initialize( true, randomFixedReference(), randomFixedReference(), randomFixedReference(), 0,
randomFixedReference(), randomFixedReference(), randomFixedReference(), randomFixedReference(),
true, true );
writeReadRecord( source, target, RelationshipRecordFormat.FIXED_FORMAT_RECORD_SIZE );
assertTrue( "Record should use fixed reference if can fit in format record.", target.isUseFixedReferences() );
verifySameReferences( source, target);
}
@Test
public void readSingleUnitRecordStoredNotInFixedReferenceFormat() throws Exception
{
RelationshipRecord oldFormatRecord = new RelationshipRecord( 1 );
RelationshipRecord newFormatRecord = new RelationshipRecord( 1 );
oldFormatRecord.initialize( true, randomSmallReference(), randomSmallReference(), randomSmallReference(), 0,
randomSmallReference(), randomSmallReference(), randomSmallReference(), randomSmallReference(),
true, true );
writeRecordWithOldFormat( oldFormatRecord );
assertFalse( "This should be single unit record.", oldFormatRecord.hasSecondaryUnitId() );
assertFalse( "Old format is not aware about fixed references.", oldFormatRecord.isUseFixedReferences() );
format.read( newFormatRecord, cursor, RecordLoad.NORMAL, RelationshipRecordFormat.RECORD_SIZE );
verifySameReferences( oldFormatRecord, newFormatRecord );
}
@Test
public void readDoubleUnitRecordStoredNotInFixedReferenceFormat() throws Exception
{
RelationshipRecord oldFormatRecord = new RelationshipRecord( 1 );
RelationshipRecord newFormatRecord = new RelationshipRecord( 1 );
oldFormatRecord.initialize( true, bigReference(), bigReference(), bigReference(), 0,
bigReference(), bigReference(), bigReference(), bigReference(),
true, true );
writeRecordWithOldFormat( oldFormatRecord );
assertTrue( "This should be double unit record.", oldFormatRecord.hasSecondaryUnitId() );
assertFalse( "Old format is not aware about fixed references.", oldFormatRecord.isUseFixedReferences() );
format.read( newFormatRecord, cursor, RecordLoad.NORMAL, RelationshipRecordFormat.RECORD_SIZE );
verifySameReferences( oldFormatRecord, newFormatRecord );
}
private void writeRecordWithOldFormat( RelationshipRecord oldFormatRecord ) throws IOException
{
int oldRecordSize = RelationshipRecordFormatV3_0.RECORD_SIZE;
RelationshipRecordFormatV3_0 recordFormatV30 = new RelationshipRecordFormatV3_0();
recordFormatV30.prepare( oldFormatRecord, oldRecordSize, idSequence );
recordFormatV30.write( oldFormatRecord, cursor, oldRecordSize );
cursor.setOffset( 0 );
}
private void verifyRecordsWithPoisonedReference( RelationshipRecord source, RelationshipRecord target,
long poisonedReference ) throws IOException
{
boolean nullPoison = poisonedReference == NULL;
// first and second node can't be empty references so excluding them in case if poisoned reference is null
int differentReferences = nullPoison ? 5 : 7;
List<Long> references = buildReferenceList( differentReferences, poisonedReference );
for ( int i = 0; i < differentReferences; i++ )
{
cursor.setOffset( 0 );
Iterator<Long> iterator = references.iterator();
source.initialize( true, iterator.next(),
nullPoison ? randomFixedReference() : iterator.next(),
nullPoison ? randomFixedReference() : iterator.next(),
0, iterator.next(), iterator.next(), iterator.next(), iterator.next(), true, true );
writeReadRecord( source, target );
if ( nullPoison )
{
assertTrue( "Record should use fixed reference format.", target.isUseFixedReferences() );
}
else
{
assertFalse( "Record should use variable length reference format.", target.isUseFixedReferences() );
}
verifySameReferences( source, target );
Collections.rotate( references, 1 );
}
}
private List<Long> buildReferenceList( int differentReferences, long poison )
{
List<Long> references = new ArrayList<>( differentReferences );
references.add( poison );
for ( int i = 1; i < differentReferences; i++ )
{
references.add( randomFixedReference() );
}
return references;
}
private void writeReadRecord( RelationshipRecord source, RelationshipRecord target ) throws java.io.IOException
{
writeReadRecord( source, target, recordSize );
}
private void writeReadRecord( RelationshipRecord source, RelationshipRecord target, int recordSize ) throws java.io.IOException
{
format.prepare( source, recordSize, idSequence );
format.write( source, cursor, recordSize );
cursor.setOffset( 0 );
format.read( target, cursor, RecordLoad.NORMAL, recordSize );
}
private boolean recordInUse( StubPageCursor cursor )
{
byte header = cursor.getByte();
return (header & IN_USE_BIT) != 0;
}
private void checkRecord( RelationshipRecordFormat format, int recordSize, StubPageCursor cursor,
long recordId, int recordOffset, RelationshipRecord record ) throws IOException
{
format.write( record, cursor, recordSize );
RelationshipRecord recordFromStore = format.newRecord();
recordFromStore.setId( recordId );
resetCursor( cursor, recordOffset );
format.read( recordFromStore, cursor, RecordLoad.NORMAL, recordSize );
// records should be the same
verifySameReferences( record, recordFromStore );
// now lets try to read same data into a record with different id - we should get different absolute references
resetCursor( cursor, recordOffset );
RelationshipRecord recordWithOtherId = format.newRecord();
recordWithOtherId.setId( 1L );
format.read( recordWithOtherId, cursor, RecordLoad.NORMAL, recordSize );
assertNotEquals( record.getFirstNextRel(), recordWithOtherId.getFirstNextRel() );
assertNotEquals( record.getFirstPrevRel(), recordWithOtherId.getFirstPrevRel() );
assertNotEquals( record.getSecondNextRel(), recordWithOtherId.getSecondNextRel() );
assertNotEquals( record.getSecondPrevRel(), recordWithOtherId.getSecondPrevRel() );
}
private void verifySameReferences( RelationshipRecord record, RelationshipRecord recordFromStore )
{
assertEquals( "First Next references should be equal.", record.getFirstNextRel(), recordFromStore.getFirstNextRel() );
assertEquals( "First Node references should be equal.", record.getFirstNode(), recordFromStore.getFirstNode() );
assertEquals( "First Prev Rel references should be equal.", record.getFirstPrevRel(), recordFromStore.getFirstPrevRel() );
assertEquals( "Second Next Rel references should be equal.", record.getSecondNextRel(), recordFromStore.getSecondNextRel() );
assertEquals( "Second Node references should be equal.", record.getSecondNode(), recordFromStore.getSecondNode() );
assertEquals( "Second Prev Rel references should be equal.", record.getSecondPrevRel(), recordFromStore.getSecondPrevRel() );
assertEquals( "Next Prop references should be equal.", record.getNextProp(), recordFromStore.getNextProp() );
}
private void resetCursor( StubPageCursor cursor, int recordOffset )
{
cursor.setOffset( recordOffset );
}
private RelationshipRecord createRecord( RelationshipRecordFormat format, long recordId,
boolean firstInFirstChain, boolean firstInSecondChain )
{
RelationshipRecord record = format.newRecord();
record.setInUse( true );
record.setFirstInFirstChain( firstInFirstChain );
record.setFirstInSecondChain( firstInSecondChain );
record.setId( recordId );
record.setFirstNextRel( 1L );
record.setFirstNode( 2L );
record.setFirstPrevRel( 3L );
record.setSecondNextRel( 4L );
record.setSecondNode( 5L );
record.setSecondPrevRel( 6L );
return record;
}
private long randomFixedReference()
{
return randomReference( 1L << (Integer.SIZE + 1 ) );
}
private long bigReference()
{
return 1L << 57;
}
private long randomSmallReference()
{
return randomReference( 1L << (Integer.SIZE - 4 ) );
}
private long randomReference( long maxValue )
{
return ThreadLocalRandom.current().nextLong( maxValue );
}
}