Skip to content

Instantly share code, notes, and snippets.

@fpapadopou
Last active November 20, 2019 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpapadopou/7ca61983bb41f125578322653ab6378d to your computer and use it in GitHub Desktop.
Save fpapadopou/7ca61983bb41f125578322653ab6378d to your computer and use it in GitHub Desktop.
A location-aware user database implemented using Google S2 Geometry and Hashicorp's go-memdb (store setup)
package store
import (
"github.com/golang/geo/s1"
"github.com/golang/geo/s2"
"github.com/hashicorp/go-memdb"
)
const (
minLevel = 15
maxLevel = 20
)
// Store contains the actual database and a search method.
type Store struct {
db *memdb.MemDB
coverer *s2.RegionCoverer
}
// User object represents a user in the database.
type User struct {
ID int
// CellIDs of users should be leafs in the S2 hierarchy (level 30)
CellID string
}
// New creates a new memDB store.
func New() (*Store, error) {
s := Store{}
err := s.initTable()
if err != nil {
return nil, err
}
s.coverer = &s2.RegionCoverer{
MinLevel: minLevel,
MaxLevel: maxLevel,
MaxCells: 50,
}
return &s, nil
}
func (s *Store) initTable() error {
schema := &memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{
"users": {
Name: "users",
Indexes: map[string]*memdb.IndexSchema{
"cellID": {
Name: "cellID",
Unique: false,
AllowMissing: false,
Indexer: &memdb.StringFieldIndex{Field: "CellID"},
},
},
},
},
}
var err error
s.db, err = memdb.NewMemDB(schema)
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment