-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ImpermanentGraphDatabase.java
218 lines (195 loc) · 7.81 KB
/
ImpermanentGraphDatabase.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.test;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.neo4j.graphdb.facade.GraphDatabaseFacadeFactory;
import org.neo4j.graphdb.facade.GraphDatabaseFacadeFactory.Dependencies;
import org.neo4j.graphdb.facade.embedded.EmbeddedGraphDatabase;
import org.neo4j.graphdb.factory.module.CommunityEditionModule;
import org.neo4j.graphdb.factory.module.PlatformModule;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.helpers.Service;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.extension.KernelExtensionFactory;
import org.neo4j.kernel.impl.factory.DatabaseInfo;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.logging.SimpleLogService;
import org.neo4j.kernel.internal.locker.StoreLocker;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider;
import static org.neo4j.graphdb.facade.GraphDatabaseDependencies.newDependencies;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.ephemeral;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.pagecache_memory;
import static org.neo4j.kernel.configuration.Settings.TRUE;
/**
* A database meant to be used in unit tests. It will always be empty on start.
*/
public class ImpermanentGraphDatabase extends EmbeddedGraphDatabase
{
/**
* If enabled will track unclosed database instances in tests. The place of instantiation
* will get printed in an exception with the message "Unclosed database instance".
*/
private static final boolean TRACK_UNCLOSED_DATABASE_INSTANCES = false;
private static final Map<File, Exception> startedButNotYetClosed = new ConcurrentHashMap<>();
protected static final File PATH = new File( "target/test-data/impermanent-db" );
/**
* This is deprecated. Use {@link TestGraphDatabaseFactory} instead
*/
@Deprecated
public ImpermanentGraphDatabase()
{
this( new HashMap<>() );
}
/*
* TODO this shouldn't be here. It so happens however that some tests may use the database
* directory as the path to store stuff and in this case we need to define the path explicitly,
* otherwise we end up writing outside the workspace and hence leave stuff behind.
* The other option is to explicitly remove all files present on startup. Either way,
* the fact that this discussion takes place is indication that things are inconsistent,
* since an ImpermanentGraphDatabase should not have any mention of a store directory in
* any case.
*/
public ImpermanentGraphDatabase( File storeDir )
{
this( storeDir, new HashMap<>() );
}
/**
* This is deprecated. Use {@link TestGraphDatabaseFactory} instead
*/
@Deprecated
public ImpermanentGraphDatabase( Map<String, String> params )
{
this( PATH, params );
}
/**
* This is deprecated. Use {@link TestGraphDatabaseFactory} instead
*/
@Deprecated
public ImpermanentGraphDatabase( File storeDir, Map<String, String> params )
{
this( storeDir, params,
Iterables.cast( Service.load( KernelExtensionFactory.class ) ) );
}
/**
* This is deprecated. Use {@link TestGraphDatabaseFactory} instead
*/
@Deprecated
public ImpermanentGraphDatabase( Map<String, String> params,
Iterable<KernelExtensionFactory<?>> kernelExtensions )
{
this( PATH, params, kernelExtensions );
}
/**
* This is deprecated. Use {@link TestGraphDatabaseFactory} instead
*/
@Deprecated
public ImpermanentGraphDatabase( File storeDir, Map<String, String> params,
Iterable<KernelExtensionFactory<?>> kernelExtensions )
{
this( storeDir, params, getDependencies( kernelExtensions ) );
}
private static Dependencies getDependencies( Iterable<KernelExtensionFactory<?>> kernelExtensions )
{
return newDependencies().kernelExtensions( kernelExtensions );
}
public ImpermanentGraphDatabase( File storeDir, Map<String, String> params, Dependencies dependencies )
{
super( storeDir, params, dependencies );
trackUnclosedUse( storeDir );
}
public ImpermanentGraphDatabase( File storeDir, Config config,
Dependencies dependencies )
{
super( storeDir, config, dependencies );
trackUnclosedUse( storeDir );
}
@Override
protected void create( File storeDir, Map<String, String> params, Dependencies dependencies )
{
new GraphDatabaseFacadeFactory( DatabaseInfo.COMMUNITY, CommunityEditionModule::new )
{
@Override
protected PlatformModule createPlatform( File storeDir, Config config, Dependencies dependencies,
GraphDatabaseFacade graphDatabaseFacade )
{
return new ImpermanentPlatformModule( storeDir, config, databaseInfo, dependencies,
graphDatabaseFacade );
}
}.initFacade( storeDir, params, dependencies, this );
}
private static void trackUnclosedUse( File storeDir )
{
if ( TRACK_UNCLOSED_DATABASE_INSTANCES )
{
Exception testThatDidNotCloseDb = startedButNotYetClosed.put( storeDir,
new Exception( "Unclosed database instance" ) );
if ( testThatDidNotCloseDb != null )
{
testThatDidNotCloseDb.printStackTrace();
}
}
}
@Override
public void shutdown()
{
if ( TRACK_UNCLOSED_DATABASE_INSTANCES )
{
startedButNotYetClosed.remove( databaseDirectory() );
}
super.shutdown();
}
private static Config withForcedInMemoryConfiguration( Config config )
{
config.augment( ephemeral, TRUE );
config.augmentDefaults( pagecache_memory, "8M" );
return config;
}
protected static class ImpermanentPlatformModule extends PlatformModule
{
public ImpermanentPlatformModule( File storeDir, Config config, DatabaseInfo databaseInfo,
Dependencies dependencies,
GraphDatabaseFacade graphDatabaseFacade )
{
super( storeDir, withForcedInMemoryConfiguration(config), databaseInfo, dependencies, graphDatabaseFacade );
}
@Override
protected StoreLocker createStoreLocker()
{
return new StoreLocker( fileSystem, storeDir );
}
@Override
protected FileSystemAbstraction createFileSystemAbstraction()
{
return new EphemeralFileSystemAbstraction();
}
@Override
protected LogService createLogService( LogProvider userLogProvider )
{
return new SimpleLogService( NullLogProvider.getInstance(), NullLogProvider.getInstance() );
}
}
}