@Test public void shardCollectionMultiFieldsTest() throws ExecutionException, UnsupportedException { TableMetadataBuilder tableMetadataBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); Mockito.when(client.getDB("admin")).thenReturn(database); CommandResult commandRes = Mockito.mock(CommandResult.class); Mockito.when(commandRes.ok()).thenReturn(true); Mockito.when(database.command(Matchers.any(BasicDBObject.class))).thenReturn(commandRes); PowerMockito.mockStatic(SelectorOptionsUtils.class); Map<String, Selector> options = new HashMap<>(); options.put(TableOptions.SHARD_KEY_TYPE.getOptionName(), new StringSelector(ShardKeyType.ASC.getKeyType())); options.put(TableOptions.SHARD_KEY_FIELDS.getOptionName(), new StringSelector(COLUMN_NAME + "," + COLUMN_NAME2)); Mockito.when(SelectorOptionsUtils.processOptions(Matchers.anyMap())).thenReturn(options); ShardUtils.shardCollection(client, tableMetadataBuilder.build()); Mockito.verify(database, Mockito.times(1)).command(new BasicDBObject("enableSharding", DB_NAME)); BasicDBObject keys = new BasicDBObject(COLUMN_NAME, 1); keys.append(COLUMN_NAME2, 1); BasicDBObject commandExpected = new BasicDBObject("shardCollection", DB_NAME + "." + COLLECTION_NAME); commandExpected.put("key", keys); Mockito.verify(database, Mockito.times(1)).command(commandExpected); }
public static void shardCollection(MongoClient mongoClient, TableMetadata tableMetadata) throws ExecutionException { final String catalogName = tableMetadata.getName().getCatalogName().getName(); enableSharding(mongoClient, catalogName); DBObject shardKey = new BasicDBObject(); Map<String, Selector> options = SelectorOptionsUtils.processOptions(tableMetadata.getOptions()); ShardKeyType shardKeyType = getShardKeyType(options); String[] shardKeyFields = getShardKeyFields(options, shardKeyType); switch (shardKeyType) { case ASC: for (String field : shardKeyFields) { shardKey.put(field, 1); } break; case HASHED: shardKey.put(shardKeyFields[0], "hashed"); break; } final DBObject cmd = new BasicDBObject("shardCollection", catalogName + "." + tableMetadata.getName().getName()); cmd.put("key", shardKey); CommandResult result = mongoClient.getDB("admin").command(cmd); if (!result.ok()) { LOGGER.error("Error executing" + cmd + " :" + result.getErrorMessage()); throw new ExecutionException(result.getErrorMessage()); } }
ShardUtils { public static void shardCollection(MongoClient mongoClient, TableMetadata tableMetadata) throws ExecutionException { final String catalogName = tableMetadata.getName().getCatalogName().getName(); enableSharding(mongoClient, catalogName); DBObject shardKey = new BasicDBObject(); Map<String, Selector> options = SelectorOptionsUtils.processOptions(tableMetadata.getOptions()); ShardKeyType shardKeyType = getShardKeyType(options); String[] shardKeyFields = getShardKeyFields(options, shardKeyType); switch (shardKeyType) { case ASC: for (String field : shardKeyFields) { shardKey.put(field, 1); } break; case HASHED: shardKey.put(shardKeyFields[0], "hashed"); break; } final DBObject cmd = new BasicDBObject("shardCollection", catalogName + "." + tableMetadata.getName().getName()); cmd.put("key", shardKey); CommandResult result = mongoClient.getDB("admin").command(cmd); if (!result.ok()) { LOGGER.error("Error executing" + cmd + " :" + result.getErrorMessage()); throw new ExecutionException(result.getErrorMessage()); } } }
ShardUtils { public static void shardCollection(MongoClient mongoClient, TableMetadata tableMetadata) throws ExecutionException { final String catalogName = tableMetadata.getName().getCatalogName().getName(); enableSharding(mongoClient, catalogName); DBObject shardKey = new BasicDBObject(); Map<String, Selector> options = SelectorOptionsUtils.processOptions(tableMetadata.getOptions()); ShardKeyType shardKeyType = getShardKeyType(options); String[] shardKeyFields = getShardKeyFields(options, shardKeyType); switch (shardKeyType) { case ASC: for (String field : shardKeyFields) { shardKey.put(field, 1); } break; case HASHED: shardKey.put(shardKeyFields[0], "hashed"); break; } final DBObject cmd = new BasicDBObject("shardCollection", catalogName + "." + tableMetadata.getName().getName()); cmd.put("key", shardKey); CommandResult result = mongoClient.getDB("admin").command(cmd); if (!result.ok()) { LOGGER.error("Error executing" + cmd + " :" + result.getErrorMessage()); throw new ExecutionException(result.getErrorMessage()); } } private  ShardUtils(); }
ShardUtils { public static void shardCollection(MongoClient mongoClient, TableMetadata tableMetadata) throws ExecutionException { final String catalogName = tableMetadata.getName().getCatalogName().getName(); enableSharding(mongoClient, catalogName); DBObject shardKey = new BasicDBObject(); Map<String, Selector> options = SelectorOptionsUtils.processOptions(tableMetadata.getOptions()); ShardKeyType shardKeyType = getShardKeyType(options); String[] shardKeyFields = getShardKeyFields(options, shardKeyType); switch (shardKeyType) { case ASC: for (String field : shardKeyFields) { shardKey.put(field, 1); } break; case HASHED: shardKey.put(shardKeyFields[0], "hashed"); break; } final DBObject cmd = new BasicDBObject("shardCollection", catalogName + "." + tableMetadata.getName().getName()); cmd.put("key", shardKey); CommandResult result = mongoClient.getDB("admin").command(cmd); if (!result.ok()) { LOGGER.error("Error executing" + cmd + " :" + result.getErrorMessage()); throw new ExecutionException(result.getErrorMessage()); } } private  ShardUtils(); static boolean isCollectionSharded(Map<String, Selector> options); static void shardCollection(MongoClient mongoClient, TableMetadata tableMetadata); }
ShardUtils { public static void shardCollection(MongoClient mongoClient, TableMetadata tableMetadata) throws ExecutionException { final String catalogName = tableMetadata.getName().getCatalogName().getName(); enableSharding(mongoClient, catalogName); DBObject shardKey = new BasicDBObject(); Map<String, Selector> options = SelectorOptionsUtils.processOptions(tableMetadata.getOptions()); ShardKeyType shardKeyType = getShardKeyType(options); String[] shardKeyFields = getShardKeyFields(options, shardKeyType); switch (shardKeyType) { case ASC: for (String field : shardKeyFields) { shardKey.put(field, 1); } break; case HASHED: shardKey.put(shardKeyFields[0], "hashed"); break; } final DBObject cmd = new BasicDBObject("shardCollection", catalogName + "." + tableMetadata.getName().getName()); cmd.put("key", shardKey); CommandResult result = mongoClient.getDB("admin").command(cmd); if (!result.ok()) { LOGGER.error("Error executing" + cmd + " :" + result.getErrorMessage()); throw new ExecutionException(result.getErrorMessage()); } } private  ShardUtils(); static boolean isCollectionSharded(Map<String, Selector> options); static void shardCollection(MongoClient mongoClient, TableMetadata tableMetadata);  }