Shard refs/starred-changes refs by change ID instead of account ID
If the ref is sharded by change ID, the lookup by change ID is cheaper. This is because now we can list all refs that start with refs/starred-changes/ plus sharded change ID and looking up refs with a prefix that end on '/' is optimized in jgit. Finding all changes that were starred by a user gets more expensive since now we need to scan the complete refs/starred-changes/ namespace for this. The ref format is changed, because we plan to store the users that have starred a change in the change index, and this means that we need to look them up each time a change is reindex. If reindexing a change would require a complete scan over the refs/starred-changes/ namespace this would result in performance issues. On the other hand looking up the changes that were starred by a user can be done via the change index so that we don't need to scan the refs/starred-changes/ namespace in this case either. Change-Id: Ib81d46283035ef753d35fbc78684ded9f074bd1f Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
		| @@ -95,37 +95,36 @@ public class RefNames { | |||||||
|  |  | ||||||
|   public static String refsDraftComments(Account.Id accountId, |   public static String refsDraftComments(Account.Id accountId, | ||||||
|       Change.Id changeId) { |       Change.Id changeId) { | ||||||
|     StringBuilder r = buildRefsPrefix(REFS_DRAFT_COMMENTS, accountId); |     StringBuilder r = buildRefsPrefix(REFS_DRAFT_COMMENTS, accountId.get()); | ||||||
|     r.append(changeId.get()); |     r.append(changeId.get()); | ||||||
|     return r.toString(); |     return r.toString(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static String refsDraftCommentsPrefix(Account.Id accountId) { |   public static String refsDraftCommentsPrefix(Account.Id accountId) { | ||||||
|     return buildRefsPrefix(REFS_DRAFT_COMMENTS, accountId).toString(); |     return buildRefsPrefix(REFS_DRAFT_COMMENTS, accountId.get()).toString(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static String refsStarredChanges(Account.Id accountId, |   public static String refsStarredChanges(Change.Id changeId, | ||||||
|       Change.Id changeId) { |       Account.Id accountId) { | ||||||
|     StringBuilder r = buildRefsPrefix(REFS_STARRED_CHANGES, accountId); |     StringBuilder r = buildRefsPrefix(REFS_STARRED_CHANGES, changeId.get()); | ||||||
|     r.append(changeId.get()); |     r.append(accountId.get()); | ||||||
|     return r.toString(); |     return r.toString(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static String refsStarredChangesPrefix(Account.Id accountId) { |   public static String refsStarredChangesPrefix(Change.Id changeId) { | ||||||
|     return buildRefsPrefix(REFS_STARRED_CHANGES, accountId).toString(); |     return buildRefsPrefix(REFS_STARRED_CHANGES, changeId.get()).toString(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private static StringBuilder buildRefsPrefix(String prefix, |   private static StringBuilder buildRefsPrefix(String prefix, int id) { | ||||||
|       Account.Id accountId) { |  | ||||||
|     StringBuilder r = new StringBuilder(); |     StringBuilder r = new StringBuilder(); | ||||||
|     r.append(prefix); |     r.append(prefix); | ||||||
|     int n = accountId.get() % 100; |     int n = id % 100; | ||||||
|     if (n < 10) { |     if (n < 10) { | ||||||
|       r.append('0'); |       r.append('0'); | ||||||
|     } |     } | ||||||
|     r.append(n); |     r.append(n); | ||||||
|     r.append('/'); |     r.append('/'); | ||||||
|     r.append(accountId.get()); |     r.append(id); | ||||||
|     r.append('/'); |     r.append('/'); | ||||||
|     return r; |     return r; | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -50,14 +50,14 @@ public class RefNamesTest { | |||||||
|  |  | ||||||
|   @Test |   @Test | ||||||
|   public void refsStarredChanges() throws Exception { |   public void refsStarredChanges() throws Exception { | ||||||
|     assertThat(RefNames.refsStarredChanges(accountId, changeId)) |     assertThat(RefNames.refsStarredChanges(changeId, accountId)) | ||||||
|       .isEqualTo("refs/starred-changes/23/1011123/67473"); |       .isEqualTo("refs/starred-changes/73/67473/1011123"); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test |   @Test | ||||||
|   public void refsStarredChangesPrefix() throws Exception { |   public void refsStarredChangesPrefix() throws Exception { | ||||||
|     assertThat(RefNames.refsStarredChangesPrefix(accountId)) |     assertThat(RefNames.refsStarredChangesPrefix(changeId)) | ||||||
|       .isEqualTo("refs/starred-changes/23/1011123/"); |       .isEqualTo("refs/starred-changes/73/67473/"); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test |   @Test | ||||||
|   | |||||||
| @@ -90,7 +90,7 @@ public class StarredChangesUtil { | |||||||
|     try (Repository repo = repoManager.openRepository(allUsers); |     try (Repository repo = repoManager.openRepository(allUsers); | ||||||
|         RevWalk rw = new RevWalk(repo)) { |         RevWalk rw = new RevWalk(repo)) { | ||||||
|       RefUpdate u = repo.updateRef( |       RefUpdate u = repo.updateRef( | ||||||
|           RefNames.refsStarredChanges(accountId, changeId)); |           RefNames.refsStarredChanges(changeId, accountId)); | ||||||
|       u.setExpectedOldObjectId(ObjectId.zeroId()); |       u.setExpectedOldObjectId(ObjectId.zeroId()); | ||||||
|       u.setNewObjectId(emptyTree(repo)); |       u.setNewObjectId(emptyTree(repo)); | ||||||
|       u.setRefLogIdent(serverIdent); |       u.setRefLogIdent(serverIdent); | ||||||
| @@ -139,7 +139,7 @@ public class StarredChangesUtil { | |||||||
|     try (Repository repo = repoManager.openRepository(allUsers); |     try (Repository repo = repoManager.openRepository(allUsers); | ||||||
|         RevWalk rw = new RevWalk(repo)) { |         RevWalk rw = new RevWalk(repo)) { | ||||||
|       RefUpdate u = repo.updateRef( |       RefUpdate u = repo.updateRef( | ||||||
|           RefNames.refsStarredChanges(accountId, changeId)); |           RefNames.refsStarredChanges(changeId, accountId)); | ||||||
|       u.setForceUpdate(true); |       u.setForceUpdate(true); | ||||||
|       u.setRefLogIdent(serverIdent); |       u.setRefLogIdent(serverIdent); | ||||||
|       u.setRefLogMessage("Unstar change " + changeId.get(), true); |       u.setRefLogMessage("Unstar change " + changeId.get(), true); | ||||||
| @@ -181,7 +181,7 @@ public class StarredChangesUtil { | |||||||
|       batchUpdate.setRefLogIdent(serverIdent); |       batchUpdate.setRefLogIdent(serverIdent); | ||||||
|       batchUpdate.setRefLogMessage("Unstar change " + changeId.get(), true); |       batchUpdate.setRefLogMessage("Unstar change " + changeId.get(), true); | ||||||
|       for (Account.Id accountId : byChange(changeId)) { |       for (Account.Id accountId : byChange(changeId)) { | ||||||
|         String refName = RefNames.refsStarredChanges(accountId, changeId); |         String refName = RefNames.refsStarredChanges(changeId, accountId); | ||||||
|         Ref ref = repo.getRefDatabase().getRef(refName); |         Ref ref = repo.getRefDatabase().getRef(refName); | ||||||
|         batchUpdate.addCommand(new ReceiveCommand(ref.getObjectId(), |         batchUpdate.addCommand(new ReceiveCommand(ref.getObjectId(), | ||||||
|             ObjectId.zeroId(), refName)); |             ObjectId.zeroId(), refName)); | ||||||
| @@ -200,7 +200,7 @@ public class StarredChangesUtil { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public Iterable<Account.Id> byChange(final Change.Id changeId) |   public Iterable<Account.Id> byChange(Change.Id changeId) | ||||||
|       throws OrmException { |       throws OrmException { | ||||||
|     if (!migration.readAccounts()) { |     if (!migration.readAccounts()) { | ||||||
|       return FluentIterable |       return FluentIterable | ||||||
| @@ -212,22 +212,18 @@ public class StarredChangesUtil { | |||||||
|             } |             } | ||||||
|           }); |           }); | ||||||
|     } |     } | ||||||
|     return FluentIterable.from(getRefNames(RefNames.REFS_STARRED_CHANGES)) |     return FluentIterable | ||||||
|         .filter(new Predicate<String>() { |         .from(getRefNames(RefNames.refsStarredChangesPrefix(changeId))) | ||||||
|           @Override |  | ||||||
|           public boolean apply(String refPart) { |  | ||||||
|             return refPart.endsWith("/" + changeId.get()); |  | ||||||
|           } |  | ||||||
|         }) |  | ||||||
|         .transform(new Function<String, Account.Id>() { |         .transform(new Function<String, Account.Id>() { | ||||||
|           @Override |           @Override | ||||||
|           public Account.Id apply(String refPart) { |           public Account.Id apply(String refPart) { | ||||||
|             return Account.Id.fromRefPart(refPart); |             return Account.Id.parse(refPart); | ||||||
|           } |           } | ||||||
|         }); |         }); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public ResultSet<Change.Id> query(Account.Id accountId) { |   @Deprecated | ||||||
|  |   public ResultSet<Change.Id> query(final Account.Id accountId) { | ||||||
|     try { |     try { | ||||||
|       if (!migration.readAccounts()) { |       if (!migration.readAccounts()) { | ||||||
|         return new ChangeIdResultSet( |         return new ChangeIdResultSet( | ||||||
| @@ -235,7 +231,13 @@ public class StarredChangesUtil { | |||||||
|       } |       } | ||||||
|  |  | ||||||
|       return new ListResultSet<>(FluentIterable |       return new ListResultSet<>(FluentIterable | ||||||
|           .from(getRefNames(RefNames.refsStarredChangesPrefix(accountId))) |           .from(getRefNames(RefNames.REFS_STARRED_CHANGES)) | ||||||
|  |           .filter(new Predicate<String>() { | ||||||
|  |             @Override | ||||||
|  |             public boolean apply(String refPart) { | ||||||
|  |               return refPart.endsWith("/" + accountId.get()); | ||||||
|  |             } | ||||||
|  |           }) | ||||||
|           .transform(new Function<String, Change.Id>() { |           .transform(new Function<String, Change.Id>() { | ||||||
|             @Override |             @Override | ||||||
|             public Change.Id apply(String changeId) { |             public Change.Id apply(String changeId) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Edwin Kempin
					Edwin Kempin