Netflix Hollow with Collections API

Viewed 166

I am writing a Map<Integer, List<String>> type object, and trying to read it back. I also tried wrapping this in a pojo, it still doesn't work. Appreciate if someone can share how to get any Collection/Map class work with Hollow. Here is my wrapper :-

import java.util.Map;

public class MapWrapper<K, V> {
   public final Map<K, V> map;

   public MapWrapper(Map<K, V> map) {
      this.map = map;
   }
}

And this is my test case (test is failing on initializeTypeState line) :-

import com.netflix.hollow.api.codegen.HollowAPIGenerator;
import com.netflix.hollow.api.objects.generic.GenericHollowObject;
import com.netflix.hollow.core.read.dataaccess.HollowDataAccess;
import com.netflix.hollow.core.write.HollowWriteStateEngine;
import com.netflix.hollow.core.write.objectmapper.HollowObjectMapper;
import com.netflix.hollow.test.HollowWriteStateEngineBuilder;
import com.netflix.hollow.test.consumer.TestAnnouncementWatcher;
import com.netflix.hollow.test.consumer.TestBlobRetriever;
import com.netflix.hollow.test.consumer.TestHollowConsumer;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CollectionsReadTests {


@Test
public void generateMapClasses() throws IOException {
    MapWrapper<Integer, String> testMap = new MapWrapper(new HashMap<>());
    HollowWriteStateEngine writeEngine = new HollowWriteStateEngine();
    HollowObjectMapper mapper = new HollowObjectMapper(writeEngine);
    mapper.initializeTypeState(testMap.getClass());

    HollowAPIGenerator generator =
            new HollowAPIGenerator.Builder().withAPIClassname("CollectionAPI")
                    .withPackageName("com.test.hollow.api")
                    .withDataModel(writeEngine)
                    .withDestination("/tmp/hollow/")
                    .build();

    generator.generateSourceFiles();
}

}
1 Answers

Hollow has no way to know that your Map is holding <Integer, String> because the class is passed to initializeTypeState.

What you are doing will work if you change MapWrapper class so it's no longer templated.

E.g.

import java.util.Map;

public class MapWrapper {
   public final Map<Integer, String> map;

   public MapWrapper(Map<Integer, String> map) {
      this.map = map;
   }
}
Related