Commit 524cd6ee authored by Luke Champine's avatar Luke Champine

convert to contractor-specific journal

parent fd8dc815
......@@ -53,7 +53,7 @@ type (
persister interface {
save(contractorPersist) error
update(...interface{}) error
update(...journalUpdate) error
load(*contractorPersist) error
}
)
......@@ -75,21 +75,19 @@ type stdPersist struct {
filename string
}
func (p stdPersist) save(data contractorPersist) error {
func (p *stdPersist) save(data contractorPersist) error {
if p.journal == nil {
var err error
p.journal, err = openJournal(p.filename, data)
return err
p.journal, err = newJournal(p.filename)
if err != nil {
return err
}
}
return p.journal.checkpoint(data)
}
func (p *stdPersist) update(us ...interface{}) error {
var updates []journalUpdate
for i := 0; i < len(us); i += 2 {
updates = append(updates, newJournalUpdate(us[i].(string), us[i+1].(interface{})))
}
return p.journal.update(updates)
func (p *stdPersist) update(us ...journalUpdate) error {
return p.journal.update(us)
}
func (p *stdPersist) load(data *contractorPersist) error {
......
......@@ -2,7 +2,6 @@ package contractor
import (
"errors"
"fmt"
"sync"
"github.com/NebulousLabs/Sia/build"
......@@ -80,12 +79,10 @@ func (hd *hostDownloader) Sector(root crypto.Hash) ([]byte, error) {
hd.contractor.mu.Lock()
hd.contractor.contracts[contract.ID] = contract
cpath := fmt.Sprintf("contracts.%s", contract.ID.String())
hd.contractor.persist.update(
cpath+".lastrevision", contract.LastRevision,
cpath+".lastrevisiontxn", contract.LastRevisionTxn,
cpath+".downloadspending", contract.DownloadSpending,
)
hd.contractor.persist.update(updateDownloadRevision{
NewRevisionTxn: contract.LastRevisionTxn,
NewDownloadSpending: contract.DownloadSpending,
})
hd.contractor.mu.Unlock()
return sector, nil
......
......@@ -2,7 +2,6 @@ package contractor
import (
"errors"
"fmt"
"sync"
"github.com/NebulousLabs/Sia/build"
......@@ -114,14 +113,12 @@ func (he *hostEditor) Upload(data []byte) (crypto.Hash, error) {
}
he.contractor.mu.Lock()
he.contractor.contracts[contract.ID] = contract
cpath := fmt.Sprintf("contracts.%s", contract.ID.String())
he.contractor.persist.update(
cpath+".lastrevision", contract.LastRevision,
cpath+".lastrevisiontxn", contract.LastRevisionTxn,
cpath+".merkleroots."+fmt.Sprint(len(contract.MerkleRoots)-1), sectorRoot,
cpath+".uploadspending", contract.UploadSpending,
cpath+".storagespending", contract.StorageSpending,
)
he.contractor.persist.update(updateUploadRevision{
NewRevisionTxn: contract.LastRevisionTxn,
NewSectorRoot: sectorRoot,
NewUploadSpending: contract.UploadSpending,
NewStorageSpending: contract.StorageSpending,
})
he.contractor.mu.Unlock()
he.contract = contract
......
This diff is collapsed.
This diff is collapsed.
package contractor
import (
"fmt"
"path/filepath"
"github.com/NebulousLabs/Sia/crypto"
......@@ -129,12 +128,11 @@ func (c *Contractor) saveUploadRevision(id types.FileContractID) func(types.File
c.mu.Lock()
defer c.mu.Unlock()
c.cachedRevisions[id] = cachedRevision{rev, newRoots}
path := "cachedRevisions." + id.String()
return c.persist.update(
path+".revision", rev,
return c.persist.update(updateCachedUploadRevision{
Revision: rev,
// only the last root is new
path+".merkleroots."+fmt.Sprint(len(newRoots)-1), newRoots[len(newRoots)-1],
)
SectorRoot: newRoots[len(newRoots)-1],
})
}
}
......@@ -146,7 +144,9 @@ func (c *Contractor) saveDownloadRevision(id types.FileContractID) func(types.Fi
defer c.mu.Unlock()
c.cachedRevisions[id] = cachedRevision{rev, newRoots}
// roots have not changed
return c.persist.update("cachedRevisions."+id.String()+".revision", rev)
return c.persist.update(updateCachedDownloadRevision{
Revision: rev,
})
}
}
......@@ -258,5 +258,6 @@ func loadv110persist(dir string, data *contractorPersist) error {
OldContracts: oldPersist.OldContracts,
RenewedIDs: oldPersist.RenewedIDs,
}
return nil
}
......@@ -16,7 +16,7 @@ import (
type memPersist contractorPersist
func (m *memPersist) save(data contractorPersist) error { *m = memPersist(data); return nil }
func (m *memPersist) update(...interface{}) error { return nil }
func (m *memPersist) update(...journalUpdate) error { return nil }
func (m memPersist) load(data *contractorPersist) error { *data = contractorPersist(m); return nil }
// TestSaveLoad tests that the contractor can save and load itself.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment