-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ToFileStoreWriter.java
166 lines (152 loc) · 6.44 KB
/
ToFileStoreWriter.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* 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 Affero General Public License for more details.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.com.storecopy;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.List;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.PagedFile;
import org.neo4j.kernel.impl.store.StoreType;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.WRITE;
import static org.neo4j.kernel.impl.store.format.RecordFormat.NO_RECORD_SIZE;
public class ToFileStoreWriter implements StoreWriter
{
private final File basePath;
private final FileSystemAbstraction fs;
private final StoreCopyClientMonitor monitor;
private final PageCache pageCache;
private final List<FileMoveAction> fileMoveActions;
public ToFileStoreWriter( File graphDbStoreDir, FileSystemAbstraction fs,
StoreCopyClientMonitor storeCopyClientMonitor, PageCache pageCache, List<FileMoveAction> fileMoveActions )
{
this.basePath = graphDbStoreDir;
this.fs = fs;
this.monitor = storeCopyClientMonitor;
this.pageCache = pageCache;
this.fileMoveActions = fileMoveActions;
}
@Override
public long write( String path, ReadableByteChannel data, ByteBuffer temporaryBuffer, boolean hasData,
int requiredElementAlignment ) throws IOException
{
try
{
temporaryBuffer.clear();
File file = new File( basePath, path );
file.getParentFile().mkdirs();
String fullFilePath = file.toString();
monitor.startReceivingStoreFile( fullFilePath );
try
{
// Note that we don't bother checking if the page cache already has a mapping for the given file.
// The reason is that we are copying to a temporary store location, and then we'll move the files later.
if ( !pageCache.fileSystemSupportsFileOperations() && StoreType.canBeManagedByPageCache( file.getName() ) )
{
int filePageSize = filePageSize( requiredElementAlignment );
try ( PagedFile pagedFile = pageCache.map( file, filePageSize, CREATE, WRITE ) )
{
final long written = writeDataThroughPageCache( pagedFile, data, temporaryBuffer, hasData );
addPageCacheMoveAction( file );
return written;
}
}
// We don't add file move actions for these files. The reason is that we will perform the file moves
// *after* we have done recovery on the store, and this may delete some files, and add other files.
return writeDataThroughFileSystem( file, data, temporaryBuffer, hasData );
}
finally
{
monitor.finishReceivingStoreFile( fullFilePath );
}
}
catch ( Throwable t )
{
throw new IOException( t );
}
}
// As only the page cache know towards which device (block device or normal file system) it is working, we use
// the page cache later on when we want to move the files written through the page cache.
private void addPageCacheMoveAction( File file )
{
fileMoveActions.add( FileMoveAction.copyViaPageCache( file, pageCache ) );
}
private int filePageSize( int alignment )
{
// We know we are dealing with a record store at this point, so the required alignment is the record size,
// and we can use this to do the page size calculation in the same way as the stores would.
final int pageCacheSize = pageCache.pageSize();
return (alignment == NO_RECORD_SIZE) ? pageCacheSize
: (pageCacheSize - (pageCacheSize % alignment));
}
private long writeDataThroughFileSystem( File file, ReadableByteChannel data, ByteBuffer temporaryBuffer,
boolean hasData ) throws IOException
{
try ( StoreChannel channel = fs.create( file ) )
{
return writeData( data, temporaryBuffer, hasData, channel );
}
}
private long writeDataThroughPageCache( PagedFile pagedFile, ReadableByteChannel data, ByteBuffer temporaryBuffer,
boolean hasData ) throws IOException
{
try ( WritableByteChannel channel = pagedFile.openWritableByteChannel() )
{
return writeData( data, temporaryBuffer, hasData, channel );
}
}
private long writeData( ReadableByteChannel data, ByteBuffer temporaryBuffer, boolean hasData,
WritableByteChannel channel ) throws IOException
{
long totalToWrite = 0;
long totalWritten = 0;
if ( hasData )
{
while ( data.read( temporaryBuffer ) >= 0 )
{
temporaryBuffer.flip();
totalToWrite += temporaryBuffer.limit();
int bytesWritten;
while ( (totalWritten += bytesWritten = channel.write( temporaryBuffer )) < totalToWrite )
{
if ( bytesWritten < 0 )
{
throw new IOException( "Unable to write to disk, reported bytes written was " + bytesWritten );
}
}
temporaryBuffer.clear();
}
}
return totalWritten;
}
@Override
public void close()
{
// Do nothing
}
}