Prepare the key associated string value for DMA access, and returns * a pointer and size (by reference), that the user can use to read or * modify the string in-place accessing it directly via pointer. * * The 'mode' is composed by bitwise OR-ing the following flags: * * REDISMODULE_READ -- Read access * REDISMODULE_WRITE -- Write access * * If the DMA is not requested for writing
| 2575 | * the string, and later call StringDMA() again to get the pointer. |
| 2576 | */ |
| 2577 | const char *RM_StringDMA(RedisModuleKey *key, size_t *len, int mode) { |
| 2578 | /* We need to return *some* pointer for empty keys, we just return |
| 2579 | * a string literal pointer, that is the advantage to be mapped into |
| 2580 | * a read only memory page, so the module will segfault if a write |
| 2581 | * attempt is performed. */ |
| 2582 | const char *emptystring = "<dma-empty-string>"; |
| 2583 | if (key->value == NULL) { |
| 2584 | *len = 0; |
| 2585 | return emptystring; |
| 2586 | } |
| 2587 | |
| 2588 | if (key->value->type != OBJ_STRING) return NULL; |
| 2589 | |
| 2590 | /* For write access, and even for read access if the object is encoded, |
| 2591 | * we unshare the string (that has the side effect of decoding it). */ |
| 2592 | if ((mode & REDISMODULE_WRITE) || key->value->encoding != OBJ_ENCODING_RAW) |
| 2593 | key->value = dbUnshareStringValue(key->db, key->key, key->value); |
| 2594 | |
| 2595 | *len = sdslen(szFromObj(key->value)); |
| 2596 | return szFromObj(key->value); |
| 2597 | } |
| 2598 | |
| 2599 | /* If the string is open for writing and is of string type, resize it, padding |
| 2600 | * with zero bytes if the new length is greater than the old one. |
nothing calls this directly
no test coverage detected