考虑一个具有以下接口的简单对象存储:
// add an object with ‘blob’ content into the system and return an id
int put(string blob);
// retrieve the contents of an object identified by ‘id’. NULL if doesn’t exist
string get(int);
// delete an object identified by ‘id’
void delete(int id);
//number of (non duplicate) objects stored in the object store
int size();
要求
对象存储必须对对象进行重复数据删除。如果相同的字节序列被存储两次——那么存储不能将数据存储两次。对象可以相当大——比如说——大小从 1K 到 5MB。Blob 是不可变的。我们正在从这些 API 调用中寻找标准的顺序一致性语义。如果一个对象是“put”——那么下一个立即的“get”调用——应该返回之前的 put 值。例如,如果客户端执行以下操作:
Id = objectstore.put(data);
data1 = objectstore.get(id);
第二个操作必须返回与“数据”所指向的字节序列相同的字节序列。没有其他客户端/进程/线程应该能够干扰它。
到目前为止我的代码:
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class ObjectStore {
String blobString;
Object objectId;
public ObjectStore(String blobString, Object objectId) {
this.blobString = blobString;
this.objectId = objectId;
}
@Override
public boolean equals(Object o){
if(!(o instanceof ObjectStore)){
return false;
}
if((o == null) || (o.getClass() != this.getClass()))
return false;
// object must be Test at this point
ObjectStore store = (ObjectStore)o;
return blobString.toString() == store.blobString &&
(objectId != null && objectId.equals(store.objectId));
}
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + blobString.hashCode();
hash = 31 * hash + ((objectId == null) ? 0 : objectId.hashCode());
return hash;
}
Set<String> set = new HashSet<String>();
/**
* Put object into store and return id.
* @param blobString
* @return
*/
public int put(String blobString) {
set.add(blobString);
return 0;
}
/**
* Get object corresponding to id. Return null if no such object exists.
* @param objectId
* @return
*/
public String get(int objectId) {
return null;
}
/**
* Release object - don't need it anymore.
* @param objectId
*/
public void delete(int objectId) {
// stub
}
/**
* Number of distinct blobs stored in the objectStore
* @return
*/
public int size() {
// stub
return 0;
}
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
}
}
我无法识别我需要使用 Map 或 Set 接口的任何内容,因为 put() 方法返回对象 ID。