@@ -72,11 +72,11 @@ func (b *blockCache) start(ctx context.Context) {
7272}
7373
7474// startRequest adds a request with its associated block hashes to the cache 
75- func  (bc  * blockCache ) startRequest (requestID  string , blocks  []uint64 ) error  {
76- 	bc .mu .Lock ()
77- 	defer  bc .mu .Unlock ()
75+ func  (b  * blockCache ) startRequest (requestID  string , blocks  []uint64 ) error  {
76+ 	b .mu .Lock ()
77+ 	defer  b .mu .Unlock ()
7878
79- 	if  _ , exists  :=  bc .requestToBlocks [requestID ]; exists  {
79+ 	if  _ , exists  :=  b .requestToBlocks [requestID ]; exists  {
8080		// request with the same id already exists 
8181		return  fmt .Errorf ("request already exists for id %s" , requestID )
8282	}
@@ -93,67 +93,67 @@ func (bc *blockCache) startRequest(requestID string, blocks []uint64) error {
9393	// count number of new blocks + number of blocks that are in the unused blocks 
9494	// don't update the data until we are sure that it's ok 
9595	for  _ , blockHash  :=  range  blocks  {
96- 		if  _ , exists  :=  bc .unusedBlocks [blockHash ]; exists  {
96+ 		if  _ , exists  :=  b .unusedBlocks [blockHash ]; exists  {
9797			blockToMoveToUsed  =  append (blockToMoveToUsed , blockHash )
98- 		} else  if  _ , exists  :=  bc .usedBlocks [blockHash ]; ! exists  {
98+ 		} else  if  _ , exists  :=  b .usedBlocks [blockHash ]; ! exists  {
9999			blocksToAdd  =  append (blocksToAdd , blockHash )
100100		} else  {
101101			blockAreadyInUse  =  append (blockAreadyInUse , blockHash )
102102		}
103103	}
104104
105- 	if  len (bc .usedBlocks )+ len (blocksToAdd )+ len (blockToMoveToUsed ) >  bc .maxBlocks  {
105+ 	if  len (b .usedBlocks )+ len (blocksToAdd )+ len (blockToMoveToUsed ) >  b .maxBlocks  {
106106		return  errors .New (capacityError )
107107	}
108108
109109	// for blocks that are already in use - update the reference 
110110	for  _ , block  :=  range  blockAreadyInUse  {
111- 		bc .usedBlocks [block ]  +=   1 
111+ 		b .usedBlocks [block ]++ 
112112	}
113113
114114	// for block used in the past - move them to the used blocks collection 
115115	for  _ , block  :=  range  blockToMoveToUsed  {
116- 		bc .usedBlocks [block ] =  1 
117- 		delete (bc .unusedBlocks , block )
116+ 		b .usedBlocks [block ] =  1 
117+ 		delete (b .unusedBlocks , block )
118118	}
119119
120120	// for new block - add them, if there is no empty slots - evict the oldest block 
121121	for  _ , block  :=  range  blocksToAdd  {
122- 		if  len (bc .usedBlocks )+ len (bc .unusedBlocks ) ==  bc .maxBlocks  {
122+ 		if  len (b .usedBlocks )+ len (b .unusedBlocks ) ==  b .maxBlocks  {
123123			// cache is full but contains unused blocks - evict the oldest 
124124			var  oldestUnusedHash  uint64 
125125			oldestUnusedTime  :=  time .Now ()
126126
127- 			for  hash , t  :=  range  bc .unusedBlocks  {
127+ 			for  hash , t  :=  range  b .unusedBlocks  {
128128				if  t .Before (oldestUnusedTime ) {
129129					oldestUnusedHash  =  hash 
130130					oldestUnusedTime  =  t 
131131				}
132132			}
133133
134- 			delete (bc .unusedBlocks , oldestUnusedHash )
135- 			bc .eventChan  <-  EventData {action : eventActionRemove , hashValues : []uint64 {oldestUnusedHash }}
134+ 			delete (b .unusedBlocks , oldestUnusedHash )
135+ 			b .eventChan  <-  EventData {action : eventActionRemove , hashValues : []uint64 {oldestUnusedHash }}
136136		}
137137
138138		// Add the new block 
139- 		bc .usedBlocks [block ] =  1 
140- 		bc .eventChan  <-  EventData {action : eventActionStore , hashValues : []uint64 {block }}
139+ 		b .usedBlocks [block ] =  1 
140+ 		b .eventChan  <-  EventData {action : eventActionStore , hashValues : []uint64 {block }}
141141	}
142142
143143	// store the request mapping 
144- 	bc .requestToBlocks [requestID ] =  make ([]uint64 , len (blocks ))
145- 	copy (bc .requestToBlocks [requestID ], blocks )
144+ 	b .requestToBlocks [requestID ] =  make ([]uint64 , len (blocks ))
145+ 	copy (b .requestToBlocks [requestID ], blocks )
146146
147147	return  nil 
148148}
149149
150150// finishRequest processes the completion of a request, decreasing reference counts 
151- func  (bc  * blockCache ) finishRequest (requestID  string ) error  {
152- 	bc .mu .Lock ()
153- 	defer  bc .mu .Unlock ()
151+ func  (b  * blockCache ) finishRequest (requestID  string ) error  {
152+ 	b .mu .Lock ()
153+ 	defer  b .mu .Unlock ()
154154
155155	// Get blocks associated with this request 
156- 	blockHashes , exists  :=  bc .requestToBlocks [requestID ]
156+ 	blockHashes , exists  :=  b .requestToBlocks [requestID ]
157157	if  ! exists  {
158158		return  errors .New ("request not found" )
159159	}
@@ -163,27 +163,27 @@ func (bc *blockCache) finishRequest(requestID string) error {
163163	// Decrease reference count for each block 
164164	errBlocks  :=  make ([]uint64 , 0 )
165165	for  _ , blockHash  :=  range  blockHashes  {
166- 		if  refCount , exists  :=  bc .usedBlocks [blockHash ]; exists  {
166+ 		if  refCount , exists  :=  b .usedBlocks [blockHash ]; exists  {
167167			if  refCount  >  1  {
168168				// this block is in use by another request, just update reference count 
169- 				bc .usedBlocks [blockHash ] =  refCount  -  1 
169+ 				b .usedBlocks [blockHash ] =  refCount  -  1 
170170			} else  {
171171				// this was the last block usage - move this block to unused 
172- 				bc .unusedBlocks [blockHash ] =  now 
173- 				delete (bc .usedBlocks , blockHash )
172+ 				b .unusedBlocks [blockHash ] =  now 
173+ 				delete (b .usedBlocks , blockHash )
174174			}
175175		} else  {
176176			errBlocks  =  append (errBlocks , blockHash )
177177		}
178178	}
179179
180180	// Remove the request mapping 
181- 	delete (bc .requestToBlocks , requestID )
181+ 	delete (b .requestToBlocks , requestID )
182182
183183	if  len (errBlocks ) >  0  {
184184		errMsg  :=  "Not existing blocks " 
185- 		for  _ , b  :=  range  errBlocks  {
186- 			errMsg  +=  fmt .Sprintf ("%d, " , b )
185+ 		for  _ , bl  :=  range  errBlocks  {
186+ 			errMsg  +=  fmt .Sprintf ("%d, " , bl )
187187		}
188188		return  fmt .Errorf ("%s for request %s" , errMsg [:len (errMsg )- 2 ], requestID )
189189	}
@@ -192,26 +192,26 @@ func (bc *blockCache) finishRequest(requestID string) error {
192192}
193193
194194// GetStats returns current cache statistics (for testing/debugging) 
195- func  (bc  * blockCache ) getStats () (int , int , int ) {
196- 	bc .mu .RLock ()
197- 	defer  bc .mu .RUnlock ()
195+ func  (b  * blockCache ) getStats () (int , int , int ) {
196+ 	b .mu .RLock ()
197+ 	defer  b .mu .RUnlock ()
198198
199- 	return  len (bc .requestToBlocks ), len (bc .usedBlocks ) +  len (bc .unusedBlocks ), len (bc .unusedBlocks )
199+ 	return  len (b .requestToBlocks ), len (b .usedBlocks ) +  len (b .unusedBlocks ), len (b .unusedBlocks )
200200}
201201
202202// getBlockInfo returns reference count and if it's in the cache for a specific block (for testing) 
203203// if block is in use by currently running requests the count will be positive, boolean is true 
204204// if block is in the unused list - count is 0, boolean is true 
205205// if block is not in both collections - count is 0, boolean is false 
206- func  (bc  * blockCache ) getBlockInfo (blockHash  uint64 ) (int , bool ) {
207- 	bc .mu .RLock ()
208- 	defer  bc .mu .RUnlock ()
206+ func  (b  * blockCache ) getBlockInfo (blockHash  uint64 ) (int , bool ) {
207+ 	b .mu .RLock ()
208+ 	defer  b .mu .RUnlock ()
209209
210- 	refCount , exists  :=  bc .usedBlocks [blockHash ]
210+ 	refCount , exists  :=  b .usedBlocks [blockHash ]
211211	if  exists  {
212212		return  refCount , true 
213213	}
214- 	_ , exists  =  bc .unusedBlocks [blockHash ]
214+ 	_ , exists  =  b .unusedBlocks [blockHash ]
215215	if  exists  {
216216		return  0 , true 
217217	}
0 commit comments