]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use stable bit set for pending pieces PriorityBitmap
authorMatt Joiner <anacrolix@gmail.com>
Thu, 27 Feb 2020 07:33:44 +0000 (18:33 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 27 Feb 2020 07:33:44 +0000 (18:33 +1100)
Fixes #382.

client.go
cmd/torrent/main.go
go.mod
go.sum
torrent_pending_pieces.go [new file with mode: 0644]

index 0e83374ecbec3b592ca79402046549cb8cfd3585..42a280d58f321eb4f7eecc07787568d334c37a8f 100644 (file)
--- a/client.go
+++ b/client.go
@@ -22,8 +22,6 @@ import (
        "github.com/anacrolix/missinggo/pproffd"
        "github.com/anacrolix/missinggo/pubsub"
        "github.com/anacrolix/missinggo/slices"
-       "github.com/anacrolix/missinggo/v2"
-       "github.com/anacrolix/missinggo/v2/conntrack"
        "github.com/anacrolix/sync"
        "github.com/davecgh/go-spew/spew"
        "github.com/dustin/go-humanize"
@@ -31,6 +29,9 @@ import (
        "golang.org/x/time/rate"
        "golang.org/x/xerrors"
 
+       "github.com/anacrolix/missinggo/v2"
+       "github.com/anacrolix/missinggo/v2/conntrack"
+
        "github.com/anacrolix/torrent/bencode"
        "github.com/anacrolix/torrent/iplist"
        "github.com/anacrolix/torrent/metainfo"
@@ -1041,6 +1042,7 @@ func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) (
                        L: cl.locker(),
                },
        }
+       t._pendingPieces.NewSet = priorityBitmapStableNewSet
        t.requestStrategy = cl.config.DefaultRequestStrategy(t.requestStrategyCallbacks(), &cl._mu)
        t.logger = cl.logger.WithValues(t).WithText(func(m log.Msg) string {
                return fmt.Sprintf("%v: %s", t, m.Text())
index 28da282d8727d9df81ade7df23cf8a2a30c83846..1270540571e4334c0878379ee95dc93c1f222f2e 100644 (file)
@@ -28,7 +28,7 @@ import (
        "github.com/anacrolix/torrent/storage"
 )
 
-func torrentBar(t *torrent.Torrent) {
+func torrentBar(t *torrent.Torrent, pieceStates bool) {
        go func() {
                if t.Info() == nil {
                        fmt.Printf("getting info for %q\n", t.Name())
@@ -54,7 +54,9 @@ func torrentBar(t *torrent.Torrent) {
                                t.NumPieces(),
                                partialPieces,
                        )
-                       //fmt.Println(psrs)
+                       if pieceStates {
+                               fmt.Println(psrs)
+                       }
                        time.Sleep(time.Second)
                }
        }()
@@ -104,7 +106,7 @@ func addTorrents(client *torrent.Client) error {
                        return xerrors.Errorf("adding torrent for %q: %w", arg, err)
                }
                if flags.Progress {
-                       torrentBar(t)
+                       torrentBar(t, flags.PieceStates)
                }
                t.AddPeers(func() (ret []torrent.Peer) {
                        for _, ta := range flags.TestPeer {
@@ -134,6 +136,7 @@ var flags = struct {
        Stats           *bool
        PublicIP        net.IP
        Progress        bool
+       PieceStates     bool
        Quiet           bool `help:"discard client logging"`
        Dht             bool
        tagflag.StartPos
diff --git a/go.mod b/go.mod
index b7a9a2d75ad310fdc07163630ede5eaa54b5f225..00578c102fca19a4ce9829b9d1166e73ec838b1c 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -9,7 +9,7 @@ require (
        github.com/anacrolix/log v0.6.0
        github.com/anacrolix/missinggo v1.2.1
        github.com/anacrolix/missinggo/perf v1.0.0
-       github.com/anacrolix/missinggo/v2 v2.4.0
+       github.com/anacrolix/missinggo/v2 v2.4.1-0.20200227072623-f02f6484f997
        github.com/anacrolix/multiless v0.0.0-20191223025854-070b7994e841
        github.com/anacrolix/sync v0.2.0
        github.com/anacrolix/tagflag v1.0.1
@@ -19,6 +19,7 @@ require (
        github.com/davecgh/go-spew v1.1.1
        github.com/dustin/go-humanize v1.0.0
        github.com/edsrzf/mmap-go v1.0.0
+       github.com/elliotchance/orderedmap v1.2.0
        github.com/etcd-io/bbolt v1.3.3
        github.com/fsnotify/fsnotify v1.4.7
        github.com/google/btree v1.0.0
diff --git a/go.sum b/go.sum
index ec3240c4e493550a35aba5c4b176a24e2ec1ef97..8e3c25d55112bffa75c0b968506dea07ee6c2ac7 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -69,8 +69,8 @@ github.com/anacrolix/missinggo/v2 v2.2.1-0.20191103010835-12360f38ced0 h1:CKpmXo
 github.com/anacrolix/missinggo/v2 v2.2.1-0.20191103010835-12360f38ced0/go.mod h1:ZzG3/cc3t+5zcYWAgYrJW0MBsSwNwOkTlNquBbP51Bc=
 github.com/anacrolix/missinggo/v2 v2.3.0/go.mod h1:ZzG3/cc3t+5zcYWAgYrJW0MBsSwNwOkTlNquBbP51Bc=
 github.com/anacrolix/missinggo/v2 v2.3.1/go.mod h1:3XNH0OEmyMUZuvXmYdl+FDfXd0vvSZhvOLy8CFx8tLg=
-github.com/anacrolix/missinggo/v2 v2.4.0 h1:qWSaH/JZyjKBgJ1hrCmNaGUzeEvKrGCbzv/8HjATBFY=
-github.com/anacrolix/missinggo/v2 v2.4.0/go.mod h1:sjPqWXxdr3jWcMO/tXhhshXAaiTkGIgJpN93clGzGr8=
+github.com/anacrolix/missinggo/v2 v2.4.1-0.20200227072623-f02f6484f997 h1:ufGZtSzDyevqcHyFtAWm2HMjmezh/mCemzC1VFEjsu0=
+github.com/anacrolix/missinggo/v2 v2.4.1-0.20200227072623-f02f6484f997/go.mod h1:KY+ij+mWvwGuqSuecLjjPv5LFw5ICUc1UvRems3VAZE=
 github.com/anacrolix/mmsg v0.0.0-20180515031531-a4a3ba1fc8bb h1:2Or5ccMoY4Kfao+WdL2w6tpY6ZEe+2VTVbIPd7A/Ajk=
 github.com/anacrolix/mmsg v0.0.0-20180515031531-a4a3ba1fc8bb/go.mod h1:x2/ErsYUmT77kezS63+wzZp8E3byYB0gzirM/WMBLfw=
 github.com/anacrolix/mmsg v1.0.0 h1:btC7YLjOn29aTUAExJiVUhQOuf/8rhm+/nWCMAnL3Hg=
@@ -135,6 +135,8 @@ github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712 h1:aaQcKT9WumO6JEJc
 github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
 github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw=
 github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
+github.com/elliotchance/orderedmap v1.2.0 h1:Z2kiPPgjjlS8NN+1EFzE4ZO/HpW02gl9ZH3MHL+bNkg=
+github.com/elliotchance/orderedmap v1.2.0/go.mod h1:8hdSl6jmveQw8ScByd3AaNHNk51RhbTazdqtTty+NFw=
 github.com/etcd-io/bbolt v1.3.3 h1:gSJmxrs37LgTqR/oyJBWok6k6SvXEUerFTbltIhXkBM=
 github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw=
 github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
diff --git a/torrent_pending_pieces.go b/torrent_pending_pieces.go
new file mode 100644 (file)
index 0000000..bfcf7bf
--- /dev/null
@@ -0,0 +1,40 @@
+package torrent
+
+import (
+       "github.com/elliotchance/orderedmap"
+
+       "github.com/anacrolix/missinggo/v2/prioritybitmap"
+)
+
+type stableSet struct {
+       om *orderedmap.OrderedMap
+}
+
+func (s stableSet) Has(bit int) bool {
+       _, ok := s.om.Get(bit)
+       return ok
+}
+
+func (s stableSet) Delete(bit int) {
+       s.om.Delete(bit)
+}
+
+func (s stableSet) Len() int {
+       return s.om.Len()
+}
+
+func (s stableSet) Set(bit int) {
+       s.om.Set(bit, struct{}{})
+}
+
+func (s stableSet) Range(f func(int) bool) {
+       for e := s.om.Front(); e != nil; e = e.Next() {
+               if !f(e.Key.(int)) {
+                       break
+               }
+       }
+}
+
+func priorityBitmapStableNewSet() prioritybitmap.Set {
+       return stableSet{om: orderedmap.NewOrderedMap()}
+}