}
public static SimpleTableDef detectTable(CouchDbConnector connector) {
final SortedMap<String, Set<ColumnType>> columnsAndTypes = new TreeMap<String, Set<ColumnType>>();
final StreamingViewResult streamingView = connector.queryForStreamingView(new ViewQuery().allDocs().includeDocs(true)
.limit(1000));
try {
final Iterator<Row> rowIterator = streamingView.iterator();
while (safeHasNext(rowIterator)) {
Row row = rowIterator.next();
JsonNode doc = row.getDocAsNode();
final Iterator<Entry<String, JsonNode>> fieldIterator = doc.getFields();
while (fieldIterator.hasNext()) {
Entry<String, JsonNode> entry = fieldIterator.next();
String key = entry.getKey();
Set<ColumnType> types = columnsAndTypes.get(key);
if (types == null) {
types = EnumSet.noneOf(ColumnType.class);
columnsAndTypes.put(key, types);
}
JsonNode value = entry.getValue();
if (value == null || value.isNull()) {
// do nothing
} else if (value.isTextual()) {
types.add(ColumnType.VARCHAR);
} else if (value.isArray()) {
types.add(ColumnType.LIST);
} else if (value.isObject()) {
types.add(ColumnType.MAP);
} else if (value.isBoolean()) {
types.add(ColumnType.BOOLEAN);
} else if (value.isInt()) {
types.add(ColumnType.INTEGER);
} else if (value.isLong()) {
types.add(ColumnType.BIGINT);
} else if (value.isDouble()) {
types.add(ColumnType.DOUBLE);
}
}
}
} finally {
streamingView.close();
}
final String[] columnNames = new String[columnsAndTypes.size()];
final ColumnType[] columnTypes = new ColumnType[columnsAndTypes.size()];