Skip to content

Commit

Permalink
HUEditorViewBuffer_HighVolume: sorting support (partial)
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Dec 5, 2017
1 parent d440b3f commit afe619d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
Expand Down Expand Up @@ -57,7 +58,10 @@ public class HUEditorViewBuffer_HighVolume implements HUEditorViewBuffer

private final HUEditorViewRepository huEditorRepo;
private final ImmutableList<DocumentFilter> stickyFilters;

private final AtomicReference<ViewRowIdsOrderedSelection> defaultSelectionRef;
private final transient ConcurrentHashMap<ImmutableList<DocumentQueryOrderBy>, ViewRowIdsOrderedSelection> selectionsByOrderBys = new ConcurrentHashMap<>();

private final CCache<DocumentId, HUEditorRow> cache_huRowsById = CCache.newLRUCache(I_M_HU.Table_Name + "#HUEditorRows#by#Id", 100, 2);

HUEditorViewBuffer_HighVolume(
Expand Down Expand Up @@ -85,12 +89,32 @@ private ViewRowIdsOrderedSelection getDefaultSelection()
{
return defaultSelectionRef.get();
}

private ViewRowIdsOrderedSelection getSelection(final List<DocumentQueryOrderBy> orderBys)
{
final ViewRowIdsOrderedSelection defaultSelection = getDefaultSelection();

if (orderBys == null || orderBys.isEmpty())
{
return defaultSelection;
}

if (Objects.equals(defaultSelection.getOrderBys(), orderBys))
{
return defaultSelection;
}

return selectionsByOrderBys.computeIfAbsent(ImmutableList.copyOf(orderBys), orderBysImmutable -> huEditorRepo.createSelectionFromSelection(defaultSelection, orderBysImmutable));
}


/** @return true if selection was really changed */
private boolean changeDefaultSelection(final UnaryOperator<ViewRowIdsOrderedSelection> mapper)
private boolean changeSelection(final UnaryOperator<ViewRowIdsOrderedSelection> mapper)
{
final ViewRowIdsOrderedSelection defaultSelectionOld = defaultSelectionRef.get();
final ViewRowIdsOrderedSelection defaultSelectionNew = defaultSelectionRef.updateAndGet(mapper);
selectionsByOrderBys.clear(); // invalidate all the other selections, let it recompute when needed

return !Objects.equals(defaultSelectionOld, defaultSelectionNew);
}

Expand Down Expand Up @@ -127,7 +151,7 @@ public boolean addHUIds(final Collection<Integer> huIdsToAdd)
return false;
}

return changeDefaultSelection(defaultSelection -> huEditorRepo.addRowIdsToSelection(defaultSelection, rowIdsToAdd));
return changeSelection(defaultSelection -> huEditorRepo.addRowIdsToSelection(defaultSelection, rowIdsToAdd));
}

@Override
Expand All @@ -142,7 +166,7 @@ public boolean removeHUIds(final Collection<Integer> huIdsToRemove)

rowIdsToRemove.forEach(rowId -> cache_huRowsById.remove(rowId));

return changeDefaultSelection(defaultSelection -> huEditorRepo.removeRowIdsFromSelection(defaultSelection, rowIdsToRemove));
return changeSelection(defaultSelection -> huEditorRepo.removeRowIdsFromSelection(defaultSelection, rowIdsToRemove));
}

@Override
Expand Down Expand Up @@ -215,23 +239,6 @@ public Stream<HUEditorRow> streamPage(
.stream();
}

private ViewRowIdsOrderedSelection getSelection(final List<DocumentQueryOrderBy> orderBys)
{
final ViewRowIdsOrderedSelection defaultSelection = getDefaultSelection();

if (orderBys == null || orderBys.isEmpty())
{
return defaultSelection;
}

if (!Objects.equals(orderBys, defaultSelection.getOrderBys()))
{
throw new UnsupportedOperationException("Sorting is not supported");
}

return defaultSelection;
}

private PageFetcher<Integer> huIdsPageFetcher(final List<DocumentQueryOrderBy> orderBys)
{
final ViewRowIdsOrderedSelection selection = getSelection(orderBys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public interface HUEditorViewRepository
{
void invalidateCache();

ViewRowIdsOrderedSelection createSelection(ViewId viewId, List<DocumentFilter> filters, List<DocumentQueryOrderBy> orderBys);

ViewRowIdsOrderedSelection createSelectionFromSelection(ViewRowIdsOrderedSelection fromSelection, List<DocumentQueryOrderBy> orderBys);

List<HUEditorRow> retrieveHUEditorRows(Set<Integer> huIds, HUEditorRowFilter filter);

/**
Expand All @@ -52,8 +56,6 @@ public interface HUEditorViewRepository

Page<Integer> retrieveHUIdsPage(ViewRowIdsOrderedSelection selection, int firstRow, int maxRows);

ViewRowIdsOrderedSelection createSelection(ViewId viewId, List<DocumentFilter> filters, List<DocumentQueryOrderBy> orderBys);

ViewRowIdsOrderedSelection addRowIdsToSelection(ViewRowIdsOrderedSelection selection, DocumentIdsSelection rowIdsToAdd);

ViewRowIdsOrderedSelection removeRowIdsFromSelection(ViewRowIdsOrderedSelection selection, DocumentIdsSelection rowIdsToRemove);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public class SqlHUEditorViewRepository implements HUEditorViewRepository
private final ViewRowIdsOrderedSelectionFactory viewSelectionFactory;
private final SqlViewSelectData sqlViewSelect;


@Builder
private SqlHUEditorViewRepository(
@NonNull final WindowId windowId,
Expand Down Expand Up @@ -558,6 +557,13 @@ public ViewRowIdsOrderedSelection createSelection(final ViewId viewId, final Lis
return viewSelectionFactory.createOrderedSelection(viewEvalCtx, viewId, filters, orderBys);
}

@Override
public ViewRowIdsOrderedSelection createSelectionFromSelection(final ViewRowIdsOrderedSelection fromSelection, final List<DocumentQueryOrderBy> orderBys)
{
final ViewEvaluationCtx viewEvalCtx = ViewEvaluationCtx.of(Env.getCtx());
return viewSelectionFactory.createOrderedSelectionFromSelection(viewEvalCtx, fromSelection, orderBys);
}

@Override
public ViewRowIdsOrderedSelection addRowIdsToSelection(final ViewRowIdsOrderedSelection selection, final DocumentIdsSelection rowIdsToAdd)
{
Expand Down

1 comment on commit afe619d

@teosarca
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.