From 6f63909c653608218e78bc96e23b13c66a2ac694 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Tue, 31 May 2016 19:29:26 +0000 Subject: [PATCH] lib/db,cmd/stindex: Expose VersionList and use it in stindex GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3231 --- cmd/stindex/dump.go | 4 +++- lib/db/leveldb.go | 4 ++-- lib/db/leveldb_dbinstance.go | 12 ++++++------ lib/db/leveldb_transactions.go | 4 ++-- lib/db/leveldb_xdr.go | 16 ++++++++-------- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/cmd/stindex/dump.go b/cmd/stindex/dump.go index 2413f306c..23632e15a 100644 --- a/cmd/stindex/dump.go +++ b/cmd/stindex/dump.go @@ -39,7 +39,9 @@ func dump(ldb *leveldb.DB) { case db.KeyTypeGlobal: folder := nulString(key[1 : 1+64]) name := nulString(key[1+64:]) - fmt.Printf("[global] F:%q N:%q V:%x\n", folder, name, it.Value()) + var flv db.VersionList + flv.UnmarshalXDR(it.Value()) + fmt.Printf("[global] F:%q N:%q V: %s\n", folder, name, flv) case db.KeyTypeBlock: folder := nulString(key[1 : 1+64]) diff --git a/lib/db/leveldb.go b/lib/db/leveldb.go index fa63ee5df..ae51f5391 100644 --- a/lib/db/leveldb.go +++ b/lib/db/leveldb.go @@ -51,11 +51,11 @@ type fileVersion struct { device []byte } -type versionList struct { +type VersionList struct { versions []fileVersion } -func (l versionList) String() string { +func (l VersionList) String() string { var b bytes.Buffer var id protocol.DeviceID b.WriteString("{") diff --git a/lib/db/leveldb_dbinstance.go b/lib/db/leveldb_dbinstance.go index cca69211e..6f321b554 100644 --- a/lib/db/leveldb_dbinstance.go +++ b/lib/db/leveldb_dbinstance.go @@ -339,7 +339,7 @@ func (db *Instance) getGlobal(folder, file []byte, truncate bool) (FileIntf, boo panic(err) } - var vl versionList + var vl VersionList err = vl.UnmarshalXDR(bs) if err != nil { panic(err) @@ -376,7 +376,7 @@ func (db *Instance) withGlobal(folder, prefix []byte, truncate bool, fn Iterator var fk []byte for dbi.Next() { - var vl versionList + var vl VersionList err := vl.UnmarshalXDR(dbi.Value()) if err != nil { panic(err) @@ -428,7 +428,7 @@ func (db *Instance) availability(folder, file []byte) []protocol.DeviceID { panic(err) } - var vl versionList + var vl VersionList err = vl.UnmarshalXDR(bs) if err != nil { panic(err) @@ -456,7 +456,7 @@ func (db *Instance) withNeed(folder, device []byte, truncate bool, fn Iterator) var fk []byte nextFile: for dbi.Next() { - var vl versionList + var vl VersionList err := vl.UnmarshalXDR(dbi.Value()) if err != nil { panic(err) @@ -593,7 +593,7 @@ func (db *Instance) checkGlobals(folder []byte, globalSize *sizeTracker) { var fk []byte for dbi.Next() { gk := dbi.Key() - var vl versionList + var vl VersionList err := vl.UnmarshalXDR(dbi.Value()) if err != nil { panic(err) @@ -605,7 +605,7 @@ func (db *Instance) checkGlobals(folder []byte, globalSize *sizeTracker) { // we find those and clear them out. name := db.globalKeyName(gk) - var newVL versionList + var newVL VersionList for i, version := range vl.versions { fk = db.deviceKeyInto(fk[:cap(fk)], folder, version.device, name) diff --git a/lib/db/leveldb_transactions.go b/lib/db/leveldb_transactions.go index c9151d28e..40d97f8bc 100644 --- a/lib/db/leveldb_transactions.go +++ b/lib/db/leveldb_transactions.go @@ -96,7 +96,7 @@ func (t readWriteTransaction) updateGlobal(folder, device []byte, file protocol. panic(err) } - var fl versionList + var fl VersionList var oldFile protocol.FileInfo var hasOldFile bool // Remove the device from the current version list @@ -205,7 +205,7 @@ func (t readWriteTransaction) removeFromGlobal(folder, device, file []byte, glob return } - var fl versionList + var fl VersionList err = fl.UnmarshalXDR(svl) if err != nil { panic(err) diff --git a/lib/db/leveldb_xdr.go b/lib/db/leveldb_xdr.go index 1348c3cb1..0835beb35 100644 --- a/lib/db/leveldb_xdr.go +++ b/lib/db/leveldb_xdr.go @@ -71,7 +71,7 @@ func (o *fileVersion) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { /* -versionList Structure: +VersionList Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 @@ -84,23 +84,23 @@ versionList Structure: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -struct versionList { +struct VersionList { fileVersion versions<>; } */ -func (o versionList) XDRSize() int { +func (o VersionList) XDRSize() int { return 4 + xdr.SizeOfSlice(o.versions) } -func (o versionList) MarshalXDR() ([]byte, error) { +func (o VersionList) MarshalXDR() ([]byte, error) { buf := make([]byte, o.XDRSize()) m := &xdr.Marshaller{Data: buf} return buf, o.MarshalXDRInto(m) } -func (o versionList) MustMarshalXDR() []byte { +func (o VersionList) MustMarshalXDR() []byte { bs, err := o.MarshalXDR() if err != nil { panic(err) @@ -108,7 +108,7 @@ func (o versionList) MustMarshalXDR() []byte { return bs } -func (o versionList) MarshalXDRInto(m *xdr.Marshaller) error { +func (o VersionList) MarshalXDRInto(m *xdr.Marshaller) error { m.MarshalUint32(uint32(len(o.versions))) for i := range o.versions { if err := o.versions[i].MarshalXDRInto(m); err != nil { @@ -118,11 +118,11 @@ func (o versionList) MarshalXDRInto(m *xdr.Marshaller) error { return m.Error } -func (o *versionList) UnmarshalXDR(bs []byte) error { +func (o *VersionList) UnmarshalXDR(bs []byte) error { u := &xdr.Unmarshaller{Data: bs} return o.UnmarshalXDRFrom(u) } -func (o *versionList) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { +func (o *VersionList) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { _versionsSize := int(u.UnmarshalUint32()) if _versionsSize < 0 { return xdr.ElementSizeExceeded("versions", _versionsSize, 0)