From b35370a1bcf688ad81f9f3507396b3400c58045e Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Mon, 19 Sep 2016 15:18:42 +0200 Subject: [PATCH] Clarify alignment field in store copy protocol Rename the 'recordSize' to 'requiredElementAlignment' in the store copy protocol, to abstract ourselves from the fact that we occasionally copy over record stores with that code. The code is mean to be generic, and is indeed used to copy over other files, such as indexes and logs, and an element alignment requirement is a generic term for what we want. --- .../org/neo4j/com/storecopy/StoreCopyClient.java | 4 ++-- .../org/neo4j/com/storecopy/StoreWriter.java | 16 +++++++++++----- .../neo4j/com/storecopy/ToFileStoreWriter.java | 12 +++++++----- .../com/storecopy/ToNetworkStoreWriter.java | 4 ++-- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/enterprise/com/src/main/java/org/neo4j/com/storecopy/StoreCopyClient.java b/enterprise/com/src/main/java/org/neo4j/com/storecopy/StoreCopyClient.java index 36ef7b4c31d20..b7b59ed64adce 100644 --- a/enterprise/com/src/main/java/org/neo4j/com/storecopy/StoreCopyClient.java +++ b/enterprise/com/src/main/java/org/neo4j/com/storecopy/StoreCopyClient.java @@ -345,10 +345,10 @@ private StoreWriter decorateWithProgressIndicator( final StoreWriter actual ) @Override public long write( String path, ReadableByteChannel data, ByteBuffer temporaryBuffer, - boolean hasData, int recordSize ) throws IOException + boolean hasData, int requiredElementAlignment ) throws IOException { log.info( "Copying %s", path ); - long written = actual.write( path, data, temporaryBuffer, hasData, recordSize ); + long written = actual.write( path, data, temporaryBuffer, hasData, requiredElementAlignment ); log.info( "Copied %s %s", path, bytes( written ) ); totalFiles++; return written; diff --git a/enterprise/com/src/main/java/org/neo4j/com/storecopy/StoreWriter.java b/enterprise/com/src/main/java/org/neo4j/com/storecopy/StoreWriter.java index 06388e1406b95..009fc54790847 100644 --- a/enterprise/com/src/main/java/org/neo4j/com/storecopy/StoreWriter.java +++ b/enterprise/com/src/main/java/org/neo4j/com/storecopy/StoreWriter.java @@ -26,11 +26,17 @@ public interface StoreWriter extends Closeable { - // "hasData" is an effect of the block format not supporting a zero length block - // whereas a neostore file may actually be 0 bytes we'll have to keep track - // of that special case. - long write( String path, ReadableByteChannel data, ByteBuffer temporaryBuffer, boolean hasData, int recordSize ) - throws IOException; + /** + * Pipe the data from the given {@link ReadableByteChannel} to a location given by the {@code path}, using the + * given {@code temporaryBuffer} for buffering if necessary. + * The {@code hasData} is an effect of the block format not supporting a zero length blocks, whereas a neostore + * file may actually be 0 bytes we'll have to keep track of that special case. + * The {@code requiredElementAlignment} parameter specifies the size in bytes to which the transferred elements + * should be aligned. For record store files, this is the record size. For files that have no special alignment + * requirements, you should use the value {@code 1} to signify that any alignment will do. + */ + long write( String path, ReadableByteChannel data, ByteBuffer temporaryBuffer, boolean hasData, + int requiredElementAlignment ) throws IOException; @Override void close(); diff --git a/enterprise/com/src/main/java/org/neo4j/com/storecopy/ToFileStoreWriter.java b/enterprise/com/src/main/java/org/neo4j/com/storecopy/ToFileStoreWriter.java index beb7874826740..c768842a09464 100644 --- a/enterprise/com/src/main/java/org/neo4j/com/storecopy/ToFileStoreWriter.java +++ b/enterprise/com/src/main/java/org/neo4j/com/storecopy/ToFileStoreWriter.java @@ -55,7 +55,7 @@ public ToFileStoreWriter( File graphDbStoreDir, StoreCopyClient.Monitor monitor, @Override public long write( String path, ReadableByteChannel data, ByteBuffer temporaryBuffer, boolean hasData, - int recordSize ) throws IOException + int requiredElementAlignment ) throws IOException { try { @@ -73,7 +73,7 @@ public long write( String path, ReadableByteChannel data, ByteBuffer temporaryBu // The reason is that we are copying to a temporary store location, and then we'll move the files later. if ( storeType.isPresent() && storeType.get().isRecordStore() ) { - int filePageSize = filePageSize( recordSize ); + int filePageSize = filePageSize( requiredElementAlignment ); try ( PagedFile pagedFile = pageCache.map( file, filePageSize, CREATE, WRITE ) ) { final long written = writeDataThroughPageCache( pagedFile, data, temporaryBuffer, hasData ); @@ -102,11 +102,13 @@ private void addPageCacheMoveAction( File file ) pageCache.renameFile( file, new File( toDir, file.getName() ), copyOptions ) ) ); } - private int filePageSize( int recordSize ) + 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 (recordSize == NO_RECORD_SIZE) ? pageCacheSize - : (pageCacheSize - (pageCacheSize % recordSize)); + return (alignment == NO_RECORD_SIZE) ? pageCacheSize + : (pageCacheSize - (pageCacheSize % alignment)); } private long writeDataThroughFileSystem( File file, ReadableByteChannel data, ByteBuffer temporaryBuffer, diff --git a/enterprise/com/src/main/java/org/neo4j/com/storecopy/ToNetworkStoreWriter.java b/enterprise/com/src/main/java/org/neo4j/com/storecopy/ToNetworkStoreWriter.java index 0f9c2233f78ef..acb0eba77b054 100644 --- a/enterprise/com/src/main/java/org/neo4j/com/storecopy/ToNetworkStoreWriter.java +++ b/enterprise/com/src/main/java/org/neo4j/com/storecopy/ToNetworkStoreWriter.java @@ -45,7 +45,7 @@ public ToNetworkStoreWriter( ChannelBuffer targetBuffer, Monitors monitors ) @Override public long write( String path, ReadableByteChannel data, ByteBuffer temporaryBuffer, - boolean hasData, int recordSize ) throws IOException + boolean hasData, int requiredElementAlignment ) throws IOException { char[] chars = path.toCharArray(); targetBuffer.writeShort( chars.length ); @@ -56,7 +56,7 @@ public long write( String path, ReadableByteChannel data, ByteBuffer temporaryBu long totalWritten = Short.BYTES + chars.length* Character.BYTES + Byte.BYTES; if ( hasData ) { - targetBuffer.writeInt( recordSize ); + targetBuffer.writeInt( requiredElementAlignment ); totalWritten += Integer.BYTES; totalWritten += buffer.write( data ); buffer.close();