Skip to content

Commit

Permalink
chore: Simpler sequence implementation (appsmithorg#32264)
Browse files Browse the repository at this point in the history
Don't use MongoDB API to decide the key name to use for the sequence
name. This API is currently used only for datasources, so this
implementation will be compatible with both MongoDB and Postgres.

The actual sequence number generation, in the `getNext` method, will
basically need to be rewritten in the Postgres branch.
  • Loading branch information
sharat87 authored Apr 1, 2024
1 parent c6a3afd commit 6bbf33b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@

public interface SequenceServiceCE {

Mono<Long> getNext(String name);

Mono<Long> getNext(Class<? extends BaseDomain> domainClass, String suffix);

Mono<String> getNextAsSuffix(Class<? extends BaseDomain> domainClass, String suffix);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.appsmith.external.models.BaseDomain;
import com.appsmith.server.domains.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.query.Update;
import reactor.core.publisher.Mono;
Expand All @@ -11,17 +11,12 @@
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

@RequiredArgsConstructor
public class SequenceServiceCEImpl implements SequenceServiceCE {

private final ReactiveMongoTemplate mongoTemplate;

@Autowired
public SequenceServiceCEImpl(ReactiveMongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}

@Override
public Mono<Long> getNext(String name) {
private Mono<Long> getNext(String name) {
return mongoTemplate
.findAndModify(
query(where("name").is(name)),
Expand All @@ -31,14 +26,10 @@ public Mono<Long> getNext(String name) {
.map(Sequence::getNextNumber);
}

@Override
public Mono<Long> getNext(Class<? extends BaseDomain> domainClass, String suffix) {
return getNext(mongoTemplate.getCollectionName(domainClass) + suffix);
}

@Override
public Mono<String> getNextAsSuffix(Class<? extends BaseDomain> domainClass, String suffix) {
return getNext(mongoTemplate.getCollectionName(domainClass) + suffix)
.map(number -> number > 1 ? " " + number : "");
final String className = domainClass.getName();
final String name = className.substring(0, 1).toLowerCase() + className.substring(1) + suffix;
return getNext(name).map(number -> number > 1 ? " " + number : "");
}
}

0 comments on commit 6bbf33b

Please sign in to comment.