Skip to content

Store encoded String values - #2243

Closed
otbutz wants to merge 5 commits into
graphhopper:masterfrom
otbutz:store_string_values
Closed

otbutz wants to merge 5 commits into
graphhopper:masterfrom
otbutz:store_string_values

Conversation

@otbutz

@otbutz otbutz commented Feb 22, 2021 •

Copy link
Copy Markdown
Contributor

Stores the encoded String values as part of the toString representation of the EncodedValue in the StorableProperties.

As discussed in #2204 (comment)

@otbutz
otbutz force-pushed the store_string_values branch from 10c92b8 to c14018a Compare February 23, 2021 08:56
@otbutz
otbutz force-pushed the store_string_values branch from c14018a to 14c5753 Compare February 23, 2021 09:04
@otbutz
otbutz marked this pull request as ready for review February 23, 2021 09:18
@otbutz

otbutz commented Mar 2, 2021

Copy link
Copy Markdown
Contributor Author

@karussell i'm afraid we have to drop the specialized StringEncodedValue.getVersion() implementation and revert back to the default getVersion() of UnsignedIntEncodedValue. Currently testing this approach for #2201 and my problem is that we can't compare the EncodedValue created from the configuration with the one saved in the StoreableProperties as the first one lacks the List of String values which causes a version mismatch.

Another problem is the simple String comparison in GraphHopperStorgage.loadExisting(). We would need to skip additional properties here:

public GraphHopperStorage create(long byteCount) {
baseGraph.checkNotInitialized();
if (encodingManager == null)
throw new IllegalStateException("EncodingManager can only be null if you call loadExisting");
dir.create();
long initSize = Math.max(byteCount, 100);
properties.create(100);
properties.put("graph.encoded_values", encodingManager.toEncodedValuesAsString());
properties.put("graph.flag_encoders", encodingManager.toFlagEncodersAsString());

I'm also a bit confused as we seem to handle EncodingManager creation from existing graphs differently for GraphHopper and GraphHopperManaged:

GraphHopper(non-managed): importOrLoad() -> load() -> EncodingManager.create(EncodedValueFactory, FlagEncoderFactory, String)

GraphHopperManaged: GraphHopper.init() -> GraphHopper.createEncodingManager()

We can't offer any safety checks for the non-managed usecase as we have no configuration to compare to but we should probably try to unify code paths here.

@karussell

Copy link
Copy Markdown
Member

implementation and revert back to the default getVersion() of UnsignedIntEncodedValue

Can you describe why this is not possible?

What I also would find interesting is if we could say: hey please store the values, if they are in the following list, and if not store as "other" and if missing store as "missing".

Now another question for me is if we should we make strings behave like enums in custom models? Then we could write
something == MISSING in a condition and convert this internally to a string comparison with "missing" (we already do some index magic for strings) and if later the encoded value changes into an enum (or an enum into a string) the custom model is still the same and users won't notice.

Obviously this won't work for tags where case is important like name but our StringEncodedValues are not appropriate for that due to the many values anyway. (for that the StringIndex seems better)

@otbutz

otbutz commented Mar 8, 2021 •

Copy link
Copy Markdown
Contributor Author

implementation and revert back to the default getVersion() of UnsignedIntEncodedValue

Can you describe why this is not possible?

Currently we populate the values only while initially creating the graph. If we load an existing graph we don't have access to them anymore, so the StringEncodedValue is not really usable at the moment. If we add this persistence functionality we shouldn't break the detection of configuration mismatches which is handled here:

String encodedValueStr = properties.get("graph.encoded_values");
if (!encodingManager.toEncodedValuesAsString().equalsIgnoreCase(encodedValueStr)) {
throw new IllegalStateException("Encoded values do not match:"
+ "\nGraphhopper config: " + encodingManager.toEncodedValuesAsString()
+ "\nGraph: " + encodedValueStr
+ "\nChange configuration to match the graph or delete " + dir.getLocation());
}

Basically we create an EncodingManager from our configuration(read: no knowledge about values) and compare it with the String representation of the EncodingManager used while importing. If we use the approach proposed in this PR we end up with a mismatch:

Graph:

country_area|version=-1739096844|bits=6|index=0|shift=9|store_both_directions=false|values=deu;aut;cze

Config:

country_area|version=-1837704844|bits=6|index=0|shift=9|store_both_directions=false|values=

One solution could be to move from String to Object comparison(e.g configEncodingManager.equals(graphEncodingManager)). But this would still fail without reverting the changes to getVersion() and equals() of the StringEncodedValue as we can only reliably compare their basic properties like bits.

What I also would find interesting is if we could say: hey please store the values, if they are in the following list, and if not store as "other" and if missing store as "missing".

Maybe we could add an additional filter? This wouldn't break the usecase of custom areas where we need to populate this List of the fly, as we could pass a filter which accepts all values.

Now another question for me is if we should we make strings behave like enums in custom models? Then we could write
something == MISSING in a condition and convert this internally to a string comparison with "missing" (we already do some index magic for strings) and if later the encoded value changes into an enum (or an enum into a string) the custom model is still the same and users won't notice.

A missing value should be represented as null which is already mapped to 0:

public final void setString(boolean reverse, IntsRef ref, String value) {
if (value == null) {
super.setInt(reverse, ref, 0);
return;
}
int index = indexMap.get(value);
if (index == 0) {
if (values.size() == maxValues)
throw new IllegalStateException("Maximum number of values reached for " + getName() + ": " + maxValues);
values.add(value);
index = values.size();
indexMap.put(value, index);
}
super.setInt(reverse, ref, index);
}

I think we could avoid String comparisons for missing values if we check for null instead.

Obviously this won't work for tags where case is important like name but our StringEncodedValues are not appropriate for that due to the many values anyway. (for that the StringIndex seems better)

The StringIndex looks like it's more geared towards being used a key-value store. I'm missing the ability to store multiple values for a single key. Something like this:

stringIndex.add(ev.getName(), ev.getValues());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants