Skip to content

Gh 4858 planb query fields #4866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ public void init(final SelectionListModel<T, I> model) {
refresh(false, false);
}

public void refresh() {
quickFilter.clear();
refresh(true, true);
}

public void destroy() {
if (model != null) {
lastFilter = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void init(final SelectionListModel<T, I> model) {
selectionList.init(model);
}

public void refresh() {
selectionList.refresh();
}

public MultiSelectionModel<I> getSelectionModel() {
return selectionList.getSelectionModel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,15 @@ protected void onUnbind() {

private void onAddColumn(final ClickEvent event) {
if (currentSearchModel != null) {
columnSelectionListModel.setDataSourceRef(currentSearchModel.getIndexLoader().getLoadedDataSourceRef());
final DocRef dataSource = currentSearchModel.getIndexLoader().getLoadedDataSourceRef();
final boolean changedDataSource = !Objects.equals(columnSelectionListModel.getDataSourceRef(), dataSource);
columnSelectionListModel.setDataSourceRef(dataSource);

if (addColumnPopup == null) {
addColumnPopup = new SelectionPopup<>();
addColumnPopup.init(columnSelectionListModel);
} else if (changedDataSource) {
addColumnPopup.refresh();
}

final Element target = event.getNativeEvent().getEventTarget().cast();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public void setDataSourceRef(final DocRef dataSourceRef) {
this.dataSourceRef = dataSourceRef;
}

public DocRef getDataSourceRef() {
return dataSourceRef;
}

@Override
public void reset() {
lastCriteria = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import stroom.lmdb2.LmdbEnvDir;
import stroom.lmdb2.LmdbKeySequence;
import stroom.query.api.v2.Column;
import stroom.query.api.v2.ExpressionUtil;
import stroom.query.api.v2.Format;
import stroom.query.common.v2.ExpressionPredicateFactory;
import stroom.query.common.v2.ExpressionPredicateFactory.ValueFunctionFactories;
Expand Down Expand Up @@ -37,7 +38,9 @@
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
Expand Down Expand Up @@ -492,6 +495,10 @@ public void search(final ExpressionCriteria criteria,
final DateTimeSettings dateTimeSettings,
final ExpressionPredicateFactory expressionPredicateFactory,
final ValuesConsumer consumer) {
// Ensure we have fields for all expression criteria.
final List<String> fields = ExpressionUtil.fields(criteria.getExpression());
fields.forEach(fieldIndex::create);

final ValueFunctionFactories<Val[]> valueFunctionFactories = createValueFunctionFactories(fieldIndex);
final Optional<Predicate<Val[]>> optionalPredicate = expressionPredicateFactory
.create(criteria.getExpression(), valueFunctionFactories, dateTimeSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

public class RangedStateDb extends AbstractDb<Key, StateValue> {

private static final ByteBuffer ZERO = ByteBuffer.allocateDirect(0);

RangedStateDb(final Path path,
final ByteBufferFactory byteBufferFactory) {
this(
Expand All @@ -43,21 +45,37 @@ public static RangedStateDb create(final Path path,
final ByteBufferFactory byteBufferFactory,
final PlanBDoc doc,
final boolean readOnly) {
if (doc.getSettings() instanceof final RangedStateSettings rangedStateSettings) {
return new RangedStateDb(path, byteBufferFactory, rangedStateSettings, readOnly);
} else {
throw new RuntimeException("No ranged state settings provided");
return new RangedStateDb(path, byteBufferFactory, getSettings(doc), readOnly);
}

private static RangedStateSettings getSettings(final PlanBDoc doc) {
if (doc.getSettings() instanceof final RangedStateSettings settings) {
return settings;
}
return RangedStateSettings.builder().build();
}

public Optional<RangedState> getState(final RangedStateRequest request) {
final ByteBuffer start = byteBufferFactory.acquire(Long.BYTES);
try {
start.putLong(request.key());
start.putLong(request.key() + 1);
start.flip();

final KeyRange<ByteBuffer> keyRange = KeyRange.atLeastBackward(start);
// read(readTxn -> {
// try (final CursorIterable<ByteBuffer> cursor = dbi.iterate(readTxn)) {
// final Iterator<KeyVal<ByteBuffer>> iterator = cursor.iterator();
// while (iterator.hasNext()
// && !Thread.currentThread().isInterrupted()) {
// final BBKV kv = BBKV.create(iterator.next());
// final long keyStart = kv.key().getLong(0);
// final long keyEnd = kv.key().getLong(Long.BYTES);
// System.out.println("start=" + keyStart + ", keyEnd=" + keyEnd);
// }
// }
// return Optional.empty();
// });

final KeyRange<ByteBuffer> keyRange = KeyRange.openBackward(start, ZERO);
return read(readTxn -> {
try (final CursorIterable<ByteBuffer> cursor = dbi.iterate(readTxn, keyRange)) {
final Iterator<KeyVal<ByteBuffer>> iterator = cursor.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import stroom.lmdb2.BBKV;
import stroom.planb.shared.PlanBDoc;
import stroom.planb.shared.SessionSettings;
import stroom.query.api.v2.ExpressionUtil;
import stroom.query.common.v2.ExpressionPredicateFactory;
import stroom.query.common.v2.ExpressionPredicateFactory.ValueFunctionFactories;
import stroom.query.language.functions.FieldIndex;
Expand All @@ -25,6 +26,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
Expand Down Expand Up @@ -58,18 +60,25 @@ public static SessionDb create(final Path path,
final ByteBufferFactory byteBufferFactory,
final PlanBDoc doc,
final boolean readOnly) {
if (doc.getSettings() instanceof final SessionSettings sessionSettings) {
return new SessionDb(path, byteBufferFactory, sessionSettings, readOnly);
} else {
throw new RuntimeException("No session settings provided");
return new SessionDb(path, byteBufferFactory, getSettings(doc), readOnly);
}

private static SessionSettings getSettings(final PlanBDoc doc) {
if (doc.getSettings() instanceof final SessionSettings settings) {
return settings;
}
return SessionSettings.builder().build();
}

public void search(final ExpressionCriteria criteria,
final FieldIndex fieldIndex,
final DateTimeSettings dateTimeSettings,
final ExpressionPredicateFactory expressionPredicateFactory,
final ValuesConsumer consumer) {
// Ensure we have fields for all expression criteria.
final List<String> fields = ExpressionUtil.fields(criteria.getExpression());
fields.forEach(fieldIndex::create);

final ValueFunctionFactories<Val[]> valueFunctionFactories = createValueFunctionFactories(fieldIndex);
final Optional<Predicate<Val[]>> optionalPredicate = expressionPredicateFactory
.create(criteria.getExpression(), valueFunctionFactories, dateTimeSettings);
Expand Down Expand Up @@ -181,16 +190,16 @@ public Optional<Session> getState(final SessionRequest request) {
final long nameHash = LongHashFunction.xx3().hashBytes(request.name());
final long time = request.time();
final ByteBuffer start = byteBufferFactory.acquire(Long.BYTES + Long.BYTES);
final ByteBuffer end = byteBufferFactory.acquire(Long.BYTES);
final ByteBuffer stop = byteBufferFactory.acquire(Long.BYTES);
try {
start.putLong(nameHash);
start.putLong(time);
start.putLong(time + 1);
start.flip();

end.putLong(nameHash);
end.flip();
stop.putLong(nameHash);
stop.flip();

final KeyRange<ByteBuffer> keyRange = KeyRange.closedBackward(start, end);
final KeyRange<ByteBuffer> keyRange = KeyRange.openBackward(start, stop);
return read(readTxn -> {
try (final CursorIterable<ByteBuffer> cursor = dbi.iterate(readTxn, keyRange)) {
final Iterator<KeyVal<ByteBuffer>> iterator = cursor.iterator();
Expand Down Expand Up @@ -219,7 +228,7 @@ public Optional<Session> getState(final SessionRequest request) {
});
} finally {
byteBufferFactory.release(start);
byteBufferFactory.release(end);
byteBufferFactory.release(stop);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class StateDb extends AbstractDb<Key, StateValue> {
}

public StateDb(final Path path,
final ByteBufferFactory byteBufferFactory,
final StateSettings settings,
final boolean readOnly) {
final ByteBufferFactory byteBufferFactory,
final StateSettings settings,
final boolean readOnly) {
super(
path,
byteBufferFactory,
Expand All @@ -36,11 +36,14 @@ public static StateDb create(final Path path,
final ByteBufferFactory byteBufferFactory,
final PlanBDoc doc,
final boolean readOnly) {
if (doc.getSettings() instanceof final StateSettings stateSettings) {
return new StateDb(path, byteBufferFactory, stateSettings, readOnly);
} else {
throw new RuntimeException("No state settings provided");
return new StateDb(path, byteBufferFactory, getSettings(doc), readOnly);
}

private static StateSettings getSettings(final PlanBDoc doc) {
if (doc.getSettings() instanceof final StateSettings settings) {
return settings;
}
return StateSettings.builder().build();
}

public Optional<State> getState(final StateRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

public class TemporalRangedStateDb extends AbstractDb<Key, StateValue> {

private static final ByteBuffer ZERO = ByteBuffer.allocateDirect(0);

TemporalRangedStateDb(final Path path,
final ByteBufferFactory byteBufferFactory) {
this(
Expand All @@ -43,20 +45,43 @@ public static TemporalRangedStateDb create(final Path path,
final ByteBufferFactory byteBufferFactory,
final PlanBDoc doc,
final boolean readOnly) {
if (doc.getSettings() instanceof final TemporalRangedStateSettings temporalRangedStateSettings) {
return new TemporalRangedStateDb(path, byteBufferFactory, temporalRangedStateSettings, readOnly);
} else {
throw new RuntimeException("No temporal ranged state settings provided");
return new TemporalRangedStateDb(path, byteBufferFactory, getSettings(doc), readOnly);
}

private static TemporalRangedStateSettings getSettings(final PlanBDoc doc) {
if (doc.getSettings() instanceof final TemporalRangedStateSettings settings) {
return settings;
}
return TemporalRangedStateSettings.builder().build();
}

public Optional<TemporalRangedState> getState(final TemporalRangedStateRequest request) {
final ByteBuffer start = byteBufferFactory.acquire(Long.BYTES);
try {
start.putLong(request.key());
start.putLong(request.key() + 1);
start.flip();

final KeyRange<ByteBuffer> keyRange = KeyRange.atLeastBackward(start);
// read(readTxn -> {
// try (final CursorIterable<ByteBuffer> cursor = dbi.iterate(readTxn)) {
// final Iterator<KeyVal<ByteBuffer>> iterator = cursor.iterator();
// while (iterator.hasNext()
// && !Thread.currentThread().isInterrupted()) {
// final BBKV kv = BBKV.create(iterator.next());
// final long keyStart = kv.key().getLong(0);
// final long keyEnd = kv.key().getLong(Long.BYTES);
// final long effectiveTime = kv.key().getLong(Long.BYTES + Long.BYTES);
// System.out.println("start=" +
// keyStart +
// ", keyEnd=" +
// keyEnd +
// ", effectiveTime=" +
// DateUtil.createNormalDateTimeString(effectiveTime));
// }
// }
// return Optional.empty();
// });

final KeyRange<ByteBuffer> keyRange = KeyRange.openBackward(start, ZERO);
return read(readTxn -> {
Optional<TemporalRangedState> result = Optional.empty();
try (final CursorIterable<ByteBuffer> cursor = dbi.iterate(readTxn, keyRange)) {
Expand All @@ -69,7 +94,7 @@ public Optional<TemporalRangedState> getState(final TemporalRangedStateRequest r
final long effectiveTime = kv.key().getLong(Long.BYTES + Long.BYTES);
if (keyEnd < request.key()) {
return result;
} else if (effectiveTime >= request.effectiveTime() &&
} else if (effectiveTime <= request.effectiveTime() &&
keyStart <= request.key()) {
final Key key = Key
.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ public static TemporalStateDb create(final Path path,
final ByteBufferFactory byteBufferFactory,
final PlanBDoc doc,
final boolean readOnly) {
if (doc.getSettings() instanceof final TemporalStateSettings temporalStateSettings) {
return new TemporalStateDb(path, byteBufferFactory, temporalStateSettings, readOnly);
} else {
throw new RuntimeException("No temporal state settings provided");
return new TemporalStateDb(path, byteBufferFactory, getSettings(doc), readOnly);
}

private static TemporalStateSettings getSettings(final PlanBDoc doc) {
if (doc.getSettings() instanceof final TemporalStateSettings settings) {
return settings;
}
return TemporalStateSettings.builder().build();
}

public Optional<TemporalState> getState(final TemporalStateRequest request) {
Expand All @@ -58,13 +61,13 @@ public Optional<TemporalState> getState(final TemporalStateRequest request) {
final ByteBuffer stop = byteBufferFactory.acquire(Long.BYTES);
try {
start.putLong(rowHash);
start.putLong(request.effectiveTime());
start.putLong(request.effectiveTime() + 1);
start.flip();

stop.putLong(rowHash);
stop.flip();

final KeyRange<ByteBuffer> keyRange = KeyRange.closedBackward(start, stop);
final KeyRange<ByteBuffer> keyRange = KeyRange.openBackward(start, stop);
return read(readTxn -> {
try (final CursorIterable<ByteBuffer> cursor = dbi.iterate(readTxn, keyRange)) {
final Iterator<KeyVal<ByteBuffer>> iterator = cursor.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ void test(@TempDir Path tempDir) {
assertThat(db.count()).isEqualTo(1);
testGet(db);

final RangedStateRequest stateRequest =
new RangedStateRequest(11);
final Optional<RangedState> optional = db.getState(stateRequest);
checkState(db, 9, false);
for (int i = 10; i <= 30; i++) {
checkState(db, i, true);
}
checkState(db, 31, false);

final Optional<RangedState> optional = getState(db, 11);
assertThat(optional).isNotEmpty();
final RangedState res = optional.get();
assertThat(res.key().keyStart()).isEqualTo(10);
Expand Down Expand Up @@ -96,6 +100,20 @@ void test(@TempDir Path tempDir) {
}
}

private Optional<RangedState> getState(final RangedStateDb db, final long key) {
final RangedStateRequest request =
new RangedStateRequest(key);
return db.getState(request);
}

private void checkState(final RangedStateDb db,
final long key,
final boolean expected) {
final Optional<RangedState> optional = getState(db, key);
final boolean actual = optional.isPresent();
assertThat(actual).isEqualTo(expected);
}

@Test
void testMerge(@TempDir final Path rootDir) throws IOException {
final Path dbPath1 = rootDir.resolve("db1");
Expand Down
Loading