001: package distributedcache;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.net.InetAddress;
006: import java.util.HashMap;
007: import java.util.Map;
008:
009: import org.xsocket.group.Address;
010:
011: import net.sf.ehcache.CacheException;
012: import net.sf.ehcache.Element;
013: import net.sf.ehcache.Status;
014: import net.sf.ehcache.store.Store;
015:
016: public class PartitionedStore implements Store {
017:
018: private Map<Integer, Address> storeAddresses = new HashMap<Integer, Address>();
019: private Address groupAddress = null;
020:
021: public PartitionedStore(InetAddress broadcastAddress,
022: int broadcastPort) throws IOException {
023: groupAddress = new Address(broadcastAddress, broadcastPort);
024: }
025:
026: public boolean containsKey(Object obj) {
027:
028: // TODO Auto-generated method stub
029: return false;
030: }
031:
032: public Element get(Object key) {
033: try {
034: int keyHashCode = computeHash(key);
035: Address address = getServiceAddress(keyHashCode);
036:
037: StoreService.IGetResult result = StoreService.callGet(
038: address, keyHashCode, key.toString());
039: storeAddresses.put(keyHashCode, result.getServiceAddress());
040:
041: if (result.getValue() != null) {
042: return new Element(key, result.getValue());
043: } else {
044: return null;
045: }
046: } catch (IOException ioe) {
047: throw new CacheException(ioe);
048: }
049: }
050:
051: public void put(Element element) throws CacheException {
052: try {
053: int keyHashCode = computeHash(element.getObjectKey());
054: Address address = getServiceAddress(keyHashCode);
055:
056: Address addr = StoreService.callPut(address, keyHashCode,
057: element.getObjectKey().toString(),
058: (Serializable) element.getObjectValue());
059: storeAddresses.put(keyHashCode, addr);
060:
061: } catch (IOException ioe) {
062: throw new CacheException(ioe);
063: }
064: }
065:
066: public boolean backedUp() {
067: // TODO Auto-generated method stub
068: return false;
069: }
070:
071: public Object[] getKeyArray() {
072: // TODO Auto-generated method stub
073: return null;
074: }
075:
076: public void expireElements() {
077: // TODO Auto-generated method stub
078:
079: }
080:
081: public void flush() throws IOException {
082: // TODO Auto-generated method stub
083:
084: }
085:
086: public Element remove(Object key) {
087: try {
088: int keyHashCode = computeHash(key);
089: Address address = getServiceAddress(keyHashCode);
090:
091: StoreService.IRemoveResult result = StoreService
092: .callRemove(address, keyHashCode, key.toString());
093: storeAddresses.put(keyHashCode, result.getServiceAddress());
094:
095: if (result.getValue() != null) {
096: return new Element(key, result.getValue());
097: } else {
098: return null;
099: }
100: } catch (IOException ioe) {
101: throw new CacheException(ioe);
102: }
103: }
104:
105: public void removeAll() throws CacheException {
106: // TODO Auto-generated method stub
107:
108: }
109:
110: private Address getServiceAddress(int keyHashCode)
111: throws IOException {
112: Address address = storeAddresses.get(keyHashCode);
113:
114: if (address == null) {
115: address = StoreServiceRegistry.getInstance(groupAddress)
116: .getStoreServiceAddresses().iterator().next();
117: }
118:
119: return address;
120: }
121:
122: public Element getQuiet(Object arg0) {
123: // TODO Auto-generated method stub
124: return null;
125: }
126:
127: public int getSize() {
128: // TODO Auto-generated method stub
129: return 0;
130: }
131:
132: public Status getStatus() {
133: // TODO Auto-generated method stub
134: return null;
135: }
136:
137: public void dispose() {
138: // TODO Auto-generated method stub
139:
140: }
141:
142: void setCompress(boolean b) {
143:
144: }
145:
146: void setCompressThreshold(int thresholdBytes) {
147:
148: }
149:
150: private int computeHash(Object obj) {
151: return (obj.hashCode() % 10);
152: }
153: }
|