diff --git a/README.md b/README.md index 0a78254..e26c9d9 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,6 @@ Alloc = 550 MiB TotalAlloc = 995 MiB Sys = 601 MiB NumGC = 10 ```golang - package main import ( "fmt" @@ -55,16 +54,12 @@ import ( var tz timezone.TimezoneInterface func main() { - // BoltDB storage with Snappy Compression and MsgPack encoding - tz = timezone.BoltdbStorage(timezone.WithSnappy, "timezone", "msgpack") - - // BoltDB storage with Snappy Compression and json encoding - //tz = timezone.BoltdbStorage(timezone.WithSnappy, "timezone", "json") - - // Storage in JSON file with Snappy Compression loaded and queried from Memory - //tz = timezone.MemoryStorage(timezone.WithSnappy, "memory") - - err := tz.LoadTimezones() + tz, err := timezone.LoadTimezones(timezone.Config{ + DatabaseType:"boltdb", // memory or boltdb + DatabaseName:"timezone", // Name without suffix + Snappy: true, + Encoding: "msgpack", // json or msgpack + }) if err != nil { fmt.Println(err) } @@ -78,5 +73,4 @@ func main() { tz.Close() } - ``` \ No newline at end of file diff --git a/cmd/benchmark.go b/cmd/benchmark.go index d9f4e4d..e722796 100644 --- a/cmd/benchmark.go +++ b/cmd/benchmark.go @@ -5,19 +5,14 @@ import ( timezone "github.com/evanoberholster/timezoneLookup" ) -var tz timezone.TimezoneInterface - func main() { - // BoltDB storage with Snappy Compression and MsgPack encoding - //tz = timezone.BoltdbStorage(timezone.WithSnappy, "timezone", "msgpack") - - // BoltDB storage with Snappy Compression and json encoding - tz = timezone.BoltdbStorage(timezone.WithSnappy, "timezone", "json") - - // Storage in JSON file with Snappy Compression loaded and queried from Memory - //tz = timezone.MemoryStorage(timezone.WithSnappy, "memory") - - err := tz.LoadTimezones() + + tz, err := timezone.LoadTimezones(timezone.Config{ + DatabaseType:"boltdb", // memory or boltdb + DatabaseName:"timezone", // Name without suffix + Snappy: true, + Encoding: "msgpack", // json or msgpack + }) if err != nil { fmt.Println(err) } diff --git a/cmd/example.go b/cmd/example.go index f41c8df..0a64923 100644 --- a/cmd/example.go +++ b/cmd/example.go @@ -6,16 +6,12 @@ import ( var tz timezone.TimezoneInterface func main() { - // BoltDB storage with Snappy Compression and MsgPack encoding - tz = timezone.BoltdbStorage(timezone.WithSnappy, "timezone", "msgpack") - - // BoltDB storage with Snappy Compression and json encoding - //tz = timezone.BoltdbStorage(timezone.WithSnappy, "timezone", "json") - - // Storage in JSON file with Snappy Compression loaded and queried from Memory - //tz = timezone.MemoryStorage(timezone.WithSnappy, "memory") - - err := tz.LoadTimezones() + tz, err := timezone.LoadTimezones(timezone.Config{ + DatabaseType:"boltdb", // memory or boltdb + DatabaseName:"timezone", // Name without suffix + Snappy: true, + Encoding: "msgpack", // json or msgpack + }) if err != nil { fmt.Println(err) } diff --git a/db.go b/db.go index bdddcb6..7558231 100644 --- a/db.go +++ b/db.go @@ -24,15 +24,7 @@ type PolygonIndex struct { Min Coord `json:"min"` } -const ( - errNotExistGeoJSON = "Error: GeoJSON file does not exist" - errExistDatabase = "Error: Destination Database file already exists" - errNotExistDatabase = "Error: Database file does not exist" - errPolygonNotFound = "Error: Polygon for Timezone not found" - errTimezoneNotFound = "Error: Timezone not found" -) - -func BoltdbStorage(snappy bool, filename string, encoding string) *Store { +func BoltdbStorage(snappy bool, filename string, encoding string) TimezoneInterface { if snappy { filename = filename + ".snap.db" } else { diff --git a/memory.go b/memory.go index 1286395..a0cccff 100644 --- a/memory.go +++ b/memory.go @@ -56,8 +56,7 @@ func (m *Memory)LoadTimezones() (error) { } } } - - + m.timezones = tzs return nil } @@ -72,7 +71,7 @@ func (m *Memory)Query(q Coord) (string, error) { } } } - return "Error", errors.New("Timezone not found") + return "Error", errors.New(errTimezoneNotFound) } func (m *Memory)writeTimezoneJSON(dbFilename string) (error) { diff --git a/timezone.go b/timezone.go index 81801b3..52321b9 100644 --- a/timezone.go +++ b/timezone.go @@ -2,6 +2,7 @@ package timezoneLookup import ( "os" "time" + "errors" "fmt" "runtime" "encoding/json" @@ -10,6 +11,14 @@ import ( const ( WithSnappy = true NoSnappy = false + + // Errors + errNotExistGeoJSON = "Error: GeoJSON file does not exist" + errExistDatabase = "Error: Destination Database file already exists" + errNotExistDatabase = "Error: Database file does not exist" + errPolygonNotFound = "Error: Polygon for Timezone not found" + errTimezoneNotFound = "Error: Timezone not found" + errDatabaseTypeUknown = "Error: Database type unknown" ) type TimezoneInterface interface { @@ -45,12 +54,33 @@ type Polygon struct { } type Coord struct { - Lat float64 `json:"lat"` - Lon float64 `json:"lon"` + Lat float32 `json:"lat"` + Lon float32 `json:"lon"` } +type Config struct { + DatabaseName string + DatabaseType string + Snappy bool + Encoding string +} + var Tz TimezoneInterface +func LoadTimezones(config Config) (TimezoneInterface, error) { + if config.DatabaseType == "memory" { + tz := MemoryStorage(config.Snappy, config.DatabaseName) + err := tz.LoadTimezones() + return tz, err + + } else if config.DatabaseType == "boltdb" { + tz := BoltdbStorage(config.Snappy, config.DatabaseName, config.Encoding) + err := tz.LoadTimezones() + return tz, err + } + return &Memory{}, errors.New(errDatabaseTypeUknown) +} + func TimezonesFromGeoJSON(filename string) ([]Timezone, error) { start_decode := time.Now() fmt.Println("Building Timezone Database from: ", filename) @@ -114,8 +144,8 @@ func (t *Timezone)newPolygon() (Polygon) { } func (p *Polygon)updatePolygon(xy []interface{}) { - lat := xy[0].(float64) - lon := xy[1].(float64) + lat := float32(xy[0].(float64)) + lon := float32(xy[1].(float64)) // Update max and min limits if p.Max.Lat < lat { p.Max.Lat = lat }