-
Notifications
You must be signed in to change notification settings - Fork 0
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
[WIP] Rocks condition with update #22
base: rocks-condition-expr-rebase
Are you sure you want to change the base?
Changes from 62 commits
82faa37
e241645
451dc1d
bf5ba27
2e2ca1c
6760853
830a5b6
a85e2e3
eb77566
3c59809
e4da222
96d9ff4
d66113c
a39be60
70d94ed
b62cb30
6369e55
93e10de
b2a50ea
991541f
fe1ca73
195cb03
95c3b78
2235baf
f687c6c
b50ee84
35faf80
a0ec3f1
acfa4b5
7383a99
1897dd3
9f03244
3da86a5
a4c15a4
14183e1
174cb9e
f89c15b
75c3552
97682b0
4ee03d0
b65deae
b82bb9a
ef42414
cba4685
0413574
ac180df
ade4e47
daa2224
8fde5f2
d7c5f61
861038b
a46d15e
4355b3c
bdafee5
1fe9354
0bdbbfd
2859a1b
4dbce25
177a0c2
9a0d9de
6a86fd6
33d20e3
dc8b996
f6de458
38af3a5
d4adde7
59df7bb
1cdeb7c
1e698b7
eab0e95
d0b5d4e
1fdaae4
c9c27d2
f9c4217
7289cc0
2123d6c
ccb9c42
fa385b6
2ec82e7
a947cc2
f0b903a
b5d5e76
409ff56
ff7e849
4714642
f2b3b1e
27d5731
e2541b0
25817ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
* Copyright (C) 2020 Dremio | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.projectnessie.versioned.rocksdb; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.projectnessie.versioned.store.Entity; | ||
|
||
public class EntityConverter { | ||
|
||
/** | ||
* This converts an Entity into Commit protobuf object. | ||
* @param entity the entity to convert | ||
* @return the object converted from the entity | ||
*/ | ||
public static ValueProtos.Commit entityToCommit(Entity entity) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why public? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to package. |
||
if (!entity.getType().equals(Entity.EntityType.MAP)) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
ValueProtos.Commit.Builder builder = ValueProtos.Commit.newBuilder(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style should be that we append final to everything, unless you have a reason to do so otherwise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
|
||
for (Map.Entry<String, Entity> level0 : entity.getMap().entrySet()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this throw already if it's not a map? |
||
switch (level0.getKey()) { | ||
case RocksRef.COMMITS_ID: | ||
builder.setId(level0.getValue().getBinary()); | ||
break; | ||
case RocksRef.COMMITS_PARENT: | ||
builder.setParent(level0.getValue().getBinary()); | ||
break; | ||
case RocksRef.COMMITS_COMMIT: | ||
builder.setCommit(level0.getValue().getBinary()); | ||
break; | ||
case RocksRef.COMMITS_DELTA: | ||
if (!level0.getValue().getType().equals(Entity.EntityType.LIST)) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
for (Entity level1 : level0.getValue().getList()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this throw if it's not a list already? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Etc. for all other checks of this sort. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are correct. Removed the checks. |
||
builder.addDelta(entityToDelta(level1)); | ||
} | ||
break; | ||
case RocksRef.COMMITS_KEY_LIST: | ||
if (!level0.getValue().getType().equals(Entity.EntityType.LIST)) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
for (Entity level1 : level0.getValue().getList()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
builder.addKeyMutation(entityToKeyMutation(level1)); | ||
} | ||
break; | ||
default: | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* This converts an Entity into Delta protobuf object. | ||
* @param entity the entity to convert | ||
* @return the object converted from the entity | ||
*/ | ||
public static ValueProtos.Delta entityToDelta(Entity entity) { | ||
if (!entity.getType().equals(Entity.EntityType.MAP)) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
ValueProtos.Delta.Builder builder = ValueProtos.Delta.newBuilder(); | ||
|
||
for (Map.Entry<String, Entity> level0 : entity.getMap().entrySet()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these all called level0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed. |
||
switch (level0.getKey()) { | ||
case RocksRef.COMMITS_POSITION: | ||
builder.setPosition((int) level0.getValue().getNumber()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
break; | ||
case RocksRef.COMMITS_OLD_ID: | ||
builder.setOldId(level0.getValue().getBinary()); | ||
break; | ||
case RocksRef.COMMITS_NEW_ID: | ||
builder.setNewId(level0.getValue().getBinary()); | ||
break; | ||
default: | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* This converts an Entity into KeyMutation protobuf object. | ||
* @param entity the entity to convert | ||
* @return the object converted from the entity | ||
*/ | ||
public static ValueProtos.KeyMutation entityToKeyMutation(Entity entity) { | ||
if (!entity.getType().equals(Entity.EntityType.MAP)) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
ValueProtos.KeyMutation.Builder builder = ValueProtos.KeyMutation.newBuilder(); | ||
|
||
for (Map.Entry<String, Entity> level0 : entity.getMap().entrySet()) { | ||
switch (level0.getKey()) { | ||
case RocksRef.COMMITS_KEY_ADDITION: | ||
builder.setType(ValueProtos.KeyMutation.MutationType.ADDITION); | ||
builder.setKey(entityToKey(level0.getValue())); | ||
break; | ||
case RocksRef.COMMITS_KEY_REMOVAL: | ||
builder.setType(ValueProtos.KeyMutation.MutationType.REMOVAL); | ||
builder.setKey(entityToKey(level0.getValue())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. builder.setKey() seems like it should be moved out of the switch given that it's common. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved. |
||
break; | ||
default: | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* This converts an Entity into Key protobuf object. | ||
* @param entity the entity to convert | ||
* @return the object converted from the entity | ||
*/ | ||
public static ValueProtos.Key entityToKey(Entity entity) { | ||
if (!entity.getType().equals(Entity.EntityType.LIST)) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
return ValueProtos.Key.newBuilder().addAllElements( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than collect into an intermediate list, consider using .forEach() to add as you process the stream. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated everywhere that I could find. |
||
entity.getList().stream().map(Entity::getString).collect(Collectors.toList())).build(); | ||
} | ||
|
||
/** | ||
* This converts an Entity into KeyDelta protobuf object. | ||
* @param entity the entity to convert | ||
* @return the object converted from the entity | ||
*/ | ||
public static ValueProtos.KeyDelta entityToKeyDelta(Entity entity) { | ||
if (entity.getType() != Entity.EntityType.MAP) { | ||
throw new UnsupportedOperationException("Invalid value for keyDelta"); | ||
} | ||
|
||
final Map<String, Entity> entityMap = entity.getMap(); | ||
final ValueProtos.KeyDelta.Builder keyDeltaBuilder = ValueProtos.KeyDelta.newBuilder(); | ||
|
||
for (Map.Entry<String, Entity> entry : entityMap.entrySet()) { | ||
switch (entry.getKey()) { | ||
case RocksL3.TREE_ID: | ||
keyDeltaBuilder.setId(entry.getValue().getBinary()); | ||
break; | ||
case RocksL3.TREE_KEY: | ||
keyDeltaBuilder.setKey(entityToKey(entry.getValue())); | ||
break; | ||
default: | ||
throw new UnsupportedOperationException(String.format("Unknown field \"%s\" for keyDelta", entry.getKey())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this the only exception which has an error message in this class? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added messages to the other exceptions. |
||
} | ||
} | ||
|
||
return keyDeltaBuilder.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (C) 2020 Dremio | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.projectnessie.versioned.rocksdb; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.projectnessie.versioned.impl.condition.ExpressionPath; | ||
|
||
class PathPattern { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this class for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A path is a nested structure, with each level wrapped in an optional. Parsing the path directly where it is used was getting really ugly, the deeper the path structure was. This makes it simpler to match a path against a pattern. An example would be to see if the path is fo a key in a keyMutation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, there's a visitor pattern already in the Path, would it be possible to reuse that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be altered to implement ValueVisitor. I'll try that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now PathPattern implements ValueVisitor and is used using the Visitor pattern. |
||
private List<java.util.function.Function<ExpressionPath.PathSegment, Boolean>> pathPatternElements = new ArrayList<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
|
||
public PathPattern nameEquals(String name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why public? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason, changed to package. |
||
pathPatternElements.add((p) -> p != null && p.isName() && name.equals(p.asName().getName())); | ||
return this; | ||
} | ||
|
||
public PathPattern anyName() { | ||
pathPatternElements.add((p) -> p != null && p.isName()); | ||
return this; | ||
} | ||
|
||
public PathPattern positionEquals(int position) { | ||
pathPatternElements.add((p) -> p != null && p.isPosition() && position == p.asPosition().getPosition()); | ||
return this; | ||
} | ||
|
||
public PathPattern anyPosition() { | ||
pathPatternElements.add((p) -> p != null && p.isPosition()); | ||
return this; | ||
} | ||
|
||
public boolean matches(ExpressionPath.PathSegment path) { | ||
for (java.util.function.Function<ExpressionPath.PathSegment, Boolean> pathPatternElement : pathPatternElements) { | ||
if (!pathPatternElement.apply(path)) { | ||
return false; | ||
} else if (path == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be switched such that the null check is first? |
||
return false; | ||
} | ||
|
||
path = path.getChild().orElse(null); | ||
} | ||
|
||
return path == null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why public? JavaDocs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason for public. Added JavaDocs.