Skip to content

Commit

Permalink
added deleteAllMatchingEntries
Browse files Browse the repository at this point in the history
Signed-off-by: munishchouhan <[email protected]>
  • Loading branch information
munishchouhan committed Jul 15, 2024
1 parent b2790ee commit 909bda1
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ abstract class AbstractCounterStore implements CounterStore {
Map<String, Long> getAllMatchingEntries(String pattern) {
provider.getAllMatchingEntries(getPrefix(), pattern)
}

@Override
void deleteAllMatchingEntries(String pattern) {
provider.getAllMatchingEntries(getPrefix(), pattern)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ interface CounterStore {
* @return all the entries whose field matches 'pattern'
*/
Map<String, Long> getAllMatchingEntries(String pattern)

/**
* @param pattern
* @return void
*/
void deleteAllMatchingEntries(String pattern)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ interface CounterProvider {
* @return all the entries whose field matches 'pattern'
*/
Map<String, Long> getAllMatchingEntries(String key, String pattern)

/**
*
* @param key
* @param pattern
* @return void
*/
void deleteAllMatchingEntries(String key, String pattern)
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ class LocalCounterProvider implements CounterProvider {
}
return result
}

@Override
void deleteAllMatchingEntries(String key, String pattern) {
Pattern compiledPattern = Pattern.compile(pattern.replace('*', '.*'))
Map keyStore = store.get(key)
if (keyStore){
def matchingPairs = keyStore.findAll {entry -> compiledPattern.matcher(entry.key).matches()}
matchingPairs.each { k, v ->
keyStore.remove(k)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,17 @@ class RedisCounterProvider implements CounterProvider {
return result
}
}

@Override
void deleteAllMatchingEntries(String key, String pattern) {
try(Jedis conn=pool.getResource() ) {
final scanResult = conn.hscan(key, "0", new ScanParams().match(pattern).count(hscanCount))
if( !scanResult )
return
for(String entry : scanResult.result) {
final parts = entry.tokenize('=')
conn.hdel(key, parts[0])
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,26 @@ class LocalCounterProviderTest extends Specification {
['pulls/o/abc.com.au/d/2024-05-30':1]
}

def 'should delete records with given pattern' () {
when:
localCounterProvider.inc('metrics/v1', 'builds/o/foo.com', 1)
localCounterProvider.inc('metrics/v1', 'builds/o/bar.io', 1)
localCounterProvider.inc('metrics/v1', 'builds/o/abc.org', 2)
localCounterProvider.inc('metrics/v1', 'pulls/o/foo.it', 1)
localCounterProvider.inc('metrics/v1', 'pulls/o/bar.es', 2)
localCounterProvider.inc('metrics/v1', 'pulls/o/abc.in', 3)
localCounterProvider.inc('metrics/v1', 'pulls/o/abc.com.au/d/2024-07-15', 1)
localCounterProvider.inc('metrics/v1', 'pulls/o/abc.com.au/d/2024-07-14', 1)
localCounterProvider.inc('metrics/v1', 'builds/o/abc.com.au/d/2024-07-14', 1)
localCounterProvider.inc('metrics/v1', 'builds/o/abc.com.au/d/2024-07-14', 1)

then:'delete all pull counter keys'
localCounterProvider.deleteAllMatchingEntries('metrics/v1', 'pulls/o/*')
localCounterProvider.getAllMatchingEntries('metrics/v1', 'pulls/o/*') == [:]
localCounterProvider.getAllMatchingEntries('metrics/v1', 'pulls/o/*/d/2024-05-30') == [:]
and:'delete only build per specific date counter keys'
localCounterProvider.deleteAllMatchingEntries('metrics/v1', 'builds/o/abc.com.au/d/2024-07-14')
localCounterProvider.getAllMatchingEntries('metrics/v1', 'builds/o/*') == ['builds/o/foo.com':1, 'builds/o/bar.io':1, 'builds/o/abc.org':2]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,26 @@ class RedisCounterProviderTest extends Specification implements RedisTestContain
redisCounterProvider.getAllMatchingEntries('metrics/v1', 'pulls/o/*/d/2024-05-30') ==
['pulls/o/abc.com.au/d/2024-05-30':1]
}

def 'should delete records with given pattern' () {
when:
redisCounterProvider.inc('metrics/v1', 'builds/o/foo.com', 1)
redisCounterProvider.inc('metrics/v1', 'builds/o/bar.io', 1)
redisCounterProvider.inc('metrics/v1', 'builds/o/abc.org', 2)
redisCounterProvider.inc('metrics/v1', 'pulls/o/foo.it', 1)
redisCounterProvider.inc('metrics/v1', 'pulls/o/bar.es', 2)
redisCounterProvider.inc('metrics/v1', 'pulls/o/abc.in', 3)
redisCounterProvider.inc('metrics/v1', 'pulls/o/abc.com.au/d/2024-07-15', 1)
redisCounterProvider.inc('metrics/v1', 'pulls/o/abc.com.au/d/2024-07-14', 1)
redisCounterProvider.inc('metrics/v1', 'builds/o/abc.com.au/d/2024-07-14', 1)
redisCounterProvider.inc('metrics/v1', 'builds/o/abc.com.au/d/2024-07-14', 1)

then:'delete all pull counter keys'
redisCounterProvider.deleteAllMatchingEntries('metrics/v1', 'pulls/o/*')
redisCounterProvider.getAllMatchingEntries('metrics/v1', 'pulls/o/*') == [:]
redisCounterProvider.getAllMatchingEntries('metrics/v1', 'pulls/o/*/d/2024-05-30') == [:]
and:'delete only build per specific date counter keys'
redisCounterProvider.deleteAllMatchingEntries('metrics/v1', 'builds/o/abc.com.au/d/2024-07-14')
redisCounterProvider.getAllMatchingEntries('metrics/v1', 'builds/o/*') == ['builds/o/foo.com':1, 'builds/o/bar.io':1, 'builds/o/abc.org':2]
}
}

0 comments on commit 909bda1

Please sign in to comment.