-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Command.java
514 lines (436 loc) · 13.9 KB
/
Command.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Copyright (c) 2002-2015 "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.command;
import java.io.IOException;
import java.util.Collection;
import org.neo4j.kernel.impl.api.CommandVisitor;
import org.neo4j.kernel.impl.store.record.AbstractBaseRecord;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.LabelTokenRecord;
import org.neo4j.kernel.impl.store.record.NeoStoreRecord;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord;
import org.neo4j.kernel.impl.store.record.PropertyRecord;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord;
import org.neo4j.kernel.impl.store.record.SchemaRule;
import org.neo4j.kernel.impl.store.record.TokenRecord;
import org.neo4j.kernel.impl.transaction.state.PropertyRecordChange;
import static java.lang.String.format;
import static java.util.Collections.unmodifiableCollection;
import static org.neo4j.helpers.collection.IteratorUtil.first;
import static org.neo4j.kernel.impl.util.IdPrettyPrinter.label;
import static org.neo4j.kernel.impl.util.IdPrettyPrinter.relationshipType;
/**
* Command implementations for all the commands that can be performed on a Neo
* store.
*/
public abstract class Command
{
private int keyHash;
private long key;
private Mode mode;
/*
* TODO: This is techdebt
* This is used to control the order of how commands are applied, which is done because
* we don't take read locks, and so the order or how we change things lowers the risk
* of reading invalid state. This should be removed once eg. MVCC or read locks has been
* implemented.
*/
public enum Mode
{
CREATE,
UPDATE,
DELETE;
public static Mode fromRecordState( boolean created, boolean inUse )
{
if ( !inUse )
{
return DELETE;
}
if ( created )
{
return CREATE;
}
return UPDATE;
}
public static Mode fromRecordState( AbstractBaseRecord record )
{
return fromRecordState( record.isCreated(), record.inUse() );
}
}
protected final void setup( long key, Mode mode )
{
this.mode = mode;
this.keyHash = (int) ((key >>> 32) ^ key);
this.key = key;
}
@Override
public int hashCode()
{
return keyHash;
}
// Force implementors to implement toString
@Override
public abstract String toString();
public long getKey()
{
return key;
}
public Mode getMode()
{
return mode;
}
@Override
public boolean equals( Object o )
{
return o != null && o.getClass().equals( getClass() ) && getKey() == ((Command) o).getKey();
}
public abstract boolean handle( CommandVisitor handler ) throws IOException;
protected String beforeAndAfterToString( AbstractBaseRecord before, AbstractBaseRecord after )
{
return format( " -%s%n +%s", before, after );
}
public static class NodeCommand extends Command
{
private NodeRecord before;
private NodeRecord after;
public NodeCommand init( NodeRecord before, NodeRecord after )
{
setup( after.getId(), Mode.fromRecordState( after ) );
this.before = before;
this.after = after;
return this;
}
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitNodeCommand( this );
}
@Override
public String toString()
{
return beforeAndAfterToString( before, after );
}
public NodeRecord getBefore()
{
return before;
}
public NodeRecord getAfter()
{
return after;
}
}
public static class RelationshipCommand extends Command
{
private RelationshipRecord before;
private RelationshipRecord after;
public RelationshipCommand init( RelationshipRecord before, RelationshipRecord after )
{
setup( after.getId(), Mode.fromRecordState( after ) );
this.before = before;
this.after = after;
return this;
}
@Override
public String toString()
{
return beforeAndAfterToString( before, after );
}
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitRelationshipCommand( this );
}
public RelationshipRecord getBefore()
{
return before;
}
public RelationshipRecord getAfter()
{
return after;
}
}
public static class RelationshipGroupCommand extends Command
{
private RelationshipGroupRecord before;
private RelationshipGroupRecord after;
public RelationshipGroupCommand init( RelationshipGroupRecord before, RelationshipGroupRecord after )
{
setup( after.getId(), Mode.fromRecordState( after ) );
this.before = before;
this.after = after;
return this;
}
@Override
public String toString()
{
return beforeAndAfterToString( before, after );
}
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitRelationshipGroupCommand( this );
}
public RelationshipGroupRecord getBefore()
{
return before;
}
public RelationshipGroupRecord getAfter()
{
return after;
}
}
public static class NeoStoreCommand extends Command
{
private NeoStoreRecord before;
private NeoStoreRecord after;
public NeoStoreCommand init( NeoStoreRecord before, NeoStoreRecord after )
{
setup( after.getId(), Mode.fromRecordState( after ) );
this.before = before;
this.after = after;
return this;
}
@Override
public String toString()
{
return beforeAndAfterToString( before, after );
}
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitNeoStoreCommand( this );
}
public NeoStoreRecord getBefore()
{
return before;
}
public NeoStoreRecord getAfter()
{
return after;
}
}
public static class PropertyKeyTokenCommand extends TokenCommand<PropertyKeyTokenRecord>
{
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitPropertyKeyTokenCommand( this );
}
}
public static class PropertyCommand extends Command implements PropertyRecordChange
{
private PropertyRecord before;
private PropertyRecord after;
// TODO as optimization the deserialized key/values could be passed in here
// so that the cost of deserializing them only applies in recovery/HA
public PropertyCommand init( PropertyRecord before, PropertyRecord after )
{
setup( after.getId(), Mode.fromRecordState( after ) );
this.before = before;
this.after = after;
return this;
}
@Override
public String toString()
{
return beforeAndAfterToString( before, after );
}
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitPropertyCommand( this );
}
@Override
public PropertyRecord getBefore()
{
return before;
}
@Override
public PropertyRecord getAfter()
{
return after;
}
public long getNodeId()
{
return after.getNodeId();
}
public long getRelId()
{
return after.getRelId();
}
}
public static abstract class TokenCommand<RECORD extends TokenRecord> extends Command
{
protected RECORD record;
public TokenCommand<RECORD> init( RECORD record )
{
setup( record.getId(), Mode.fromRecordState( record ) );
this.record = record;
return this;
}
public RECORD getRecord()
{
return record;
}
@Override
public String toString()
{
return record.toString();
}
}
public static class RelationshipTypeTokenCommand extends TokenCommand<RelationshipTypeTokenRecord>
{
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitRelationshipTypeTokenCommand( this );
}
}
public static class LabelTokenCommand extends TokenCommand<LabelTokenRecord>
{
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitLabelTokenCommand( this );
}
}
public static class SchemaRuleCommand extends Command
{
private Collection<DynamicRecord> recordsBefore;
private Collection<DynamicRecord> recordsAfter;
private SchemaRule schemaRule;
public SchemaRuleCommand init( Collection<DynamicRecord> recordsBefore,
Collection<DynamicRecord> recordsAfter, SchemaRule schemaRule )
{
setup( first( recordsAfter ).getId(), Mode.fromRecordState( first( recordsAfter ) ) );
this.recordsBefore = recordsBefore;
this.recordsAfter = recordsAfter;
this.schemaRule = schemaRule;
return this;
}
@Override
public String toString()
{
if ( schemaRule != null )
{
return getMode() + ":" + schemaRule.toString();
}
return "SchemaRule" + recordsAfter;
}
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitSchemaRuleCommand( this );
}
public Collection<DynamicRecord> getRecordsAfter()
{
return unmodifiableCollection( recordsAfter );
}
public SchemaRule getSchemaRule()
{
return schemaRule;
}
public Collection<DynamicRecord> getRecordsBefore()
{
return recordsBefore;
}
}
public static class NodeCountsCommand extends Command
{
private int labelId;
private long delta;
public NodeCountsCommand init( int labelId, long delta )
{
setup( labelId, Mode.UPDATE );
assert delta != 0 : "Tried to create a NodeCountsCommand for something that didn't change any count";
this.labelId = labelId;
this.delta = delta;
return this;
}
@Override
public String toString()
{
return String.format( "UpdateCounts[(%s) %s %d]",
label( labelId ), delta < 0 ? "-" : "+", Math.abs( delta ) );
}
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitNodeCountsCommand( this );
}
public int labelId()
{
return labelId;
}
public long delta()
{
return delta;
}
}
public static class RelationshipCountsCommand extends Command
{
private int startLabelId;
private int typeId;
private int endLabelId;
private long delta;
public RelationshipCountsCommand init( int startLabelId, int typeId, int endLabelId, long delta )
{
setup( typeId, Mode.UPDATE );
assert delta !=
0 : "Tried to create a RelationshipCountsCommand for something that didn't change any count";
this.startLabelId = startLabelId;
this.typeId = typeId;
this.endLabelId = endLabelId;
this.delta = delta;
return this;
}
@Override
public String toString()
{
return String.format( "UpdateCounts[(%s)-%s->(%s) %s %d]",
label( startLabelId ), relationshipType( typeId ), label( endLabelId ),
delta < 0 ? "-" : "+", Math.abs( delta ) );
}
@Override
public boolean handle( CommandVisitor handler ) throws IOException
{
return handler.visitRelationshipCountsCommand( this );
}
public int startLabelId()
{
return startLabelId;
}
public int typeId()
{
return typeId;
}
public int endLabelId()
{
return endLabelId;
}
public long delta()
{
return delta;
}
}
}