@Override public void add(Entity e) { Preconditions.checkState(entityLists != null, "build() already called"); if (e.hasAttribute(attributeName)) { long value = e.getLong(attributeName); ImmutableList.Builder<Entity> lb = entityLists.get(value); if (lb == null) { lb = ImmutableList.builder(); entityLists.put(value, lb); } lb.add(e); } }
@Override public Map<Long,List<Entity>> grouped(TypedName<Long> attr) { Preconditions.checkArgument(attr != CommonAttributes.ENTITY_ID, "cannot group by entity ID"); EntityIndex idx = indexes.get(attr.getName()); if (idx == null) { return store.values() .stream() .filter(e -> e.hasAttribute(attr)) .collect(Collectors.groupingBy(e -> e.getLong(attr))); } else { return idx.getValues() .stream() .collect(Collectors.toMap(l -> (Long) l, idx::getEntities)); } }
@Override public Map<Long, List<Entity>> grouped(TypedName<Long> attr) { Preconditions.checkArgument(attr != CommonAttributes.ENTITY_ID, "cannot group by entity ID"); int idx = attributes.lookup(attr); if (idx < 0) { return Collections.emptyMap(); } PackIndex index = indexes[idx]; if (index != null) { return index.getValues() .stream() .collect(Collectors.toMap(l -> (Long) l, l -> new EntityList(index.getPositions(l)))); } else { return stream() .filter(e -> e.hasAttribute(attr)) .collect(Collectors.groupingBy(e -> e.getLong(attr))); } }
public LongSet getTrainItems() { LongSet items = trainItems; if (items == null) { items = new LongOpenHashSet(); for (Entity e : trainHistory) { if(e.hasAttribute(CommonAttributes.ITEM_ID)) { items.add(e.getLong(CommonAttributes.ITEM_ID)); } } trainItems = items; } return items; }
public LongSet getTestItems() { LongSet items = testItems; if (items == null) { items = new LongOpenHashSet(); for (Entity e : testHistory) { if(e.hasAttribute(CommonAttributes.ITEM_ID)) { items.add(e.getLong(CommonAttributes.ITEM_ID)); } } testItems = items; } return items; }
/** * Derive bare entities from the values in another type of entity. This method only consults the entities added * so far, so it should be called *after* all other calls to {@link #addEntity(Entity)} and friends. If an entity * has already been added with the same type and ID as one of the derived entities, it is kept instead of the derived * entity. * * @param derived The derived entity type. * @param source The source entity type. * @param attr The source attribute. * @return The builder (for chaining). */ public EntityCollectionDAOBuilder deriveEntities(EntityType derived, EntityType source, TypedName<Long> attr) { EntityCollectionBuilder src = entitySets.get(source); if (src == null) { // no source entities, skip return this; } EntityCollectionBuilder ecb = entitySets.get(derived); if (ecb == null) { ecb = EntityCollection.newBareBuilder(derived); entitySets.put(derived, ecb); } for (Entity e: src.entities()) { if (e.hasAttribute(attr)) { long key = e.getLong(attr); ecb.add(Entities.create(derived, key), false); } } return this; }
@Override public LongSet valueSet(TypedName<Long> attr) { LongSet values = new LongOpenHashSet(); try (ObjectStream<E> stream = stream()) { for (E entity: stream) { if (entity.hasAttribute(attr)) { values.add(entity.getLong(attr)); } } } return values; } }
@Test public void testBareSetters() { EntityBuilder eb = Entities.newBuilder(CommonTypes.USER) .setId(42); Entity e = eb.build(); assertThat(e, notNullValue()); assertThat(e.getType(), equalTo(CommonTypes.USER)); assertThat(e.getId(), equalTo(42L)); assertThat(e.getTypedAttributeNames(), contains((TypedName) CommonAttributes.ENTITY_ID)); assertThat(e.getAttributeNames(), contains("id")); assertThat(e.hasAttribute("user"), equalTo(false)); assertThat(e.hasAttribute(CommonAttributes.USER_ID), equalTo(false)); }
@Test public void testBasicEntity() { Entity e = Entities.newBuilder(CommonTypes.USER, 42) .setAttribute(CommonAttributes.NAME, "HACKEM MUCHE") .build(); assertThat(e, notNullValue()); assertThat(e.getType(), equalTo(CommonTypes.USER)); assertThat(e.getId(), equalTo(42L)); assertThat(e.getTypedAttributeNames(), containsInAnyOrder((TypedName) CommonAttributes.NAME, CommonAttributes.ENTITY_ID)); assertThat(e.getAttributeNames(), containsInAnyOrder("name", "id")); assertThat(e.get("name"), equalTo((Object) "HACKEM MUCHE")); assertThat(e.get(CommonAttributes.NAME), equalTo("HACKEM MUCHE")); assertThat(e.hasAttribute("name"), equalTo(true)); assertThat(e.hasAttribute("user"), equalTo(false)); assertThat(e.hasAttribute(CommonAttributes.NAME), equalTo(true)); assertThat(e.hasAttribute(CommonAttributes.USER_ID), equalTo(false)); assertThat(e.get(CommonAttributes.ENTITY_ID), equalTo(42L)); } }
@Test public void testAttributeSetters() { EntityBuilder eb = Entities.newBuilder(CommonTypes.USER) .setAttribute(CommonAttributes.ENTITY_ID, 42L); Entity e = eb.build(); assertThat(e, notNullValue()); assertThat(e.getType(), equalTo(CommonTypes.USER)); assertThat(e.getId(), equalTo(42L)); assertThat(e.getTypedAttributeNames(), contains((TypedName) CommonAttributes.ENTITY_ID)); assertThat(e.getAttributeNames(), contains("id")); assertThat(e.hasAttribute("user"), equalTo(false)); assertThat(e.hasAttribute(CommonAttributes.USER_ID), equalTo(false)); assertThat(e.get(CommonAttributes.ENTITY_ID), equalTo(42L)); }
@Test public void testThingNullField() { JSONEntityFormat fmt = new JSONEntityFormat(); fmt.setEntityType(CommonTypes.ITEM); fmt.addAttribute(CommonAttributes.ENTITY_ID); fmt.addAttribute("title", CommonAttributes.NAME); LineEntityParser lep = fmt.makeParser(Collections.EMPTY_LIST); Entity res = lep.parse("{\"id\": 204, \"title\": null, \"extra\": \"wumpus\"}"); assertThat(res, notNullValue()); assertThat(res.getId(), equalTo(204L)); assertThat(res.maybeGet(CommonAttributes.NAME), nullValue()); assertThat(res.hasAttribute("extra"), equalTo(false)); }
@Test public void testThingFields() { JSONEntityFormat fmt = new JSONEntityFormat(); fmt.setEntityType(CommonTypes.ITEM); fmt.addAttribute(CommonAttributes.ENTITY_ID); fmt.addAttribute("title", CommonAttributes.NAME); LineEntityParser lep = fmt.makeParser(Collections.EMPTY_LIST); Entity res = lep.parse("{\"id\": 204, \"title\": \"hamster\", \"extra\": \"wumpus\"}"); assertThat(res, notNullValue()); assertThat(res.getId(), equalTo(204L)); assertThat(res.get(CommonAttributes.NAME), equalTo("hamster")); assertThat(res.hasAttribute("extra"), equalTo(false)); }
assertThat(first.get(CommonAttributes.USER_ID), equalTo(10L)); assertThat(first.get(CommonAttributes.RATING), equalTo(3.5)); assertThat(first.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false)); assertThat(second.get(CommonAttributes.USER_ID), equalTo(11L)); assertThat(second.get(CommonAttributes.RATING), equalTo(4.0)); assertThat(second.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false));
assertThat(first.get(CommonAttributes.USER_ID), equalTo(20L)); assertThat(first.get(CommonAttributes.RATING), equalTo(3.5)); assertThat(first.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false)); assertThat(second.get(CommonAttributes.USER_ID), equalTo(20L)); assertThat(second.get(CommonAttributes.RATING), equalTo(4.0)); assertThat(second.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false));
assertThat(first.get(CommonAttributes.ITEM_ID), equalTo(20L)); assertThat(first.get(CommonAttributes.RATING), equalTo(3.5)); assertThat(first.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false)); assertThat(second.get(CommonAttributes.ITEM_ID), equalTo(20L)); assertThat(second.get(CommonAttributes.RATING), equalTo(4.0)); assertThat(second.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false)); assertThat(((Rating) second).getUserId(),
assertThat(first.get(CommonAttributes.USER_ID), equalTo(20L)); assertThat(first.get(CommonAttributes.RATING), equalTo(3.5)); assertThat(first.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false)); assertThat(second.get(CommonAttributes.USER_ID), equalTo(20L)); assertThat(second.get(CommonAttributes.RATING), equalTo(4.0)); assertThat(second.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false));
@Override public void add(Entity e) { Preconditions.checkState(entityLists != null, "build() already called"); if (e.hasAttribute(attributeName)) { long value = e.getLong(attributeName); ImmutableList.Builder<Entity> lb = entityLists.get(value); if (lb == null) { lb = ImmutableList.builder(); entityLists.put(value, lb); } lb.add(e); } }
@Override public Map<Long,List<Entity>> grouped(TypedName<Long> attr) { Preconditions.checkArgument(attr != CommonAttributes.ENTITY_ID, "cannot group by entity ID"); EntityIndex idx = indexes.get(attr.getName()); if (idx == null) { return store.values() .stream() .filter(e -> e.hasAttribute(attr)) .collect(Collectors.groupingBy(e -> e.getLong(attr))); } else { return idx.getValues() .stream() .collect(Collectors.toMap(l -> (Long) l, idx::getEntities)); } }
@Override public Map<Long, List<Entity>> grouped(TypedName<Long> attr) { Preconditions.checkArgument(attr != CommonAttributes.ENTITY_ID, "cannot group by entity ID"); int idx = attributes.lookup(attr); if (idx < 0) { return Collections.emptyMap(); } PackIndex index = indexes[idx]; if (index != null) { return index.getValues() .stream() .collect(Collectors.toMap(l -> (Long) l, l -> new EntityList(index.getPositions(l)))); } else { return stream() .filter(e -> e.hasAttribute(attr)) .collect(Collectors.groupingBy(e -> e.getLong(attr))); } }
@Override public LongSet valueSet(TypedName<Long> attr) { LongSet values = new LongOpenHashSet(); try (ObjectStream<E> stream = stream()) { for (E entity: stream) { if (entity.hasAttribute(attr)) { values.add(entity.getLong(attr)); } } } return values; } }