-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TemporalIndexAccessor.java
282 lines (250 loc) · 10 KB
/
TemporalIndexAccessor.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
/*
* 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.index.schema;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.helpers.collection.BoundedIterable;
import org.neo4j.helpers.collection.CombiningIterable;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.index.internal.gbptree.Layout;
import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.IOLimiter;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.api.index.IndexAccessor;
import org.neo4j.kernel.api.index.IndexPopulator;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.index.PropertyAccessor;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptor;
import org.neo4j.kernel.impl.api.index.IndexUpdateMode;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
import org.neo4j.storageengine.api.schema.IndexReader;
import static org.neo4j.helpers.collection.Iterators.concatResourceIterators;
import static org.neo4j.kernel.impl.index.schema.fusion.FusionIndexBase.forAll;
class TemporalIndexAccessor extends TemporalIndexCache<TemporalIndexAccessor.PartAccessor<?>> implements IndexAccessor
{
private final SchemaIndexDescriptor descriptor;
TemporalIndexAccessor( long indexId,
SchemaIndexDescriptor descriptor,
IndexSamplingConfig samplingConfig,
PageCache pageCache,
FileSystemAbstraction fs,
RecoveryCleanupWorkCollector recoveryCleanupWorkCollector,
IndexProvider.Monitor monitor,
TemporalIndexFiles temporalIndexFiles ) throws IOException
{
super( new PartFactory( pageCache, fs, recoveryCleanupWorkCollector, monitor, descriptor, indexId, samplingConfig, temporalIndexFiles ) );
this.descriptor = descriptor;
temporalIndexFiles.loadExistingIndexes( this );
}
@Override
public void drop() throws IOException
{
forAll( NativeSchemaIndexAccessor::drop, this );
}
@Override
public IndexUpdater newUpdater( IndexUpdateMode mode )
{
return new TemporalIndexUpdater( this, mode );
}
@Override
public void force( IOLimiter ioLimiter ) throws IOException
{
for ( NativeSchemaIndexAccessor part : this )
{
part.force( ioLimiter );
}
}
@Override
public void refresh()
{
// not required in this implementation
}
@Override
public void close() throws IOException
{
forAll( NativeSchemaIndexAccessor::close, this );
}
@Override
public IndexReader newReader()
{
return new TemporalIndexReader( descriptor, this );
}
@Override
public BoundedIterable<Long> newAllEntriesReader()
{
ArrayList<BoundedIterable<Long>> allEntriesReader = new ArrayList<>();
for ( NativeSchemaIndexAccessor<?,?> part : this )
{
allEntriesReader.add( part.newAllEntriesReader() );
}
return new BoundedIterable<Long>()
{
@Override
public long maxCount()
{
long sum = 0L;
for ( BoundedIterable<Long> part : allEntriesReader )
{
long partMaxCount = part.maxCount();
if ( partMaxCount == UNKNOWN_MAX_COUNT )
{
return UNKNOWN_MAX_COUNT;
}
sum += partMaxCount;
}
return sum;
}
@Override
public void close() throws Exception
{
forAll( BoundedIterable::close, allEntriesReader );
}
@Override
public Iterator<Long> iterator()
{
return new CombiningIterable<>( allEntriesReader ).iterator();
}
};
}
@Override
public ResourceIterator<File> snapshotFiles()
{
List<ResourceIterator<File>> snapshotFiles = new ArrayList<>();
for ( NativeSchemaIndexAccessor<?,?> part : this )
{
snapshotFiles.add( part.snapshotFiles() );
}
return concatResourceIterators( snapshotFiles.iterator() );
}
@Override
public void verifyDeferredConstraints( PropertyAccessor propertyAccessor )
{
// Not needed since uniqueness is verified automatically w/o cost for every update.
}
@Override
public boolean isDirty()
{
return Iterators.stream( iterator() ).anyMatch( NativeSchemaIndexAccessor::isDirty );
}
static class PartAccessor<KEY extends NativeSchemaKey> extends NativeSchemaIndexAccessor<KEY, NativeSchemaValue>
{
private final Layout<KEY,NativeSchemaValue> layout;
private final SchemaIndexDescriptor descriptor;
private final IndexSamplingConfig samplingConfig;
PartAccessor( PageCache pageCache,
FileSystemAbstraction fs,
TemporalIndexFiles.FileLayout<KEY> fileLayout,
RecoveryCleanupWorkCollector recoveryCleanupWorkCollector,
IndexProvider.Monitor monitor,
SchemaIndexDescriptor descriptor,
long indexId,
IndexSamplingConfig samplingConfig ) throws IOException
{
super( pageCache, fs, fileLayout.indexFile, fileLayout.layout, recoveryCleanupWorkCollector, monitor, descriptor, indexId, samplingConfig );
this.layout = fileLayout.layout;
this.descriptor = descriptor;
this.samplingConfig = samplingConfig;
}
@Override
public TemporalIndexPartReader<KEY> newReader()
{
return new TemporalIndexPartReader<>( tree, layout, samplingConfig, descriptor );
}
}
static class PartFactory implements TemporalIndexCache.Factory<PartAccessor<?>>
{
private final PageCache pageCache;
private final FileSystemAbstraction fs;
private final RecoveryCleanupWorkCollector recoveryCleanupWorkCollector;
private final IndexProvider.Monitor monitor;
private final SchemaIndexDescriptor descriptor;
private final long indexId;
private final IndexSamplingConfig samplingConfig;
private final TemporalIndexFiles temporalIndexFiles;
PartFactory( PageCache pageCache,
FileSystemAbstraction fs,
RecoveryCleanupWorkCollector recoveryCleanupWorkCollector,
IndexProvider.Monitor monitor,
SchemaIndexDescriptor descriptor,
long indexId,
IndexSamplingConfig samplingConfig,
TemporalIndexFiles temporalIndexFiles )
{
this.pageCache = pageCache;
this.fs = fs;
this.recoveryCleanupWorkCollector = recoveryCleanupWorkCollector;
this.monitor = monitor;
this.descriptor = descriptor;
this.indexId = indexId;
this.samplingConfig = samplingConfig;
this.temporalIndexFiles = temporalIndexFiles;
}
@Override
public PartAccessor<?> newDate() throws IOException
{
return createPartAccessor( temporalIndexFiles.date() );
}
@Override
public PartAccessor<?> newLocalDateTime() throws IOException
{
return createPartAccessor( temporalIndexFiles.localDateTime() );
}
@Override
public PartAccessor<?> newZonedDateTime() throws IOException
{
return createPartAccessor( temporalIndexFiles.zonedDateTime() );
}
@Override
public PartAccessor<?> newLocalTime() throws IOException
{
return createPartAccessor( temporalIndexFiles.localTime() );
}
@Override
public PartAccessor<?> newZonedTime() throws IOException
{
return createPartAccessor( temporalIndexFiles.zonedTime() );
}
@Override
public PartAccessor<?> newDuration() throws IOException
{
return createPartAccessor( temporalIndexFiles.duration() );
}
private <KEY extends NativeSchemaKey> PartAccessor<KEY> createPartAccessor( TemporalIndexFiles.FileLayout<KEY> fileLayout ) throws IOException
{
if ( !fs.fileExists( fileLayout.indexFile ) )
{
createEmptyIndex( fileLayout );
}
return new PartAccessor<>( pageCache, fs, fileLayout, recoveryCleanupWorkCollector, monitor, descriptor, indexId, samplingConfig );
}
private <KEY extends NativeSchemaKey> void createEmptyIndex( TemporalIndexFiles.FileLayout<KEY> fileLayout ) throws IOException
{
IndexPopulator populator = new TemporalIndexPopulator.PartPopulator<>( pageCache, fs, fileLayout, monitor, descriptor, indexId, samplingConfig );
populator.create();
populator.close( true );
}
}
}