| 
									
										
										
										
											2022-06-12 14:43:43 +02:00
										 |  |  | package index | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2021-08-08 00:38:17 +02:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 	"sync" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/restic/restic/internal/restic" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ForAllIndexes loads all index files in parallel and calls the given callback. | 
					
						
							|  |  |  | // It is guaranteed that the function is not run concurrently. If the callback | 
					
						
							|  |  |  | // returns an error, this function is cancelled and also returns that error. | 
					
						
							| 
									
										
										
										
											2023-08-13 20:33:13 +02:00
										 |  |  | func ForAllIndexes(ctx context.Context, lister restic.Lister, repo restic.Repository, | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 	fn func(id restic.ID, index *Index, oldFormat bool, err error) error) error { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-15 17:24:47 +02:00
										 |  |  | 	// decoding an index can take quite some time such that this can be both CPU- or IO-bound | 
					
						
							|  |  |  | 	// as the whole index is kept in memory anyways, a few workers too much don't matter | 
					
						
							|  |  |  | 	workerCount := repo.Connections() + uint(runtime.GOMAXPROCS(0)) | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var m sync.Mutex | 
					
						
							| 
									
										
										
										
											2023-08-13 20:33:13 +02:00
										 |  |  | 	return restic.ParallelList(ctx, lister, restic.IndexFile, workerCount, func(ctx context.Context, id restic.ID, size int64) error { | 
					
						
							| 
									
										
										
										
											2022-10-15 17:24:47 +02:00
										 |  |  | 		var err error | 
					
						
							|  |  |  | 		var idx *Index | 
					
						
							|  |  |  | 		oldFormat := false | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:01:54 +01:00
										 |  |  | 		buf, err := repo.LoadUnpacked(ctx, restic.IndexFile, id) | 
					
						
							| 
									
										
										
										
											2022-10-15 17:24:47 +02:00
										 |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			idx, oldFormat, err = DecodeIndex(buf, id) | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-15 17:24:47 +02:00
										 |  |  | 		m.Lock() | 
					
						
							|  |  |  | 		defer m.Unlock() | 
					
						
							|  |  |  | 		return fn(id, idx, oldFormat, err) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-12-18 19:37:08 +01:00
										 |  |  | } |