Attempt to fix issue converting MongoDB document _id to UUID (#2110)

This commit is contained in:
Luck 2020-04-05 16:42:09 +01:00
parent c71742a996
commit d2e60eb5ac
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -250,7 +250,7 @@ public class MongoStorage implements StorageImplementation {
while (cursor.hasNext()) { while (cursor.hasNext()) {
Document d = cursor.next(); Document d = cursor.next();
UUID uuid = d.get("_id", UUID.class); UUID uuid = getDocumentId(d);
Set<Node> nodes = new HashSet<>(nodesFromDoc(d)); Set<Node> nodes = new HashSet<>(nodesFromDoc(d));
Set<Node> results = nodes.stream() Set<Node> results = nodes.stream()
.map(bulkUpdate::apply) .map(bulkUpdate::apply)
@ -354,8 +354,7 @@ public class MongoStorage implements StorageImplementation {
MongoCollection<Document> c = this.database.getCollection(this.prefix + "users"); MongoCollection<Document> c = this.database.getCollection(this.prefix + "users");
try (MongoCursor<Document> cursor = c.find().iterator()) { try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) { while (cursor.hasNext()) {
Document d = cursor.next(); uuids.add(getDocumentId(cursor.next()));
uuids.add(d.get("_id", UUID.class));
} }
} }
return uuids; return uuids;
@ -368,7 +367,7 @@ public class MongoStorage implements StorageImplementation {
try (MongoCursor<Document> cursor = c.find().iterator()) { try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) { while (cursor.hasNext()) {
Document d = cursor.next(); Document d = cursor.next();
UUID holder = d.get("_id", UUID.class); UUID holder = getDocumentId(d);
Set<Node> nodes = new HashSet<>(nodesFromDoc(d)); Set<Node> nodes = new HashSet<>(nodesFromDoc(d));
for (Node e : nodes) { for (Node e : nodes) {
@ -601,8 +600,8 @@ public class MongoStorage implements StorageImplementation {
Set<UUID> conflicting = new HashSet<>(); Set<UUID> conflicting = new HashSet<>();
try (MongoCursor<Document> cursor = c.find(new Document("name", username)).iterator()) { try (MongoCursor<Document> cursor = c.find(new Document("name", username)).iterator()) {
if (cursor.hasNext()) { while (cursor.hasNext()) {
conflicting.add(cursor.next().get("_id", UUID.class)); conflicting.add(getDocumentId(cursor.next()));
} }
} }
conflicting.remove(uniqueId); conflicting.remove(uniqueId);
@ -621,7 +620,7 @@ public class MongoStorage implements StorageImplementation {
MongoCollection<Document> c = this.database.getCollection(this.prefix + "uuid"); MongoCollection<Document> c = this.database.getCollection(this.prefix + "uuid");
Document doc = c.find(new Document("name", username.toLowerCase())).first(); Document doc = c.find(new Document("name", username.toLowerCase())).first();
if (doc != null) { if (doc != null) {
return doc.get("_id", UUID.class); return getDocumentId(doc);
} }
return null; return null;
} }
@ -636,6 +635,17 @@ public class MongoStorage implements StorageImplementation {
return null; return null;
} }
private static UUID getDocumentId(Document doc) {
Object id = doc.get("_id");
if (id instanceof UUID) {
return (UUID) id;
} else if (id instanceof String) {
return UUID.fromString((String) id);
} else {
throw new IllegalArgumentException("Unknown id type: " + id.getClass().getName());
}
}
private static Document userToDoc(User user) { private static Document userToDoc(User user) {
List<Document> nodes = user.normalData().immutable().values().stream() List<Document> nodes = user.normalData().immutable().values().stream()
.map(MongoStorage::nodeToDoc) .map(MongoStorage::nodeToDoc)