Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
devinrsmith committed Mar 30, 2022
1 parent 3def8ed commit c021511
Showing 1 changed file with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class ApplicationServiceGrpcImpl extends ApplicationServiceGrpc.Applicati
implements ScriptSession.Listener, ApplicationState.Listener {
private static final Logger log = LoggerFactory.getLogger(ApplicationServiceGrpcImpl.class);

private static final String QUERY_SCOPE_DESCRIPTION = "query scope variable";

private final AppMode mode;
private final Scheduler scheduler;
private final SessionService sessionService;
Expand Down Expand Up @@ -65,16 +67,16 @@ public ApplicationServiceGrpcImpl(final AppMode mode,
this.typeLookup = typeLookup;
}

private void create(AppFieldId id, String type) {
accumulated.computeIfAbsent(id, this::newState).create(type);
private void create(AppFieldId id, String description, String type) {
accumulated.computeIfAbsent(id, this::newState).create(description, type);
}

private void update(AppFieldId id, String type) {
accumulated.computeIfAbsent(id, this::newState).update(type);
private void update(AppFieldId id, String description, String type) {
accumulated.computeIfAbsent(id, this::newState).update(description, type);
}

private void remove(AppFieldId id, String type) {
accumulated.computeIfAbsent(id, this::newState).remove(type);
private void remove(AppFieldId id, String description, String type) {
accumulated.computeIfAbsent(id, this::newState).remove(description, type);
}

private State newState(AppFieldId id) {
Expand All @@ -91,21 +93,21 @@ public synchronized void onScopeChanges(final ScriptSession scriptSession, final
final String key = e.getKey();
final String type = e.getValue();
final AppFieldId id = AppFieldId.fromScopeName(key);
remove(id, type);
remove(id, QUERY_SCOPE_DESCRIPTION, type);
}

for (Entry<String, String> e : changes.updated.entrySet()) {
final String key = e.getKey();
final String type = e.getValue();
final AppFieldId id = AppFieldId.fromScopeName(key);
update(id, type);
update(id, QUERY_SCOPE_DESCRIPTION, type);
}

for (Entry<String, String> e : changes.created.entrySet()) {
final String key = e.getKey();
final String type = e.getValue();
final AppFieldId id = AppFieldId.fromScopeName(key);
create(id, type);
create(id, QUERY_SCOPE_DESCRIPTION, type);
}

schedulePropagationOrClearIncrementalState();
Expand All @@ -120,7 +122,7 @@ public synchronized void onNewField(final ApplicationState app, final Field<?> f
}
final AppFieldId id = AppFieldId.from(app, field.name());
final String type = typeLookup.type(field.value()).orElse(null);
create(id, type);
create(id, field.description().orElse(null), type);
schedulePropagationOrClearIncrementalState();
}

Expand All @@ -132,7 +134,7 @@ public synchronized void onRemoveField(ApplicationState app, Field<?> oldField)
final AppFieldId id = AppFieldId.from(app, oldField.name());
// todo: or should we rely on the known type
final String type = typeLookup.type(oldField.value()).orElse(null);
remove(id, type);
remove(id, oldField.description().orElse(null), type);
schedulePropagationOrClearIncrementalState();
}

Expand Down Expand Up @@ -242,28 +244,36 @@ public static State existingState(AppFieldId id) {

private final AppFieldId id;
private final CreateUpdateRemoveState cur;
private String description;
private String type;

private State(AppFieldId id, CreateUpdateRemoveState cur) {
this.id = Objects.requireNonNull(id);
this.cur = Objects.requireNonNull(cur);
}

public void create(String type) {
public void create(String description, String type) {
cur.create();
this.description = description;
this.type = type;
}

public void update(String type) {
public void update(String description, String type) {
cur.update();
this.description = description;
this.type = type;
}

public void remove(String type) {
public void remove(String description, String type) {
cur.remove();
this.description = description;
this.type = type;
}

public boolean isRemoved() {
return cur.out() == CUR.REMOVED;
}

public Optional<FieldInfo> append(FieldsChangeUpdate.Builder builder) {
final CUR out = cur.out();
if (out == CUR.NOOP) {
Expand All @@ -272,6 +282,7 @@ public Optional<FieldInfo> append(FieldsChangeUpdate.Builder builder) {
final FieldInfo info = FieldInfo.newBuilder()
.setTypedTicket(typedTicket(id, type))
.setFieldName(id.fieldName)
.setFieldDescription(description == null ? "" : description)
.setApplicationId(id.applicationId())
.setApplicationName(id.applicationName())
.build();
Expand Down Expand Up @@ -300,7 +311,11 @@ private synchronized void propagateUpdates() {
final Optional<FieldInfo> info = state.append(builder);
if (info.isPresent()) {
isEmpty = false;
known.put(state.id, info.get());
if (state.isRemoved()) {
known.remove(state.id);
} else {
known.put(state.id, info.get());
}
}
}
accumulated.clear();
Expand Down

0 comments on commit c021511

Please sign in to comment.