Refine search
@SuppressWarnings("unchecked") @Override public final List<Entity> add(FullEntity<?>... entities) { validateActive(); List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (!(key instanceof Key)) { if (!incompleteKeys.isEmpty()) { IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class); allocated = getDatastore().allocateId(toAllocate).iterator(); } else { allocated = Collections.emptyIterator(); if (entity.getKey() instanceof Key) { addInternal((FullEntity<Key>) entity); answer.add(Entity.convert((FullEntity<Key>) entity)); } else { Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build(); addInternal(entityWithAllocatedId); answer.add(entityWithAllocatedId);
private void addInternal(FullEntity<Key> entity) { Key key = entity.getKey(); if (toAdd.containsKey(key) || toUpdate.containsKey(key) || toPut.containsKey(key)) { throw newInvalidRequest( "Entity with the key %s was already added or updated in this %s", entity.getKey(), name); } if (toDelete.remove(key)) { toPut.put(key, entity); } else { toAdd.put(key, entity); } }
@SuppressWarnings("unchecked") @Override public final List<Entity> put(FullEntity<?>... entities) { validateActive(); List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (!(key instanceof Key)) { if (!incompleteKeys.isEmpty()) { IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class); allocated = getDatastore().allocateId(toAllocate).iterator(); } else { allocated = Collections.emptyIterator(); if (entity.getKey() instanceof Key) { putInternal((FullEntity<Key>) entity); answer.add(Entity.convert((FullEntity<Key>) entity)); } else { Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build(); putInternal(entityWithAllocatedId); answer.add(entityWithAllocatedId);
@SuppressWarnings("unchecked") @Override public final void addWithDeferredIdAllocation(FullEntity<?>... entities) { validateActive(); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (key instanceof Key) { addInternal((FullEntity<Key>) entity); } else { toAddAutoId.add((FullEntity<IncompleteKey>) entity); } } }
@SuppressWarnings("unchecked") @Override public final void putWithDeferredIdAllocation(FullEntity<?>... entities) { validateActive(); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (key instanceof Key) { putInternal(Entity.convert((FullEntity<Key>) entity)); } else { toAddAutoId.add((FullEntity<IncompleteKey>) entity); } } }
@SafeVarargs @Override public final void update(Entity... entities) { validateActive(); for (Entity entity : entities) { Key key = entity.getKey(); if (toDelete.contains(key)) { throw newInvalidRequest( "Entity with the key %s was already deleted in this %s", entity.getKey(), name); } if (toAdd.remove(key) != null || toPut.containsKey(key)) { toPut.put(key, entity); } else { toUpdate.put(key, entity); } } }
protected List<com.google.datastore.v1.Mutation> toMutationPbList() { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); for (FullEntity<IncompleteKey> entity : toAddAutoId()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toAdd().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toUpdate().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } for (FullEntity<Key> entity : toPut().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); } for (Key key : toDelete()) { mutationsPb.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(key.toPb()).build()); } return mutationsPb; }
protected void validateActive() { if (!active) { throw newInvalidRequest("%s is no longer active", name); } }
@Override public final void delete(Key... keys) { validateActive(); for (Key key : keys) { toAdd.remove(key); toUpdate.remove(key); toPut.remove(key); toDelete.add(key); } }
protected List<com.google.datastore.v1.Mutation> toMutationPbList() { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); for (FullEntity<IncompleteKey> entity : toAddAutoId()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toAdd().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toUpdate().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } for (FullEntity<Key> entity : toPut().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); } for (Key key : toDelete()) { mutationsPb.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(key.toPb()).build()); } return mutationsPb; }
@SuppressWarnings("unchecked") @Override public final void addWithDeferredIdAllocation(FullEntity<?>... entities) { validateActive(); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (key instanceof Key) { addInternal((FullEntity<Key>) entity); } else { toAddAutoId.add((FullEntity<IncompleteKey>) entity); } } }
@SuppressWarnings("unchecked") @Override public final void putWithDeferredIdAllocation(FullEntity<?>... entities) { validateActive(); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (key instanceof Key) { putInternal(Entity.convert((FullEntity<Key>) entity)); } else { toAddAutoId.add((FullEntity<IncompleteKey>) entity); } } }
@SafeVarargs @Override public final void update(Entity... entities) { validateActive(); for (Entity entity : entities) { Key key = entity.getKey(); if (toDelete.contains(key)) { throw newInvalidRequest( "Entity with the key %s was already deleted in this %s", entity.getKey(), name); } if (toAdd.remove(key) != null || toPut.containsKey(key)) { toPut.put(key, entity); } else { toUpdate.put(key, entity); } } }
protected void validateActive() { if (!active) { throw newInvalidRequest("%s is no longer active", name); } }
@Override public final void delete(Key... keys) { validateActive(); for (Key key : keys) { toAdd.remove(key); toUpdate.remove(key); toPut.remove(key); toDelete.add(key); } }
@SuppressWarnings("unchecked") @Override public final List<Entity> put(FullEntity<?>... entities) { validateActive(); List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (!(key instanceof Key)) { if (!incompleteKeys.isEmpty()) { IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class); allocated = getDatastore().allocateId(toAllocate).iterator(); } else { allocated = Collections.emptyIterator(); if (entity.getKey() instanceof Key) { putInternal((FullEntity<Key>) entity); answer.add(Entity.convert((FullEntity<Key>) entity)); } else { Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build(); putInternal(entityWithAllocatedId); answer.add(entityWithAllocatedId);
@SuppressWarnings("unchecked") @Override public final List<Entity> add(FullEntity<?>... entities) { validateActive(); List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (!(key instanceof Key)) { if (!incompleteKeys.isEmpty()) { IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class); allocated = getDatastore().allocateId(toAllocate).iterator(); } else { allocated = Collections.emptyIterator(); if (entity.getKey() instanceof Key) { addInternal((FullEntity<Key>) entity); answer.add(Entity.convert((FullEntity<Key>) entity)); } else { Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build(); addInternal(entityWithAllocatedId); answer.add(entityWithAllocatedId);
private void addInternal(FullEntity<Key> entity) { Key key = entity.getKey(); if (toAdd.containsKey(key) || toUpdate.containsKey(key) || toPut.containsKey(key)) { throw newInvalidRequest( "Entity with the key %s was already added or updated in this %s", entity.getKey(), name); } if (toDelete.remove(key)) { toPut.put(key, entity); } else { toAdd.put(key, entity); } }