use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.
the class Schema_144 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
Set<ExternalId> toAdd = new HashSet<>();
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
ResultSet rs = stmt.executeQuery("SELECT " + "account_id, " + "email_address, " + "password, " + "external_id " + "FROM account_external_ids")) {
while (rs.next()) {
Account.Id accountId = new Account.Id(rs.getInt(1));
String email = rs.getString(2);
String password = rs.getString(3);
String externalId = rs.getString(4);
toAdd.add(ExternalId.create(ExternalId.Key.parse(externalId), accountId, email, password));
}
}
try {
try (Repository repo = repoManager.openRepository(allUsersName);
RevWalk rw = new RevWalk(repo);
ObjectInserter ins = repo.newObjectInserter()) {
ObjectId rev = ExternalIdReader.readRevision(repo);
NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
for (ExternalId extId : toAdd) {
ExternalIdsUpdate.upsert(rw, ins, noteMap, extId);
}
ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, COMMIT_MSG, serverIdent, serverIdent);
}
} catch (IOException | ConfigInvalidException e) {
throw new OrmException("Failed to migrate external IDs to NoteDb", e);
}
}
use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.
the class Schema_148 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
try (Repository repo = repoManager.openRepository(allUsersName);
RevWalk rw = new RevWalk(repo);
ObjectInserter ins = repo.newObjectInserter()) {
ObjectId rev = ExternalIdReader.readRevision(repo);
NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
boolean dirty = false;
for (Note note : noteMap) {
byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(ExternalIdReader.MAX_NOTE_SZ);
try {
ExternalId extId = ExternalId.parse(note.getName(), raw);
if (needsUpdate(extId)) {
ExternalIdsUpdate.upsert(rw, ins, noteMap, extId);
dirty = true;
}
} catch (ConfigInvalidException e) {
ui.message(String.format("Warning: Ignoring invalid external ID note %s", note.getName()));
}
}
if (dirty) {
ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, COMMIT_MSG, serverUser, serverUser);
}
} catch (IOException e) {
throw new OrmException("Failed to update external IDs", e);
}
}
use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.
the class ExternalIdReader method all.
/** Reads and returns all external IDs. */
private Set<ExternalId> all(Repository repo, ObjectId rev) throws IOException {
if (rev.equals(ObjectId.zeroId())) {
return ImmutableSet.of();
}
try (Timer0.Context ctx = readAllLatency.start();
RevWalk rw = new RevWalk(repo)) {
NoteMap noteMap = readNoteMap(rw, rev);
Set<ExternalId> extIds = new HashSet<>();
for (Note note : noteMap) {
byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(MAX_NOTE_SZ);
try {
extIds.add(ExternalId.parse(note.getName(), raw));
} catch (Exception e) {
log.error(String.format("Ignoring invalid external ID note %s", note.getName()), e);
}
}
return extIds;
}
}
use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.
the class ExternalIdsBatchUpdate method commit.
/**
* Commits this batch.
*
* <p>This means external ID replacements which were prepared by invoking {@link
* #replace(ExternalId, ExternalId)} are now executed. Deletion of external IDs is done before
* adding the new external IDs. This means if an external ID is specified for deletion and an
* external ID with the same key is specified to be added, the old external ID with that key is
* deleted first and then the new external ID is added (so the external ID for that key is
* replaced).
*
* <p>For NoteDb a single commit is created that contains all the external ID updates.
*/
public void commit(String commitMessage) throws IOException, OrmException, ConfigInvalidException {
if (toDelete.isEmpty() && toAdd.isEmpty()) {
return;
}
try (Repository repo = repoManager.openRepository(allUsersName);
RevWalk rw = new RevWalk(repo);
ObjectInserter ins = repo.newObjectInserter()) {
ObjectId rev = ExternalIdReader.readRevision(repo);
NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
for (ExternalId extId : toDelete) {
ExternalIdsUpdate.remove(rw, noteMap, extId);
}
for (ExternalId extId : toAdd) {
ExternalIdsUpdate.insert(rw, ins, noteMap, extId);
}
ObjectId newRev = ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, commitMessage, serverIdent, serverIdent);
externalIdCache.onReplace(rev, newRev, toDelete, toAdd);
}
toAdd.clear();
toDelete.clear();
}
use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.
the class GroupNameNotesTest method readNameNote.
private String readNameNote(GroupReference g) throws Exception {
ObjectId k = getNoteKey(g);
try (RevWalk rw = new RevWalk(repo)) {
ObjectReader reader = rw.getObjectReader();
Ref ref = repo.exactRef(RefNames.REFS_GROUPNAMES);
NoteMap noteMap = NoteMap.read(reader, rw.parseCommit(ref.getObjectId()));
return new String(reader.open(noteMap.get(k), OBJ_BLOB).getCachedBytes(), UTF_8);
}
}
Aggregations