Skip to content

Commit

Permalink
when invalidating HUEditorView(s) also invalidate the selection
Browse files Browse the repository at this point in the history
so next time, the HUIds query will be executed and the selection will be
created again.

#751
  • Loading branch information
teosarca committed Dec 15, 2017
1 parent c01d048 commit 220054c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.adempiere.util.lang.ExtendedMemorizingSupplier;
import org.compiere.util.DB;

import com.google.common.base.Predicates;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -67,7 +65,7 @@ class HUEditorViewBuffer_FullyCached implements HUEditorViewBuffer
private final ImmutableList<DocumentFilter> stickyFiltersWithoutHUIdsFilter;

private final HUIdsFilterData huIdsFilterData;
private final Supplier<Set<Integer>> huIdsSupplier;
private final ExtendedMemorizingSupplier<CopyOnWriteArraySet<Integer>> huIdsSupplier;
private final ExtendedMemorizingSupplier<IndexedHUEditorRows> rowsSupplier = ExtendedMemorizingSupplier.of(() -> retrieveHUEditorRows());

private final ImmutableList<DocumentQueryOrderBy> defaultOrderBys;
Expand Down Expand Up @@ -99,7 +97,7 @@ class HUEditorViewBuffer_FullyCached implements HUEditorViewBuffer

final List<DocumentFilter> filtersAll = ImmutableList.copyOf(Iterables.concat(stickyFiltersWithoutHUIdsFilter, filters));

huIdsSupplier = Suppliers.memoize(() -> new CopyOnWriteArraySet<>(huEditorRepo.retrieveHUIdsEffective(this.huIdsFilterData, filtersAll)));
huIdsSupplier = ExtendedMemorizingSupplier.of(() -> new CopyOnWriteArraySet<>(huEditorRepo.retrieveHUIdsEffective(this.huIdsFilterData, filtersAll)));

this.defaultOrderBys = orderBys != null ? ImmutableList.copyOf(orderBys) : ImmutableList.of();
}
Expand All @@ -119,7 +117,7 @@ public List<DocumentFilter> getStickyFilters()
.build();
}

private Set<Integer> getHUIds()
private CopyOnWriteArraySet<Integer> getHUIds()
{
return huIdsSupplier.get();
}
Expand Down Expand Up @@ -198,6 +196,7 @@ public HUEditorRow getById(final DocumentId rowId) throws EntityNotFoundExceptio
@Override
public void invalidateAll()
{
huIdsSupplier.forget();
huEditorRepo.invalidateCache();
rowsSupplier.forget();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

import org.adempiere.util.collections.IteratorUtils;
import org.adempiere.util.collections.PagedIterator.PageFetcher;
import org.adempiere.util.lang.Mutables;
import org.adempiere.util.lang.SynchronizedMutable;
import org.compiere.util.CCache;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -58,10 +60,11 @@ public class HUEditorViewBuffer_HighVolume implements HUEditorViewBuffer

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

private final AtomicReference<ViewRowIdsOrderedSelection> defaultSelectionRef;

private Supplier<ViewRowIdsOrderedSelection> defaultSelectionFactory;
private final SynchronizedMutable<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 All @@ -75,8 +78,8 @@ public class HUEditorViewBuffer_HighVolume implements HUEditorViewBuffer
this.stickyFilters = ImmutableList.copyOf(stickyFilters);

final List<DocumentFilter> filtersAll = ImmutableList.copyOf(Iterables.concat(stickyFilters, filters));
final ViewRowIdsOrderedSelection defaultSelection = huEditorRepo.createSelection(viewId, filtersAll, orderBys);
defaultSelectionRef = new AtomicReference<>(defaultSelection);
defaultSelectionFactory = () -> huEditorRepo.createSelection(viewId, filtersAll, orderBys);
defaultSelectionRef = Mutables.synchronizedMutable(defaultSelectionFactory.get());
}

@Override
Expand All @@ -87,13 +90,13 @@ public List<DocumentFilter> getStickyFilters()

private ViewRowIdsOrderedSelection getDefaultSelection()
{
return defaultSelectionRef.get();
return defaultSelectionRef.computeIfNull(defaultSelectionFactory);
}

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

if (orderBys == null || orderBys.isEmpty())
{
return defaultSelection;
Expand All @@ -107,14 +110,15 @@ private ViewRowIdsOrderedSelection getSelection(final List<DocumentQueryOrderBy>
return selectionsByOrderBys.computeIfAbsent(ImmutableList.copyOf(orderBys), orderBysImmutable -> huEditorRepo.createSelectionFromSelection(defaultSelection, orderBysImmutable));
}


/** @return true if selection was really changed */
private boolean changeSelection(final UnaryOperator<ViewRowIdsOrderedSelection> mapper)
{
final ViewRowIdsOrderedSelection defaultSelectionOld = defaultSelectionRef.get();
final ViewRowIdsOrderedSelection defaultSelectionNew = defaultSelectionRef.updateAndGet(mapper);
final ViewRowIdsOrderedSelection defaultSelectionOld = defaultSelectionRef.getValue();

defaultSelectionRef.computeIfNull(defaultSelectionFactory); // make sure it's not null (might be null if it was invalidated)
final ViewRowIdsOrderedSelection defaultSelectionNew = defaultSelectionRef.compute(mapper);
selectionsByOrderBys.clear(); // invalidate all the other selections, let it recompute when needed

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

Expand All @@ -133,6 +137,11 @@ public long size()
@Override
public void invalidateAll()
{
defaultSelectionRef.computeIfNotNull(defaultSelection -> {
huEditorRepo.deleteSelection(defaultSelection);
return null;
});

huEditorRepo.invalidateCache();
cache_huRowsById.clear();
}
Expand Down

0 comments on commit 220054c

Please sign in to comment.