public class GroupNameNotes extends VersionedMetaData
The way groups are stored in NoteDb (see GroupConfig
) doesn't enforce unique names,
even though groups in Gerrit must not have duplicate names. The storage format doesn't allow to
quickly look up whether a name has already been used either. That's why we additionally keep a
map of name/UUID pairs and manage it with this class.
To claim the name for a new group, create an instance of GroupNameNotes
via forNewGroup(Project.NameKey, Repository, AccountGroup.UUID, AccountGroup.NameKey)
and call
commit(MetaDataUpdate)
on it.
For renaming, call forRename(Project.NameKey, Repository, AccountGroup.UUID,
AccountGroup.NameKey, AccountGroup.NameKey)
and also commit the returned GroupNameNotes
.
Both times, the creation of the GroupNameNotes
will fail if the (new) name is already
used. Committing the GroupNameNotes
is necessary to make the adjustments for real.
The map has an additional benefit: We can quickly iterate over all group name/UUID pairs without having to load all groups completely (which is costly).
Internal details
The map of names is represented by Git notes
. They are stored on the branch
RefNames.REFS_GROUPNAMES
. Each commit on the branch reflects one moment in time of the
complete map.
As key for the notes, we use the SHA-1 of the name. As data, they contain a text version of a
JGit Config
file. That config file has two entries:
VersionedMetaData.BatchMetaDataUpdate, VersionedMetaData.PathInfo
inserter, newTree, projectName, reader, revision, rw
Modifier and Type | Method and Description |
---|---|
static GroupNameNotes |
forNewGroup(Project.NameKey projectName,
org.eclipse.jgit.lib.Repository repository,
AccountGroup.UUID groupUuid,
AccountGroup.NameKey groupName)
Creates an instance of
GroupNameNotes for use when creating a new group. |
static GroupNameNotes |
forRename(Project.NameKey projectName,
org.eclipse.jgit.lib.Repository repository,
AccountGroup.UUID groupUuid,
AccountGroup.NameKey oldName,
AccountGroup.NameKey newName)
Creates an instance of
GroupNameNotes for use when renaming a group. |
static org.eclipse.jgit.lib.ObjectId |
getNoteKey(AccountGroup.NameKey groupName) |
protected java.lang.String |
getRefName() |
static com.google.common.collect.ImmutableList<GroupReference> |
loadAllGroups(org.eclipse.jgit.lib.Repository repository)
Loads the
GroupReference s (name/UUID pairs) for all groups. |
static java.util.Optional<GroupReference> |
loadGroup(org.eclipse.jgit.lib.Repository repository,
AccountGroup.NameKey groupName)
Loads the
GroupReference (name/UUID pair) for the group with the specified name. |
protected void |
onLoad()
Set up the metadata, parsing any state from the loaded revision.
|
protected boolean |
onSave(org.eclipse.jgit.lib.CommitBuilder commit)
Save any changes to the metadata in a commit.
|
static void |
updateAllGroups(org.eclipse.jgit.lib.Repository repository,
org.eclipse.jgit.lib.ObjectInserter inserter,
org.eclipse.jgit.lib.BatchRefUpdate bru,
java.util.Collection<GroupReference> groupReferences,
org.eclipse.jgit.lib.PersonIdent ident)
Replaces the map of name/UUID pairs with a new version which matches exactly the passed
GroupReference s. |
commit, commitToNewRef, getObjectId, getPathInfos, getRevision, load, load, load, load, load, openUpdate, readConfig, readConfig, readFile, readTree, readUTF8, saveConfig, saveFile, saveUTF8, set, set, set
public static GroupNameNotes forRename(Project.NameKey projectName, org.eclipse.jgit.lib.Repository repository, AccountGroup.UUID groupUuid, AccountGroup.NameKey oldName, AccountGroup.NameKey newName) throws java.io.IOException, org.eclipse.jgit.errors.ConfigInvalidException, com.google.gerrit.exceptions.DuplicateKeyException
GroupNameNotes
for use when renaming a group.
Note: The returned instance of GroupNameNotes
has to be committed
via commit(MetaDataUpdate)
in
order to claim the new name and free up the old one.
projectName
- the name of the project which holds the commits of the notesrepository
- the repository which holds the commits of the notesgroupUuid
- the UUID of the group which is renamedoldName
- the current name of the groupnewName
- the new name of the groupGroupNameNotes
configured for a specific renaming of a groupjava.io.IOException
- if the repository can't be accessed for some reasonorg.eclipse.jgit.errors.ConfigInvalidException
- if the note for the specified group doesn't exist or is in an
invalid statecom.google.gerrit.exceptions.DuplicateKeyException
- if a group with the new name already existspublic static GroupNameNotes forNewGroup(Project.NameKey projectName, org.eclipse.jgit.lib.Repository repository, AccountGroup.UUID groupUuid, AccountGroup.NameKey groupName) throws java.io.IOException, org.eclipse.jgit.errors.ConfigInvalidException, com.google.gerrit.exceptions.DuplicateKeyException
GroupNameNotes
for use when creating a new group.
Note: The returned instance of GroupNameNotes
has to be committed
via commit(MetaDataUpdate)
in
order to claim the new name.
projectName
- the name of the project which holds the commits of the notesrepository
- the repository which holds the commits of the notesgroupUuid
- the UUID of the new groupgroupName
- the name of the new groupGroupNameNotes
configured for a specific group creationjava.io.IOException
- if the repository can't be accessed for some reasonorg.eclipse.jgit.errors.ConfigInvalidException
- in no case so farcom.google.gerrit.exceptions.DuplicateKeyException
- if a group with the new name already existspublic static java.util.Optional<GroupReference> loadGroup(org.eclipse.jgit.lib.Repository repository, AccountGroup.NameKey groupName) throws java.io.IOException, org.eclipse.jgit.errors.ConfigInvalidException
GroupReference
(name/UUID pair) for the group with the specified name.repository
- the repository which holds the commits of the notesgroupName
- the name of the groupGroupReference
if a group/note with the given name existsjava.io.IOException
- if the repository can't be accessed for some reasonorg.eclipse.jgit.errors.ConfigInvalidException
- if the note for the specified group is in an invalid statepublic static com.google.common.collect.ImmutableList<GroupReference> loadAllGroups(org.eclipse.jgit.lib.Repository repository) throws java.io.IOException, org.eclipse.jgit.errors.ConfigInvalidException
GroupReference
s (name/UUID pairs) for all groups.
Even though group UUIDs should be unique, this class doesn't enforce it. For this reason,
it's technically possible that two of the GroupReference
s have a duplicate UUID but a
different name. In practice, this shouldn't occur unless we introduce a bug in the future.
repository
- the repository which holds the commits of the notesGroupReference
s of all existing groups/notesjava.io.IOException
- if the repository can't be accessed for some reasonorg.eclipse.jgit.errors.ConfigInvalidException
- if one of the notes is in an invalid statepublic static void updateAllGroups(org.eclipse.jgit.lib.Repository repository, org.eclipse.jgit.lib.ObjectInserter inserter, org.eclipse.jgit.lib.BatchRefUpdate bru, java.util.Collection<GroupReference> groupReferences, org.eclipse.jgit.lib.PersonIdent ident) throws java.io.IOException
GroupReference
s.
All old entries are discarded and replaced by the new ones.
This operation also works if the previous map has invalid entries or can't be read anymore.
Note: This method doesn't flush the ObjectInserter
. It doesn't
execute the BatchRefUpdate
either.
repository
- the repository which holds the commits of the notesinserter
- an ObjectInserter
for that repositorybru
- a BatchRefUpdate
to which this method adds commandsgroupReferences
- all GroupReference
s (name/UUID pairs) which should be contained
in the map of name/UUID pairsident
- the PersonIdent
which is used as author and committer for commitsjava.io.IOException
- if the repository can't be accessed for some reasonprotected java.lang.String getRefName()
getRefName
in class VersionedMetaData
protected void onLoad() throws java.io.IOException, org.eclipse.jgit.errors.ConfigInvalidException
VersionedMetaData
onLoad
in class VersionedMetaData
java.io.IOException
org.eclipse.jgit.errors.ConfigInvalidException
protected boolean onSave(org.eclipse.jgit.lib.CommitBuilder commit) throws java.io.IOException, org.eclipse.jgit.errors.ConfigInvalidException
VersionedMetaData
onSave
in class VersionedMetaData
java.io.IOException
org.eclipse.jgit.errors.ConfigInvalidException
public static org.eclipse.jgit.lib.ObjectId getNoteKey(AccountGroup.NameKey groupName)