Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN curl -L "https://github.com/sbt/sbt/releases/download/v1.8.2/sbt-1.8.2.tgz"
ENV PATH="/usr/local/sbt/bin:${PATH}"

# thrift
ARG THRIFT_VERSION=0.13.0
ARG THRIFT_VERSION=0.21.0
RUN apk add --no-cache \
build-base \
cmake \
Expand Down
148 changes: 148 additions & 0 deletions api/src/main/java/ai/chronon/api/thrift/EncodingUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package ai.chronon.api.thrift;

/** Utility methods for use when encoding/decoding raw data as byte arrays. */
public class EncodingUtils {

/**
* Encode <code>integer</code> as a series of 4 bytes into <code>buf</code> starting at position 0
* within that buffer.
*
* @param integer The integer to encode.
* @param buf The buffer to write to.
*/
public static final void encodeBigEndian(final int integer, final byte[] buf) {
encodeBigEndian(integer, buf, 0);
}

/**
* Encode <code>integer</code> as a series of 4 bytes into <code>buf</code> starting at position
* <code>offset</code>.
*
* @param integer The integer to encode.
* @param buf The buffer to write to.
* @param offset The offset within <code>buf</code> to start the encoding.
*/
public static final void encodeBigEndian(final int integer, final byte[] buf, int offset) {
buf[offset] = (byte) (0xff & (integer >> 24));
buf[offset + 1] = (byte) (0xff & (integer >> 16));
buf[offset + 2] = (byte) (0xff & (integer >> 8));
buf[offset + 3] = (byte) (0xff & (integer));
}

/**
* Decode a series of 4 bytes from <code>buf</code>, starting at position 0, and interpret them as
* an integer.
*
* @param buf The buffer to read from.
* @return An integer, as read from the buffer.
*/
public static final int decodeBigEndian(final byte[] buf) {
return decodeBigEndian(buf, 0);
}

/**
* Decode a series of 4 bytes from <code>buf</code>, start at <code>offset</code>, and interpret
* them as an integer.
*
* @param buf The buffer to read from.
* @param offset The offset with <code>buf</code> to start the decoding.
* @return An integer, as read from the buffer.
*/
public static final int decodeBigEndian(final byte[] buf, int offset) {
return ((buf[offset] & 0xff) << 24)
| ((buf[offset + 1] & 0xff) << 16)
| ((buf[offset + 2] & 0xff) << 8)
| ((buf[offset + 3] & 0xff));
}

/**
* Bitfield utilities. Returns true if the bit at position is set in v.
*
* @param v the value whose bit is to be checked.
* @param position the 0 based bit number indicating the bit to check.
* @return true if the bit at position is set in v.
*/
public static final boolean testBit(byte v, int position) {
return testBit((int) v, position);
}

public static final boolean testBit(short v, int position) {
return testBit((int) v, position);
}

public static final boolean testBit(int v, int position) {
return (v & (1 << position)) != 0;
}

public static final boolean testBit(long v, int position) {
return (v & (1L << position)) != 0L;
}

/**
* Returns v, with the bit at position set to zero.
*
* @param v the value whose bit is to be cleared.
* @param position the 0 based bit number indicating the bit to clear.
* @return v, with the bit at position set to zero.
*/
public static final byte clearBit(byte v, int position) {
return (byte) clearBit((int) v, position);
}

public static final short clearBit(short v, int position) {
return (short) clearBit((int) v, position);
}

public static final int clearBit(int v, int position) {
return v & ~(1 << position);
}

public static final long clearBit(long v, int position) {
return v & ~(1L << position);
}

/**
* Returns v, with the bit at position set to 1 or 0 depending on value.
*
* @param v the value whose bit is to be set.
* @param position the 0 based bit number indicating the bit to set.
* @param value if true, the given bit is set to 1; otherwise it is set to 0.
* @return v, with the bit at position set to 0 (if value is false) or 1 (if value is true).
*/
public static final byte setBit(byte v, int position, boolean value) {
return (byte) setBit((int) v, position, value);
}

public static final short setBit(short v, int position, boolean value) {
return (short) setBit((int) v, position, value);
}

public static final int setBit(int v, int position, boolean value) {
if (value) return v | (1 << position);
else return clearBit(v, position);
}

public static final long setBit(long v, int position, boolean value) {
if (value) return v | (1L << position);
else return clearBit(v, position);
}
}
143 changes: 143 additions & 0 deletions api/src/main/java/ai/chronon/api/thrift/Option.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package ai.chronon.api.thrift;

import java.util.Optional;

/** Implementation of the Option type pattern */
public abstract class Option<T> {

@SuppressWarnings("rawtypes")
private static final Option NONE = new None();

/**
* Whether the Option is defined or not
*
* @return true if the Option is defined (of type Some) false if the Option is not defined (of
* type None)
*/
public abstract boolean isDefined();

/**
* Get the value of the Option (if it is defined)
*
* @return the value
* @throws IllegalStateException if called on a None
*/
public abstract T get();

/**
* Get the contained value (if defined) or else return a default value
*
* @param other what to return if the value is not defined (a None)
* @return either the value, or other if the value is not defined
*/
public T or(T other) {
if (isDefined()) {
return get();
} else {
return other;
}
}

/**
* Turn this Option into Java 8 Optional type
*
* @return Java 8+ Optional Type
*/
public Optional<T> toOptional() {
if (isDefined()) {
return Optional.of(get());
} else {
return Optional.empty();
}
}

/** The None type, representing an absent value (instead of "null") */
public static class None<T> extends Option<T> {
public boolean isDefined() {
return false;
}

public T get() {
throw new IllegalStateException("Cannot call get() on None");
}

public String toString() {
return "None";
}
}

/**
* The Some type, representing an existence of some value
*
* @param <T> The type of value
*/
public static class Some<T> extends Option<T> {
private final T value;

public Some(T value) {
this.value = value;
}

public boolean isDefined() {
return true;
}

public T get() {
return value;
}

public String toString() {
return "Some(" + value + ")";
}
}

/**
* Wraps value in an Option type, depending on whether or not value is null
*
* @param value the value to wrap in Option
* @param <T> the type of value
* @return Some(value) if value is not null, None if value is null
*/
public static <T> Option<T> fromNullable(T value) {
if (value != null) {
return some(value);
} else {
return none();
}
}

/**
* Wrap value in a Some type (NB! value must not be null!)
*
* @param value the value to wrap.
* @param <T> the type of value
* @return a new Some(value)
*/
public static <T> Some<T> some(T value) {
return new Some<T>(value);
}

@SuppressWarnings("unchecked")
public static <T> None<T> none() {
return (None<T>) NONE;
}
}
Loading
Loading