pariz / gountries

Gountries provides: Countries (ISO-3166-1), Country Subdivisions(ISO-3166-2), Currencies (ISO 4217), Geo Coordinates(ISO-6709) as well as translations, country borders and other stuff exposed as struct data.
MIT License
408 stars 67 forks source link

Embed data using go-bindata and use bi-directional map to speed up lookups #5

Closed BTBurke closed 7 years ago

BTBurke commented 7 years ago

I'm planning on using your library as part of an API and I was going to make a few changes to get faster lookups and embed the data directly into the binary to make it easier to deploy. Interested in a pull request?

Planned features:

  1. Embed the country data from the YAML files using go-bindata so that the deployment remains a single binary without relying on an external data directory. This would be backwards compatible so that when you populateCountries or populateSubdivisions it checks the bindata first and then falls back to the data directory.
  2. Use a bidirectional tree map from the gods library so that lookups by either country name or alpha code are more efficient.
  3. Support returning either all of the data or a particular subset of countries as a struct that is easily marshaled to JSON. This is useful to populate a frontend web UI for entering billing/shipping addresses.

This could simplify some of the query functions by using the tree to do lookups, rather than looping through all of the available data.

I'm going to fork your library and work on this a little bit. Let me know if you have any other ideas.

pariz commented 7 years ago
  1. Embedding the data is something that would simplify using the library for sure. Go ahead.
  2. Using a proper search algoritm is something i've been thinking about implementing for quite some time. I'm absolutely open for a PR regarding this. If you could provide some performance bench as well, i would be grateful!
  3. Here i'm a bit more hesitant. Can you elaborate on how you would go about implementing this?

Thanks for your ideas!

BTBurke commented 7 years ago

I didn't implement point 3 on this. That function might be specific to my use case and it's fairly easy to construct it in my own API from what gountries already provides. The query indexes yielded a pretty good speedup even without using any fancy data structures. I just used a simple map[string]string to provide lookups for Alpha2, Alpha3, and common name. Query time is reduced by 99%.

pariz commented 7 years ago

Fixed in https://github.com/pariz/gountries/pull/6