Skip to content

Instantly share code, notes, and snippets.

@swook
Last active December 18, 2015 09:49
Show Gist options
  • Save swook/5764374 to your computer and use it in GitHub Desktop.
Save swook/5764374 to your computer and use it in GitHub Desktop.
Maxmind GeoLiteCity-Blocks.csv in IPTrie
package main
import (
"code.google.com/p/iptrie"
"encoding/csv"
"fmt"
"io"
"os"
"runtime"
"runtime/debug"
"strconv"
)
const (
_ = iota
KB uint64 = 1 << (10 * iota)
MB
GB
)
func main() {
m := &runtime.MemStats{}
printMemStats(m)
t := getMaxmindData()
t.Get("127.0.0.1")
// Free any memory if possible
runtime.GC()
debug.FreeOSMemory()
printMemStats(m)
}
func printMemStats(m *runtime.MemStats) {
runtime.ReadMemStats(m)
fmt.Printf("Alloc: %dMB\n", m.Alloc/MB)
fmt.Printf("TotalAlloc: %dMB\n", m.TotalAlloc/MB)
fmt.Printf("Sys: %dMB\n", m.Sys/MB)
fmt.Printf("Mallocs: %d\n", m.Mallocs)
fmt.Printf("Frees: %d\n\n", m.Frees)
}
func getMaxmindData() *iptrie.IPTrie {
// Open stream to Blocks CSV db
f, err := os.Open("GeoLiteCity-Blocks.csv")
defer f.Close()
if err != nil {
panic(err)
}
// Create CSV reader
r := csv.NewReader(f)
// Create IPTrie
t := iptrie.NewIPTrie()
var record []string
n := 0
var i uint64
var ipstart, ipend, data uint32
for {
// Read until EOF
record, err = r.Read()
if err == io.EOF {
break
}
// Parse begin_ip_num
i, err = strconv.ParseUint(record[0], 10, 32)
if err != nil {
fmt.Println("Error parsing ipstart")
continue
}
ipstart = uint32(i)
// Parse end_ip_num
i, err = strconv.ParseUint(record[1], 10, 32)
if err != nil {
fmt.Println("Error parsing ipend")
continue
}
ipend = uint32(i)
// Parse location ID
i, err = strconv.ParseUint(record[2], 10, 32)
if err != nil {
fmt.Println("Error parsing data")
continue
}
data = uint32(i)
// Add to IPTrie
t.AddRangeNum(ipstart, ipend, data)
n++
}
fmt.Println("Done adding", n, "records.\n")
return t
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment