-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LegacyIndexTransactionStateImpl.java
263 lines (240 loc) · 9.55 KB
/
LegacyIndexTransactionStateImpl.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
/*
* 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.api.state;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.neo4j.function.Function;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.index.IndexCommandFactory;
import org.neo4j.graphdb.index.IndexImplementation;
import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.graphdb.index.LegacyIndexProviderTransaction;
import org.neo4j.kernel.api.LegacyIndex;
import org.neo4j.kernel.api.exceptions.legacyindex.LegacyIndexNotFoundKernelException;
import org.neo4j.kernel.api.txstate.LegacyIndexTransactionState;
import org.neo4j.kernel.impl.index.IndexCommand;
import org.neo4j.kernel.impl.index.IndexCommand.AddNodeCommand;
import org.neo4j.kernel.impl.index.IndexCommand.AddRelationshipCommand;
import org.neo4j.kernel.impl.index.IndexCommand.CreateCommand;
import org.neo4j.kernel.impl.index.IndexCommand.DeleteCommand;
import org.neo4j.kernel.impl.index.IndexCommand.RemoveCommand;
import org.neo4j.kernel.impl.index.IndexConfigStore;
import org.neo4j.kernel.impl.index.IndexDefineCommand;
import org.neo4j.kernel.impl.index.IndexEntityType;
import org.neo4j.kernel.impl.transaction.command.Command;
import org.neo4j.kernel.impl.transaction.state.TransactionRecordState;
/**
* Provides access to {@link LegacyIndex indexes}. Holds transaction state for all providers in a transaction.
* A equivalent to TransactionRecordState, but for legacy indexes.
*
* @see TransactionRecordState
*/
public class LegacyIndexTransactionStateImpl implements LegacyIndexTransactionState, IndexCommandFactory
{
private final Map<String, LegacyIndexProviderTransaction> transactions = new HashMap<>();
private final IndexConfigStore indexConfigStore;
private final Function<String,IndexImplementation> providerLookup;
// Commands
private IndexDefineCommand defineCommand;
private final Map<String, List<IndexCommand>> nodeCommands = new HashMap<>();
private final Map<String, List<IndexCommand>> relationshipCommands = new HashMap<>();
public LegacyIndexTransactionStateImpl( IndexConfigStore indexConfigStore,
Function<String,IndexImplementation> providerLookup )
{
this.indexConfigStore = indexConfigStore;
this.providerLookup = providerLookup;
}
@Override
public LegacyIndex nodeChanges( String indexName ) throws LegacyIndexNotFoundKernelException
{
Map<String, String> configuration = indexConfigStore.get( Node.class, indexName );
if ( configuration == null )
{
throw new LegacyIndexNotFoundKernelException( "Node index '" + indexName + " not found" );
}
String providerName = configuration.get( IndexManager.PROVIDER );
IndexImplementation provider = providerLookup.apply( providerName );
LegacyIndexProviderTransaction transaction = transactions.get( providerName );
if ( transaction == null )
{
transactions.put( providerName, transaction = provider.newTransaction( this ) );
}
return transaction.nodeIndex( indexName, configuration );
}
@Override
public LegacyIndex relationshipChanges( String indexName ) throws LegacyIndexNotFoundKernelException
{
Map<String, String> configuration = indexConfigStore.get( Relationship.class, indexName );
if ( configuration == null )
{
throw new LegacyIndexNotFoundKernelException( "Relationship index '" + indexName + " not found" );
}
String providerName = configuration.get( IndexManager.PROVIDER );
IndexImplementation provider = providerLookup.apply( providerName );
LegacyIndexProviderTransaction transaction = transactions.get( providerName );
if ( transaction == null )
{
transactions.put( providerName, transaction = provider.newTransaction( this ) );
}
return transaction.relationshipIndex( indexName, configuration );
}
@Override
public void extractCommands( Collection<Command> target )
{
if ( defineCommand != null )
{
target.add( defineCommand );
extractCommands( target, nodeCommands );
extractCommands( target, relationshipCommands );
}
for ( LegacyIndexProviderTransaction providerTransaction : transactions.values() )
{
providerTransaction.close();
}
}
private void extractCommands( Collection<Command> target, Map<String, List<IndexCommand>> commandMap )
{
if ( commandMap != null )
{
for ( List<IndexCommand> commands : commandMap.values() )
{
target.addAll( commands );
}
}
}
// Methods for adding commands
private IndexDefineCommand definitions()
{
if ( defineCommand == null )
{
defineCommand = new IndexDefineCommand();
}
return defineCommand;
}
private void addCommand( String indexName, IndexCommand command )
{
addCommand( indexName, command, false );
}
private void addCommand( String indexName, IndexCommand command, boolean clearFirst )
{
List<IndexCommand> commands = null;
if ( command.getEntityType() == IndexEntityType.Node.id() )
{
commands = nodeCommands.get( indexName );
if ( commands == null )
{
nodeCommands.put( indexName, commands = new ArrayList<>() );
}
}
else if ( command.getEntityType() == IndexEntityType.Relationship.id() )
{
commands = relationshipCommands.get( indexName );
if ( commands == null )
{
relationshipCommands.put( indexName, commands = new ArrayList<>() );
}
}
else
{
throw new IllegalArgumentException( "" + command.getEntityType() );
}
if ( clearFirst )
{
commands.clear();
}
commands.add( command );
}
@Override
public void addNode( String indexName, long id, String key, Object value )
{
AddNodeCommand command = new AddNodeCommand();
command.init( definitions().getOrAssignIndexNameId( indexName ),
id, definitions().getOrAssignKeyId( key ), value );
addCommand( indexName, command );
}
@Override
public void addRelationship( String indexName, long id, String key, Object value,
long startNode, long endNode )
{
AddRelationshipCommand command = new AddRelationshipCommand();
command.init( definitions().getOrAssignIndexNameId( indexName ),
id, definitions().getOrAssignKeyId( key ), value, startNode, endNode );
addCommand( indexName, command );
}
@Override
public void removeNode( String indexName, long id,
String keyOrNull, Object valueOrNull )
{
RemoveCommand command = new RemoveCommand();
command.init( definitions().getOrAssignIndexNameId( indexName ),
IndexEntityType.Node.id(), id, definitions().getOrAssignKeyId( keyOrNull ), valueOrNull );
addCommand( indexName, command );
}
@Override
public void removeRelationship( String indexName, long id,
String keyOrNull, Object valueOrNull )
{
RemoveCommand command = new RemoveCommand();
command.init( definitions().getOrAssignIndexNameId( indexName ),
IndexEntityType.Relationship.id(), id, definitions().getOrAssignKeyId( keyOrNull ), valueOrNull );
addCommand( indexName, command );
}
@Override
public void deleteIndex( IndexEntityType entityType, String indexName )
{
DeleteCommand command = new DeleteCommand();
command.init( definitions().getOrAssignIndexNameId( indexName ), entityType.id() );
addCommand( indexName, command, true );
}
@Override
public void createIndex( IndexEntityType entityType, String indexName, Map<String, String> config )
{
CreateCommand command = new CreateCommand();
command.init( definitions().getOrAssignIndexNameId( indexName ), entityType.id(), config );
addCommand( indexName, command );
}
@Override
public boolean hasChanges()
{
return defineCommand != null;
}
/** Set this data structure to it's initial state, allowing it to be re-used as if it had just been new'ed up. */
@Override
public void clear()
{
if ( !transactions.isEmpty() )
{
transactions.clear();
}
defineCommand = null;
if ( !nodeCommands.isEmpty() )
{
nodeCommands.clear();
}
if ( !relationshipCommands.isEmpty() )
{
relationshipCommands.clear();
}
}
}