1

Specify unique ids for restaurants

This commit is contained in:
Alex Yatskov 2015-09-22 12:30:27 +09:00
parent 6db18ea5ea
commit f23e54c022
2 changed files with 24 additions and 12 deletions

View File

@ -43,6 +43,8 @@ type restaurant struct {
closestStnName string closestStnName string
closestStnDist float64 closestStnDist float64
id uint32
} }
func loadConverters(directory string) ([]*converter, error) { func loadConverters(directory string) ([]*converter, error) {

View File

@ -29,8 +29,8 @@ import (
"hash/fnv" "hash/fnv"
) )
func collateData(reviews []review) map[uint64]*restaurant { func collateData(reviews []review) map[uint32]*restaurant {
restaurants := make(map[uint64]*restaurant) restaurants := make(map[uint32]*restaurant)
for _, rev := range reviews { for _, rev := range reviews {
var buff bytes.Buffer var buff bytes.Buffer
@ -38,13 +38,20 @@ func collateData(reviews []review) map[uint64]*restaurant {
binary.Write(&buff, binary.LittleEndian, rev.longitude) binary.Write(&buff, binary.LittleEndian, rev.longitude)
binary.Write(&buff, binary.LittleEndian, rev.name) binary.Write(&buff, binary.LittleEndian, rev.name)
hash := fnv.New64() hash := fnv.New32()
hash.Write(buff.Bytes()) hash.Write(buff.Bytes())
id := hash.Sum32()
var rest *restaurant var rest *restaurant
if rest, _ = restaurants[hash.Sum64()]; rest == nil { if rest, _ = restaurants[id]; rest == nil {
rest = &restaurant{name: rev.name, address: rev.address, latitude: rev.latitude, longitude: rev.longitude} rest = &restaurant{
restaurants[hash.Sum64()] = rest name: rev.name,
address: rev.address,
latitude: rev.latitude,
longitude: rev.longitude,
id: id,
}
restaurants[id] = rest
} }
rest.reviews = append(rest.reviews, rev) rest.reviews = append(rest.reviews, rev)
@ -53,7 +60,7 @@ func collateData(reviews []review) map[uint64]*restaurant {
return restaurants return restaurants
} }
func computeSemantics(restaraunts map[uint64]*restaurant) { func computeSemantics(restaraunts map[uint32]*restaurant) {
type definer interface { type definer interface {
define(keyword string) semantics define(keyword string) semantics
} }
@ -83,7 +90,7 @@ func computeSemantics(restaraunts map[uint64]*restaurant) {
} }
} }
func computeStations(restaurants map[uint64]*restaurant, stationsPath string) error { func computeStations(restaurants map[uint32]*restaurant, stationsPath string) error {
sq, err := newStationQuery(stationsPath) sq, err := newStationQuery(stationsPath)
if err != nil { if err != nil {
return err return err
@ -96,7 +103,7 @@ func computeStations(restaurants map[uint64]*restaurant, stationsPath string) er
return nil return nil
} }
func dumpData(dbPath string, restaraunts map[uint64]*restaurant) error { func dumpData(dbPath string, restaraunts map[uint32]*restaurant) error {
db, err := sql.Open("sqlite3", dbPath) db, err := sql.Open("sqlite3", dbPath)
if err != nil { if err != nil {
return err return err
@ -137,8 +144,9 @@ func dumpData(dbPath string, restaraunts map[uint64]*restaurant) error {
longitude, longitude,
closestStnDist, closestStnDist,
closestStnName, closestStnName,
accessCount accessCount,
) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, id
) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
rest.name, rest.name,
rest.address, rest.address,
rest.sem.Delicious, rest.sem.Delicious,
@ -149,7 +157,9 @@ func dumpData(dbPath string, restaraunts map[uint64]*restaurant) error {
rest.longitude, rest.longitude,
rest.closestStnDist, rest.closestStnDist,
rest.closestStnName, rest.closestStnName,
0) 0,
rest.id,
)
if err != nil { if err != nil {
return err return err