From 0415b697318a22a64b80a0fb112943950599bf9b Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 18 Jan 2019 15:51:10 +0100 Subject: [PATCH] mapobjectdb --- mapobject/clearmapdata.go | 4 +-- mapobject/poi.go | 4 +-- {mapdb => mapobjectdb}/accessor.go | 8 +++--- mapobjectdb/logger.go | 11 ++++++++ mapobjectdb/sqlite.go | 42 ++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 8 deletions(-) rename {mapdb => mapobjectdb}/accessor.go (74%) create mode 100644 mapobjectdb/logger.go create mode 100644 mapobjectdb/sqlite.go diff --git a/mapobject/clearmapdata.go b/mapobject/clearmapdata.go index 8cafd88..1f68c8e 100644 --- a/mapobject/clearmapdata.go +++ b/mapobject/clearmapdata.go @@ -3,11 +3,11 @@ package mapobject import ( "mapserver/coords" "mapserver/mapblockparser" - "mapserver/mapdb" + "mapserver/mapobjectdb" ) type ClearMapData struct { - db mapdb.DBAccessor + db mapobjectdb.DBAccessor } func (this *ClearMapData) OnParsedMapBlock(block *mapblockparser.MapBlock, pos coords.MapBlockCoords) { diff --git a/mapobject/poi.go b/mapobject/poi.go index 5229dbc..c013865 100644 --- a/mapobject/poi.go +++ b/mapobject/poi.go @@ -3,11 +3,11 @@ package mapobject import ( "mapserver/coords" "mapserver/mapblockparser" - "mapserver/mapdb" + "mapserver/mapobjectdb" ) type POI struct { - db mapdb.DBAccessor + db mapobjectdb.DBAccessor } func (this *POI) OnParsedMapBlock(block *mapblockparser.MapBlock, pos coords.MapBlockCoords) { diff --git a/mapdb/accessor.go b/mapobjectdb/accessor.go similarity index 74% rename from mapdb/accessor.go rename to mapobjectdb/accessor.go index 1c4c9c4..d636c48 100644 --- a/mapdb/accessor.go +++ b/mapobjectdb/accessor.go @@ -1,10 +1,10 @@ -package mapdb +package mapobjectdb import ( "mapserver/coords" ) -type MapData struct { +type MapObject struct { //mapblock position MBPos coords.MapBlockCoords @@ -24,7 +24,7 @@ type SearchQuery struct { type DBAccessor interface { Migrate() error - GetMapData(q SearchQuery) ([]MapData, error) + GetMapData(q SearchQuery) ([]MapObject, error) RemoveMapData(pos coords.MapBlockCoords) error - AddMapData(data MapData) error + AddMapData(data MapObject) error } diff --git a/mapobjectdb/logger.go b/mapobjectdb/logger.go new file mode 100644 index 0000000..3dbcf6a --- /dev/null +++ b/mapobjectdb/logger.go @@ -0,0 +1,11 @@ +package mapobjectdb + +import ( + "github.com/sirupsen/logrus" +) + +var log *logrus.Entry + +func init() { + log = logrus.WithFields(logrus.Fields{"prefix": "mapobjectdb"}) +} diff --git a/mapobjectdb/sqlite.go b/mapobjectdb/sqlite.go new file mode 100644 index 0000000..4d0c11e --- /dev/null +++ b/mapobjectdb/sqlite.go @@ -0,0 +1,42 @@ +package mapobjectdb + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" + "github.com/sirupsen/logrus" + "time" +) + +const migrateScript = ` +create table if not exists objects( + id bigint primary key auto_increment, + x int, + y int, + z int, + posx int, + posy int, + posz int, + type varchar, + data blob, + mtime bigint, +); +` + +type Sqlite3Accessor struct { + db *sql.DB + filename string +} + +func (db *Sqlite3Accessor) Migrate() error { + log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database") + start := time.Now() + _, err := db.db.Exec(migrateScript) + if err != nil { + return err + } + t := time.Now() + elapsed := t.Sub(start) + log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed") + + return nil +}