package org.couchbase.mock;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.couchbase.mock.memcached.MemcachedServer;
public CouchbaseBucket(String name, String hostname,
int port,
int numNodes,
int bucketStartPort,
int numVBuckets, CouchbaseMock cluster, String password)
throws IOException {
super(name, hostname, port, numNodes, bucketStartPort, numVBuckets, cluster, password);
}
public CouchbaseBucket(String name, String hostname,
int port,
int numNodes,
int bucketStartPort,
int numVBuckets)
throws IOException {
super(name, hostname, port, numNodes, bucketStartPort, numVBuckets, null);
}
@Override
Map<String, Object> map = new HashMap<String, Object>();
List<MemcachedServer> active = activeServers();
map.put("name", name);
map.put("bucketType", "membase");
map.put("authType", "sasl");
map.put("saslPassword", getPassword());
map.put("proxyPort", 0);
map.put("uri", "/pools/" + poolName + "/buckets/" + name);
map.put("streamingUri", "/pools/" + poolName + "/bucketsStreaming/" + name);
map.put("flushCacheUri", "/pools/" + poolName + "/buckets/" + name + "/controller/doFlush");
List<String> nodes = new ArrayList<String>();
for (MemcachedServer server : active) {
nodes.add(server.toString());
}
map.put("nodes", nodes);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Map<String, String> stats = new HashMap<String, String>();
stats.put("uri", "/pools/" + poolName + "/buckets/" + name + "/stats");
map.put("stats", stats);
map.put("nodeLocator", "vbucket");
Map<String, Object> vbm = new HashMap<String, Object>();
vbm.put("hashAlgorithm", "CRC");
vbm.put("numReplicas", 0);
List<String> serverList = new ArrayList<String>();
for (MemcachedServer server : active) {
serverList.add(server.getSocketName());
}
vbm.put("serverList", serverList);
ArrayList<ArrayList<Integer>> m = new ArrayList<ArrayList<Integer>>();
for (short ii = 0; ii < numVBuckets; ++ii) {
MemcachedServer resp = datastore.getVBucket(ii).getOwner();
ArrayList<Integer> line = new ArrayList<Integer>();
line.add(active.indexOf(resp));
m.add(line);
}
vbm.put("vBucketMap", m);
map.put("vBucketServerMap", vbm);
return JSONObject.fromObject(map).toString();
}
@Override
return BucketType.COUCHBASE;
}
}