From 4dc43881c638d5894900e69d9d8866d45b04c96f Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Sun, 24 Apr 2016 19:41:15 +0200 Subject: [PATCH] Added a few more comments to mtredisalize and unexport a few symbols. --- cmd/mtredisalize/backend.go | 15 +++++++++++++++ cmd/mtredisalize/changetracker.go | 10 +++++----- cmd/mtredisalize/leveldb.go | 4 ++-- cmd/mtredisalize/main.go | 12 ++++++------ cmd/mtredisalize/sqlite.go | 4 ++-- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/cmd/mtredisalize/backend.go b/cmd/mtredisalize/backend.go index 4c87d61..567664c 100644 --- a/cmd/mtredisalize/backend.go +++ b/cmd/mtredisalize/backend.go @@ -5,24 +5,39 @@ package main type ( + // Block is the essential transfer unit from to the database. + // Key is the serialized spatial position. + // Data is the serialized from of the corresponding block data. Block struct { Key []byte Data []byte } + // Session is a database session. Session interface { + // Fetch fetches the block data for a given position. Fetch(hash, key []byte) ([]byte, error) + // InTransaction returns true if a transaction is running. InTransaction() bool + // Store stores a block with a given position and data. Store(hash, key, value []byte) (bool, error) + // AllKeys returns all keys in the database. AllKeys(hash []byte, done chan struct{}) (chan []byte, int, error) + // SpatialQuery performs a box query between the positions first and second. SpatialQuery(hash, first, second []byte, done chan struct{}) (chan Block, error) + // BeginTransaction starts a transcation. BeginTransaction() error + // CommitTransaction finishes a transaction. CommitTransaction() error + // Close closes the database session. Close() error } + // Backend is interface representing a database. Backend interface { + // NewSession opens a new session. NewSession() (Session, error) + // Shutdown shuts down the database server. Shutdown() error } ) diff --git a/cmd/mtredisalize/changetracker.go b/cmd/mtredisalize/changetracker.go index 6819aee..331b46f 100644 --- a/cmd/mtredisalize/changetracker.go +++ b/cmd/mtredisalize/changetracker.go @@ -22,16 +22,16 @@ type quantizedXZ struct { X, Z int16 } -type ChangeTracker struct { +type changeTracker struct { changes map[quantizedXZ]bool mutex sync.Mutex } -func NewChangeTracker() *ChangeTracker { - return &ChangeTracker{changes: make(map[quantizedXZ]bool)} +func newChangeTracker() *changeTracker { + return &changeTracker{changes: make(map[quantizedXZ]bool)} } -func (ct *ChangeTracker) BlockChanged(coord common.Coord) { +func (ct *changeTracker) BlockChanged(coord common.Coord) { ct.mutex.Lock() ct.changes[quantizedXZ{ X: coord.X / quantizationFactor, @@ -39,7 +39,7 @@ func (ct *ChangeTracker) BlockChanged(coord common.Coord) { ct.mutex.Unlock() } -func (ct *ChangeTracker) FlushChanges(url string) { +func (ct *changeTracker) FlushChanges(url string) { var oldChanges map[quantizedXZ]bool ct.mutex.Lock() if len(ct.changes) > 0 { diff --git a/cmd/mtredisalize/leveldb.go b/cmd/mtredisalize/leveldb.go index 7e92e8c..c1e7e1d 100644 --- a/cmd/mtredisalize/leveldb.go +++ b/cmd/mtredisalize/leveldb.go @@ -21,7 +21,7 @@ type LevelDBBackend struct { encoder common.KeyTranscoder decoder common.KeyTranscoder - changeTracker *ChangeTracker + changeTracker *changeTracker mutex sync.RWMutex } @@ -32,7 +32,7 @@ type LevelDBSession struct { func NewLeveDBBackend( path string, - changeTracker *ChangeTracker, + changeTracker *changeTracker, interleaved bool, cacheSize int) (ldb *LevelDBBackend, err error) { diff --git a/cmd/mtredisalize/main.go b/cmd/mtredisalize/main.go index 48086f9..7e5ea67 100644 --- a/cmd/mtredisalize/main.go +++ b/cmd/mtredisalize/main.go @@ -19,8 +19,8 @@ import ( const ( defaultMaxBulkStringSize = 32 * 1024 * 1024 - GCDuration = "24h" - ChangeDuration = "30s" + defaultGCDuration = "24h" + defaultChangeDuration = "30s" ) func usage() { @@ -55,9 +55,9 @@ func main() { flag.BoolVar(&interleaved, "interleaved", false, "Backend stores key in interleaved form.") flag.StringVar(&gcDuration, - "gc-duration", GCDuration, "Duration between forced GCs.") + "gc-duration", defaultGCDuration, "Duration between forced GCs.") flag.StringVar(&changeDuration, - "change-duration", ChangeDuration, "Duration to aggregate changes.") + "change-duration", defaultChangeDuration, "Duration to aggregate changes.") flag.StringVar(&changeURL, "change-url", "", "URL to send changes to.") flag.Int64Var(&maxBulkStringSize, "max-bulk-string-size", defaultMaxBulkStringSize, "max size of a bulk string to be accepted as input (in bytes).") @@ -76,7 +76,7 @@ func main() { backend Backend gcDur time.Duration chDur time.Duration - changeTracker *ChangeTracker + changeTracker *changeTracker ) if gcDur, err = time.ParseDuration(gcDuration); err != nil { @@ -94,7 +94,7 @@ func main() { log.Fatal(err) } changeChan = time.Tick(chDur) - changeTracker = NewChangeTracker() + changeTracker = newChangeTracker() } else { // We will never receive ticks on this. changeChan = make(<-chan time.Time) diff --git a/cmd/mtredisalize/sqlite.go b/cmd/mtredisalize/sqlite.go index b8b63e8..ad0d2c6 100644 --- a/cmd/mtredisalize/sqlite.go +++ b/cmd/mtredisalize/sqlite.go @@ -30,7 +30,7 @@ type SqliteBackend struct { db *sql.DB encoder common.KeyEncoder decoder common.KeyDecoder - changeTracker *ChangeTracker + changeTracker *changeTracker interleaved bool coverage *common.Coverage3D existsStmt *sql.Stmt @@ -62,7 +62,7 @@ func (ss *SqliteSession) Close() error { func NewSqliteBackend( path string, - changeTracker *ChangeTracker, interleaved bool) (sqlb *SqliteBackend, err error) { + changeTracker *changeTracker, interleaved bool) (sqlb *SqliteBackend, err error) { res := SqliteBackend{interleaved: interleaved, changeTracker: changeTracker}