dimas-b commented on code in PR #4474:
URL: https://github.com/apache/polaris/pull/4474#discussion_r3283418656
##########
persistence/nosql/persistence/impl/src/main/java/org/apache/polaris/persistence/nosql/impl/indexes/InternalIndexElement.java:
##########
@@ -47,5 +47,7 @@ interface InternalIndexElement<V> extends Index.Element<V> {
int contentSerializedSize(IndexValueSerializer<V> ser);
+ boolean hasNonNullValue();
Review Comment:
suggestion: `hasValue()` or `valuePresent()` (cf. `Optional.isPresent()`)
##########
persistence/nosql/persistence/impl/src/main/java/org/apache/polaris/persistence/nosql/impl/indexes/IndexImpl.java:
##########
@@ -802,10 +802,15 @@ private final class LazyIndexElement extends
AbstractIndexElement<V> {
/** The materialized key or {@code null}. */
private IndexKey key;
- /** The materialized content or {@code null}. */
- private V content;
+ /**
+ * Holds either one of the {@code VALUE_STATE_*} sentinels or a
materialized non-{@code null}
+ * content value.
+ */
+ private volatile Object contentState = VALUE_STATE_UNMATERIALIZED;
- private boolean hasContent;
+ private static final Object VALUE_STATE_UNMATERIALIZED = new Object();
+ private static final Object VALUE_STATE_NULL = new Object();
+ private static final Object VALUE_STATE_NON_NULL = new Object();
Review Comment:
suggestion: `VALUE_STATE_PRESENT`
##########
persistence/nosql/persistence/impl/src/main/java/org/apache/polaris/persistence/nosql/impl/indexes/IndexImpl.java:
##########
@@ -904,26 +909,43 @@ public IndexKey key() {
return k;
}
+ @Override
+ public boolean hasNonNullValue() {
+ var state = contentState;
+ if (state == VALUE_STATE_NULL) {
+ return false;
+ }
+ if (state != VALUE_STATE_UNMATERIALIZED) {
+ return true;
+ }
+ var nonNull =
+ !serializer.isNullSerialized(
+ serializedThreadSafe().limit(endOffset).position(valueOffset));
+ if (contentState == VALUE_STATE_UNMATERIALIZED) {
Review Comment:
Do we really need this `if`? It does not prevent races WRT line 941, but
adds confusion WRT line 918 🤔
##########
persistence/nosql/persistence/impl/src/main/java/org/apache/polaris/persistence/nosql/impl/indexes/IndexImpl.java:
##########
@@ -904,26 +909,43 @@ public IndexKey key() {
return k;
}
+ @Override
+ public boolean hasNonNullValue() {
+ var state = contentState;
+ if (state == VALUE_STATE_NULL) {
+ return false;
+ }
+ if (state != VALUE_STATE_UNMATERIALIZED) {
+ return true;
+ }
+ var nonNull =
+ !serializer.isNullSerialized(
+ serializedThreadSafe().limit(endOffset).position(valueOffset));
+ if (contentState == VALUE_STATE_UNMATERIALIZED) {
+ contentState = nonNull ? VALUE_STATE_NON_NULL : VALUE_STATE_NULL;
Review Comment:
WDYT about `AtomicReference.compareAndSet(VALUE_STATE_UNMATERIALIZED,
nonNull ? VALUE_STATE_NON_NULL : VALUE_STATE_NULL)`?
##########
persistence/nosql/persistence/impl/src/main/java/org/apache/polaris/persistence/nosql/impl/indexes/IndexImpl.java:
##########
@@ -904,26 +909,43 @@ public IndexKey key() {
return k;
}
+ @Override
+ public boolean hasNonNullValue() {
+ var state = contentState;
+ if (state == VALUE_STATE_NULL) {
+ return false;
+ }
+ if (state != VALUE_STATE_UNMATERIALIZED) {
+ return true;
+ }
+ var nonNull =
+ !serializer.isNullSerialized(
+ serializedThreadSafe().limit(endOffset).position(valueOffset));
+ if (contentState == VALUE_STATE_UNMATERIALIZED) {
+ contentState = nonNull ? VALUE_STATE_NON_NULL : VALUE_STATE_NULL;
+ }
+ return nonNull;
+ }
+
@Override
@Nullable
public V valueNullable() {
- var c = content;
- if (c == null) {
- if (!hasContent) {
- c =
- content =
- serializer.deserialize(
-
serializedThreadSafe().limit(endOffset).position(valueOffset));
- hasContent = true;
- }
+ var state = contentState;
+ if (state == VALUE_STATE_NULL) {
+ return null;
}
+ if (state != VALUE_STATE_UNMATERIALIZED && state !=
VALUE_STATE_NON_NULL) {
+ return materializedContent(state);
Review Comment:
the result of `materializedContent(state)` in this case is known to be
`state`... why use this indirection? 🤔
##########
persistence/nosql/persistence/impl/src/main/java/org/apache/polaris/persistence/nosql/impl/indexes/IndexImpl.java:
##########
@@ -943,6 +965,22 @@ public String toString() {
return sb.toString();
}
+
+ @Nullable
+ private V materializedContent() {
+ return materializedContent(contentState);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Nullable
+ private V materializedContent(Object state) {
+ if (state == VALUE_STATE_UNMATERIALIZED
+ || state == VALUE_STATE_NULL
+ || state == VALUE_STATE_NON_NULL) {
+ return null;
Review Comment:
This actually returns something other that the materialized content (unless
it's `null`) despite the method name 🤔
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]