|
| 1 | +package com.google.gcloud.datastore; |
| 2 | + |
| 3 | +import static com.google.gcloud.datastore.DatastoreServiceException.Code.FAILED_PRECONDITION; |
| 4 | + |
| 5 | +import com.google.api.services.datastore.DatastoreV1; |
| 6 | + |
| 7 | +import java.util.LinkedHashMap; |
| 8 | +import java.util.LinkedHashSet; |
| 9 | + |
| 10 | +class BatchWriterImpl implements BatchWriter { |
| 11 | + |
| 12 | + private final LinkedHashMap<Key, Entity> toAdd = new LinkedHashMap<>(); |
| 13 | + private final LinkedHashMap<Key, Entity> toUpdate = new LinkedHashMap<>(); |
| 14 | + private final LinkedHashMap<Key, Entity> toPut = new LinkedHashMap<>(); |
| 15 | + private final LinkedHashSet<Key> toDelete = new LinkedHashSet<>(); |
| 16 | + private final DatastoreServiceImpl datastore; |
| 17 | + |
| 18 | + private boolean force; |
| 19 | + protected boolean isValid = true; |
| 20 | + |
| 21 | + BatchWriterImpl(DatastoreServiceImpl datastore, BatchWriteOption... options) { |
| 22 | + this.datastore = datastore; |
| 23 | + force = datastore.getOptions().force(); |
| 24 | + for (BatchWriteOption option : options) { |
| 25 | + option.apply(this); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // Apply all valid options |
| 30 | + |
| 31 | + void apply(BatchWriteOption.ForceWrites forceOptions) { |
| 32 | + this.force = forceOptions.force(); |
| 33 | + } |
| 34 | + |
| 35 | + void apply(BatchWriteOption other) { |
| 36 | + // dont care |
| 37 | + } |
| 38 | + |
| 39 | + //////////////////// |
| 40 | + |
| 41 | + DatastoreServiceException newBatchFailure(Entity entity, String msg) { |
| 42 | + isValid = false; |
| 43 | + return new DatastoreServiceException(FAILED_PRECONDITION, |
| 44 | + new RuntimeException("Entity with the key " + entity.key() + " " + msg)); |
| 45 | + } |
| 46 | + |
| 47 | + protected void checkValid() { |
| 48 | + if (!isValid) { |
| 49 | + throw new DatastoreServiceException(FAILED_PRECONDITION, |
| 50 | + new RuntimeException("BatchWriter is in an invalid state")); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void add(Entity... entities) { |
| 56 | + checkValid(); |
| 57 | + for (Entity entity : entities) { |
| 58 | + Key key = entity.key(); |
| 59 | + if (toAdd.containsKey(key) || toUpdate.containsKey(key) || toPut.containsKey(key)) { |
| 60 | + throw newBatchFailure(entity, "was already added or updated in this batch"); |
| 61 | + } |
| 62 | + if (toDelete.remove(key)) { |
| 63 | + toPut.put(key, entity); |
| 64 | + } else { |
| 65 | + toAdd.put(key, entity); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void update(Entity... entities) { |
| 72 | + checkValid(); |
| 73 | + for (Entity entity : entities) { |
| 74 | + Key key = entity.key(); |
| 75 | + if (toDelete.contains(key)) { |
| 76 | + throw newBatchFailure(entity, "was alredy deleted in this batch"); |
| 77 | + } |
| 78 | + if (toAdd.remove(key) != null || toPut.containsKey(key)) { |
| 79 | + toPut.put(key, entity); |
| 80 | + } else { |
| 81 | + toUpdate.put(key, entity); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void put(Entity... entities) { |
| 88 | + checkValid(); |
| 89 | + for (Entity entity : entities) { |
| 90 | + Key key = entity.key(); |
| 91 | + toAdd.remove(key); |
| 92 | + toUpdate.remove(key); |
| 93 | + toDelete.remove(key); |
| 94 | + toPut.put(key, entity); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void delete(Key... keys) { |
| 100 | + checkValid(); |
| 101 | + for (Key key : keys) { |
| 102 | + toAdd.remove(key); |
| 103 | + toUpdate.remove(key); |
| 104 | + toPut.remove(key); |
| 105 | + toDelete.add(key); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void submit() { |
| 111 | + checkValid(); |
| 112 | + DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder(); |
| 113 | + for (Entity entity : toAdd.values()) { |
| 114 | + mutationPb.addInsert(entity.toPb()); |
| 115 | + } |
| 116 | + for (Entity entity : toUpdate.values()) { |
| 117 | + mutationPb.addUpdate(entity.toPb()); |
| 118 | + } |
| 119 | + for (Entity entity : toPut.values()) { |
| 120 | + mutationPb.addUpsert(entity.toPb()); |
| 121 | + } |
| 122 | + for (Key key : toDelete) { |
| 123 | + mutationPb.addDelete(key.toPb()); |
| 124 | + } |
| 125 | + if (force) { |
| 126 | + mutationPb.setForce(force); |
| 127 | + } |
| 128 | + DatastoreV1.CommitRequest.Builder requestPb = newCommitRequest(); |
| 129 | + requestPb.setMutation(mutationPb); |
| 130 | + datastore.comitMutation(requestPb); |
| 131 | + isValid = false; |
| 132 | + } |
| 133 | + |
| 134 | + protected DatastoreV1.CommitRequest.Builder newCommitRequest() { |
| 135 | + DatastoreV1.CommitRequest.Builder requestPb = DatastoreV1.CommitRequest.newBuilder(); |
| 136 | + requestPb.setMode(DatastoreV1.CommitRequest.Mode.NON_TRANSACTIONAL); |
| 137 | + return requestPb; |
| 138 | + } |
| 139 | +} |
0 commit comments