Skip to content

Commit

Permalink
fixes #17: Added ICloudFile interface. MetadataManagers have to return
Browse files Browse the repository at this point in the history
ICloudFiles for different methods.
  • Loading branch information
Florian Bausch committed Jul 28, 2012
1 parent ef7ebf3 commit cdf8e13
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 210 deletions.
44 changes: 18 additions & 26 deletions core/src/de/dhbw_mannheim/cloudraid/core/impl/CoreAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;

Expand All @@ -45,6 +43,7 @@
import de.dhbw_mannheim.cloudraid.core.ICoreAccess;
import de.dhbw_mannheim.cloudraid.core.impl.jni.RaidAccessInterface;
import de.dhbw_mannheim.cloudraid.core.net.connector.IStorageConnector;
import de.dhbw_mannheim.cloudraid.metadatamgr.ICloudFile;
import de.dhbw_mannheim.cloudraid.metadatamgr.IMetadataManager;
import de.dhbw_mannheim.cloudraid.metadatamgr.IMetadataManager.FILE_STATUS;

Expand Down Expand Up @@ -104,9 +103,9 @@ public boolean deleteData(int fileid) {
boolean state = false;
try {
// Retrieve the metadata from the database
ResultSet rs = this.metadata.fileById(this.fileid);
if (rs != null) {
setByResultSet(rs);
ICloudFile cf = this.metadata.fileById(this.fileid);
if (cf != null) {
setByCloudFile(cf);

if (FILE_STATUS.valueOf(this.status) != FILE_STATUS.READY) {
throw new IllegalStateException(String.format(
Expand All @@ -128,8 +127,6 @@ public boolean deleteData(int fileid) {
state = true;

}
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
Expand All @@ -141,9 +138,9 @@ public boolean finishGetData(int fileid) {
this.fileid = fileid;
try {
// Retrieve the meta data from the database
ResultSet rs = this.metadata.fileById(this.fileid);
if (rs != null) {
setByResultSet(rs);
ICloudFile cf = this.metadata.fileById(this.fileid);
if (cf != null) {
setByCloudFile(cf);

if (FILE_STATUS.valueOf(this.status) != FILE_STATUS.READY) {
throw new IllegalStateException(String.format(
Expand All @@ -168,9 +165,9 @@ public InputStream getData(int fileid) {
int bufsize = 4096;
try {
// Retrieve the metadata from the database
ResultSet rs = this.metadata.fileById(this.fileid);
if (rs != null) {
setByResultSet(rs);
ICloudFile cf = this.metadata.fileById(this.fileid);
if (cf != null) {
setByCloudFile(cf);

if (FILE_STATUS.valueOf(this.status) != FILE_STATUS.READY) {
throw new IllegalStateException(String.format(
Expand Down Expand Up @@ -297,8 +294,6 @@ public InputStream getData(int fileid) {
new FileInputStream(this.file), bufsize);
return bis;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (MissingConfigValueException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
Expand Down Expand Up @@ -327,9 +322,9 @@ public boolean putData(InputStream is, int fileid, boolean update) {
BufferedOutputStream bos = null;
try {
// Retrieve the metadata from the database
ResultSet rs = this.metadata.fileById(this.fileid);
if (rs != null) {
setByResultSet(rs);
ICloudFile cf = this.metadata.fileById(this.fileid);
if (cf != null) {
setByCloudFile(cf);

if (FILE_STATUS.valueOf(this.status) != FILE_STATUS.UPLOADING) {
throw new IllegalStateException(String.format(
Expand Down Expand Up @@ -366,9 +361,6 @@ public boolean putData(InputStream is, int fileid, boolean update) {
this.run();
}
}
} catch (SQLException e) {
this.uploadstate = false;
e.printStackTrace();
} catch (MissingConfigValueException e) {
this.uploadstate = false;
e.printStackTrace();
Expand Down Expand Up @@ -526,11 +518,11 @@ public void run() {
this.uploadstate = true;
}

private void setByResultSet(ResultSet rs) throws SQLException {
this.path = rs.getString("path_name");
this.hash = rs.getString("hash_name");
this.userid = rs.getInt("user_id");
this.status = rs.getString("status");
private void setByCloudFile(ICloudFile cf) {
this.path = cf.getName();
this.hash = cf.getHash();
this.userid = cf.getUserId();
this.status = cf.getStatus();
}

private void silentRemove(String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.dhbw_mannheim.cloudraid.metadatamgr;

/**
* An interface for the representation of a file stored by CloudRAID.
*
* @author Florian Bausch
*
*/
public interface ICloudFile {

/**
* Returns the file ID.
*
* @return The file ID.
*/
public int getFileId();

/**
* Returns the hashed file name.
*
* @return The hashed file name.
*/
public String getHash();

/**
* Returns the date (milliseconds since 01/01/1970) of the last modification
* of a file.
*
* @return The last modification date.
*/
public long getLastMod();

/**
* Returns the file name.
*
* @return The file name.
*/
public String getName();

/**
* Returns the file's status.
*
* @return The status.
*/
public String getStatus();

/**
* Returns the user ID the file belongs to.
*
* @return The user ID.
*/
public int getUserId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

package de.dhbw_mannheim.cloudraid.metadatamgr;

import java.sql.ResultSet;
import java.util.Collection;

import de.dhbw_mannheim.cloudraid.config.ICloudRAIDConfig;

Expand Down Expand Up @@ -145,7 +145,7 @@ public enum FILE_STATUS {
* The id of the file
* @return The SQL ResultSet matching the given file id.
*/
public ResultSet fileById(int fileId);
public ICloudFile fileById(int fileId);

/**
* Deletes a data set in the database defined by the path.
Expand All @@ -165,7 +165,7 @@ public enum FILE_STATUS {
* The user id this file belongs to
* @return The SQL ResultSet of the file
*/
public ResultSet fileGet(String path, int userId);
public ICloudFile fileGet(String path, int userId);

/**
* Returns a ResultSet that contains all files of a user.
Expand All @@ -174,7 +174,7 @@ public enum FILE_STATUS {
* The user id this file belongs to
* @return The SQL ResultSet of all file belonging to the given user.
*/
public ResultSet fileList(int userId);
public Collection<ICloudFile> fileList(int userId);

/**
* Inserts a data set into the database.
Expand Down Expand Up @@ -221,51 +221,6 @@ public boolean fileUpdate(int id, String path, String hash, long lastMod,
*/
public boolean fileUpdateState(int id, FILE_STATUS state);

/**
* Looks up the hash value of an entry in the database.
*
* @deprecated Not needed
*
* @param path
* The path of the file (identifies the data set).
* @param userId
* The user id this file belongs to
* @return The hash value. Or <code>null</code>, if the path does not exist
* in the database.
*/
@Deprecated
public String getHash(String path, int userId);

/**
* Looks up the last modification date of a file.
*
* @deprecated Not needed
*
* @param path
* The path of the file.
* @param userId
* The user id this file belongs to
* @return The last modification date. Or <code>-1L</code>, if the path does
* not exist in the database.
*/
@Deprecated
public long getLastMod(String path, int userId);

/**
* Looks up a file name for a given hash value.
*
* @deprecated Not needed
*
* @param hash
* The hash value.
* @param userId
* The user id this file belongs to
* @return The path of the file. Or <code>null</code>, if the hash does not
* exist in the database.
*/
@Deprecated
public String getName(String hash, int userId);

/**
* Creates the database schemas.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package de.dhbw_mannheim.cloudraid.metadatamgr.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Vector;

import de.dhbw_mannheim.cloudraid.metadatamgr.ICloudFile;

/**
* An implementation of {@link ICloudFile} that can read data from the
* {@link HSQLMetadataManager} database.
*
* @author Florian Bausch
*
*/
public class HSQLCloudFile implements ICloudFile {

/**
* Creates from a {@link ResultSet} a Collection of {@link ICloudFile}s. The
* cursor must be set before the first row.
*
* @param rs
* A SQL {@link ResultSet}.
* @return The Collection.
* @throws SQLException
*/
protected static Collection<ICloudFile> createFileList(ResultSet rs)
throws SQLException {
Vector<ICloudFile> ret = new Vector<ICloudFile>();
while (rs.next()) {
ret.add(new HSQLCloudFile(rs));
}
return ret;
}

private String path;
private String hash;
private int userid;
private String status;
private long lastMod;
private int fileid;

/**
* Creates a {@link ICloudFile} from a {@link ResultSet}. The cursor must be
* set on a row.
*
* @param rs
* The {@link ResultSet}.
* @throws SQLException
*/
protected HSQLCloudFile(ResultSet rs) throws SQLException {
this.path = rs.getString("path_name");
this.hash = rs.getString("hash_name");
this.userid = rs.getInt("user_id");
this.status = rs.getString("status");
this.lastMod = rs.getTimestamp("last_mod").getTime();
this.fileid = rs.getInt("id");
}

@Override
public int getFileId() {
return this.fileid;
}

@Override
public String getHash() {
return this.hash;
}

@Override
public long getLastMod() {
return this.lastMod;
}

@Override
public String getName() {
return this.path;
}

@Override
public String getStatus() {
return this.status;
}

@Override
public int getUserId() {
return this.userid;
}

}
Loading

0 comments on commit cdf8e13

Please sign in to comment.