下面列出了怎么用com.mongodb.client.ListIndexesIterable的API类实例代码及写法,或者点击链接到github查看源代码。
public static List<MongoIndex> parse(ListIndexesIterable<Document> indexes)
{
ImmutableList.Builder<MongoIndex> builder = ImmutableList.builder();
for (Document index : indexes) {
// TODO: v, ns, sparse fields
Document key = (Document) index.get("key");
String name = index.getString("name");
boolean unique = index.getBoolean("unique", false);
if (key.containsKey("_fts")) { // Full Text Search
continue;
}
builder.add(new MongoIndex(name, parseKey(key), unique));
}
return builder.build();
}
private void createTransactionsGlobalTxKeyIndexIfNecessary() {
String databaseName = CommonUtils.getApplication(this.endpoint).replaceAll("\\W", "_");
MongoDatabase database = this.mongoClient.getDatabase(databaseName);
MongoCollection<Document> transactions = database.getCollection(CONSTANTS_TB_TRANSACTIONS);
ListIndexesIterable<Document> transactionIndexList = transactions.listIndexes();
boolean transactionIndexExists = false;
MongoCursor<Document> transactionCursor = null;
try {
transactionCursor = transactionIndexList.iterator();
while (transactionIndexExists == false && transactionCursor.hasNext()) {
Document document = transactionCursor.next();
Boolean unique = document.getBoolean("unique");
Document key = (Document) document.get("key");
boolean globalExists = key.containsKey(CONSTANTS_FD_GLOBAL);
boolean lengthEquals = key.size() == 1;
transactionIndexExists = lengthEquals && globalExists;
if (transactionIndexExists && (unique == null || unique == false)) {
throw new IllegalStateException();
}
}
} finally {
IOUtils.closeQuietly(transactionCursor);
}
if (transactionIndexExists == false) {
Document index = new Document(CONSTANTS_FD_GLOBAL, 1);
transactions.createIndex(index, new IndexOptions().unique(true));
}
}
private void createLocksIndexIfNecessary() {
String databaseName = CommonUtils.getApplication(this.endpoint).replaceAll("\\W", "_");
MongoDatabase database = this.mongoClient.getDatabase(databaseName);
MongoCollection<Document> locks = database.getCollection(CONSTANTS_TB_LOCKS);
ListIndexesIterable<Document> lockIndexList = locks.listIndexes();
boolean transactionIndexExists = false;
MongoCursor<Document> lockCursor = null;
try {
lockCursor = lockIndexList.iterator();
while (transactionIndexExists == false && lockCursor.hasNext()) {
Document document = lockCursor.next();
Boolean unique = document.getBoolean("unique");
Document key = (Document) document.get("key");
boolean globalExists = key.containsKey(CONSTANTS_FD_GLOBAL);
boolean lengthEquals = key.size() == 1;
transactionIndexExists = lengthEquals && globalExists;
if (transactionIndexExists && (unique == null || unique == false)) {
throw new IllegalStateException();
}
}
} finally {
IOUtils.closeQuietly(lockCursor);
}
if (transactionIndexExists == false) {
Document index = new Document(CONSTANTS_FD_GLOBAL, 1);
locks.createIndex(index, new IndexOptions().unique(true));
}
}
private HashMap<String, Document> getIndexesProperties(MongoCollection collection){
final HashMap<String, Document> result = new HashMap<>();
final ListIndexesIterable<Document> currentIndexes = collection.listIndexes();
for(Document doc: currentIndexes){
String indexName = doc.getString("name");
if(indexName != null){
result.put(indexName, doc);
}
}
return result;
}