-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PropertyDeleter.java
197 lines (185 loc) · 8.61 KB
/
PropertyDeleter.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
/*
* Copyright (c) 2002-2018 "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.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.PrimitiveRecord;
import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.kernel.impl.store.record.PropertyRecord;
import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.kernel.impl.transaction.state.RecordAccess.RecordProxy;
public class PropertyDeleter
{
private final PropertyTraverser traverser;
public PropertyDeleter( PropertyTraverser traverser )
{
this.traverser = traverser;
}
public void deletePropertyChain( PrimitiveRecord primitive,
RecordAccess<PropertyRecord, PrimitiveRecord> propertyRecords )
{
long nextProp = primitive.getNextProp();
while ( nextProp != Record.NO_NEXT_PROPERTY.intValue() )
{
RecordProxy<PropertyRecord, PrimitiveRecord> propertyChange =
propertyRecords.getOrLoad( nextProp, primitive );
// TODO forChanging/forReading piggy-backing
PropertyRecord propRecord = propertyChange.forChangingData();
deletePropertyRecordIncludingValueRecords( propRecord );
nextProp = propRecord.getNextProp();
propRecord.setChanged( primitive );
}
primitive.setNextProp( Record.NO_NEXT_PROPERTY.intValue() );
}
public static void deletePropertyRecordIncludingValueRecords( PropertyRecord record )
{
for ( PropertyBlock block : record )
{
for ( DynamicRecord valueRecord : block.getValueRecords() )
{
assert valueRecord.inUse();
valueRecord.setInUse( false );
record.addDeletedRecord( valueRecord );
}
}
record.clearPropertyBlocks();
record.setInUse( false );
}
/**
* Removes property with given {@code propertyKey} from property chain owner by the primitive found in
* {@code primitiveProxy} if it exists.
*
* @param primitiveProxy access to the primitive record pointing to the start of the property chain.
* @param propertyKey the property key token id to look for and remove.
* @param propertyRecords access to records.
* @return {@code true} if the property was found and removed, otherwise {@code false}.
*/
public <P extends PrimitiveRecord> boolean removePropertyIfExists( RecordProxy<P,Void> primitiveProxy,
int propertyKey, RecordAccess<PropertyRecord,PrimitiveRecord> propertyRecords )
{
PrimitiveRecord primitive = primitiveProxy.forReadingData();
long propertyId = // propertyData.getId();
traverser.findPropertyRecordContaining( primitive, propertyKey, propertyRecords, false );
if ( !Record.NO_NEXT_PROPERTY.is( propertyId ) )
{
removeProperty( primitiveProxy, propertyKey, propertyRecords, primitive, propertyId );
return true;
}
return false;
}
/**
* Removes property with given {@code propertyKey} from property chain owner by the primitive found in
* {@code primitiveProxy}.
*
* @param primitiveProxy access to the primitive record pointing to the start of the property chain.
* @param propertyKey the property key token id to look for and remove.
* @param propertyRecords access to records.
* @throws IllegalStateException if property key was not found in the property chain.
*/
public <P extends PrimitiveRecord> void removeProperty( RecordProxy<P,Void> primitiveProxy, int propertyKey,
RecordAccess<PropertyRecord,PrimitiveRecord> propertyRecords )
{
PrimitiveRecord primitive = primitiveProxy.forReadingData();
long propertyId = // propertyData.getId();
traverser.findPropertyRecordContaining( primitive, propertyKey, propertyRecords, true );
removeProperty( primitiveProxy, propertyKey, propertyRecords, primitive, propertyId );
}
private <P extends PrimitiveRecord> void removeProperty( RecordProxy<P,Void> primitiveProxy, int propertyKey,
RecordAccess<PropertyRecord,PrimitiveRecord> propertyRecords, PrimitiveRecord primitive,
long propertyId )
{
RecordProxy<PropertyRecord, PrimitiveRecord> recordChange =
propertyRecords.getOrLoad( propertyId, primitive );
PropertyRecord propRecord = recordChange.forChangingData();
if ( !propRecord.inUse() )
{
throw new IllegalStateException( "Unable to delete property[" +
propertyId + "] since it is already deleted." );
}
PropertyBlock block = propRecord.removePropertyBlock( propertyKey );
if ( block == null )
{
throw new IllegalStateException( "Property with index["
+ propertyKey
+ "] is not present in property["
+ propertyId + "]" );
}
for ( DynamicRecord valueRecord : block.getValueRecords() )
{
assert valueRecord.inUse();
valueRecord.setInUse( false, block.getType().intValue() );
propRecord.addDeletedRecord( valueRecord );
}
if ( propRecord.size() > 0 )
{
/*
* There are remaining blocks in the record. We do not unlink yet.
*/
propRecord.setChanged( primitive );
assert traverser.assertPropertyChain( primitive, propertyRecords );
}
else
{
unlinkPropertyRecord( propRecord, propertyRecords, primitiveProxy );
}
}
private <P extends PrimitiveRecord> void unlinkPropertyRecord( PropertyRecord propRecord,
RecordAccess<PropertyRecord,PrimitiveRecord> propertyRecords,
RecordProxy<P, Void> primitiveRecordChange )
{
P primitive = primitiveRecordChange.forReadingLinkage();
assert traverser.assertPropertyChain( primitive, propertyRecords );
assert propRecord.size() == 0;
long prevProp = propRecord.getPrevProp();
long nextProp = propRecord.getNextProp();
if ( primitive.getNextProp() == propRecord.getId() )
{
assert propRecord.getPrevProp() == Record.NO_PREVIOUS_PROPERTY.intValue() : propRecord
+ " for "
+ primitive;
primitiveRecordChange.forChangingLinkage().setNextProp( nextProp );
}
if ( prevProp != Record.NO_PREVIOUS_PROPERTY.intValue() )
{
PropertyRecord prevPropRecord = propertyRecords.getOrLoad( prevProp, primitive ).forChangingLinkage();
assert prevPropRecord.inUse() : prevPropRecord + "->" + propRecord
+ " for " + primitive;
prevPropRecord.setNextProp( nextProp );
prevPropRecord.setChanged( primitive );
}
if ( nextProp != Record.NO_NEXT_PROPERTY.intValue() )
{
PropertyRecord nextPropRecord = propertyRecords.getOrLoad( nextProp, primitive ).forChangingLinkage();
assert nextPropRecord.inUse() : propRecord + "->" + nextPropRecord
+ " for " + primitive;
nextPropRecord.setPrevProp( prevProp );
nextPropRecord.setChanged( primitive );
}
propRecord.setInUse( false );
/*
* The following two are not needed - the above line does all the work (PropertyStore
* does not write out the prev/next for !inUse records). It is nice to set this
* however to check for consistency when assertPropertyChain().
*/
propRecord.setPrevProp( Record.NO_PREVIOUS_PROPERTY.intValue() );
propRecord.setNextProp( Record.NO_NEXT_PROPERTY.intValue() );
propRecord.setChanged( primitive );
assert traverser.assertPropertyChain( primitive, propertyRecords );
}
}