@Override public boolean hasAttribute(TypedName<?> name) { return layout.attributes.contains(name); }
@Override public Entity build() { Preconditions.checkState(type != null, "Entity type not set"); Preconditions.checkState(idSet, "ID not set"); if (attributes.isEmpty() || attributes.keySet().equals(Collections.singleton(CommonAttributes.ENTITY_ID))) { return new BareEntity(type, id); } else { List<TypedName<?>> names = new ArrayList<>(attributes.size()); names.add(CommonAttributes.ENTITY_ID); for (Attribute<?> a: attributes.values()) { names.add(a.getTypedName()); } assert names.lastIndexOf(CommonAttributes.ENTITY_ID) == 0; AttributeSet aset = AttributeSet.create(names); Object[] values = new Object[aset.size()]; for (Attribute<?> a: attributes.values()) { int i = aset.lookup(a.getTypedName()); values[i-1] = a.getValue(); } return new BasicEntity(type, id, aset, values); } } }
protected static BeanEntityLayout makeLayout(Class<? extends AbstractBeanEntity> type) { BeanEntityLayout res = cache.get(type); if (res != null) { return res; } DynamicClassLoader dlc = new DynamicClassLoader(type.getClassLoader()); Map<String, BeanAttributeGetter> attrs = new HashMap<>(); List<TypedName<?>> names = new ArrayList<>(); for (Method m: type.getMethods()) { EntityAttribute annot = m.getAnnotation(EntityAttribute.class); if (annot != null) { BeanAttributeGetter gfunc = generateGetter(dlc, type, m); attrs.put(annot.value(), gfunc); names.add(TypedName.create(annot.value(), TypeToken.of(m.getGenericReturnType()))); } } AttributeSet aset = AttributeSet.create(names); ImmutableList.Builder<BeanAttributeGetter> mhlb = ImmutableList.builder(); for (String name: aset.nameSet()) { mhlb.add(attrs.get(name)); } res = new BeanEntityLayout(aset, mhlb.build()); cache.put(type, res); return res; }
/** * Create a new packed entity collection builder. * @param type The entity type. * @param attrs The attributes to store. * @return An entity collection builder. */ public static EntityCollectionBuilder newBuilder(EntityType type, AttributeSet attrs) { Preconditions.checkArgument(attrs.lookup(CommonAttributes.ENTITY_ID) == 0, "could not find entity ID in: %s", attrs); if (attrs.size() > 1) { return new PackedEntityCollectionBuilder(type, attrs, null); } else { return new BareEntityCollectionBuilder(type); } }
@Override public Iterator<Attribute<?>> iterator() { return (Iterator) IntStream.range(0, layout.attributes.size()) .mapToObj(i -> { Object val = layout.getters.get(i).get(AbstractBeanEntity.this); if (val == null) { return null; } else { return Attribute.create((TypedName) layout.attributes.getAttribute(i), val); } }) .filter(Predicates.notNull()) .iterator(); }
@Override public Set<TypedName<?>> getTypedAttributeNames() { int missing = 0; int na = attributes.size(); for (int i = 0; i < na; i++) { if (attrStores[i].isNull(position)) { missing |= 1 << i; } } if (missing != 0) { AttributeSet set = attrSets.get(missing); if (set == null) { set = AttributeSet.create(IntStream.range(0, na) .filter(i -> !attrStores[i].isNull(position)) .mapToObj(attributes::getAttribute) .collect(Collectors.toList())); attrSets.put(missing, set); } return set; } return attributes; }
/** * Look up an attribute. * @param name The attribute. * @return The attribute's index, or a negative value if it does not exist. Nonexistence is further * differentiated with -1 for no attribute, -2 for type mismatch. */ public int lookup(TypedName<?> name) { return lookup(name, false); }
@Test public void testBasicSingleAttribute() { AttributeSet attrs = AttributeSet.create(CommonAttributes.ENTITY_ID); assertThat(attrs.size(), equalTo(1)); assertThat(attrs.lookup(CommonAttributes.ENTITY_ID), equalTo(0)); assertThat(attrs.lookup(CommonAttributes.ITEM_ID), equalTo(-1)); assertThat(attrs, contains(CommonAttributes.ENTITY_ID)); assertThat(attrs.contains(CommonAttributes.ENTITY_ID), equalTo(true)); assertThat(attrs.contains(CommonAttributes.NAME), equalTo(false)); }
private Object readResolve() throws ObjectStreamException { return create(names); } }
@Override public int size() { return attributes.size(); } };
@Override public Set<String> getAttributeNames() { return layout.attributes.nameSet(); }
private PackIndex buildIndex(int aidx) { TypedName<?> tn = attributes.getAttribute(aidx); logger.debug("indexing column {} of {}", tn, entityType); PackIndex.Builder builder; if (tn.getRawType().equals(Long.class)) { builder = new PackIndex.LongBuilder(); } else { builder = new PackIndex.GenericBuilder(); } for (int i = 0; i < size; i++) { builder.add(storeBuilders[aidx].get(i), i); } return builder.build(); }
/** * Create a new attribute set. * @param names The list of attributes. * @return The attribute set. */ public static AttributeSet create(List<? extends TypedName<?>> names) { Preconditions.checkArgument(names.size() > 0, "no attribute names"); TypedName[] arr = names.toArray(new TypedName[names.size()]); if (arr[0] != CommonAttributes.ENTITY_ID) { int iidx = names.indexOf(CommonAttributes.ENTITY_ID); if (iidx >= 0) { TypedName n = arr[0]; arr[0] = arr[iidx]; arr[iidx] = n; assert arr[0] == CommonAttributes.ENTITY_ID; } } return setCache.intern(new AttributeSet(arr)); }
@Override public EntityCollectionBuilder addIndex(String attrName) { int pos = attributes.lookup(attrName); if (pos >= 0) { needIndex[pos] = true; } return this; }
@Override public Iterator<Attribute<?>> iterator() { return (Iterator) IntStream.range(0, attributeNames.size()) .mapToObj(i -> { if (i == 0) { return Attribute.create(CommonAttributes.ENTITY_ID, getId()); } Object val = attributeValues[i-1]; if (val == null) { return null; } else { return Attribute.create((TypedName) attributeNames.getAttribute(i), val); } }) .filter(Predicates.notNull()) .iterator(); }
/** * Create a new packed entity collection builder. * @param type The entity type. * @param attrs The attributes to store. * @param eb An entity builder to use when reconstituting entities. * @return An entity collection builder. */ public static EntityCollectionBuilder newBuilder(EntityType type, AttributeSet attrs, Class<? extends EntityBuilder> eb) { Preconditions.checkArgument(attrs.lookup(CommonAttributes.ENTITY_ID) == 0, "could not find entity ID in: %s", attrs); if (attrs.size() > 1) { return new PackedEntityCollectionBuilder(type, attrs, eb); } else { return new BareEntityCollectionBuilder(type); } }
/** * Create a new attribute set. * @param names The list of attributes. * @return The attribute set. */ public static AttributeSet create(TypedName<?>... names) { return create(Arrays.asList(names)); }
@Override public int size() { return layout.attributes.size(); } };
@Override public Set<TypedName<?>> getTypedAttributeNames() { int missing = 0; int na = attributes.size(); for (int i = 0; i < na; i++) { if (attrStores[i].isNull(position)) { missing |= 1 << i; } } if (missing != 0) { AttributeSet set = attrSets.get(missing); if (set == null) { set = AttributeSet.create(IntStream.range(0, na) .filter(i -> !attrStores[i].isNull(position)) .mapToObj(attributes::getAttribute) .collect(Collectors.toList())); attrSets.put(missing, set); } return set; } return attributes; }