Attempts a CompareAndSet storage call, loading the object from storage, synchronizing changes, and attempting a put. Note: If the local object didn't have any fields set by the caller or there weren't any changes, then the data will not be written and an exception will be thrown. @param tsdb
(final TSDB tsdb,
final Boolean overwrite)
| 154 | * @throws JSONException if the object could not be serialized |
| 155 | */ |
| 156 | public Deferred<Boolean> syncToStorage(final TSDB tsdb, |
| 157 | final Boolean overwrite) { |
| 158 | if (start_time < 1) { |
| 159 | throw new IllegalArgumentException("The start timestamp has not been set"); |
| 160 | } |
| 161 | |
| 162 | boolean has_changes = false; |
| 163 | for (Map.Entry<String, Boolean> entry : changed.entrySet()) { |
| 164 | if (entry.getValue()) { |
| 165 | has_changes = true; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | if (!has_changes) { |
| 170 | LOG.debug(this + " does not have changes, skipping sync to storage"); |
| 171 | throw new IllegalStateException("No changes detected in Annotation data"); |
| 172 | } |
| 173 | |
| 174 | final class StoreCB implements Callback<Deferred<Boolean>, Annotation> { |
| 175 | |
| 176 | @Override |
| 177 | public Deferred<Boolean> call(final Annotation stored_note) |
| 178 | throws Exception { |
| 179 | final byte[] original_note = stored_note == null ? new byte[0] : |
| 180 | stored_note.getStorageJSON(); |
| 181 | |
| 182 | if (stored_note != null) { |
| 183 | Annotation.this.syncNote(stored_note, overwrite); |
| 184 | } |
| 185 | |
| 186 | final byte[] tsuid_byte = tsuid != null && !tsuid.isEmpty() ? |
| 187 | UniqueId.stringToUid(tsuid) : null; |
| 188 | final PutRequest put = RequestBuilder.buildPutRequest(tsdb.getConfig(), tsdb.dataTable(), |
| 189 | getRowKey(start_time, tsuid_byte), FAMILY, |
| 190 | getQualifier(start_time), |
| 191 | Annotation.this.getStorageJSON(), start_time); |
| 192 | return tsdb.getClient().compareAndSet(put, original_note); |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | |
| 197 | if (tsuid != null && !tsuid.isEmpty()) { |
| 198 | return getAnnotation(tsdb, UniqueId.stringToUid(tsuid), start_time) |
| 199 | .addCallbackDeferring(new StoreCB()); |
| 200 | } |
| 201 | return getAnnotation(tsdb, start_time).addCallbackDeferring(new StoreCB()); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Attempts to mark an Annotation object for deletion. Note that if the |