Skip to content

Commit cfd52fd

Browse files
author
chris-hoefgen
committed
fixing some logic and adding new WASM variable function
1 parent 9b34ad9 commit cfd52fd

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

src/main/java/com/devcycle/sdk/server/local/api/DVCLocalClient.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.devcycle.sdk.server.local.model.BucketedUserConfig;
1313
import com.devcycle.sdk.server.local.model.DVCLocalOptions;
1414
import com.fasterxml.jackson.core.JsonProcessingException;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
1516

1617
public final class DVCLocalClient {
1718

@@ -100,10 +101,6 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) {
100101
throw new IllegalArgumentException("Missing parameter: defaultValue");
101102
}
102103

103-
if (!configManager.isConfigInitialized()) {
104-
System.out.println("Variable called before DVCClient has initialized, returning default value");
105-
}
106-
107104
TypeEnum variableType = TypeEnum.fromClass(defaultValue.getClass());
108105
Variable<T> defaultVariable = (Variable<T>) Variable.builder()
109106
.key(key)
@@ -113,40 +110,41 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) {
113110
.isDefaulted(true)
114111
.build();
115112

116-
if (!isInitialized) {
113+
if (!configManager.isConfigInitialized() || !isInitialized) {
114+
System.out.println("Variable called before DVCClient has initialized, returning default value");
115+
try {
116+
eventQueueManager.queueAggregateEvent(Event.builder().type("aggVariableDefaulted").target(key).build(), null);
117+
} catch (Exception e) {
118+
System.out.printf("Unable to parse aggVariableDefaulted event for Variable %s due to error: %s", key, e.toString());
119+
}
117120
return defaultVariable;
118121
}
119122

120123
try {
121-
BucketedUserConfig bucketedUserConfig = localBucketing.generateBucketedConfig(sdkKey, user);
122-
if (bucketedUserConfig.variables.containsKey(key)) {
123-
BaseVariable baseVariable = bucketedUserConfig.variables.get(key);
124+
String variableJSON = localBucketing.getVariable(sdkKey, user, key, variableType, true);
125+
if (variableJSON == null || variableJSON.isEmpty()) {
126+
return defaultVariable;
127+
} else {
128+
ObjectMapper mapper = new ObjectMapper();
129+
Variable baseVariable = mapper.readValue(variableJSON, Variable.class);
130+
124131
Variable<T> variable = (Variable<T>) Variable.builder()
125-
.key(key)
126-
.type(baseVariable.getType())
127-
.value(baseVariable.getValue())
128-
.defaultValue(defaultValue)
129-
.isDefaulted(false)
130-
.build();
132+
.key(key)
133+
.type(baseVariable.getType())
134+
.value(baseVariable.getValue())
135+
.defaultValue(defaultValue)
136+
.isDefaulted(false)
137+
.build();
131138
if (variable.getType() != variableType) {
132-
throw new IllegalArgumentException("Variable type mismatch, returning default value");
139+
System.out.printf("Variable type mismatch, returning default value");
140+
return variable;
133141
}
134142
variable.setDefaultValue(defaultValue);
135143
variable.setIsDefaulted(false);
136-
eventQueueManager.queueAggregateEvent(Event.builder().type("aggVariableEvaluated").target(key).build(), bucketedUserConfig);
137144
return variable;
138-
} else {
139-
eventQueueManager.queueAggregateEvent(Event.builder().type("aggVariableDefaulted").target(key).build(), bucketedUserConfig);
140-
return defaultVariable;
141145
}
142146
} catch (Exception e) {
143-
System.out.printf("Unable to parse JSON for Variable %s due to error: %s", key, e.toString());
144-
}
145-
146-
try {
147-
eventQueueManager.queueAggregateEvent(Event.builder().type("aggVariableDefaulted").target(key).build(), null);
148-
} catch (Exception e) {
149-
System.out.printf("Unable to parse aggVariableDefaulted event for Variable %s due to error: %s", key, e.toString());
147+
System.out.printf("Unable to parse load Variable %s due to error: %s", key, e);
150148
}
151149
return defaultVariable;
152150
}

src/main/java/com/devcycle/sdk/server/local/bucketing/LocalBucketing.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.concurrent.atomic.AtomicReference;
1313

1414
import com.devcycle.sdk.server.common.model.User;
15+
import com.devcycle.sdk.server.common.model.Variable;
1516
import com.devcycle.sdk.server.local.model.BucketedUserConfig;
1617
import com.devcycle.sdk.server.local.model.FlushPayload;
1718
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -31,6 +32,8 @@ public class LocalBucketing {
3132
private Set<Integer> pinnedAddresses;
3233
private HashMap<String, Integer> sdkKeyAddresses;
3334

35+
private HashMap<Variable.TypeEnum, Integer> variableTypeMap = new HashMap<Variable.TypeEnum, Integer>();
36+
3437
public LocalBucketing() {
3538
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
3639

@@ -53,6 +56,12 @@ public LocalBucketing() {
5356

5457
Memory mem = linker.get(store, "", "memory").get().memory();
5558
memRef.set(mem);
59+
60+
// WASM time seems problematic for getting global values so we'll just hardcode them
61+
variableTypeMap.put(Variable.TypeEnum.BOOLEAN, 0);
62+
variableTypeMap.put(Variable.TypeEnum.NUMBER, 1);
63+
variableTypeMap.put(Variable.TypeEnum.STRING, 2);
64+
variableTypeMap.put(Variable.TypeEnum.JSON, 3);
5665
}
5766

5867
private Collection<Extern> setImportsOnLinker() {
@@ -166,6 +175,26 @@ public BucketedUserConfig generateBucketedConfig(String sdkKey, User user) throw
166175
return config;
167176
}
168177

178+
public String getVariable(String sdkKey, User user, String key, Variable.TypeEnum variableTypeEnum, boolean shouldTrackEvent) throws JsonProcessingException {
179+
// need some kind of mutex?
180+
String userString = OBJECT_MAPPER.writeValueAsString(user);
181+
182+
int wasmVariableType = this.variableTypeMap.get(variableTypeEnum);
183+
184+
unpinAll();
185+
int sdkKeyAddress = getSDKKeyAddress(sdkKey);
186+
int userAddress = newWasmString(userString);
187+
int keyAddress = newWasmString(key);
188+
189+
Func getVariablePtr = linker.get(store, "", "variableForUser").get().func();
190+
WasmFunctions.Function5<Integer, Integer, Integer, Integer, Integer, Integer > variableForUser = WasmFunctions.func(
191+
store, getVariablePtr, I32, I32, I32, I32, I32, I32);
192+
193+
int resultAddress = variableForUser.call(sdkKeyAddress, userAddress, keyAddress, wasmVariableType, shouldTrackEvent ? 1 : 0);
194+
String variableString = readWasmString(resultAddress);
195+
return variableString;
196+
}
197+
169198
public void initEventQueue(String sdkKey, String options) {
170199
unpinAll();
171200
int sdkKeyAddress = getSDKKeyAddress(sdkKey);

0 commit comments

Comments
 (0)