Skip to content

add DeepCopy to ColumnVisibility #5217

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

Closed
wants to merge 5 commits into from
Closed
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 @@ -125,6 +125,24 @@ public Node(int start, int end) {
this.end = end;
}

/**
* Creates a new node by performing a deep copy of an existing node object
*
* @param node Node object
* @since 2.1.4
*/
public Node(Node node) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since Node is removed in newer versions of accumulo this constructor could be switched to private to avoid standardizing its usage

Suggested change
public Node(Node node) {
private Node(Node node) {

List<Node> childrenNew =
node.children.isEmpty() ? EMPTY : new ArrayList<>(node.children.size());
for (Node child : node.children) {
childrenNew.add(new Node(child));
}
this.type = node.type;
this.start = node.start;
this.end = node.end;
this.children = childrenNew;
}

public void add(Node child) {
if (children == EMPTY) {
children = new ArrayList<>();
Expand Down Expand Up @@ -505,6 +523,19 @@ public ColumnVisibility(byte[] expression) {
validate(expression);
}

/**
* Creates a new column visibility by performing a deep copy of an existing column visibility
* object
*
* @param visibility ColumnVisibility object
* @since 2.1.4
*/
public ColumnVisibility(ColumnVisibility visibility) {
byte[] incomingExpression = visibility.expression;
this.expression = Arrays.copyOf(incomingExpression, incomingExpression.length);
this.node = new Node(visibility.node);
Copy link
Contributor

Choose a reason for hiding this comment

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

I made a comment in #5293 that is applicable here also. I think it's possible that another thread could be mutating the parse tree (visibility.node) while this thread is trying to make a copy of the ColumnVisibility because the parse tree is accessible and mutable.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am experimenting w/ adding an immutable parse tree to accumulo-access as a possible way to address this.

}

@Override
public String toString() {
return "[" + new String(expression, UTF_8) + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.apache.accumulo.core.security.ColumnVisibility.quote;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -238,6 +239,15 @@ public void testParseTreesOrdering() {
assertTrue(flat.indexOf('b') < flat.indexOf('a'), "shortest children sort first");
}

@Test
public void testDeepCopy() {
ColumnVisibility cv1 = new ColumnVisibility("(b&c&d)|((a|m)&y&z)|(e&f)");
ColumnVisibility cv2 = new ColumnVisibility(cv1);
assertNotSame(cv1.getExpression(), cv2.getExpression());
assertNotSame(cv1.getParseTree(), cv2.getParseTree());
assertEquals(cv1.toString(), cv2.toString());
}

private Node parse(String s) {
ColumnVisibility v = new ColumnVisibility(s);
return v.getParseTree();
Expand Down