为什么Collection属性在客观化中被索引?

时间:2016-06-04 20:11:38

标签: google-app-engine objectify google-cloud-datastore

在客观化中,当我使用String数据类型定义集合属性时,

@IgnoreSave(IfEmpty.class)
private Set<String> collectionProperty = new HashSet<>();

然后查看数据存储区中的记录,即使我没有使用@Index对其进行注释,它也会显示为索引。

相反,当我使用复杂的Object代替String时,它不会显示为索引。

为什么Collection属性有时会被索引,有时不会被索引?有没有办法确定这个?

-

来自管理控制台/数据存储区的未经修改的代码和屏幕截图:

@Entity
@Cache(expirationSeconds = 900)
public class Item extends StringId implements Serializable {
    private static final Logger log = Logger.getLogger(Item.class.getSimpleName());
    private static final long serialVersionUID = 1;


    // Constructors
    private Item() {}

    @Nonnull
    private static Item create(@Nonnull String itemId) {
        Item item = (Item) new Item().setId(itemId);
        item.piecesFromId();
        log.info("item = " + JsonHelper.logToJson(item));
        return item;
    }

    @Nonnull
    public static Item create(@Nonnull String provider, @Nonnull String type, @Nonnull String identifier) {
        String itemId = IdHelper.createItemId(provider, type, identifier);
        Item item = ((Item) new Item().setId(itemId))
                .setProvider(provider)
                .setType(type)
                .setIdentifier(identifier);
        log.info("item = " + JsonHelper.logToJson(item));
        return item;
    }

    @Nonnull
    public static Item loadOrCreate(@Nonnull String itemId) {
        Item item = ofy().load().type(Item.class).id(itemId).now();
        if (item == null) {
            item = Item.create(itemId);
        }
        return item;
    }

    @Nullable
    public static Item load(@Nonnull String itemId) {
        return ofy().load().type(Item.class).id(itemId).now();
    }

    @OnLoad
    private void piecesFromId() {
        provider = IdHelper.getProvider(id);
        type = IdHelper.getType(id);
        identifier = IdHelper.getIdentifier(id);
    }

    public Item save() {
        ofy().defer().save().entity(this);
        return this;
    }

    @OnSave
    private void integrity() {
        if (id == null) { throw new RuntimeException("Id must not be null."); }
        if (itemPreview == null) { throw new RuntimeException("itemPreview must not be null."); }
        if (provider == null || type == null || identifier == null) { throw new RuntimeException("provider, type and identifier must not be null."); }
        if (!id.equals(IdHelper.createItemId(provider, type, identifier))) { throw new RuntimeException("id does not coincide with provider, type and identifier."); }
        if (!id.equals(itemPreview.getItemId())) { throw new RuntimeException("id does not coincide with id in itemPreview."); }
    }

    @OnSave
    private void timestamp() {
        if (created == null) {
            created = System.currentTimeMillis();
        }
    }

    // Properties
    @Ignore
    private String provider;

    @Ignore
    private String type;

    @Ignore
    private String identifier;

    @Ignore // json
    private ItemPreview itemPreview;

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    @IgnoreSave(IfEmpty.class)
    private Set<String> subscribedUserIds = new HashSet<>();

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    @IgnoreSave(IfEmpty.class)
    private Set<String> notifyUserIds = new HashSet<>();

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    @IgnoreSave(IfEmpty.class)
    private Set<String> blacklistingUserIds = new HashSet<>();

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    private Long created;

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    @IgnoreSave(IfDefault.class)
    @Index
    private Status status = Status.ACTIVE;

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    @IgnoreSave(IfNull.class)
    private String suspensionNotice;


    // Json
    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    @IgnoreSave(IfNull.class)
    private String itemPreviewJson;

    private static Type itemPreviewType = new TypeToken<ItemPreview>(){}.getType();

    @OnLoad
    private void itemPreviewFromJson() {
        if (itemPreviewJson != null) {
            itemPreview = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
                    .fromJson(itemPreviewJson, itemPreviewType);
        }
    }

    @OnSave
    private void itemPreviewToJson() {
        itemPreviewJson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
                .toJson(itemPreview, itemPreviewType);
    }


    // Accessors
    public String getProvider() {
        return provider;
    }

    public Item setProvider(String provider) {
        this.provider = provider;
        return this;
    }

    public String getType() {
        return type;
    }

    public Item setType(String type) {
        this.type = type;
        return this;
    }

    public String getIdentifier() {
        return identifier;
    }

    public Item setIdentifier(String identifier) {
        this.identifier = identifier;
        return this;
    }

    public ItemPreview getItemPreview() {
        return itemPreview;
    }

    public Item setItemPreview(ItemPreview itemPreview) {
        this.itemPreview = itemPreview;
        return this;
    }

    public Set<String> getSubscribedUserIds() {
        return subscribedUserIds;
    }

    public Item setSubscribedUserIds(Set<String> subscribedUserIds) {
        this.subscribedUserIds = subscribedUserIds;
        return this;
    }

    public Set<String> getNotifyUserIds() {
        return notifyUserIds;
    }

    public Item setNotifyUserIds(Set<String> notifyUserIds) {
        this.notifyUserIds = notifyUserIds;
        return this;
    }

    public Set<String> getBlacklistingUserIds() {
        return blacklistingUserIds;
    }

    public Item setBlacklistingUserIds(Set<String> blacklistingUserIds) {
        this.blacklistingUserIds = blacklistingUserIds;
        return this;
    }

    public Long getCreated() {
        return created;
    }

    public Item setCreated(Long created) {
        this.created = created;
        return this;
    }

    public Status getStatus() {
        return status;
    }

    public Item setStatus(Status status) {
        this.status = status;
        return this;
    }

    public String getSuspensionNotice() {
        return suspensionNotice;
    }

    public Item setSuspensionNotice(String suspensionNotice) {
        this.suspensionNotice = suspensionNotice;
        return this;
    }


    // Collections
    public static Map<String, Item> loadAll(Set<String> itemIds) {
        return ofy().load().type(Item.class).ids(itemIds);
    }

}

subscribedUserIds is indexed

0 个答案:

没有答案