mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-885] Create database.Key type * [NOD-885] Rename FullKey()->FullKeyBytes() and Key()->KeyBytes() * [NOD-885] Make Key.String return a hex string * [NOD-885] Rename key parts * [NOD-885] Rename separator->bucketSeparator * [NOD-885] Rename SuffixBytes->Suffix and PrefixBytes->Prefix * [NOD-885] Change comments * [NOD-885] Change key prefix to bucket * [NOD-885] Don't use database.NewKey inside dbaccess * [NOD-885] Fix nil bug in Bucket.Path() * [NOD-885] Rename helpers.go -> keys.go * [NOD-885] Unexport database.NewKey * [NOD-885] Remove redundant code in Bucket.Path()
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package ffldb
|
|
|
|
// initialize initializes the database. If this function fails then the
|
|
// database is irrecoverably corrupted.
|
|
func (db *ffldb) initialize() error {
|
|
flatFiles, err := db.flatFiles()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for storeName, currentLocation := range flatFiles {
|
|
err := db.tryRepair(storeName, currentLocation)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (db *ffldb) flatFiles() (map[string][]byte, error) {
|
|
flatFilesBucketPath := flatFilesBucket.Path()
|
|
flatFilesCursor := db.levelDB.Cursor(flatFilesBucketPath)
|
|
defer func() {
|
|
err := flatFilesCursor.Close()
|
|
if err != nil {
|
|
log.Warnf("cursor failed to close")
|
|
}
|
|
}()
|
|
|
|
flatFiles := make(map[string][]byte)
|
|
for flatFilesCursor.Next() {
|
|
storeNameKey, err := flatFilesCursor.Key()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
storeName := string(storeNameKey.Suffix())
|
|
|
|
currentLocation, err := flatFilesCursor.Value()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
flatFiles[storeName] = currentLocation
|
|
}
|
|
return flatFiles, nil
|
|
}
|
|
|
|
// tryRepair attempts to sync the store with the current location value.
|
|
// Possible scenarios:
|
|
// a. currentLocation and the store are synced. Rollback does nothing.
|
|
// b. currentLocation is smaller than the store's location. Rollback truncates
|
|
// the store.
|
|
// c. currentLocation is greater than the store's location. Rollback returns an
|
|
// error. This indicates definite database corruption and is irrecoverable.
|
|
func (db *ffldb) tryRepair(storeName string, currentLocation []byte) error {
|
|
return db.flatFileDB.Rollback(storeName, currentLocation)
|
|
}
|