github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/explorer/persist.go (about) 1 package explorer 2 3 import ( 4 "os" 5 "path/filepath" 6 7 "github.com/NebulousLabs/Sia/encoding" 8 "github.com/NebulousLabs/Sia/modules" 9 "github.com/NebulousLabs/Sia/persist" 10 "github.com/NebulousLabs/Sia/types" 11 12 "github.com/NebulousLabs/bolt" 13 ) 14 15 var explorerMetadata = persist.Metadata{ 16 Header: "Sia Explorer", 17 Version: "0.5.2", 18 } 19 20 // initPersist initializes the persistent structures of the explorer module. 21 func (e *Explorer) initPersist() error { 22 // Make the persist directory 23 err := os.MkdirAll(e.persistDir, 0700) 24 if err != nil { 25 return err 26 } 27 28 // Open the database 29 db, err := persist.OpenDatabase(explorerMetadata, filepath.Join(e.persistDir, "explorer.db")) 30 if err != nil { 31 return err 32 } 33 e.db = db 34 35 // Initialize the database 36 err = e.db.Update(func(tx *bolt.Tx) error { 37 buckets := [][]byte{ 38 bucketBlockFacts, 39 bucketBlockIDs, 40 bucketBlocksDifficulty, 41 bucketBlockTargets, 42 bucketFileContractHistories, 43 bucketFileContractIDs, 44 bucketInternal, 45 bucketSiacoinOutputIDs, 46 bucketSiacoinOutputs, 47 bucketSiafundOutputIDs, 48 bucketSiafundOutputs, 49 bucketTransactionIDs, 50 bucketUnlockHashes, 51 } 52 for _, b := range buckets { 53 _, err := tx.CreateBucketIfNotExists(b) 54 if err != nil { 55 return err 56 } 57 } 58 59 // set default values for the bucketInternal 60 internalDefaults := []struct { 61 key, val []byte 62 }{ 63 {internalBlockHeight, encoding.Marshal(types.BlockHeight(0))}, 64 {internalRecentChange, encoding.Marshal(modules.ConsensusChangeID{})}, 65 } 66 b := tx.Bucket(bucketInternal) 67 for _, d := range internalDefaults { 68 if b.Get(d.key) != nil { 69 continue 70 } 71 err := b.Put(d.key, d.val) 72 if err != nil { 73 return err 74 } 75 } 76 77 return nil 78 }) 79 if err != nil { 80 return err 81 } 82 83 return nil 84 }