Skip to content

Commit

Permalink
Returning a copied collection to avoid corrupted iterator by multiple…
Browse files Browse the repository at this point in the history
… threads

Signed-off-by: ntisseyre <[email protected]>
  • Loading branch information
ntisseyre committed Feb 26, 2024
1 parent 1530f0a commit 9b1fa1f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface AddedRelationsContainer {
* of the transaction after there are no additional changes. Otherwise the behavior is non deterministic.
* @return
*/
Collection<InternalRelation> getAll();
Collection<InternalRelation> getAllUnsafe();

/**
* Clears the container which releases allocated memory.
Expand Down Expand Up @@ -69,7 +69,7 @@ public boolean isEmpty() {
}

@Override
public Collection<InternalRelation> getAll() {
public Collection<InternalRelation> getAllUnsafe() {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package org.janusgraph.graphdb.transaction.addedrelations;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.janusgraph.graphdb.internal.InternalRelation;

import java.util.ArrayList;
import java.util.Collection;

/**
Expand All @@ -37,11 +39,17 @@ public synchronized boolean remove(final InternalRelation relation) {

@Override
public synchronized Iterable<InternalRelation> getView(final Predicate<InternalRelation> filter) {
return super.getView(filter);
return copyView(super.getView(filter));
}

@Override
public synchronized Collection<InternalRelation> getAll() {
return super.getAll();
public synchronized Collection<InternalRelation> getAllUnsafe() {
return super.getAllUnsafe();
}

private Iterable<InternalRelation> copyView(Iterable<InternalRelation> currentView) {
ArrayList<InternalRelation> view = new ArrayList<>();
Iterables.addAll(view, currentView);
return view;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean isEmpty() {
}

@Override
public Collection<InternalRelation> getAll() {
public Collection<InternalRelation> getAllUnsafe() {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean isEmpty() {
}

@Override
public Collection<InternalRelation> getAll() {
public Collection<InternalRelation> getAllUnsafe() {
return Collections.unmodifiableCollection(new AbstractCollection<InternalRelation>() {
@Override
@Nonnull
Expand Down

0 comments on commit 9b1fa1f

Please sign in to comment.