0 Replies Latest reply on Jan 18, 2018 6:10 AM by 3480917

    modeling secondary indices

    3480917

      Hello everyone,

       

      In my project, we inherited a code which has an entity modeled this way

       

      @Entity

      class DbEntity {

       

      @PrimaryKey

      private String itemName;

       

      @SecondaryKey(relate = Relationship.MANY_TO_MANY)

      private List<Integer> zipCodes;

       

       

      @SecondaryKey(relate = Relationship.MANY_TO_MANY)

      private List<Integer> storeIds;

      }

       

       

      The application takes a csv file as input. Each row in the csv file has an item and we call an API to get the list of zipCodes and stores in which the item is available.

       

       

      DbEntity entity = new DbEntity();

      entity.setItemName(// from file);

      entity.setZipCodes(// from API);

      entity.setStoreIds(// from API);

       

      primaryIndex.putNoReturn(entity);

       

       

      On an avg, each item is mapped to 50 different zipcodes and 1000s of store ids.

       

       

      The primary use case is to return the list of items given a zipcode or a store id.

       

       

      The entity is read back from the db using the secondary indices and *never* using the primary index.

       

      zipCodeSecondaryIndex.keys() // returns an Entity Cursor and calling nextNoDup returns unique zipcode.

      zipCodeSecondaryIndex.subIndex(zipCode).entities() returns all items for the given zipcode.

       

       

      This doesn't seem natural to me.

       

       

      Is this modeling valid? 

       

       

      One obvious thing is, it uses more disk space to maintain the index. Are there other constraints/drawbacks?

       

      Version: JE 4.1.10

       

       

      - Thanks!