Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public class DeviceDataHashDefault
*/
private final List<ResultsHashSwig> resultsList = new ArrayList<>();

/**
* The engine which created this instance, used to turn a property name
* into the required property index the engine was built with.
*/
private final DeviceDetectionHashEngine hashEngine;

/**
* Constructs a new instance.
* @param logger used for logging
Expand All @@ -83,6 +89,18 @@ public class DeviceDataHashDefault
DeviceDetectionHashEngine engine,
MissingPropertyService missingPropertyService) {
super(logger, flowData, engine, missingPropertyService);
this.hashEngine = engine;
}

/**
* Get the required property index for a property name, or null when the
* engine was not built with that name, so the read falls back to the name.
* @param propertyName name of the property
* @return required property index, or null
*/
private Integer indexOf(String propertyName) {
return hashEngine == null ?
null : hashEngine.getPropertyIndex(propertyName);
}

/**
Expand Down Expand Up @@ -256,8 +274,10 @@ public AspectPropertyValue<List<String>> getValues(String propertyName) {
new AspectPropertyValueDefault<>();
ResultsHashSwig results = getSingleResults(propertyName);
if (results != null) {
try (VectorStringValuesSwig value = results.getValues(
Swig.asBytes(propertyName))) {
Integer index = indexOf(propertyName);
try (VectorStringValuesSwig value = index == null ?
results.getValues(Swig.asBytes(propertyName)) :
results.getValues(index)) {
if (value.hasValue()) {
try (VectorStringSwig vector = value.getValue()) {
result.setValue(Collections.unmodifiableList(
Expand All @@ -277,9 +297,10 @@ protected AspectPropertyValue<String> getValueAsString(String propertyName) {
AspectPropertyValue<String> result = new AspectPropertyValueDefault<>();
ResultsHashSwig results = getSingleResults(propertyName);
if (results != null) {

try (StringValueSwig value = results.getValueAsString(
Swig.asBytes(propertyName))) {
Integer index = indexOf(propertyName);
try (StringValueSwig value = index == null ?
results.getValueAsString(Swig.asBytes(propertyName)) :
results.getValueAsString(index)) {
if (value.hasValue()) {
result.setValue(value.getValue());
}
Expand All @@ -298,8 +319,10 @@ protected AspectPropertyValue<JavaScript> getValueAsJavaScript(
new AspectPropertyValueDefault<>();
ResultsHashSwig results = getSingleResults(propertyName);
if (results != null) {
try (StringValueSwig value = results.getValueAsString(
Swig.asBytes(propertyName))) {
Integer index = indexOf(propertyName);
try (StringValueSwig value = index == null ?
results.getValueAsString(Swig.asBytes(propertyName)) :
results.getValueAsString(index)) {
if (value.hasValue()) {
result.setValue(new JavaScript(value.getValue()));
}
Expand All @@ -317,8 +340,10 @@ protected AspectPropertyValue<Integer> getValueAsInteger(
AspectPropertyValue<Integer> result = new AspectPropertyValueDefault<>();
ResultsHashSwig results = getSingleResults(propertyName);
if (results != null) {
try (IntegerValueSwig value = results.getValueAsInteger(
Swig.asBytes(propertyName))) {
Integer index = indexOf(propertyName);
try (IntegerValueSwig value = index == null ?
results.getValueAsInteger(Swig.asBytes(propertyName)) :
results.getValueAsInteger(index)) {
if (value.hasValue()) {
result.setValue(value.getValue());
}
Expand All @@ -335,8 +360,10 @@ protected AspectPropertyValue<Boolean> getValueAsBool(String propertyName) {
AspectPropertyValue<Boolean> result = new AspectPropertyValueDefault<>();
ResultsHashSwig results = getSingleResults(propertyName);
if (results != null) {
try (BoolValueSwig value = results.getValueAsBool(
Swig.asBytes(propertyName))) {
Integer index = indexOf(propertyName);
try (BoolValueSwig value = index == null ?
results.getValueAsBool(Swig.asBytes(propertyName)) :
results.getValueAsBool(index)) {
if (value.hasValue()) {
result.setValue(value.getValue());
}
Expand All @@ -353,8 +380,10 @@ protected AspectPropertyValue<Double> getValueAsDouble(String propertyName) {
AspectPropertyValue<Double> result = new AspectPropertyValueDefault<>();
ResultsHashSwig results = getSingleResults(propertyName);
if (results != null) {
try (DoubleValueSwig value = results.getValueAsDouble(
Swig.asBytes(propertyName))) {
Integer index = indexOf(propertyName);
try (DoubleValueSwig value = index == null ?
results.getValueAsDouble(Swig.asBytes(propertyName)) :
results.getValueAsDouble(index)) {
if (value.hasValue()) {
result.setValue(value.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ public class DeviceDetectionHashEngine
private volatile boolean propertiesPopulated = false;
private final Random rand = new Random();

/**
* Case insensitive map from each property name the engine was built with
* to its required property index, which is the place of the name in that
* list, the first where a name is given more than once, as the native
* engine gives it. Made from the names given at build and never from the
* data set, so an index keeps naming the same property after a reload
* from a data file whose property order differs. Empty when the engine
* was built with no names, in which case reads go by name.
*/
private final Map<String, Integer> propertyIndexes;

/**
* Construct a new instance of the {@link DeviceDetectionHashEngine}.
* @param logger logger instance to use for logging
Expand All @@ -70,6 +81,8 @@ public class DeviceDetectionHashEngine
* @param properties native required properties configuration which define
* the properties which the engine should be initialised
* with
* @param propertyNames the property names {@code properties} was made
* from, in the same order
* @param deviceDataFactory the factory to use when creating a
* {@link DeviceDataHash} instance
* @param tempDataFileDir the file where a temporary data file copy
Expand All @@ -80,11 +93,13 @@ public class DeviceDetectionHashEngine
AspectEngineDataFile dataFile,
ConfigHashSwig config,
RequiredPropertiesConfigSwig properties,
List<String> propertyNames,
ElementDataFactory<DeviceDataHash> deviceDataFactory,
String tempDataFileDir) {
super(logger, deviceDataFactory, tempDataFileDir);
this.config = config;
this.propertiesConfigSwig = properties;
this.propertyIndexes = indexesOf(propertyNames);
addDataFile(dataFile);
}

Expand Down Expand Up @@ -113,6 +128,37 @@ public MetaDataSwig getMetaData() {
return engine.getMetaData();
}

/**
* Get the required property index for a property name, or null when the
* engine was not built with that name and the read has to go by name.
* @param propertyName name of the property
* @return required property index, or null
*/
Integer getPropertyIndex(String propertyName) {
return propertyIndexes.get(propertyName);
}

/**
* Map each name to its place in the list, keeping the first place of a
* name given more than once in any letter case.
* @param names the property names in the order the engine is built with,
* or null
* @return unmodifiable case insensitive map from name to index
*/
private static Map<String, Integer> indexesOf(List<String> names) {
Map<String, Integer> indexes =
new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
if (names != null) {
for (int i = 0; i < names.size(); i++) {
String name = names.get(i);
if (name != null && name.isEmpty() == false) {
indexes.putIfAbsent(name, i);
}
}
}
return Collections.unmodifiableMap(indexes);
}

@Override
public List<FiftyOneAspectPropertyMetaData> getProperties() {
if (isFalse(propertiesPopulated)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ protected DeviceDetectionHashEngine newEngine(List<String> properties) {
dataFile,
config,
requiredProperties,
properties,
new HashDataFactory(loggerFactory),
tempDir);
}
Expand Down
Loading
Loading