Skip to content

Commit f6b7e69

Browse files
author
jmarkerink
committed
feat: moved code to functions
1 parent eb35818 commit f6b7e69

2 files changed

Lines changed: 113 additions & 87 deletions

File tree

core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoBackend.java

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -154,27 +154,10 @@ private Document getLog(String argument) {
154154
private Document handleAdminCommand(DatabaseCommand command, Document query) {
155155
switch (command.getCommand()) {
156156
case LIST_DATABASES: {
157-
List<Document> databases = listDatabaseNames().stream()
158-
.sorted()
159-
.map(databaseName -> {
160-
MongoDatabase database = openOrCreateDatabase(databaseName);
161-
Document dbObj = new Document("name", database.getDatabaseName());
162-
dbObj.put("empty", Boolean.valueOf(database.isEmpty()));
163-
return dbObj;
164-
})
165-
.toList();
166-
Document response = new Document();
167-
response.put("databases", databases);
168-
Utils.markOkay(response);
169-
return response;
157+
return handleListDatabases();
170158
}
171159
case FIND: {
172-
String collectionName = query.get(command.getQueryValue()).toString();
173-
if (collectionName.equals("$cmd.sys.inprog")) {
174-
return Utils.firstBatchCursorResponse(collectionName, new Document("inprog", Collections.emptyList()));
175-
} else {
176-
throw new NoSuchCommandException(new Document(command.getQueryValue(), collectionName).toString());
177-
}
160+
return handleFind(command, query);
178161
}
179162
case REPL_SET_GET_STATUS: {
180163
throw new NoReplicationEnabledException();
@@ -191,13 +174,7 @@ private Document handleAdminCommand(DatabaseCommand command, Document query) {
191174
return successResponse();
192175
}
193176
case CONNECTION_STATUS: {
194-
Document response = new Document();
195-
response.append("authInfo", new Document()
196-
.append("authenticatedUsers", Collections.emptyList())
197-
.append("authenticatedUserRoles", Collections.emptyList())
198-
);
199-
Utils.markOkay(response);
200-
return response;
177+
return handleConnectionStatus();
201178
}
202179
case HOST_INFO: {
203180
return handleHostInfo();
@@ -217,12 +194,47 @@ private Document handleAdminCommand(DatabaseCommand command, Document query) {
217194
}
218195
}
219196

197+
private Document handleListDatabases() {
198+
List<Document> databases = listDatabaseNames().stream()
199+
.sorted()
200+
.map(databaseName -> {
201+
MongoDatabase database = openOrCreateDatabase(databaseName);
202+
Document dbObj = new Document("name", database.getDatabaseName());
203+
dbObj.put("empty", Boolean.valueOf(database.isEmpty()));
204+
return dbObj;
205+
})
206+
.toList();
207+
Document response = new Document();
208+
response.put("databases", databases);
209+
Utils.markOkay(response);
210+
return response;
211+
}
212+
213+
private static Document handleFind(DatabaseCommand command, Document query) {
214+
String collectionName = query.get(command.getQueryValue()).toString();
215+
if (collectionName.equals("$cmd.sys.inprog")) {
216+
return Utils.firstBatchCursorResponse(collectionName, new Document("inprog", Collections.emptyList()));
217+
} else {
218+
throw new NoSuchCommandException(new Document(command.getQueryValue(), collectionName).toString());
219+
}
220+
}
221+
220222
private static Document successResponse() {
221223
Document response = new Document();
222224
Utils.markOkay(response);
223225
return response;
224226
}
225227

228+
private static Document handleConnectionStatus() {
229+
Document response = new Document();
230+
response.append("authInfo", new Document()
231+
.append("authenticatedUsers", Collections.emptyList())
232+
.append("authenticatedUserRoles", Collections.emptyList())
233+
);
234+
Utils.markOkay(response);
235+
return response;
236+
}
237+
226238
private Document handleHostInfo() {
227239
Document response = new Document();
228240
String osName = System.getProperty("os.name");
@@ -312,31 +324,9 @@ private MongoCollection<?> resolveCollection(String namespace) {
312324
@Override
313325
public Document handleCommand(Channel channel, String databaseName, DatabaseCommand command, Document query) {
314326
return switch (command.getCommand()) {
315-
case WHATS_MY_URI -> {
316-
Document response = new Document();
317-
InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
318-
response.put("you", remoteAddress.getAddress().getHostAddress() + ":" + remoteAddress.getPort());
319-
Utils.markOkay(response);
320-
yield response;
321-
}
322-
case IS_MASTER -> {
323-
Document response = new Document("ismaster", Boolean.TRUE);
324-
response.put("maxBsonObjectSize", Integer.valueOf(BsonConstants.MAX_BSON_OBJECT_SIZE));
325-
response.put("maxWriteBatchSize", Integer.valueOf(MongoWireProtocolHandler.MAX_WRITE_BATCH_SIZE));
326-
response.put("maxMessageSizeBytes", Integer.valueOf(MongoWireProtocolHandler.MAX_MESSAGE_SIZE_BYTES));
327-
response.put("maxWireVersion", Integer.valueOf(version.getWireVersion()));
328-
response.put("minWireVersion", Integer.valueOf(0));
329-
response.put("localTime", Instant.now(clock));
330-
Utils.markOkay(response);
331-
yield response;
332-
}
333-
case BUILD_INFO -> {
334-
Document response = new Document("version", version.toVersionString());
335-
response.put("versionArray", version.getVersionArray());
336-
response.put("maxBsonObjectSize", Integer.valueOf(BsonConstants.MAX_BSON_OBJECT_SIZE));
337-
Utils.markOkay(response);
338-
yield response;
339-
}
327+
case WHATS_MY_URI -> handleWhatsMyUri(channel);
328+
case IS_MASTER -> handleIsMaster();
329+
case BUILD_INFO -> handleBuildInfo();
340330
case DROP_DATABASE -> handleDropDatabase(databaseName);
341331
case GET_MORE -> handleGetMore(databaseName, command.getQueryValue(), query);
342332
case KILL_CURSORS -> handleKillCursors(query);
@@ -368,6 +358,34 @@ public void closeCursors(List<Long> cursorIds) {
368358
cursorIds.forEach(cursorRegistry::remove);
369359
}
370360

361+
private static Document handleWhatsMyUri(Channel channel) {
362+
Document response = new Document();
363+
InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
364+
response.put("you", remoteAddress.getAddress().getHostAddress() + ":" + remoteAddress.getPort());
365+
Utils.markOkay(response);
366+
return response;
367+
}
368+
369+
private Document handleIsMaster() {
370+
Document response = new Document("ismaster", Boolean.TRUE);
371+
response.put("maxBsonObjectSize", Integer.valueOf(BsonConstants.MAX_BSON_OBJECT_SIZE));
372+
response.put("maxWriteBatchSize", Integer.valueOf(MongoWireProtocolHandler.MAX_WRITE_BATCH_SIZE));
373+
response.put("maxMessageSizeBytes", Integer.valueOf(MongoWireProtocolHandler.MAX_MESSAGE_SIZE_BYTES));
374+
response.put("maxWireVersion", Integer.valueOf(version.getWireVersion()));
375+
response.put("minWireVersion", Integer.valueOf(0));
376+
response.put("localTime", Instant.now(clock));
377+
Utils.markOkay(response);
378+
return response;
379+
}
380+
381+
private Document handleBuildInfo() {
382+
Document response = new Document("version", version.toVersionString());
383+
response.put("versionArray", version.getVersionArray());
384+
response.put("maxBsonObjectSize", Integer.valueOf(BsonConstants.MAX_BSON_OBJECT_SIZE));
385+
Utils.markOkay(response);
386+
return response;
387+
}
388+
371389
protected Document handleKillCursors(Document query) {
372390
List<Long> cursorIds = (List<Long>) query.get("cursors");
373391
List<Long> cursorsKilled = new ArrayList<>();

core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoDatabase.java

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -123,45 +123,13 @@ public Document handleCommand(Channel channel, DatabaseCommand command, Document
123123
case CREATE_INDEXES -> commandCreateIndexes(query, query.get(commandName).toString());
124124
case COUNT -> commandCount(commandName, query);
125125
case AGGREGATE -> commandAggregate(commandName, query, databaseResolver, oplog);
126-
case DISTINCT -> {
127-
MongoCollection<P> collection = resolveCollection(commandName, query);
128-
if (collection == null) {
129-
Document response = new Document("values", Collections.emptyList());
130-
Utils.markOkay(response);
131-
yield response;
132-
} else {
133-
yield collection.handleDistinct(query);
134-
}
135-
}
126+
case DISTINCT -> commandDistinct(commandName, query);
136127
case DROP -> commandDrop(commandName, query, oplog);
137128
case DROP_INDEXES -> commandDropIndexes(commandName, query);
138129
case DB_STATS -> commandDatabaseStats();
139-
case COLL_STATS -> {
140-
MongoCollection<P> collection = resolveCollection(commandName, query);
141-
if (collection == null) {
142-
Document emptyStats = new Document()
143-
.append("count", 0)
144-
.append("size", 0);
145-
Utils.markOkay(emptyStats);
146-
yield emptyStats;
147-
} else {
148-
yield collection.getStats();
149-
}
150-
}
151-
case VALIDATE -> {
152-
MongoCollection<P> collection = resolveCollection(commandName, query);
153-
if (collection == null) {
154-
String collectionName = query.get(commandName).toString();
155-
String fullCollectionName = getDatabaseName() + "." + collectionName;
156-
throw new MongoServerError(26, "NamespaceNotFound", "Collection '" + fullCollectionName + "' does not exist to validate.");
157-
}
158-
yield collection.validate();
159-
}
160-
case FIND_AND_MODIFY -> {
161-
String collectionName = query.get(commandName).toString();
162-
MongoCollection<P> collection = resolveOrCreateCollection(collectionName);
163-
yield collection.findAndModify(query);
164-
}
130+
case COLL_STATS -> commandCollStats(commandName, query);
131+
case VALIDATE -> commandValidate(commandName, query);
132+
case FIND_AND_MODIFY -> findAndModify(commandName, query);
165133
case LIST_COLLECTIONS -> listCollections();
166134
case LIST_INDEXES -> listIndexes(query.get(commandName).toString());
167135
case TRIGGER_INTERNAL_EXCEPTION -> throw new NullPointerException("For testing purposes");
@@ -501,6 +469,35 @@ private Document commandDatabaseStats() {
501469
return response;
502470
}
503471

472+
private Document commandCollStats(String commandName, Document query) {
473+
MongoCollection<P> collection = resolveCollection(commandName, query);
474+
if (collection == null) {
475+
Document emptyStats = new Document()
476+
.append("count", 0)
477+
.append("size", 0);
478+
Utils.markOkay(emptyStats);
479+
return emptyStats;
480+
} else {
481+
return collection.getStats();
482+
}
483+
}
484+
485+
private Document commandValidate(String commandName, Document query) {
486+
MongoCollection<P> collection = resolveCollection(commandName, query);
487+
if (collection == null) {
488+
String collectionName = query.get(commandName).toString();
489+
String fullCollectionName = getDatabaseName() + "." + collectionName;
490+
throw new MongoServerError(26, "NamespaceNotFound", "Collection '" + fullCollectionName + "' does not exist to validate.");
491+
}
492+
return collection.validate();
493+
}
494+
495+
private Document findAndModify(String commandName, Document query) {
496+
String collectionName = query.get(commandName).toString();
497+
MongoCollection<P> collection = resolveOrCreateCollection(collectionName);
498+
return collection.findAndModify(query);
499+
}
500+
504501
protected abstract long getFileSize();
505502

506503
protected abstract long getStorageSize();
@@ -611,6 +608,17 @@ private Document commandAggregate(String command, Document query, DatabaseResolv
611608
return Utils.firstBatchCursorResponse(getFullCollectionNamespace(collectionName), aggregation.computeResult());
612609
}
613610

611+
private Document commandDistinct(String commandName, Document query) {
612+
MongoCollection<P> collection = resolveCollection(commandName, query);
613+
if (collection == null) {
614+
Document response = new Document("values", Collections.emptyList());
615+
Utils.markOkay(response);
616+
return response;
617+
} else {
618+
return collection.handleDistinct(query);
619+
}
620+
}
621+
614622
private Aggregation getAggregation(List<Document> pipeline, Document query, DatabaseResolver databaseResolver,
615623
MongoCollection<?> collection, Oplog oplog) {
616624
Aggregation aggregation = Aggregation.fromPipeline(pipeline, databaseResolver, this, collection, oplog);

0 commit comments

Comments
 (0)