-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NeoStoreFileListingTest.java
247 lines (223 loc) · 9.73 KB
/
NeoStoreFileListingTest.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
/*
* Copyright (c) 2002-2017 "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.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.kernel.NeoStoreDataSource;
import org.neo4j.kernel.api.labelscan.LabelScanStore;
import org.neo4j.kernel.impl.api.ExplicitIndexProviderLookup;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.impl.store.StoreType;
import org.neo4j.kernel.impl.storemigration.LogFiles;
import org.neo4j.kernel.impl.transaction.log.PhysicalLogFile;
import org.neo4j.storageengine.api.StorageEngine;
import org.neo4j.storageengine.api.StoreFileMetadata;
import org.neo4j.test.rule.EmbeddedDatabaseRule;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.neo4j.helpers.collection.Iterators.asResourceIterator;
public class NeoStoreFileListingTest
{
@Rule
public EmbeddedDatabaseRule db = new EmbeddedDatabaseRule();
private NeoStoreDataSource neoStoreDataSource;
private static final String[] STANDARD_STORE_DIR_FILES = new String[]{
"index",
"lock",
"debug.log",
"neostore",
"neostore.id",
"neostore.counts.db.a",
"neostore.counts.db.b",
"neostore.labelscanstore.db",
"neostore.labeltokenstore.db",
"neostore.labeltokenstore.db.id",
"neostore.labeltokenstore.db.names",
"neostore.labeltokenstore.db.names.id",
"neostore.nodestore.db",
"neostore.nodestore.db.id",
"neostore.nodestore.db.labels",
"neostore.nodestore.db.labels.id",
"neostore.propertystore.db",
"neostore.propertystore.db.arrays",
"neostore.propertystore.db.arrays.id",
"neostore.propertystore.db.id",
"neostore.propertystore.db.index",
"neostore.propertystore.db.index.id",
"neostore.propertystore.db.index.keys",
"neostore.propertystore.db.index.keys.id",
"neostore.propertystore.db.strings",
"neostore.propertystore.db.strings.id",
"neostore.relationshipgroupstore.db",
"neostore.relationshipgroupstore.db.id",
"neostore.relationshipstore.db",
"neostore.relationshipstore.db.id",
"neostore.relationshiptypestore.db",
"neostore.relationshiptypestore.db.id",
"neostore.relationshiptypestore.db.names",
"neostore.relationshiptypestore.db.names.id",
"neostore.schemastore.db",
"neostore.schemastore.db.id",
PhysicalLogFile.DEFAULT_NAME + ".active",
PhysicalLogFile.DEFAULT_NAME + PhysicalLogFile.DEFAULT_VERSION_SUFFIX + "0",
PhysicalLogFile.DEFAULT_NAME + PhysicalLogFile.DEFAULT_VERSION_SUFFIX + "1",
PhysicalLogFile.DEFAULT_NAME + PhysicalLogFile.DEFAULT_VERSION_SUFFIX + "2",
"store_lock"};
private static final String[] STANDARD_STORE_DIR_DIRECTORIES = new String[]{"schema", "index", "branched"};
private static final Function<StoreFileMetadata,Optional<StoreType>> toStoreType =
fileMetaData -> StoreType.typeOf( fileMetaData.file().getName() );
@Before
public void setUp() throws IOException
{
createIndexDbFile();
neoStoreDataSource = db.getDependencyResolver().resolveDependency( NeoStoreDataSource.class );
}
private void createIndexDbFile() throws IOException
{
File storeDir = db.getStoreDir();
final File indexFile = new File( storeDir, "index.db" );
if ( !indexFile.exists() )
{
assertTrue( indexFile.createNewFile() );
}
}
@Test
public void shouldCloseIndexAndLabelScanSnapshots() throws Exception
{
// Given
LabelScanStore labelScanStore = mock( LabelScanStore.class );
IndexingService indexingService = mock( IndexingService.class );
ExplicitIndexProviderLookup explicitIndexes = mock( ExplicitIndexProviderLookup.class );
when( explicitIndexes.all() ).thenReturn( Collections.emptyList() );
File storeDir = mock( File.class );
filesInStoreDirAre( storeDir, STANDARD_STORE_DIR_FILES, STANDARD_STORE_DIR_DIRECTORIES );
StorageEngine storageEngine = mock( StorageEngine.class );
NeoStoreFileListing fileListing = new NeoStoreFileListing(
storeDir, labelScanStore, indexingService, explicitIndexes, storageEngine );
ResourceIterator<File> scanSnapshot = scanStoreFilesAre( labelScanStore,
new String[]{"blah/scan.store", "scan.more"} );
ResourceIterator<File> indexSnapshot = indexFilesAre( indexingService, new String[]{"schema/index/my.index"} );
ResourceIterator<StoreFileMetadata> result = fileListing.listStoreFiles( false );
// When
result.close();
// Then
verify( scanSnapshot ).close();
verify( indexSnapshot ).close();
}
@Test
public void shouldListMetaDataStoreLast() throws Exception
{
assertTrue( neoStoreDataSource.listStoreFiles( false ).stream()
.reduce( ( a, b ) -> b )
.flatMap( toStoreType )
.filter( StoreType.META_DATA::equals )
.isPresent() );
}
@Test
public void shouldListMetaDataStoreLastWithTxLogs() throws Exception
{
assertTrue( neoStoreDataSource.listStoreFiles( true ).stream()
.reduce( ( a, b ) -> b )
.flatMap( toStoreType )
.filter( StoreType.META_DATA::equals )
.isPresent() );
}
@Test
public void shouldListTxLogFiles() throws Exception
{
assertTrue( neoStoreDataSource.listStoreFiles( true ).stream()
.map( metaData -> metaData.file().getName() )
.anyMatch( fileName -> LogFiles.FILENAME_FILTER.accept( null, fileName ) ) );
}
@Test
public void shouldNotListTxLogFiles() throws Exception
{
assertTrue( neoStoreDataSource.listStoreFiles( false ).stream()
.map( metaData -> metaData.file().getName() )
.noneMatch( fileName -> LogFiles.FILENAME_FILTER.accept( null, fileName ) ) );
}
@Test
public void shouldListNeostoreFiles() throws Exception
{
StoreType[] values = StoreType.values();
ResourceIterator<StoreFileMetadata> storeFiles =
neoStoreDataSource.listStoreFiles( false );
List<StoreType> listedStoreFiles = storeFiles.stream()
.map( toStoreType )
.filter( Optional::isPresent )
.map( Optional::get )
.collect( Collectors.toList() );
assertEquals( Arrays.asList(values), listedStoreFiles );
}
private void filesInStoreDirAre( File storeDir, String[] filenames, String[] dirs )
{
ArrayList<File> files = new ArrayList<>();
mockFiles( filenames, files, false );
mockFiles( dirs, files, true );
when( storeDir.listFiles() ).thenReturn( files.toArray( new File[files.size()] ) );
}
private ResourceIterator<File> scanStoreFilesAre( LabelScanStore labelScanStore, String[] fileNames )
throws IOException
{
ArrayList<File> files = new ArrayList<>();
mockFiles( fileNames, files, false );
ResourceIterator<File> snapshot = spy( asResourceIterator( files.iterator() ) );
when( labelScanStore.snapshotStoreFiles() ).thenReturn( snapshot );
return snapshot;
}
private ResourceIterator<File> indexFilesAre( IndexingService indexingService, String[] fileNames )
throws IOException
{
ArrayList<File> files = new ArrayList<>();
mockFiles( fileNames, files, false );
ResourceIterator<File> snapshot = spy( asResourceIterator( files.iterator() ) );
when( indexingService.snapshotStoreFiles() ).thenReturn( snapshot );
return snapshot;
}
private void mockFiles( String[] filenames, ArrayList<File> files, boolean isDirectories )
{
for ( String filename : filenames )
{
File file = mock( File.class );
String[] fileNameParts = filename.split( "/" );
when( file.getName() ).thenReturn( fileNameParts[fileNameParts.length - 1] );
when( file.isFile() ).thenReturn( !isDirectories );
when( file.isDirectory() ).thenReturn( isDirectories );
when( file.exists() ).thenReturn( true );
when( file.getPath() ).thenReturn( filename );
files.add( file );
}
}
}