Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A new IndexUpdateStorage for external updates
- Loading branch information
Showing
7 changed files
with
481 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/IndexUpdateCursor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright (c) 2002-2019 "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.kernel.impl.index.schema; | ||
|
||
import java.io.IOException; | ||
|
||
import org.neo4j.index.internal.gbptree.Layout; | ||
import org.neo4j.io.pagecache.PageCursor; | ||
import org.neo4j.kernel.impl.api.index.UpdateMode; | ||
import org.neo4j.kernel.impl.transaction.log.ReadAheadChannel; | ||
|
||
import static org.neo4j.kernel.impl.index.schema.IndexUpdateStorage.STOP_TYPE; | ||
|
||
public class IndexUpdateCursor<KEY, VALUE> implements BlockEntryCursor<KEY,VALUE> | ||
{ | ||
private final ReadAheadChannel channel; | ||
private final PageCursor cursor; | ||
private final Layout<KEY,VALUE> layout; | ||
|
||
// Fields for the last entry | ||
private UpdateMode updateMode; | ||
private KEY key1; | ||
private KEY key2; | ||
private VALUE value; | ||
|
||
public IndexUpdateCursor( ReadAheadChannel channel, PageCursor cursor, Layout<KEY,VALUE> layout ) | ||
{ | ||
this.channel = channel; | ||
this.cursor = cursor; | ||
this.layout = layout; | ||
this.key1 = layout.newKey(); | ||
this.key2 = layout.newKey(); | ||
this.value = layout.newValue(); | ||
} | ||
|
||
@Override | ||
public boolean next() throws IOException | ||
{ | ||
byte updateModeType = cursor.getByte(); | ||
if ( updateModeType == STOP_TYPE ) | ||
{ | ||
return false; | ||
} | ||
|
||
updateMode = UpdateMode.MODES[updateModeType]; | ||
IndexUpdateEntry.read( cursor, layout, updateMode, key1, key2, value ); | ||
return true; | ||
} | ||
|
||
@Override | ||
public KEY key() | ||
{ | ||
return key1; | ||
} | ||
|
||
@Override | ||
public VALUE value() | ||
{ | ||
return value; | ||
} | ||
|
||
public KEY key2() | ||
{ | ||
return key2; | ||
} | ||
|
||
public UpdateMode updateMode() | ||
{ | ||
return updateMode; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException | ||
{ | ||
cursor.close(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/IndexUpdateEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2002-2019 "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.kernel.impl.index.schema; | ||
|
||
import org.neo4j.index.internal.gbptree.Layout; | ||
import org.neo4j.io.pagecache.PageCursor; | ||
import org.neo4j.kernel.impl.api.index.UpdateMode; | ||
|
||
public class IndexUpdateEntry | ||
{ | ||
public static <KEY, VALUE> void read( PageCursor cursor, Layout<KEY,VALUE> layout, UpdateMode updateMode, KEY key1, KEY key2, VALUE value ) | ||
{ | ||
switch ( updateMode ) | ||
{ | ||
case ADDED: | ||
BlockEntry.read( cursor, layout, key1, value ); | ||
break; | ||
case REMOVED: | ||
BlockEntry.read( cursor, layout, key1 ); | ||
break; | ||
case CHANGED: | ||
BlockEntry.read( cursor, layout, key1 ); | ||
BlockEntry.read( cursor, layout, key2, value ); | ||
break; | ||
default: | ||
throw new IllegalArgumentException( "Unknown update mode " + updateMode ); | ||
} | ||
} | ||
|
||
public static <KEY,VALUE> void write( PageCursor cursor, Layout<KEY,VALUE> layout, UpdateMode updateMode, KEY key1, KEY key2, VALUE value ) | ||
{ | ||
switch ( updateMode ) | ||
{ | ||
case ADDED: | ||
BlockEntry.write( cursor, layout, key1, value ); | ||
break; | ||
case REMOVED: | ||
BlockEntry.write( cursor, layout, key1 ); | ||
break; | ||
case CHANGED: | ||
BlockEntry.write( cursor, layout, key1 ); | ||
BlockEntry.write( cursor, layout, key2, value ); | ||
break; | ||
default: | ||
throw new IllegalArgumentException( "Unknown update mode " + updateMode ); | ||
} | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/IndexUpdateStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (c) 2002-2019 "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.kernel.impl.index.schema; | ||
|
||
import java.io.Closeable; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
import org.neo4j.index.internal.gbptree.Layout; | ||
import org.neo4j.io.fs.FileSystemAbstraction; | ||
import org.neo4j.io.fs.OpenMode; | ||
import org.neo4j.io.fs.StoreChannel; | ||
import org.neo4j.io.pagecache.ByteArrayPageCursor; | ||
import org.neo4j.io.pagecache.PageCursor; | ||
import org.neo4j.kernel.api.index.IndexEntryUpdate; | ||
import org.neo4j.kernel.impl.api.index.UpdateMode; | ||
import org.neo4j.kernel.impl.transaction.log.ReadAheadChannel; | ||
|
||
import static org.neo4j.kernel.impl.index.schema.NativeIndexUpdater.initializeKeyAndValueFromUpdate; | ||
import static org.neo4j.kernel.impl.index.schema.NativeIndexUpdater.initializeKeyFromUpdate; | ||
|
||
public class IndexUpdateStorage<KEY extends NativeIndexKey<KEY>,VALUE extends NativeIndexValue> implements Closeable | ||
{ | ||
private static final int TYPE_SIZE = Byte.BYTES; | ||
static final byte STOP_TYPE = -1; | ||
|
||
private final Layout<KEY,VALUE> layout; | ||
private final FileSystemAbstraction fs; | ||
private final File file; | ||
private final ByteBuffer buffer; | ||
private final ByteArrayPageCursor pageCursor; | ||
private final StoreChannel storeChannel; | ||
private final KEY key1; | ||
private final KEY key2; | ||
private final VALUE value; | ||
|
||
IndexUpdateStorage( Layout<KEY,VALUE> layout, FileSystemAbstraction fs, File file, ByteBuffer buffer ) throws IOException | ||
{ | ||
this.layout = layout; | ||
this.fs = fs; | ||
this.file = file; | ||
this.buffer = buffer; | ||
this.pageCursor = new ByteArrayPageCursor( buffer ); | ||
this.storeChannel = fs.create( file ); | ||
this.key1 = layout.newKey(); | ||
this.key2 = layout.newKey(); | ||
this.value = layout.newValue(); | ||
} | ||
|
||
public void add( IndexEntryUpdate<?> update ) throws IOException | ||
{ | ||
int entrySize = TYPE_SIZE; | ||
UpdateMode updateMode = update.updateMode(); | ||
switch ( updateMode ) | ||
{ | ||
case ADDED: | ||
initializeKeyAndValueFromUpdate( key1, value, update.getEntityId(), update.values() ); | ||
entrySize += BlockEntry.entrySize( layout, key1, value ); | ||
break; | ||
case REMOVED: | ||
initializeKeyFromUpdate( key1, update.getEntityId(), update.values() ); | ||
entrySize += BlockEntry.keySize( layout, key1 ); | ||
break; | ||
case CHANGED: | ||
initializeKeyFromUpdate( key1, update.getEntityId(), update.beforeValues() ); | ||
initializeKeyAndValueFromUpdate( key2, value, update.getEntityId(), update.values() ); | ||
entrySize += BlockEntry.keySize( layout, key1 ) + BlockEntry.entrySize( layout, key2, value ); | ||
break; | ||
default: | ||
throw new IllegalArgumentException( "Unknown update mode " + updateMode ); | ||
} | ||
|
||
if ( entrySize > buffer.remaining() ) | ||
{ | ||
flush(); | ||
} | ||
|
||
pageCursor.putByte( (byte) updateMode.ordinal() ); | ||
IndexUpdateEntry.write( pageCursor, layout, updateMode, key1, key2, value ); | ||
} | ||
|
||
public void doneAdding() throws IOException | ||
{ | ||
if ( buffer.remaining() < TYPE_SIZE ) | ||
{ | ||
flush(); | ||
} | ||
pageCursor.putByte( STOP_TYPE ); | ||
flush(); | ||
} | ||
|
||
public IndexUpdateCursor<KEY,VALUE> reader() throws IOException | ||
{ | ||
ReadAheadChannel<StoreChannel> channel = new ReadAheadChannel<>( fs.open( file, OpenMode.READ ) ); | ||
PageCursor pageCursor = new ReadableChannelPageCursor( channel ); | ||
return new IndexUpdateCursor<>( channel, pageCursor, layout ); | ||
} | ||
|
||
private void flush() throws IOException | ||
{ | ||
buffer.flip(); | ||
storeChannel.write( buffer ); | ||
buffer.clear(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException | ||
{ | ||
storeChannel.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.