Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Commit a4fd18e

Browse files
committed
Added support for createIndexes
1 parent 8612b6a commit a4fd18e

File tree

5 files changed

+87
-15
lines changed

5 files changed

+87
-15
lines changed

driver/src/main/com/mongodb/reactivestreams/client/MongoCollection.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.mongodb.ReadPreference;
2121
import com.mongodb.WriteConcern;
2222
import com.mongodb.annotations.ThreadSafe;
23-
import com.mongodb.async.SingleResultCallback;
2423
import com.mongodb.bulk.BulkWriteResult;
2524
import com.mongodb.client.model.BulkWriteOptions;
2625
import com.mongodb.client.model.CountOptions;
@@ -448,7 +447,7 @@ public interface MongoCollection<TDocument> {
448447
* @return a publisher with a single element indicating when the operation has completed
449448
* @mongodb.driver.manual reference/method/db.collection.ensureIndex Ensure Index
450449
*/
451-
Publisher<Success> createIndex(Bson key);
450+
Publisher<String> createIndex(Bson key);
452451

453452
/**
454453
* Creates an index.
@@ -458,7 +457,7 @@ public interface MongoCollection<TDocument> {
458457
* @return a publisher with a single element indicating when the operation has completed
459458
* @mongodb.driver.manual reference/method/db.collection.ensureIndex Ensure Index
460459
*/
461-
Publisher<Success> createIndex(Bson key, IndexOptions options);
460+
Publisher<String> createIndex(Bson key, IndexOptions options);
462461

463462

464463
/**
@@ -469,7 +468,7 @@ public interface MongoCollection<TDocument> {
469468
* @return a publisher with a single element indicating when the operation has completed
470469
* @mongodb.server.release 2.6
471470
*/
472-
Publisher<Success> createIndexes(List<IndexModel> indexes);
471+
Publisher<String> createIndexes(List<IndexModel> indexes);
473472

474473
/**
475474
* Get all the indexes in this collection.

driver/src/main/com/mongodb/reactivestreams/client/MongoCollectionImpl.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -326,26 +326,26 @@ void execute(final SingleResultCallback<Success> callback) {
326326
}
327327

328328
@Override
329-
public Publisher<Success> createIndex(final Bson key) {
329+
public Publisher<String> createIndex(final Bson key) {
330330
return createIndex(key, new IndexOptions());
331331
}
332332

333333
@Override
334-
public Publisher<Success> createIndex(final Bson key, final IndexOptions options) {
335-
return new SingleResultPublisher<Success>() {
334+
public Publisher<String> createIndex(final Bson key, final IndexOptions options) {
335+
return new SingleResultPublisher<String>() {
336336
@Override
337-
void execute(final SingleResultCallback<Success> callback) {
338-
wrapped.createIndex(key, options, voidToSuccessCallback(callback));
337+
void execute(final SingleResultCallback<String> callback) {
338+
wrapped.createIndex(key, options, callback);
339339
}
340340
};
341341
}
342342

343343
@Override
344-
public Publisher<Success> createIndexes(final List<IndexModel> indexes) {
345-
return new SingleResultPublisher<Success>() {
344+
public Publisher<String> createIndexes(final List<IndexModel> indexes) {
345+
return new SingleResultListPublisher<String>() {
346346
@Override
347-
void execute(final SingleResultCallback<Success> callback) {
348-
wrapped.createIndexes(indexes, voidToSuccessCallback(callback));
347+
void execute(final SingleResultCallback<List<String>> callback) {
348+
wrapped.createIndexes(indexes, callback);
349349
}
350350
};
351351
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.mongodb.reactivestreams.client;
2+
3+
import com.mongodb.async.SingleResultCallback;
4+
import org.reactivestreams.Publisher;
5+
import org.reactivestreams.Subscriber;
6+
7+
import java.util.List;
8+
import java.util.concurrent.atomic.AtomicBoolean;
9+
10+
abstract class SingleResultListPublisher<TResult> implements Publisher<TResult> {
11+
12+
@Override
13+
public void subscribe(final Subscriber<? super TResult> s) {
14+
new SingleResultSubscription(s).start();
15+
}
16+
17+
SingleResultCallback<List<TResult>> getCallback(final SubscriptionSupport<TResult> subscription) {
18+
return new SingleResultCallback<List<TResult>>() {
19+
@Override
20+
public void onResult(final List<TResult> results, final Throwable t) {
21+
subscription.log("result - " + results + " : " + t);
22+
if (t != null) {
23+
subscription.onError(t);
24+
} else {
25+
if (results != null) {
26+
for (TResult result : results) {
27+
subscription.onNext(result);
28+
}
29+
}
30+
subscription.onComplete();
31+
}
32+
}
33+
};
34+
}
35+
36+
private class SingleResultSubscription extends SubscriptionSupport<TResult> {
37+
private final AtomicBoolean operationCompleted = new AtomicBoolean();
38+
39+
public SingleResultSubscription(final Subscriber<? super TResult> subscriber) {
40+
super(subscriber);
41+
}
42+
43+
@Override
44+
protected void doRequest(final long n) {
45+
log("doRequest : " + n);
46+
if (operationCompleted.compareAndSet(false, true)) {
47+
execute(getCallback(this));
48+
}
49+
}
50+
}
51+
52+
abstract void execute(SingleResultCallback<List<TResult>> callback);
53+
54+
SingleResultListPublisher() {
55+
}
56+
}

driver/src/test/functional/com/mongodb/reactivestreams/client/SmokeTestSpecification.groovy

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 MongoDB, Inc.
2+
* Copyright 2014-2015 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package com.mongodb.reactivestreams.client
1818

1919
import com.mongodb.MongoNamespace
20+
import com.mongodb.client.model.IndexModel
2021
import com.mongodb.diagnostics.logging.Loggers
2122
import org.bson.Document
2223

@@ -89,14 +90,29 @@ class SmokeTestSpecification extends FunctionalSpecification {
8990
run('The count is zero', collection.&count)[0] == 0
9091

9192
then:
92-
run('create an index', collection.&createIndex, new Document('test', 1))[0] == Success.SUCCESS
93+
run('create an index', collection.&createIndex, new Document('test', 1))[0] == 'test_1'
9394

9495
then:
9596
def indexNames = run('has the newly created index', collection.&listIndexes)*.name
9697

9798
then:
9899
indexNames.containsAll('_id_', 'test_1')
99100

101+
then:
102+
run('create multiple indexes', collection.&createIndexes, [new IndexModel(new Document('multi', 1))])[0] == 'multi_1'
103+
104+
then:
105+
def indexNamesUpdated = run('has the newly created index', collection.&listIndexes)*.name
106+
107+
then:
108+
indexNamesUpdated.containsAll('_id_', 'test_1', 'multi_1')
109+
110+
then:
111+
run('drop the index', collection.&dropIndex, 'multi_1')[0] == Success.SUCCESS
112+
113+
then:
114+
run('has a single index left "_id" ', collection.&listIndexes).size == 2
115+
100116
then:
101117
run('drop the index', collection.&dropIndex, 'test_1')[0] == Success.SUCCESS
102118

driver/src/test/unit/com/mongodb/reactivestreams/client/MongoCollectionSpecification.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
package com.mongodb.reactivestreams.client
18+
1819
import com.mongodb.MongoNamespace
1920
import com.mongodb.ReadPreference
2021
import com.mongodb.WriteConcern

0 commit comments

Comments
 (0)