@SuppressWarnings("rawtypes")
private void init() throws IOException {
MongoClient client = null;
try {
MongoClientURI clientURI = new MongoClientURI(
this.storagePluginConfig.getConnection());
client = new MongoClient(clientURI);
chunksMapping = Maps.newHashMap();
chunksInverseMapping = Maps.newLinkedHashMap();
if (isShardedCluster(client)) {
DB db = client.getDB(CONFIG);
db.setReadPreference(ReadPreference.nearest());
DBCollection chunksCollection = db.getCollectionFromString(CHUNKS);
DBObject query = new BasicDBObject(1);
query
.put(
NS,
this.scanSpec.getDbName() + "."
+ this.scanSpec.getCollectionName());
DBObject fields = new BasicDBObject();
fields.put(SHARD, select);
fields.put(MIN, select);
fields.put(MAX, select);
DBCursor chunkCursor = chunksCollection.find(query, fields);
DBCollection shardsCollection = db.getCollectionFromString(SHARDS);
fields = new BasicDBObject();
fields.put(HOST, select);
while (chunkCursor.hasNext()) {
DBObject chunkObj = chunkCursor.next();
String shardName = (String) chunkObj.get(SHARD);
String chunkId = (String) chunkObj.get(ID);
query = new BasicDBObject().append(ID, shardName);
DBCursor hostCursor = shardsCollection.find(query, fields);
while (hostCursor.hasNext()) {
DBObject hostObj = hostCursor.next();
String hostEntry = (String) hostObj.get(HOST);
String[] tagAndHost = StringUtils.split(hostEntry, '/');
String[] hosts = tagAndHost.length > 1 ? StringUtils.split(
tagAndHost[1], ',') : StringUtils.split(tagAndHost[0], ',');
Set<ServerAddress> addressList = chunksMapping.get(chunkId);
if (addressList == null) {
addressList = Sets.newHashSet();
chunksMapping.put(chunkId, addressList);
}
for (String host : hosts) {
addressList.add(new ServerAddress(host));
}
ServerAddress address = addressList.iterator().next();
List<ChunkInfo> chunkList = chunksInverseMapping.get(address
.getHost());
if (chunkList == null) {
chunkList = Lists.newArrayList();
chunksInverseMapping.put(address.getHost(), chunkList);
}
ChunkInfo chunkInfo = new ChunkInfo(Arrays.asList(hosts), chunkId);
DBObject minObj = (BasicDBObject) chunkObj.get(MIN);
Map<String, Object> minFilters = Maps.newHashMap();
Map minMap = minObj.toMap();
Set keySet = minMap.keySet();
for (Object keyObj : keySet) {
Object object = minMap.get(keyObj);
if (!(object instanceof MinKey)) {
minFilters.put(keyObj.toString(), object);
}
}
chunkInfo.setMinFilters(minFilters);
DBObject maxObj = (BasicDBObject) chunkObj.get(MAX);
Map<String, Object> maxFilters = Maps.newHashMap();
Map maxMap = maxObj.toMap();
keySet = maxMap.keySet();
for (Object keyObj : keySet) {
Object object = maxMap.get(keyObj);
if (!(object instanceof MaxKey)) {
maxFilters.put(keyObj.toString(), object);
}
}
chunkInfo.setMaxFilters(maxFilters);
chunkList.add(chunkInfo);
}
}
} else {
String chunkName = scanSpec.getDbName() + "."
+ scanSpec.getCollectionName();
List<String> hosts = clientURI.getHosts();
Set<ServerAddress> addressList = Sets.newHashSet();
for (String host : hosts) {
addressList.add(new ServerAddress(host));
}