Skip to content

Commit

Permalink
Merge pull request Terracotta-OSS#200 from lorban/issue-199
Browse files Browse the repository at this point in the history
Revert "lazily initialize metadata to be able to create struct 'alias…
  • Loading branch information
mathieucarbou authored Nov 24, 2016
2 parents 8a9833e + a7dfad3 commit 128f15f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 29 deletions.
17 changes: 4 additions & 13 deletions runnel/src/main/java/org/terracotta/runnel/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,12 @@
*/
public class Metadata {

private final List<? extends Field> metadata;
private Map<String, Field> fieldsByName;
private final Map<String, Field> fieldsByName;

public Metadata(List<? extends Field> metadata) {
this.metadata = metadata;
}

private void initFields(List<? extends Field> metadata) {
if (fieldsByName == null) {
fieldsByName = new HashMap<String, Field>(metadata.size(), 1.0F);
for (Field field : metadata) {
fieldsByName.put(field.name(), field);
}
fieldsByName = new HashMap<String, Field>();
for (Field field : metadata) {
fieldsByName.put(field.name(), field);
}
}

Expand All @@ -52,7 +45,6 @@ public FieldDecoder fieldDecoder(ReadBuffer readBuffer) {
}

public Map<Integer, Field> buildFieldsByIndexMap() {
initFields(metadata);
Map<Integer, Field> map = new HashMap<Integer, Field>();
for (Field field : fieldsByName.values()) {
map.put(field.index(), field);
Expand All @@ -61,7 +53,6 @@ public Map<Integer, Field> buildFieldsByIndexMap() {
}

Field getFieldByName(String name) {
initFields(metadata);
return fieldsByName.get(name);
}

Expand Down
16 changes: 0 additions & 16 deletions runnel/src/test/java/org/terracotta/runnel/StructBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,4 @@ public void checkValidConfigWorks() throws Exception {
StructBuilder.newStructBuilder().enm("a", 2, ENM).enm("b", 3, ENM);
}

/**
* Check that when calling StructBuilder.build(), then modifying the builder again then
* building a second struct, we do end up with two identical structs.
*
* Ugly, but that's the quick way to solve the struct loop struct1 -> struct2 -> struct1 problem.
*/
@Test
public void checkLazyStructAliasesWork() throws Exception {
StructBuilder structBuilder = StructBuilder.newStructBuilder();

Struct lazilyInitializedStruct = structBuilder.build();
Struct struct = structBuilder.int32("a", 2).int32("b", 3).build();

lazilyInitializedStruct.encoder().int32("a", 1).int32("b", -1).encode();
struct.encoder().int32("a", 1).int32("b", -1).encode();
}
}

0 comments on commit 128f15f

Please sign in to comment.