From 3bef149ec16066e99ca694d1835ed3c2e4302835 Mon Sep 17 00:00:00 2001 From: "Matthew D. Groves" Date: Mon, 25 Sep 2023 17:12:21 -0400 Subject: [PATCH] #20 I think it was an index issue, but turns out don't need an index --- .../009_CreateIndexForGetComments.cs | 33 ------------------- .../Articles/Services/CommentsDataService.cs | 9 ++--- .../DataAccess/Queries/GetComments.n1qlnb | 2 +- 3 files changed, 6 insertions(+), 38 deletions(-) delete mode 100644 Conduit/Conduit.Migrations/009_CreateIndexForGetComments.cs diff --git a/Conduit/Conduit.Migrations/009_CreateIndexForGetComments.cs b/Conduit/Conduit.Migrations/009_CreateIndexForGetComments.cs deleted file mode 100644 index e44b0a90ea..0000000000 --- a/Conduit/Conduit.Migrations/009_CreateIndexForGetComments.cs +++ /dev/null @@ -1,33 +0,0 @@ -using NoSqlMigrator.Infrastructure; - -namespace Conduit.Migrations; - -// Manual alternative: CREATE INDEX `ix_get_comments` ON `Conduit`.`_default`.`Comments`(META().id, DISTINCT ARRAY c.authorUsername FOR c IN c2 END); -[Migration(9)] -public class CreateIndexForGetComments : MigrateBase -{ - private readonly string? _scopeName; - - public CreateIndexForGetComments() - { - _scopeName = _config["Couchbase:ScopeName"]; - } - - public override void Up() - { - // there are other options for more complex and/or covering indexes - // but this index is the simplest for the List Articles SQL++ query - Create.Index("ix_get_comments") - .OnScope(_scopeName) - .OnCollection("Comments") - .OnFieldRaw("META().`id`") - .OnFieldRaw($"DISTINCT ARRAY c.authorUsername FOR c IN c2 END"); - } - - public override void Down() - { - Delete.Index("ix_get_comments") - .FromScope(_scopeName) - .FromCollection("Comments"); - } -} \ No newline at end of file diff --git a/Conduit/Conduit.Web/Articles/Services/CommentsDataService.cs b/Conduit/Conduit.Web/Articles/Services/CommentsDataService.cs index 5774da2161..735653fd4f 100644 --- a/Conduit/Conduit.Web/Articles/Services/CommentsDataService.cs +++ b/Conduit/Conduit.Web/Articles/Services/CommentsDataService.cs @@ -59,8 +59,10 @@ public async Task>> Get(string slug, var bucket = scope.Bucket; var loggedInJoin = ""; + var loggedInProjection = " \"following\" : false "; if (currentUsername != null) { + loggedInProjection = $" \"following\": ARRAY_CONTAINS(COALESCE(follow,[]), c.authorUsername) "; loggedInJoin = $" LEFT JOIN `{bucket.Name}`.`{scope.Name}`.`Follows` follow ON ($currentUsername || \"::follows\") = META(follow).id "; } @@ -73,17 +75,16 @@ public async Task>> Get(string slug, author.bio, author.image, ""username"": c.authorUsername, - ""following"": ARRAY_CONTAINS(COALESCE(follow,[]), c.authorUsername) + {loggedInProjection} }} }} FROM {bucket.Name}.`{scope.Name}`.`Comments` c2 + USE KEYS $commentsKey UNNEST c2 AS c JOIN `{bucket.Name}`.`{scope.Name}`.`Users` author ON c.authorUsername = META(author).id - {loggedInJoin} - - WHERE META(c2).id = $commentsKey;"; + {loggedInJoin} ;"; var cluster = collection.Scope.Bucket.Cluster; // try diff --git a/Conduit/Conduit.Web/DataAccess/Queries/GetComments.n1qlnb b/Conduit/Conduit.Web/DataAccess/Queries/GetComments.n1qlnb index a672cdbedd..3f93bfcc49 100644 --- a/Conduit/Conduit.Web/DataAccess/Queries/GetComments.n1qlnb +++ b/Conduit/Conduit.Web/DataAccess/Queries/GetComments.n1qlnb @@ -1 +1 @@ -{"cells":[{"kind":2,"language":"SQL++","value":"SELECT VALUE\r\n {\r\n c.body,\r\n c.createdAt,\r\n c.id,\r\n \"author\" : {\r\n author.bio,\r\n author.image,\r\n \"username\": c.authorUsername,\r\n \"following\": ARRAY_CONTAINS(COALESCE(follow,[]), c.authorUsername)\r\n }\r\n }\r\n\r\nFROM Conduit._default.Comments c2\r\nUNNEST c2 AS c\r\nJOIN Conduit._default.Users author ON c.authorUsername = META(author).id\r\n\r\n/* join to the logged in user if not anonymous */\r\n/* parameterized with logged in username */\r\nLEFT JOIN Conduit._default.Follows follow ON (\"mgroves\" || \"::follows\") = META(follow).id\r\n\r\n/* parameterized with comments key */\r\nWHERE META(c2).id = \"HLbwIpJEajZi::comments\";\r\n"}]} \ No newline at end of file +{"cells":[{"kind":2,"language":"SQL++","value":"SELECT VALUE\r\n {\r\n c.body,\r\n c.createdAt,\r\n c.id,\r\n \"author\" : {\r\n author.bio,\r\n author.image,\r\n \"username\": c.authorUsername,\r\n \"following\": ARRAY_CONTAINS(COALESCE(follow,[]), c.authorUsername)\r\n }\r\n }\r\n\r\nFROM Conduit._default.Comments c2\r\nUSE KEYS \"OjeIfc7nudAW::comments\"\r\nUNNEST c2 AS c\r\nJOIN Conduit._default.Users author ON c.authorUsername = META(author).id\r\n\r\n/* join to the logged in user if not anonymous */\r\n/* parameterized with logged in username */\r\nLEFT JOIN Conduit._default.Follows follow ON (\"mgroves\" || \"::follows\") = META(follow).id\r\n\r\n\r\n"}]} \ No newline at end of file