Skip to content

Commit

Permalink
Issue #24 - you can now set and remove single metadata key/value pair…
Browse files Browse the repository at this point in the history
…s. In order to economize calls to the server, the interface gives you the choice to either save directly (per pair) or assemble all the pairs before saving.
  • Loading branch information
robert-bor committed May 30, 2013
1 parent 85cebb1 commit 92889d9
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public String getPrivateURL() {
}

@Override
protected void saveMetadata() {
protected void saveSpecificMetadata() {
commandFactory.createAccountMetadataCommand(this, info.getMetadata()).call();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void delete() {
}

@Override
protected void saveMetadata() {
protected void saveSpecificMetadata() {
commandFactory.createContainerMetadataCommand(getAccount(), this, info.getMetadata()).call();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,47 @@ public AbstractObjectStoreEntity(boolean allowCaching) {
this.allowCaching = allowCaching;
}

@Override
public void setMetadata(Map<String, Object> metadata) {
info.clear();
for (String key : metadata.keySet()) {
info.addMetadata(createMetadataEntry(key, metadata.get(key).toString()));
setAndDoNotSaveMetadata(key, metadata.get(key));
}
saveMetadata();
invalidate();
}

@Override
public void setAndSaveMetadata(String key, Object value) {
setAndDoNotSaveMetadata(key, value);
saveMetadata();
}

@Override
public void setAndDoNotSaveMetadata(String key, Object value) {
info.addMetadata(createMetadataEntry(key, value.toString()));
}

@Override
public void removeAndSaveMetadata(String key) {
setAndSaveMetadata(key, "");
}

@Override
public void removeAndDoNotSaveMetadata(String key) {
setAndDoNotSaveMetadata(key, "");
}

protected abstract Metadata createMetadataEntry(String name, String value);

protected abstract void saveMetadata();
@Override
public void saveMetadata() {
saveSpecificMetadata();
invalidate();
}

protected abstract void saveSpecificMetadata();

@Override
public Map<String, Object> getMetadata() {
checkForInfo();
Map<String, Object> metadataValues = new TreeMap<String, Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.javaswift.joss.model.Account;
import org.javaswift.joss.model.Container;
import org.javaswift.joss.model.StoredObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
Expand All @@ -21,8 +19,6 @@
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.javaswift.joss.util.HashSignature.getSignature;

public abstract class AbstractStoredObject extends AbstractObjectStoreEntity<ObjectInformation> implements StoredObject {

protected String name;
Expand Down Expand Up @@ -244,7 +240,7 @@ public StoredObject setDeleteAfter(long seconds) {
public StoredObject setDeleteAt(Date date) {
checkForInfo();
info.setDeleteAt(new DeleteAt(date));
saveMetadata();
saveSpecificMetadata();
return this;
}

Expand All @@ -253,7 +249,7 @@ protected Account getAccount() {
}

@Override
protected void saveMetadata() {
protected void saveSpecificMetadata() {
commandFactory.createObjectMetadataCommand(getAccount(), getContainer(), this, info.getHeaders()).call();
}

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/org/javaswift/joss/model/ObjectStoreEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,39 @@ public interface ObjectStoreEntity {
*/
public void setMetadata(Map<String, Object> metadata);

/**
* Sets a single metadata field and immediately places a call to the server to save
* @param key metadata field to add
* @param value value of the metadata field
*/
public void setAndSaveMetadata(String key, Object value);

/**
* Sets a single metadata field, but does not place a call to the server to save. This must be explicitly called
* with saveMetadata.
* @param key metadata field to add
* @param value value of the metadata field
*/
public void setAndDoNotSaveMetadata(String key, Object value);

/**
* Removes a single metadata field and immediately places a call to the server to save
* @param key metadata field to remove
*/
public void removeAndSaveMetadata(String key);

/**
* Removes a single metadata field, but does not place a call to the server to save. This must be explicitly
* called with saveMetadata.
* @param key metadata field to remove
*/
public void removeAndDoNotSaveMetadata(String key);

/**
* Places a call to the server to save all metadata, previously set with setMetadata excluding the save option
*/
public void saveMetadata();

/**
* Gets the metadata headers
* @return the metadata on the entity
Expand All @@ -25,4 +58,5 @@ public interface ObjectStoreEntity {
* @return the path to append to the host
*/
public String getPath();

}
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,40 @@ public void setMetadata() throws IOException {
verifyHeaderValue("42 BV", X_OBJECT_META_PREFIX + "Company");
}

@Test
public void setAndSaveMetadata() throws IOException {
expectStatusCode(202);
object.setAndSaveMetadata("Year", "1989");
verifyHeaderValue("1989", X_OBJECT_META_PREFIX + "Year");
}

@Test
public void setAndDoNotSaveMetadata() throws IOException {
expectStatusCode(202);
object.setAndDoNotSaveMetadata("Year", "1989");
object.setAndDoNotSaveMetadata("Company", "42 BV");
object.saveMetadata();
verifyHeaderValue("1989", X_OBJECT_META_PREFIX + "Year");
verifyHeaderValue("42 BV", X_OBJECT_META_PREFIX + "Company");
}

@Test
public void removeAndSaveMetadata() throws IOException {
expectStatusCode(202);
object.removeAndSaveMetadata("Year");
verifyHeaderValue("", X_OBJECT_META_PREFIX + "Year");
}

@Test
public void removeAndDoNotSaveMetadata() throws IOException {
expectStatusCode(202);
object.removeAndDoNotSaveMetadata("Year");
object.removeAndDoNotSaveMetadata("Company");
object.saveMetadata();
verifyHeaderValue("", X_OBJECT_META_PREFIX + "Year");
verifyHeaderValue("", X_OBJECT_META_PREFIX + "Company");
}

@Test
public void getMetadata() throws IOException, DateParseException {
expectStatusCode(202);
Expand Down

0 comments on commit 92889d9

Please sign in to comment.