Skip to content

Commit

Permalink
Merge pull request #32228 from vespa-engine/hmusum/check-for-null-in-…
Browse files Browse the repository at this point in the history
…method

Check for null in method instead of scattered all around the code
  • Loading branch information
baldersheim authored Aug 22, 2024
2 parents cbfb040 + 99597f5 commit b100ceb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,6 @@ static void checkIfActiveIsNewerThanSessionToBeActivated(long sessionId, long cu
*/
public boolean delete(ApplicationId applicationId) {
Tenant tenant = getTenant(applicationId);
if (tenant == null) return false;

TenantApplications tenantApplications = tenant.getApplicationRepo();
NestedTransaction transaction = new NestedTransaction();
Expand Down Expand Up @@ -716,7 +715,10 @@ public ApplicationFile getApplicationFileFromSession(TenantName tenantName, long
}

public Tenant getTenant(ApplicationId applicationId) {
return tenantRepository.getTenant(applicationId.tenant());
var tenant = tenantRepository.getTenant(applicationId.tenant());
if (tenant == null) throw new NotFoundException("Tenant '" + applicationId.tenant() + "' not found");

return tenant;
}

Application getApplication(ApplicationId applicationId) {
Expand All @@ -725,8 +727,6 @@ Application getApplication(ApplicationId applicationId) {

private Application getApplication(ApplicationId applicationId, Optional<Version> version) {
Tenant tenant = getTenant(applicationId);
if (tenant == null) throw new NotFoundException("Tenant '" + applicationId.tenant() + "' not found");

Optional<ApplicationVersions> activeApplicationVersions = tenant.getSessionRepository().activeApplicationVersions(applicationId);
if (activeApplicationVersions.isEmpty()) throw new NotFoundException("Unknown application id '" + applicationId + "'");

Expand Down Expand Up @@ -905,15 +905,11 @@ public Activation activate(Session session, ApplicationId applicationId, Tenant
* @return the active session, or null if there is no active session for the given application id.
*/
public Optional<Session> getActiveSession(ApplicationId applicationId) {
Tenant tenant = getTenant(applicationId);
if (tenant == null) throw new IllegalArgumentException("Could not find any tenant for '" + applicationId + "'");
return getActiveSession(tenant, applicationId);
return getActiveSession(getTenant(applicationId), applicationId);
}

public long getSessionIdForApplication(ApplicationId applicationId) {
Tenant tenant = getTenant(applicationId);
if (tenant == null)
throw new NotFoundException("Tenant '" + applicationId.tenant() + "' not found");
if (! tenant.getApplicationRepo().exists(applicationId))
throw new NotFoundException("Unknown application id '" + applicationId + "'");

Expand Down Expand Up @@ -1037,11 +1033,7 @@ public ApplicationMetaData getMetadataFromLocalSession(Tenant tenant, long sessi
}

private ApplicationCuratorDatabase requireDatabase(ApplicationId id) {
Tenant tenant = getTenant(id);
if (tenant == null)
throw new NotFoundException("Tenant '" + id.tenant().value() + "' not found");

return tenant.getApplicationRepo().database();
return getTenant(id).getApplicationRepo().database();
}

public ApplicationReindexing getReindexing(ApplicationId id) {
Expand All @@ -1050,11 +1042,7 @@ public ApplicationReindexing getReindexing(ApplicationId id) {
}

public void modifyReindexing(ApplicationId id, UnaryOperator<ApplicationReindexing> modifications) {
Tenant tenant = getTenant(id);
if (tenant == null)
throw new NotFoundException("Tenant '" + id.tenant().value() + "' not found");

tenant.getApplicationRepo().database().modifyReindexing(id, ApplicationReindexing.empty(), modifications);
getTenant(id).getApplicationRepo().database().modifyReindexing(id, ApplicationReindexing.empty(), modifications);
}

public PendingRestarts getPendingRestarts(ApplicationId id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,6 @@ private HttpResponse enableReindexing(ApplicationId applicationId) {
}

private HttpResponse getReindexingStatus(ApplicationId applicationId) {
Tenant tenant = applicationRepository.getTenant(applicationId);
if (tenant == null)
throw new NotFoundException("Tenant '" + applicationId.tenant().value() + "' not found");

try {
Map<String, Set<String>> documentTypes = getActiveModelOrThrow(applicationId).documentTypesByCluster();
ApplicationReindexing reindexing = applicationRepository.getReindexing(applicationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ private void assertNotAllowed(Method method) throws IOException {

private void deleteAndAssertOKResponseMocked(ApplicationId applicationId, boolean fullAppIdInUrl) throws IOException {
Tenant tenant = applicationRepository.getTenant(applicationId);
long sessionId = tenant.getApplicationRepo().requireActiveSessionOf(applicationId);
tenant.getApplicationRepo().requireActiveSessionOf(applicationId);
deleteAndAssertResponse(applicationId, Zone.defaultZone(), Response.Status.OK, null, fullAppIdInUrl);
}

Expand Down

0 comments on commit b100ceb

Please sign in to comment.