Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4396b39
commit aef8091
Showing
4 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
...k/src/main/java/net/sansa_stack/query/spark/ontop/kryo/ShadedImmutableListSerializer.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
66 changes: 66 additions & 0 deletions
66
.../src/main/java/net/sansa_stack/query/spark/ontop/kryo/ShadedImmutableTableSerializer.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,66 @@ | ||
package net.sansa_stack.query.spark.ontop.kryo; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
|
||
import it.unibz.inf.ontop.com.google.common.collect.ImmutableTable; | ||
|
||
/** | ||
* A kryo {@link Serializer} for guava-libraries {@link ImmutableTable}. | ||
*/ | ||
public class ShadedImmutableTableSerializer<R, C, V> extends ShadedTableSerializerBase<R, C, V, ImmutableTable<R, C, V>> { | ||
|
||
private static final boolean HANDLES_NULL = false; | ||
private static final boolean IMMUTABLE = true; | ||
|
||
public ShadedImmutableTableSerializer() { | ||
super(HANDLES_NULL, IMMUTABLE); | ||
} | ||
|
||
@Override | ||
public void write(Kryo kryo, Output output, ImmutableTable<R, C, V> immutableTable) { | ||
super.writeTable(kryo, output, immutableTable); | ||
} | ||
|
||
@Override | ||
public ImmutableTable<R, C, V> read(Kryo kryo, Input input, Class<ImmutableTable<R, C, V>> type) { | ||
final ImmutableTable.Builder<R, C, V> builder = ImmutableTable.builder(); | ||
super.readTable(kryo, input, new CellConsumer<R, C, V>() { | ||
@Override | ||
public void accept(R r, C c, V v) { | ||
builder.put(r, c, v); | ||
} | ||
}); | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ShadedImmutableTableSerializer} and registers its serializer. | ||
* | ||
* @param kryo the {@link Kryo} instance to set the serializer on | ||
*/ | ||
public static void registerSerializers(final Kryo kryo) { | ||
|
||
// ImmutableTable (abstract class) | ||
// +- SparseImmutableTable | ||
// | SparseImmutableTable | ||
// +- DenseImmutableTable | ||
// | Used when more than half of the cells have values | ||
// +- SingletonImmutableTable | ||
// | Optimized for Table with only 1 element. | ||
|
||
final ShadedImmutableTableSerializer serializer = new ShadedImmutableTableSerializer(); | ||
|
||
kryo.register(ImmutableTable.class, serializer); // ImmutableTable | ||
kryo.register(ImmutableTable.of().getClass(), serializer); // SparseImmutableTable | ||
kryo.register(ImmutableTable.of(1, 2, 3).getClass(), serializer); // SingletonImmutableTable | ||
|
||
kryo.register(ImmutableTable.builder() | ||
.put("a", 1, 1) | ||
.put("b", 1, 1) | ||
.build().getClass(), serializer); // DenseImmutableTable | ||
|
||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...spark/src/main/java/net/sansa_stack/query/spark/ontop/kryo/ShadedTableSerializerBase.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,57 @@ | ||
package net.sansa_stack.query.spark.ontop.kryo; | ||
|
||
import java.util.Map; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
import it.unibz.inf.ontop.com.google.common.collect.Table; | ||
|
||
public abstract class ShadedTableSerializerBase<R, C, V, T extends Table<R, C, V>> extends Serializer<T> { | ||
|
||
public ShadedTableSerializerBase(boolean doesNotAcceptNull, boolean immutable) { | ||
super(doesNotAcceptNull, immutable); | ||
} | ||
|
||
public void writeTable(Kryo kryo, Output output, Table<R, C, V> table) { | ||
Map<R, Map<C, V>> rowMap = table.rowMap(); | ||
output.writeInt(rowMap.size(), true); | ||
for (R r : rowMap.keySet()) { | ||
kryo.writeClassAndObject(output, r); | ||
Map<C, V> colMap = rowMap.get(r); | ||
output.writeInt(colMap.size(), true); | ||
for (C c : colMap.keySet()) { | ||
V v = colMap.get(c); | ||
kryo.writeClassAndObject(output, c); | ||
kryo.writeClassAndObject(output, v); | ||
} | ||
} | ||
} | ||
|
||
public void readTable(Kryo kryo, Input input, final Table<R, C, V> table) { | ||
this.readTable(kryo, input, new CellConsumer<R, C, V>() { | ||
@Override | ||
public void accept(R r, C c, V v) { | ||
table.put(r, c, v); | ||
} | ||
}); | ||
} | ||
|
||
public void readTable(Kryo kryo, Input input, CellConsumer<R, C, V> cellConsumer) { | ||
int rows = input.readInt(true); | ||
for (int i = 0; i < rows; i++) { | ||
R r = (R) kryo.readClassAndObject(input); | ||
int cols = input.readInt(true); | ||
for (int j = 0; j < cols; j++) { | ||
C c = (C) kryo.readClassAndObject(input); | ||
V v = (V) kryo.readClassAndObject(input); | ||
cellConsumer.accept(r, c, v); | ||
} | ||
} | ||
} | ||
|
||
interface CellConsumer<R, C, V> { | ||
void accept(R rowKey, C columnKey, V value); | ||
} | ||
} |
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