-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
8 changed files
with
242 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[edgedb] | ||
server-version = "3.0" | ||
server-version = "4.0" |
56 changes: 56 additions & 0 deletions
56
src/driver/src/main/java/com/edgedb/driver/binary/codecs/MultiRangeCodec.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,56 @@ | ||
package com.edgedb.driver.binary.codecs; | ||
|
||
import com.edgedb.driver.binary.PacketReader; | ||
import com.edgedb.driver.binary.PacketWriter; | ||
import com.edgedb.driver.binary.protocol.common.descriptors.CodecMetadata; | ||
import com.edgedb.driver.datatypes.MultiRange; | ||
import com.edgedb.driver.datatypes.Range; | ||
import com.edgedb.driver.exceptions.EdgeDBException; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.naming.OperationNotSupportedException; | ||
import java.util.UUID; | ||
|
||
public final class MultiRangeCodec<T> extends CodecBase<MultiRange<T>> { | ||
private final RangeCodec<T> rangeCodec; | ||
|
||
@SuppressWarnings("unchecked") | ||
public MultiRangeCodec(UUID id, @Nullable CodecMetadata metadata, Class<?> cls, Codec<T> elementCodec) { | ||
super(id, metadata, (Class<MultiRange<T>>)cls); | ||
this.rangeCodec = new RangeCodec<>(id, metadata, cls, elementCodec); | ||
} | ||
|
||
@Override | ||
public void serialize(PacketWriter writer, @Nullable MultiRange<T> value, CodecContext context) throws OperationNotSupportedException, EdgeDBException { | ||
if(value == null) { | ||
return; | ||
} | ||
|
||
writer.write(value.length); | ||
|
||
for(int i = 0; i != value.length; i++) { | ||
var element = value.get(i); | ||
writer.writeDelegateWithLength(w -> rangeCodec.serialize(w, element, context)); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public @Nullable MultiRange<T> deserialize(PacketReader reader, CodecContext context) throws EdgeDBException, OperationNotSupportedException { | ||
var length = reader.readInt32(); | ||
|
||
if(length == 0) { | ||
return MultiRange.empty(); | ||
} | ||
|
||
var elements = new Range[length]; | ||
|
||
for(int i = 0; i != length; i++) { | ||
try(var scoped = reader.scopedSlice(reader.readInt32())) { | ||
elements[i] = rangeCodec.deserialize(scoped, context); | ||
} | ||
} | ||
|
||
return new MultiRange<T>(elements); | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
.../src/main/java/com/edgedb/driver/binary/protocol/v2/descriptors/MultiRangeDescriptor.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,43 @@ | ||
package com.edgedb.driver.binary.protocol.v2.descriptors; | ||
|
||
import com.edgedb.driver.binary.PacketReader; | ||
import com.edgedb.driver.binary.codecs.Codec; | ||
import com.edgedb.driver.binary.protocol.TypeDescriptor; | ||
import com.edgedb.driver.binary.protocol.TypeDescriptorInfo; | ||
import com.edgedb.driver.binary.protocol.common.descriptors.CodecMetadata; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.joou.UShort; | ||
|
||
import java.util.UUID; | ||
import java.util.function.Function; | ||
|
||
public class MultiRangeDescriptor implements TypeDescriptor, MetadataDescriptor { | ||
|
||
public final UUID id; | ||
public final String name; | ||
public final boolean isSchemaDefined; | ||
public final UShort[] ancestors; | ||
public final UShort type; | ||
|
||
public MultiRangeDescriptor(UUID id, PacketReader reader) { | ||
this.id = id; | ||
this.name = reader.readString(); | ||
this.isSchemaDefined = reader.readBoolean(); | ||
this.ancestors = reader.readArrayOf(UShort.class, PacketReader::readUInt16, UShort.class); | ||
this.type = reader.readUInt16(); | ||
} | ||
|
||
@Override | ||
public UUID getId() { | ||
return this.id; | ||
} | ||
|
||
@Override | ||
public @Nullable CodecMetadata getMetadata(Function<Integer, Codec<?>> getRelativeCodec, Function<Integer, TypeDescriptorInfo<?>> getRelativeDescriptor) { | ||
return new CodecMetadata( | ||
name, | ||
isSchemaDefined, | ||
MetadataDescriptor.constructAncestors(ancestors, getRelativeCodec, getRelativeDescriptor) | ||
); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/driver/src/main/java/com/edgedb/driver/datatypes/MultiRange.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,80 @@ | ||
package com.edgedb.driver.datatypes; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
|
||
/** | ||
* Represents the {@code multirange} type in EdgeDB | ||
* @param <T> The inner type of the multirange. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public class MultiRange<T> { | ||
private static final Range<?>[] EMPTY_RANGE_ARRAY = new Range<?>[0]; | ||
private static final MultiRange<?> EMPTY_MULTI_RANGE = new MultiRange<>(); | ||
|
||
/** | ||
* Gets the length of this multirange. | ||
*/ | ||
public final int length; | ||
|
||
private final Range<T>[] ranges; | ||
|
||
/** | ||
* Constructs a new empty multirange | ||
*/ | ||
public MultiRange() { | ||
ranges = (Range<T>[]) EMPTY_RANGE_ARRAY; | ||
length = 0; | ||
} | ||
|
||
/** | ||
* Constructs a new multirange with the provided elements. | ||
* @param elements The elements to construct the multirange with. | ||
*/ | ||
public MultiRange(Collection<? extends Range<T>> elements) { | ||
ranges = elements.toArray((Range<T>[])EMPTY_RANGE_ARRAY); | ||
length = ranges.length; | ||
} | ||
|
||
public MultiRange(Range<T>[] elements) { | ||
ranges = elements.clone(); | ||
length = elements.length; | ||
} | ||
|
||
|
||
/** | ||
* Gets an element within this multirange by index. | ||
* @param i The index of the element to get. | ||
* @return The element at the specified index. | ||
*/ | ||
public Range<T> get(int i) throws IndexOutOfBoundsException { | ||
return ranges[i]; | ||
} | ||
|
||
/** | ||
* Converts this multirange into a hashset. | ||
* @return A hashset representing this multirange. | ||
*/ | ||
public HashSet<Range<T>> toSet() { | ||
return new HashSet<>(Arrays.asList(ranges)); | ||
} | ||
|
||
public static <U> MultiRange<U> empty(Class<U> cls) { | ||
return new MultiRange<>(); | ||
} | ||
|
||
public static <U> MultiRange<U> empty() { | ||
return (MultiRange<U>) EMPTY_MULTI_RANGE; | ||
} | ||
|
||
/** | ||
* Gets a {@linkplain Class} that represents a multirange of a specified type. | ||
* @param cls The inner type of the multirange to represent. | ||
* @return A class that represents a multirange of the provided type. | ||
* @param <U> The inner type of the multirange. | ||
*/ | ||
public static <U> Class<MultiRange<U>> ofType(Class<U> cls) { | ||
return (Class<MultiRange<U>>) EMPTY_MULTI_RANGE.getClass(); | ||
} | ||
} |
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