|
6 | 6 | "sync"
|
7 | 7 | "time"
|
8 | 8 |
|
| 9 | + "gopkg.in/cheggaaa/pb.v2" |
| 10 | + |
9 | 11 | "github.com/clems4ever/go-graphkb/internal/knowledge"
|
10 | 12 | "github.com/clems4ever/go-graphkb/internal/schema"
|
11 | 13 | )
|
@@ -64,34 +66,71 @@ func withRetryOnTooManyRequests(fn func() error, maxRetries int) error {
|
64 | 66 | func (cgt *Transaction) Commit() (*knowledge.Graph, error) {
|
65 | 67 | sg := cgt.newGraph.ExtractSchema()
|
66 | 68 |
|
| 69 | + fmt.Println("Start uploading the schema of the graph...") |
67 | 70 | if err := cgt.client.UpdateSchema(sg); err != nil {
|
68 | 71 | return nil, fmt.Errorf("Unable to update the schema of the graph")
|
69 | 72 | }
|
70 | 73 |
|
| 74 | + fmt.Println("Finished uploading the schema of the graph...") |
| 75 | + |
71 | 76 | bulk := knowledge.GenerateGraphUpdatesBulk(cgt.currentGraph, cgt.newGraph)
|
72 | 77 |
|
73 |
| - for _, r := range bulk.GetRelationRemovals() { |
74 |
| - if err := withRetryOnTooManyRequests(func() error { return cgt.client.DeleteRelation(r) }, 10); err != nil { |
75 |
| - return nil, fmt.Errorf("Unable to remove the relation %v: %v", r, err) |
| 78 | + fmt.Println("Start uploading the graph...") |
| 79 | + |
| 80 | + progress := pb.New(len(bulk.GetAssetRemovals()) + len(bulk.GetAssetUpserts()) + len(bulk.GetRelationRemovals()) + len(bulk.GetRelationUpserts())) |
| 81 | + progress.Start() |
| 82 | + defer progress.Finish() |
| 83 | + |
| 84 | + var wg sync.WaitGroup |
| 85 | + |
| 86 | + wg.Add(2) |
| 87 | + |
| 88 | + var err1, err2 error |
| 89 | + |
| 90 | + go func() { |
| 91 | + defer wg.Done() |
| 92 | + for _, r := range bulk.GetRelationRemovals() { |
| 93 | + if err := withRetryOnTooManyRequests(func() error { return cgt.client.DeleteRelation(r) }, 10); err != nil { |
| 94 | + err1 = fmt.Errorf("Unable to remove the relation %v: %v", r, err) |
| 95 | + return |
| 96 | + } |
| 97 | + progress.Increment() |
76 | 98 | }
|
77 |
| - } |
78 |
| - for _, a := range bulk.GetAssetRemovals() { |
79 |
| - if err := withRetryOnTooManyRequests(func() error { return cgt.client.DeleteAsset(a) }, 10); err != nil { |
80 |
| - return nil, fmt.Errorf("Unable to remove the asset %v: %v", a, err) |
| 99 | + for _, a := range bulk.GetAssetRemovals() { |
| 100 | + if err := withRetryOnTooManyRequests(func() error { return cgt.client.DeleteAsset(a) }, 10); err != nil { |
| 101 | + err1 = fmt.Errorf("Unable to remove the asset %v: %v", a, err) |
| 102 | + } |
| 103 | + progress.Increment() |
81 | 104 | }
|
82 |
| - } |
| 105 | + }() |
83 | 106 |
|
84 |
| - for _, a := range bulk.GetAssetUpserts() { |
85 |
| - if err := withRetryOnTooManyRequests(func() error { return cgt.client.UpsertAsset(a) }, 10); err != nil { |
86 |
| - return nil, fmt.Errorf("Unable to upsert the asset %v: %v", a, err) |
| 107 | + go func() { |
| 108 | + defer wg.Done() |
| 109 | + for _, a := range bulk.GetAssetUpserts() { |
| 110 | + if err := withRetryOnTooManyRequests(func() error { return cgt.client.UpsertAsset(a) }, 10); err != nil { |
| 111 | + err2 = fmt.Errorf("Unable to upsert the asset %v: %v", a, err) |
| 112 | + } |
| 113 | + progress.Increment() |
87 | 114 | }
|
88 |
| - } |
89 |
| - for _, r := range bulk.GetRelationUpserts() { |
90 |
| - if err := withRetryOnTooManyRequests(func() error { return cgt.client.UpsertRelation(r) }, 10); err != nil { |
91 |
| - return nil, fmt.Errorf("Unable to upsert the relation %v: %v", r, err) |
| 115 | + for _, r := range bulk.GetRelationUpserts() { |
| 116 | + if err := withRetryOnTooManyRequests(func() error { return cgt.client.UpsertRelation(r) }, 10); err != nil { |
| 117 | + err2 = fmt.Errorf("Unable to upsert the relation %v: %v", r, err) |
| 118 | + } |
| 119 | + progress.Increment() |
92 | 120 | }
|
| 121 | + }() |
| 122 | + |
| 123 | + wg.Wait() |
| 124 | + |
| 125 | + if err1 != nil { |
| 126 | + return nil, err1 |
| 127 | + } |
| 128 | + if err2 != nil { |
| 129 | + return nil, err2 |
93 | 130 | }
|
94 | 131 |
|
| 132 | + fmt.Println("Finished uploading the graph...") |
| 133 | + |
95 | 134 | g := cgt.newGraph
|
96 | 135 | cgt.newGraph = knowledge.NewGraph()
|
97 | 136 | return g, nil // give ownership of the transaction graph so that it can be cached if needed
|
|
0 commit comments