Skip to content

Commit

Permalink
[TH2-5165] Use cache in BookInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Feb 15, 2024
1 parent 74d139f commit c360d21
Show file tree
Hide file tree
Showing 23 changed files with 602 additions and 186 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 Exactpro (Exactpro Systems Limited)
* Copyright 2020-2024 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,10 @@
import com.exactpro.cradle.errors.BookNotFoundException;
import com.exactpro.cradle.utils.CradleStorageException;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -80,6 +84,23 @@ public Collection<PageInfo> loadPageInfo(BookId bookId, boolean loadRemoved) {
return result;
}

public Collection<PageInfo> loadPageInfo(BookId bookId, Instant start, Instant end, boolean loadRemoved) {
Collection<PageInfo> result = new ArrayList<>();
for (PageEntity pageEntity : operators.getPageOperator().get(
bookId.getName(),
LocalDate.ofInstant(start, ZoneOffset.UTC),
LocalTime.ofInstant(start, ZoneOffset.UTC),
LocalDate.ofInstant(end, ZoneOffset.UTC),
LocalTime.ofInstant(end, ZoneOffset.UTC),
readAttrs
)) {
if (loadRemoved || pageEntity.getRemoved() == null || pageEntity.getRemoved().equals(DEFAULT_PAGE_REMOVE_TIME)) {
result.add(pageEntity.toPageInfo());
}
}
return result;
}

