Skip to content

StringEncodedValues - #2204

Merged
karussell merged 4 commits into
graphhopper:masterfrom
otbutz:str_encoded_value
Dec 17, 2020
Merged

karussell merged 4 commits into
graphhopper:masterfrom
otbutz:str_encoded_value

Conversation

@otbutz

@otbutz otbutz commented Dec 8, 2020

Copy link
Copy Markdown
Contributor
  • minor code cleanup: add generic parameters to Enum references
  • add StringEncodedValue which encodes Strings based on their index within a predefined list
  • StringDetails for retrieval via PathDetails

@otbutz otbutz mentioned this pull request Dec 8, 2020
@karussell

Copy link
Copy Markdown
Member

Would it make sense to make this dynamic? I.e. only provide a maximum number of strings at construction time and feed strings as they come while import? This way we could easier introduce new tags. Hmmh, but it would also mean that a smaller area is fine and the larger area requires a higher number and breaks.

@otbutz

otbutz commented Dec 9, 2020

Copy link
Copy Markdown
Contributor Author

Would it make sense to make this dynamic? I.e. only provide a maximum number of strings at construction time and feed strings as they come while import? This way we could easier introduce new tags. Hmmh, but it would also mean that a smaller area is fine and the larger area requires a higher number and breaks.

I though a bit about dynamic approaches but encountered a few problems:

  • we need the number of bits at construction time so we can't really safe space
  • if we'd solve this somehow and only encode values we encounter while importing, we would still need to store the encountered values. Otherwise it would be impossible to recreate the StringEncodedValue when loading from an already existing graph.

I dodged the problem in my custom area PR by providing two filter mechanisms(bbox+id) which should allow to conveniently limit the number of areas without having to deal with GeoJSON file itself.

@otbutz
otbutz force-pushed the str_encoded_value branch from 3ecd60b to 48267b3 Compare December 9, 2020 13:57
@otbutz
otbutz requested a review from karussell December 9, 2020 15:13
@otbutz

otbutz commented Dec 16, 2020

Copy link
Copy Markdown
Contributor Author

Anything missing?

@karussell

Copy link
Copy Markdown
Member

we need the number of bits at construction time so we can't really safe space

If bits are full we could either throw an exception or everything goes into a special "OTHER" string (?)

we would still need to store the encountered values

Yes, it would be necessary to serialize this into the StorableProperties.

But let us do this later in a separate issue (if required at all).

Can you add an integration test where you collect e.g. "surface" or some other string tag via one of our test OSM PBFs? Because it might be useful to introduce some automatic normalization features like white space removal or lower case conversion etc? Hmmh, maybe we don't add such a test here and have a real integration test using the new Janino scripting (an important optimization there would be to do the comparison based on the index not on the String).

@otbutz

otbutz commented Dec 16, 2020

Copy link
Copy Markdown
Contributor Author

If bits are full we could either throw an exception

From my custom area PR:

   custom_area_files:
     - location: core/files/spatialrules/countries.geojson
       id_field: "ISO3166-1:alpha3"
       encoded_value: country
       filter: deu,aut
       max_bbox: -180,180,-90,90

Would you add an extra option to define the maximum number of encoded entries?

or everything goes into a special "OTHER" string (?)

I fear that this might cause erratic behavior and problems which are hard to reproduce.

e.g if we encode countries and reach our maximum number of entries, the resulting EV would completely depend on the order of the processed ways

Yes, it would be necessary to serialize this into the StorableProperties.

Don't we store the toString representation of the EncodedValues? Maybe we should extend this to a load/store mechanic.

Can you add an integration test where you collect e.g. "surface" or some other string tag via one of our test OSM PBFs? Because it might be useful to introduce some automatic normalization features like white space removal or lower case conversion etc?

The first usecase would be custom areas and i think it's ok to expect the IDs in the GeoJSON to be already normalized.

Hmmh, maybe we don't add such a test here and have a real integration test using the new Janino scripting (an important optimization there would be to do the comparison based on the index not on the String).

Not sure if feasible but you could lookup the String within the EncodedValue array once and rely on reference checks later on if you want to avoid String.equals()

@otbutz

otbutz commented Dec 16, 2020

Copy link
Copy Markdown
Contributor Author

We would also need to switch to a hashmap based approach as the Strings passed in via setString() are unordered, so we can't rely on binary search.

@otbutz

otbutz commented Dec 16, 2020

Copy link
Copy Markdown
Contributor Author

Something like this?

public final class StringEncodedValue extends UnsignedIntEncodedValue {
    private final List<String> values;
    private final ObjectIntMap<String> indexMap;
    private final int maxEntries;
    
    public StringEncodedValue(String name, int maxEntries) {
        this(name, maxEntries, false);
    }
    
    public StringEncodedValue(String name, int maxEntries, List<String> values, boolean storeTwoDirections) {
        super(name, 32 - Integer.numberOfLeadingZeros(maxEntries), storeTwoDirections);
        
        this.values = new ArrayList<>(values);
        this.indexMap = new ObjectIntHashMap<>(values.size());
        int index = 1;
        for (String value : values) {
            indexMap.put(value, index++);
        }
        this.maxEntries = maxEntries;
    }
    
    public StringEncodedValue(String name, int maxEntries, boolean storeTwoDirections) {
        super(name, 32 - Integer.numberOfLeadingZeros(maxEntries), storeTwoDirections);
        
        this.values = new ArrayList<>(maxEntries);
        this.indexMap = new ObjectIntHashMap<>(maxEntries);
        this.maxEntries = maxEntries;
    }

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

    public final String getString(boolean reverse, IntsRef ref) {
        int value = super.getInt(reverse, ref);
        if (value == 0) {
            return null;
        }
        return values.get(value);
    }

    public List<String> getValues() {
        return Collections.unmodifiableList(values);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof StringEncodedValue)) {
            return false;
        }
        StringEncodedValue other = (StringEncodedValue) obj;
        return Objects.equals(values, other.values);
    }

    @Override
    public int getVersion() {
        return 31 * super.getVersion() + staticHashCode(values);
    }
}

@karussell

karussell commented Dec 16, 2020 •

Copy link
Copy Markdown
Member

Yes, thanks :)

It might also be that for small arrays a simple List is faster for querying.

An additional indexOf method would be nice, so we can easier get the int (for fast usage inside Weighting.calc)

@otbutz
otbutz force-pushed the str_encoded_value branch 2 times, most recently from 332360a to 05716bc Compare December 17, 2020 13:58
@otbutz

otbutz commented Dec 17, 2020

Copy link
Copy Markdown
Contributor Author

Changed my implementation and added a few unittests

@karussell karussell left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - looks good!

Also for the additional tests regarding the edge cases.

@karussell karussell added this to the 3.0 milestone Dec 17, 2020
@karussell
karussell merged commit bc7ae61 into graphhopper:master Dec 17, 2020
@otbutz
otbutz deleted the str_encoded_value branch December 17, 2020 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants