Skip to content

Instantly share code, notes, and snippets.

@jkominek
Created August 12, 2019 21:32
Show Gist options
  • Save jkominek/4d7248b9eba86d39aea5489d2f60f72b to your computer and use it in GitHub Desktop.
Save jkominek/4d7248b9eba86d39aea5489d2f60f72b to your computer and use it in GitHub Desktop.
numeric negation function key expression
age record.layer.demo;
import com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression;
import com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression;
import com.apple.foundationdb.record.metadata.expressions.KeyExpression;
import com.apple.foundationdb.annotation.API;
import com.apple.foundationdb.record.metadata.Key;
import com.apple.foundationdb.record.metadata.MetaDataException;
import com.apple.foundationdb.record.provider.foundationdb.FDBRecord;
import com.google.protobuf.Message;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
public class NumericNegationFunctionKeyExpression
extends FunctionKeyExpression
implements QueryableKeyExpression
{
protected NumericNegationFunctionKeyExpression(@Nonnull String name,
@Nonnull KeyExpression arguments)
{
super(name, arguments);
}
@Override
public int getMinArguments() {
return 1;
}
@Override
public int getMaxArguments() {
return 1;
}
@Nonnull
@Override
public <M extends Message> List<Key.Evaluated> evaluateFunction(@Nullable FDBRecord<M> record,
@Nullable Message message,
@Nonnull Key.Evaluated arguments) {
final Long value = arguments.getNullableLong(0);
if (value == null) {
return Collections.singletonList(Key.Evaluated.NULL);
}
return Collections.singletonList(Key.Evaluated.scalar(-value));
}
@Override
public boolean createsDuplicates() {
return getArguments().createsDuplicates();
}
@Override
public int getColumnSize() {
return 1;
}
@Nullable
@Override
public Function<Object, Object> getComparandConversionFunction() {
return o -> (-(Long)o);
}
}
package record.layer.demo;
import com.apple.foundationdb.record.metadata.expressions.*;
import com.apple.foundationdb.annotation.API;
import com.google.auto.service.AutoService;
import com.apple.foundationdb.record.provider.common.text.TextCollatorRegistryJRE;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
@AutoService(FunctionKeyExpression.Factory.class)
public class NumericNegationFunctionKeyExpressionFactory
implements FunctionKeyExpression.Factory {
public static final String FUNCTION_NAME = "numericnegation";
@Nonnull
@Override
public List<FunctionKeyExpression.Builder> getBuilders() {
return Collections.singletonList(new FunctionKeyExpression.BiFunctionBuilder(FUNCTION_NAME, NumericNegationFunctionKeyExpression::new));
}
}
@jkominek
Copy link
Author

  1. public domain or whatever license you want, in the unlikely event this is of immediate use to anyone.
  2. evaluateFunction should almost certainly handle the other numeric types better. be careful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment