Skip to content

Commit

Permalink
Updates and cleaning code
Browse files Browse the repository at this point in the history
- Changed float64 to float32 due to unneed precision. Saves several MB in storage and Memory
- Simplified code
- Added global definition for error messages
- Updated Readme
  • Loading branch information
evanoberholster committed Oct 28, 2018
1 parent 8a22452 commit 4a3a5b7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 50 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Alloc = 550 MiB TotalAlloc = 995 MiB Sys = 601 MiB NumGC = 10


```golang

package main
import (
"fmt"
Expand All @@ -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)
}
Expand All @@ -78,5 +73,4 @@ func main() {

tz.Close()
}

```
19 changes: 7 additions & 12 deletions cmd/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 6 additions & 10 deletions cmd/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 1 addition & 9 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (m *Memory)LoadTimezones() (error) {
}
}
}



m.timezones = tzs
return nil
}
Expand All @@ -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) {
Expand Down
38 changes: 34 additions & 4 deletions timezone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package timezoneLookup
import (
"os"
"time"
"errors"
"fmt"
"runtime"
"encoding/json"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 }
Expand Down

0 comments on commit 4a3a5b7

Please sign in to comment.