From 8a7f255cd005a13aaebb77468ab77dc2c6501d23 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 24 Apr 2020 16:49:27 +0200 Subject: [PATCH] LCS -> STRALGO LCS. STRALGO should be a container for mostly read-only string algorithms in Redis. The algorithms should have two main characteristics: 1. They should be non trivial to compute, and often not part of programming language standard libraries. 2. They should be fast enough that it is a good idea to have optimized C implementations. Next thing I would love to see? A small strings compression algorithm. --- src/server.c | 2 +- src/server.h | 2 +- src/t_string.c | 23 ++++++++++++++++++----- tests/unit/type/string.tcl | 16 ++++++++-------- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/server.c b/src/server.c index fc9b87aae..f7af79c3f 100644 --- a/src/server.c +++ b/src/server.c @@ -1006,7 +1006,7 @@ struct redisCommand redisCommandTable[] = { "admin no-script no-slowlog ok-loading ok-stale", 0,NULL,0,0,0,0,0,0}, - {"lcs",lcsCommand,-4, + {"stralgo",stralgoCommand,-2, "write use-memory @string", 0,lcsGetKeys,0,0,0,0,0,0} }; diff --git a/src/server.h b/src/server.h index d39359dce..9e1e506af 100644 --- a/src/server.h +++ b/src/server.h @@ -2389,7 +2389,7 @@ void xdelCommand(client *c); void xtrimCommand(client *c); void lolwutCommand(client *c); void aclCommand(client *c); -void lcsCommand(client *c); +void stralgoCommand(client *c); #if defined(__GNUC__) void *calloc(size_t count, size_t size) __attribute__ ((deprecated)); diff --git a/src/t_string.c b/src/t_string.c index ef382bb0c..d4eb04769 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -480,18 +480,31 @@ void strlenCommand(client *c) { addReplyLongLong(c,stringObjectLen(o)); } -/* LCS -- Longest common subsequence. + +/* STRALGO -- Implement complex algorithms on strings. * - * LCS [IDX] [MINMATCHLEN ] - * STRINGS | KEYS */ -void lcsCommand(client *c) { + * STRALGO ... arguments ... */ +void stralgoLCS(client *c); /* This implements the LCS algorithm. */ +void stralgoCommand(client *c) { + /* Select the algorithm. */ + if (!strcasecmp(c->argv[1]->ptr,"lcs")) { + stralgoLCS(c); + } else { + addReply(c,shared.syntaxerr); + } +} + +/* STRALGO [IDX] [MINMATCHLEN ] [WITHMATCHLEN] + * STRINGS | KEYS + */ +void stralgoLCS(client *c) { uint32_t i, j; long long minmatchlen = 0; sds a = NULL, b = NULL; int getlen = 0, getidx = 0, withmatchlen = 0; robj *obja = NULL, *objb = NULL; - for (j = 1; j < (uint32_t)c->argc; j++) { + for (j = 2; j < (uint32_t)c->argc; j++) { char *opt = c->argv[j]->ptr; int moreargs = (c->argc-1) - j; diff --git a/tests/unit/type/string.tcl b/tests/unit/type/string.tcl index b9ef9de7a..8126cdee8 100644 --- a/tests/unit/type/string.tcl +++ b/tests/unit/type/string.tcl @@ -424,29 +424,29 @@ start_server {tags {"string"}} { set rna2 {ATTAAAGGTTTATACCTTCCCAGGTAACAAACCAACCAACTTTCGATCTCTTGTAGATCTGTTCTCTAAACGAACTTTAAAATCTGTGTGGCTGTCACTCGGCTGCATGCTTAGTGCACTCACGCAGTATAATTAATAACTAATTACTGTCGTTGACAGGACACGAGTAACTCGTCTATCTTCTGCAGGCTGCTTACGGTTTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAGGTTT} set rnalcs {ACCTTCCCAGGTAACAAACCAACCAACTTTCGATCTCTTGTAGATCTGTTCTCTAAACGAACTTTAAAATCTGTGTGGCTGTCACTCGGCTGCATGCTTAGTGCACTCACGCAGTATAATTAATAACTAATTACTGTCGTTGACAGGACACGAGTAACTCGTCTATCTTCTGCAGGCTGCTTACGGTTTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAGGTTT} - test {LCS string output with STRINGS option} { - r LCS STRINGS $rna1 $rna2 + test {STRALGO LCS string output with STRINGS option} { + r STRALGO LCS STRINGS $rna1 $rna2 } $rnalcs - test {LCS len} { - r LCS LEN STRINGS $rna1 $rna2 + test {STRALGO LCS len} { + r STRALGO LCS LEN STRINGS $rna1 $rna2 } [string length $rnalcs] test {LCS with KEYS option} { r set virus1 $rna1 r set virus2 $rna2 - r LCS KEYS virus1 virus2 + r STRALGO LCS KEYS virus1 virus2 } $rnalcs test {LCS indexes} { - dict get [r LCS IDX KEYS virus1 virus2] matches + dict get [r STRALGO LCS IDX KEYS virus1 virus2] matches } {{{238 238} {239 239}} {{236 236} {238 238}} {{229 230} {236 237}} {{224 224} {235 235}} {{1 222} {13 234}}} test {LCS indexes with match len} { - dict get [r LCS IDX KEYS virus1 virus2 WITHMATCHLEN] matches + dict get [r STRALGO LCS IDX KEYS virus1 virus2 WITHMATCHLEN] matches } {{{238 238} {239 239} 1} {{236 236} {238 238} 1} {{229 230} {236 237} 2} {{224 224} {235 235} 1} {{1 222} {13 234} 222}} test {LCS indexes with match len and minimum match len} { - dict get [r LCS IDX KEYS virus1 virus2 WITHMATCHLEN MINMATCHLEN 5] matches + dict get [r STRALGO LCS IDX KEYS virus1 virus2 WITHMATCHLEN MINMATCHLEN 5] matches } {{{1 222} {13 234} 222}} }