Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Metadata store account for IOExceptions #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,65 @@ public void addAdditionalProperties(Map<String, Object> properties) {
this.additionalProperties.putAll(properties);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((additionalProperties == null) ? 0 : additionalProperties.hashCode());
result = prime * result + ((body == null) ? 0 : body.hashCode());
result = prime * result + ((createdAt == null) ? 0 : createdAt.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((updatedAt == null) ? 0 : updatedAt.hashCode());
result = prime * result + ((url == null) ? 0 : url.hashCode());
result = prime * result + ((user == null) ? 0 : user.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
GistCommentResponse other = (GistCommentResponse) obj;
if (additionalProperties == null) {
if (other.additionalProperties != null)
return false;
} else if (!additionalProperties.equals(other.additionalProperties))
return false;
if (body == null) {
if (other.body != null)
return false;
} else if (!body.equals(other.body))
return false;
if (createdAt == null) {
if (other.createdAt != null)
return false;
} else if (!createdAt.equals(other.createdAt))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (updatedAt == null) {
if (other.updatedAt != null)
return false;
} else if (!updatedAt.equals(other.updatedAt))
return false;
if (url == null) {
if (other.url != null)
return false;
} else if (!url.equals(other.url))
return false;
if (user == null) {
if (other.user != null)
return false;
} else if (!user.equals(other.user))
return false;
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.mangosolutions.rcloud.rawgist.model.GistCommentResponse;
import com.mangosolutions.rcloud.rawgist.repository.GistError;
import com.mangosolutions.rcloud.rawgist.repository.GistErrorCode;
Expand All @@ -33,6 +37,9 @@ public class GistCommentStore implements CommentStore {
@Autowired
private ObjectMapper objectMapper;

@Value("${gists.commentstore.workingCopySuffix:.tmp}")
private String workingCopySuffix = ".tmp";

public GistCommentStore() {
this.objectMapper = new ObjectMapper();
}
Expand All @@ -54,7 +61,8 @@ public void setObjectMapper(ObjectMapper objectMapper) {
@Cacheable(value = "commentstore", key = "#store.getAbsolutePath()")
public List<GistCommentResponse> load(File store) {
List<GistCommentResponse> comments = new ArrayList<>();
if (store.exists()) {
File workingCopy = workingCopyFor(store);
if (store.exists() || restoreState(workingCopy, store)) {
try {
comments = objectMapper.readValue(store, new TypeReference<List<GistCommentResponse>>() {
});
Expand All @@ -72,7 +80,15 @@ public List<GistCommentResponse> load(File store) {
public List<GistCommentResponse> save(File store, List<GistCommentResponse> comments) {
if(comments != null) {
try {
objectMapper.writeValue(store, comments);
File workingCopy = new File(store.getParent(), store.getName() + workingCopySuffix);
if(!store.exists()) {
restoreState(workingCopy, store);
}
write(workingCopy, comments);
if(store.exists()) {
Files.delete(store.toPath());
}
Files.move(workingCopy.toPath(), store.toPath(), StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
GistError error = new GistError(GistErrorCode.ERR_COMMENTS_NOT_WRITEABLE, "Could not save comments");
logger.error(error.getFormattedMessage() + " with path {}", store);
Expand All @@ -82,6 +98,46 @@ public List<GistCommentResponse> save(File store, List<GistCommentResponse> comm
return comments;
}

private void write(File output, List<GistCommentResponse> comments) throws IOException {
try {
objectMapper.writeValue(output, comments);
} catch(IOException e) {
if(output.exists()) {
Files.delete(output.toPath());
}
GistError error = new GistError(GistErrorCode.ERR_COMMENTS_NOT_WRITEABLE, "Could not save comments");
logger.error(error.getFormattedMessage() + " with path {}", output);
throw new GistRepositoryError(error, e);
}
}

private File workingCopyFor(File store) {
return new File(store.getParent(), store.getName() + workingCopySuffix);
}

/**
* Restores state of <code>to</code> file from <code>from</code> by performing atomic move.
*
* @param from file
* @param to file
* @return <code>true</code> if the state was restored, <code>false</code> if from file does not exist.
* @throws GistRepositoryError if file could not be restored
* @throws IllegalStateException if to file already exists
*/
private boolean restoreState(File from, File to) {
Preconditions.checkState(!to.exists(), "Target file '" + to.getAbsolutePath() + "' must not exist");
if(from.exists()) {
try {
Files.move(from.toPath(), to.toPath(), StandardCopyOption.ATOMIC_MOVE);
logger.warn("{} recreated state from working copy.", from);
return true;
} catch (IOException e) {
GistError error = new GistError(GistErrorCode.ERR_METADATA_NOT_READABLE, "Could not load comments from working copy for this gist");
logger.error(error.getFormattedMessage() + " with path {}", to);
throw new GistRepositoryError(error, e);
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,74 @@ public Fork getForkOf() {
return this.forkOf;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (_public ? 1231 : 1237);
result = prime * result + ((additionalProperties == null) ? 0 : additionalProperties.hashCode());
result = prime * result + ((createdAt == null) ? 0 : createdAt.hashCode());
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((forkOf == null) ? 0 : forkOf.hashCode());
result = prime * result + ((forks == null) ? 0 : forks.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((owner == null) ? 0 : owner.hashCode());
result = prime * result + ((updatedAt == null) ? 0 : updatedAt.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
GistMetadata other = (GistMetadata) obj;
if (_public != other._public)
return false;
if (additionalProperties == null) {
if (other.additionalProperties != null)
return false;
} else if (!additionalProperties.equals(other.additionalProperties))
return false;
if (createdAt == null) {
if (other.createdAt != null)
return false;
} else if (!createdAt.equals(other.createdAt))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (forkOf == null) {
if (other.forkOf != null)
return false;
} else if (!forkOf.equals(other.forkOf))
return false;
if (forks == null) {
if (other.forks != null)
return false;
} else if (!forks.equals(other.forks))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (owner == null) {
if (other.owner != null)
return false;
} else if (!owner.equals(other.owner))
return false;
if (updatedAt == null) {
if (other.updatedAt != null)
return false;
} else if (!updatedAt.equals(other.updatedAt))
return false;
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.mangosolutions.rcloud.rawgist.repository.GistError;
import com.mangosolutions.rcloud.rawgist.repository.GistErrorCode;
import com.mangosolutions.rcloud.rawgist.repository.GistRepositoryError;
Expand All @@ -29,6 +33,9 @@ public class GistMetadataStore implements MetadataStore {
@Autowired
private ObjectMapper objectMapper;

@Value("${gists.metadatastore.workingCopySuffix:.tmp}")
private String workingCopySuffix = ".tmp";

public GistMetadataStore() {
this.objectMapper = new ObjectMapper();
}
Expand All @@ -45,13 +52,13 @@ public ObjectMapper getObjectMapper() {
public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}



@Override
@Cacheable(value = "metadatastore", key = "#store.getAbsolutePath()")
public GistMetadata load(File store) {
GistMetadata metadata = null;
if(store.exists()) {
File workingCopy = workingCopyFor(store);
if(store.exists() || restoreState(workingCopy, store)) {
try {
metadata = objectMapper.readValue(store, GistMetadata.class);
} catch (IOException e) {
Expand All @@ -69,7 +76,15 @@ public GistMetadata load(File store) {
@CachePut(cacheNames = "metadatastore", key = "#store.getAbsolutePath()")
public GistMetadata save(File store, GistMetadata metadata) {
try {
objectMapper.writeValue(store, metadata);
File workingCopy = workingCopyFor(store);
if(!store.exists()) {
restoreState(workingCopy, store);
}
write(workingCopy, metadata);
if(store.exists()) {
Files.delete(store.toPath());
}
Files.move(workingCopy.toPath(), store.toPath(), StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
GistError error = new GistError(GistErrorCode.ERR_METADATA_NOT_WRITEABLE, "Could not update metadata for gist {}", metadata.getId());
logger.error(error.getFormattedMessage() + " with path {}", store);
Expand All @@ -78,4 +93,46 @@ public GistMetadata save(File store, GistMetadata metadata) {
return metadata;
}


private File workingCopyFor(File store) {
return new File(store.getParent(), store.getName() + workingCopySuffix);
}

/**
* Restores state of <code>to</code> file from <code>from</code> by performing atomic move.
*
* @param from file
* @param to file
* @return <code>true</code> if the state was restored, <code>false</code> if from file does not exist.
* @throws GistRepositoryError if file could not be restored
* @throws IllegalStateException if to file already exists
*/
private boolean restoreState(File from, File to) {
Preconditions.checkState(!to.exists(), "Target file '" + to.getAbsolutePath() + "' must not exist");
if(from.exists()) {
try {
Files.move(from.toPath(), to.toPath(), StandardCopyOption.ATOMIC_MOVE);
logger.warn("{} recreated state from working copy.", from);
return true;
} catch (IOException e) {
GistError error = new GistError(GistErrorCode.ERR_METADATA_NOT_READABLE, "Could not load metadata from working copy for this gist");
logger.error(error.getFormattedMessage() + " with path {}", to);
throw new GistRepositoryError(error, e);
}
}
return false;
}

private void write(File output, GistMetadata metadata) throws IOException {
try {
objectMapper.writeValue(output, metadata);
} catch(IOException e) {
if(output.exists()) {
Files.delete(output.toPath());
}
GistError error = new GistError(GistErrorCode.ERR_METADATA_NOT_WRITEABLE, "Could not update metadata for gist {}", metadata.getId());
logger.error(error.getFormattedMessage() + " with path {}", output);
throw new GistRepositoryError(error, e);
}
}
}
Loading