-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement ldb database functionality for optional addr index. #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,3 +290,41 @@ func (db *LevelDb) NewestSha() (rsha *btcwire.ShaHash, rblkid int64, err error) | |
|
||
return &sha, db.lastBlkIdx, nil | ||
} | ||
|
||
// fetchAddrIndexTip returns the last block height and block sha to be indexed. | ||
// Meta-data about the address tip is currently cached in memory, and will be | ||
// updated accordingly by functions that modify the state. This function is | ||
// used on start up to load the info into memory. Callers will use the public | ||
// version of this function below, which returns our cached copy. | ||
func (db *LevelDb) fetchAddrIndexTip() (*btcwire.ShaHash, int64, error) { | ||
db.dbLock.Lock() | ||
defer db.dbLock.Unlock() | ||
|
||
data, err := db.lDb.Get(addrIndexMetaDataKey, db.ro) | ||
if err != nil { | ||
return &btcwire.ShaHash{}, -1, btcdb.ErrAddrIndexDoesNotExist | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This answers my question above.. but why not return nil here instead? Otherwise we end up creating additional garbage for no good reason, for a value that shouldn't be touched to begin with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good point, returning nil instead does seem to be better behavior. It currently returns a pointer to the btcwire zero hash simply to emulate the behavior of If changed to return nil, then following the same logic, should we also change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm true, I didn't realize NewestSha behaved the same way. I think it should be changed, but with the existing db interface being rewritten anyways, I wouldn't worry too much about it. |
||
} | ||
|
||
var blkSha btcwire.ShaHash | ||
blkSha.SetBytes(data[0:32]) | ||
|
||
blkHeight := binary.LittleEndian.Uint64(data[32:]) | ||
|
||
return &blkSha, int64(blkHeight), nil | ||
} | ||
|
||
// FetchAddrIndexTip returns the hash and block height of the most recent | ||
// block whose transactions have been indexed by address. It will return | ||
// ErrAddrIndexDoesNotExist along with a zero hash, and -1 if the | ||
// addrindex hasn't yet been built up. | ||
func (db *LevelDb) FetchAddrIndexTip() (*btcwire.ShaHash, int64, error) { | ||
db.dbLock.Lock() | ||
defer db.dbLock.Unlock() | ||
|
||
if db.lastAddrIndexBlkIdx == -1 { | ||
return &btcwire.ShaHash{}, -1, btcdb.ErrAddrIndexDoesNotExist | ||
} | ||
sha := db.lastAddrIndexBlkSha | ||
|
||
return &sha, db.lastAddrIndexBlkIdx, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zero hash, or nil pointer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yeah technically it's actually a pointer to a
btcwire.Shahash
which is itself the zero hash. Full response below.