public BookInfo loadBook(BookId bookId) throws CradleStorageException {
BookEntity bookEntity = operators.getBookOperator().get(bookId.getName(), readAttrs);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 Exactpro (Exactpro Systems Limited)
* Copyright 2021-2024 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,11 +46,11 @@ public class PageEntity {
public static final String FIELD_UPDATED = "updated";
public static final String FIELD_REMOVED = "removed";

@PartitionKey(0)
@PartitionKey()
@CqlName(FIELD_BOOK)
private final String book;

@ClusteringColumn(0)
@ClusteringColumn()
@CqlName(FIELD_START_DATE)
private final LocalDate startDate;

Expand Down Expand Up @@ -145,8 +145,9 @@ public Instant getRemoved() {
}

public PageInfo toPageInfo() {
return new PageInfo(new PageId(new BookId(book), name),
TimeUtils.toInstant(getStartDate(), getStartTime()),
Instant start = TimeUtils.toInstant(getStartDate(), getStartTime());
return new PageInfo(new PageId(new BookId(book), start, name),
start,
TimeUtils.toInstant(getEndDate(), getEndTime()),
getComment(),
getUpdated(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 Exactpro (Exactpro Systems Limited)
* Copyright 2021-2024 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +31,8 @@
import com.datastax.oss.driver.api.mapper.annotations.*;
import com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy;

//TODO: implement getFirstPage / getLastPage methods

@Dao
public interface PageOperator {
@Select
Expand All @@ -43,6 +45,16 @@ public interface PageOperator {
PagingIterable<PageEntity> get(String book, LocalDate startDate, LocalTime startTime,
Function<BoundStatementBuilder, BoundStatementBuilder> attributes);

@Query("SELECT * FROM ${qualifiedTableId} " +
"WHERE " +
FIELD_BOOK +"=:book AND " +
"(" + FIELD_START_DATE + ", " + FIELD_START_TIME + ") >= (:startDate, :startTime) " +
"AND " +
"(" + FIELD_START_DATE + ", " + FIELD_START_TIME + ") <= (:endDate, :endTime)")
PagingIterable<PageEntity> get(String book, LocalDate startDate, LocalTime startTime,
LocalDate endDate, LocalTime endTime,
Function<BoundStatementBuilder, BoundStatementBuilder> attributes);

@Update(nullSavingStrategy = NullSavingStrategy.SET_TO_NULL)
ResultSet update(PageEntity entity, Function<BoundStatementBuilder, BoundStatementBuilder> attributes);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Exactpro (Exactpro Systems Limited)
* Copyright 2021-2024 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -188,8 +188,7 @@ protected CassandraStoredMessageFilter createInitialFilter(MessageFilter filter)
{
if (filter.getOrder() == Order.DIRECT) {
return new CassandraStoredMessageFilter(
firstPage.getId().getBookId().getName(),
firstPage.getId().getName(),
firstPage.getId(),
filter.getSessionAlias(),
filter.getDirection().getLabel(),
leftBoundFilter,
Expand All @@ -198,8 +197,7 @@ protected CassandraStoredMessageFilter createInitialFilter(MessageFilter filter)
filter.getOrder());
} else {
return new CassandraStoredMessageFilter(
lastPage.getId().getBookId().getName(),
lastPage.getId().getName(),
lastPage.getId(),
filter.getSessionAlias(),
filter.getDirection().getLabel(),
leftBoundFilter,
Expand All @@ -213,7 +211,7 @@ protected CassandraStoredMessageFilter createInitialFilter(MessageFilter filter)

protected CassandraStoredMessageFilter createNextFilter(CassandraStoredMessageFilter prevFilter, int updatedLimit)
{
PageInfo oldPage = book.getPage(new PageId(book.getId(), prevFilter.getPage()));
PageInfo oldPage = book.getPage(prevFilter.getPageId());
PageInfo newPage;

if (filter.getOrder() == Order.DIRECT) {
Expand All @@ -230,8 +228,7 @@ protected CassandraStoredMessageFilter createNextFilter(CassandraStoredMessageFi
}

return new CassandraStoredMessageFilter(
newPage.getId().getBookId().getName(),
newPage.getId().getName(),
newPage.getId(),
prevFilter.getSessionAlias(),
prevFilter.getDirection(),
leftBoundFilter,
Expand Down Expand Up @@ -259,7 +256,7 @@ protected boolean performNextIteratorChecks () {
}

protected Iterator<StoredMessageBatch> getBatchedIterator (MappedAsyncPagingIterable<MessageBatchEntity> resultSet) {
PageId pageId = new PageId(book.getId(), cassandraFilter.getPage());
PageId pageId = cassandraFilter.getPageId();
// Updated limit should be smaller, since we already got entities from previous batch
cassandraFilter = createNextFilter(cassandraFilter, Math.max(limit - returned.get(),0));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 Exactpro (Exactpro Systems Limited)
* Copyright 2021-2024 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,8 +19,8 @@
import com.datastax.oss.driver.api.core.cql.BoundStatementBuilder;
import com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder;
import com.datastax.oss.driver.api.querybuilder.select.Select;
import com.datastax.oss.driver.shaded.guava.common.base.Preconditions;
import com.exactpro.cradle.Order;
import com.exactpro.cradle.PageId;
import com.exactpro.cradle.cassandra.dao.CassandraFilter;
import com.exactpro.cradle.cassandra.utils.FilterUtils;
import com.exactpro.cradle.filters.FilterForGreater;
Expand All @@ -30,27 +30,33 @@
import java.util.StringJoiner;

import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker;
import static com.exactpro.cradle.cassandra.dao.messages.GroupedMessageBatchEntity.*;
import static com.exactpro.cradle.cassandra.dao.messages.CassandraStoredMessageFilter.*;
import static com.exactpro.cradle.cassandra.dao.messages.CassandraStoredMessageFilter.DATE_FROM;
import static com.exactpro.cradle.cassandra.dao.messages.CassandraStoredMessageFilter.DATE_TO;
import static com.exactpro.cradle.cassandra.dao.messages.CassandraStoredMessageFilter.TIME_FROM;
import static com.exactpro.cradle.cassandra.dao.messages.CassandraStoredMessageFilter.TIME_TO;
import static com.exactpro.cradle.cassandra.dao.messages.GroupedMessageBatchEntity.FIELD_ALIAS_GROUP;
import static com.exactpro.cradle.cassandra.dao.messages.GroupedMessageBatchEntity.FIELD_BOOK;
import static com.exactpro.cradle.cassandra.dao.messages.GroupedMessageBatchEntity.FIELD_FIRST_MESSAGE_DATE;
import static com.exactpro.cradle.cassandra.dao.messages.GroupedMessageBatchEntity.FIELD_FIRST_MESSAGE_TIME;
import static com.exactpro.cradle.cassandra.dao.messages.GroupedMessageBatchEntity.FIELD_PAGE;

public class CassandraGroupedMessageFilter implements CassandraFilter<GroupedMessageBatchEntity> {
private final String book, page, groupName;
private final String groupName;
private final PageId pageId;

/** limit must be strictly positive ( limit greater than 0 ) */
private final int limit;
private final FilterForGreater<Instant> messageTimeFrom;
private final FilterForLess<Instant> messageTimeTo;
private final Order order;

public CassandraGroupedMessageFilter(String book,
String page,
public CassandraGroupedMessageFilter(PageId pageId,
String groupName,
FilterForGreater<Instant> messageTimeFrom,
FilterForLess<Instant> messageTimeTo,
Order order,
int limit) {
this.book = book;
this.page = page;
this.pageId = pageId;
this.groupName = groupName;
this.messageTimeFrom = messageTimeFrom;
this.messageTimeTo = messageTimeTo;
Expand Down Expand Up @@ -88,8 +94,8 @@ public Select addConditions(Select select) {
@Override
public BoundStatementBuilder bindParameters(BoundStatementBuilder builder) {
builder = builder
.setString(FIELD_BOOK, book)
.setString(FIELD_PAGE, page)
.setString(FIELD_BOOK, pageId.getBookId().getName())
.setString(FIELD_PAGE, pageId.getName())
.setString(FIELD_ALIAS_GROUP, groupName);

if (messageTimeFrom != null)
Expand All @@ -100,12 +106,16 @@ public BoundStatementBuilder bindParameters(BoundStatementBuilder builder) {
return builder;
}

public PageId getPageId() {
return pageId;
}

public String getBook() {
return book;
return pageId.getBookId().getName();
}

public String getPage() {
return page;
return pageId.getName();
}

public String getGroupName() {
Expand All @@ -127,8 +137,7 @@ public FilterForLess<Instant> getMessageTimeTo() {
@Override
public String toString() {
return new StringJoiner(", ", CassandraGroupedMessageFilter.class.getSimpleName() + "[", "]")
.add("book='" + book + "'")
.add("page='" + page + "'")
.add("pageId='" + pageId + "'")
.add("groupName='" + groupName + "'")
.add("limit=" + limit)
.add("messageTimeFrom " + messageTimeFrom)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Exactpro (Exactpro Systems Limited)
* Copyright 2021-2024 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,8 +20,8 @@
import com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder;
import com.datastax.oss.driver.api.querybuilder.relation.MultiColumnRelationBuilder;
import com.datastax.oss.driver.api.querybuilder.select.Select;
import com.datastax.oss.driver.shaded.guava.common.base.Preconditions;
import com.exactpro.cradle.Order;
import com.exactpro.cradle.PageId;
import com.exactpro.cradle.cassandra.dao.CassandraFilter;
import com.exactpro.cradle.cassandra.utils.FilterUtils;
import com.exactpro.cradle.filters.ComparisonOperation;
Expand All @@ -35,16 +35,23 @@

import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker;
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.tuple;

import static com.exactpro.cradle.cassandra.dao.messages.MessageBatchEntity.*;
import static com.exactpro.cradle.cassandra.dao.messages.MessageBatchEntity.FIELD_BOOK;
import static com.exactpro.cradle.cassandra.dao.messages.MessageBatchEntity.FIELD_DIRECTION;
import static com.exactpro.cradle.cassandra.dao.messages.MessageBatchEntity.FIELD_FIRST_MESSAGE_DATE;
import static com.exactpro.cradle.cassandra.dao.messages.MessageBatchEntity.FIELD_FIRST_MESSAGE_TIME;
import static com.exactpro.cradle.cassandra.dao.messages.MessageBatchEntity.FIELD_PAGE;
import static com.exactpro.cradle.cassandra.dao.messages.MessageBatchEntity.FIELD_SEQUENCE;
import static com.exactpro.cradle.cassandra.dao.messages.MessageBatchEntity.FIELD_SESSION_ALIAS;

public class CassandraStoredMessageFilter implements CassandraFilter<MessageBatchEntity>
{
public static final String DATE_FROM = "dateFrom", DATE_TO = "dateTo",
TIME_FROM = "timeFrom", TIME_TO = "timeTo",
SEQ_FROM = "seqFrom", SEQ_TO = "seqTo";

private final String book, page, sessionAlias, direction;
private final String sessionAlias;
private final String direction;
private final PageId pageId;

private final FilterForGreater<Instant> messageTimeFrom;
private final FilterForLess<Instant> messageTimeTo;
Expand All @@ -55,25 +62,10 @@ public class CassandraStoredMessageFilter implements CassandraFilter<MessageBatc

private final Order order;

public CassandraStoredMessageFilter(String book, String page, String sessionAlias, String direction,
FilterForGreater<Instant> messageTimeFrom, FilterForLess<Instant> messageTimeTo)
{
this.book = book;
this.page = page;
this.sessionAlias = sessionAlias;
this.direction = direction;
this.messageTimeFrom = messageTimeFrom;
this.messageTimeTo = messageTimeTo;
this.sequence = null;
this.limit = 0;
this.order = Order.DIRECT;
}

public CassandraStoredMessageFilter(String book, String page, String sessionAlias, String direction,
public CassandraStoredMessageFilter(PageId pageId, String sessionAlias, String direction,
FilterForGreater<Instant> messageTimeFrom, FilterForLess<Instant> messageTimeTo, int limit, Order order)
{
this.book = book;
this.page = page;
this.pageId = pageId;
this.sessionAlias = sessionAlias;
this.direction = direction;
this.messageTimeFrom = messageTimeFrom;
Expand Down Expand Up @@ -118,8 +110,8 @@ public Select addConditions(Select select)
public BoundStatementBuilder bindParameters(BoundStatementBuilder builder)
{
builder = builder
.setString(FIELD_BOOK, book)
.setString(FIELD_PAGE, page)
.setString(FIELD_BOOK, pageId.getBookId().getName())
.setString(FIELD_PAGE, pageId.getName())
.setString(FIELD_SESSION_ALIAS, sessionAlias)
.setString(FIELD_DIRECTION, direction);

Expand All @@ -137,12 +129,16 @@ public BoundStatementBuilder bindParameters(BoundStatementBuilder builder)

public String getBook()
{
return book;
return pageId.getBookId().getName();
}

public String getPage()
{
return page;
return pageId.getName();
}

public PageId getPageId() {
return pageId;
}

public String getSessionAlias()
Expand All @@ -166,10 +162,8 @@ public FilterForAny<Long> getSequence()
public String toString()
{
List<String> result = new ArrayList<>(10);
if (book != null)
result.add("book=" + book);
if (page != null)
result.add("page=" + page);
if (pageId != null)
result.add("pageId=" + pageId);
if (sessionAlias != null)
result.add("sessionAlias=" + sessionAlias);
if (direction != null)
Expand Down
Loading

0 comments on commit c360d21

Please sign in to comment.