mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-01-18 04:58:51 +02:00
ed0d48f52c
It is often useful to represent identifiers as strings when they cannot easily be represented as an enum/integer, e.g. because they are distributed among a number of unrelated modules or need to be passed to remote processes. Strings are also more helpful in debugging since they can be recognized without cross-referencing the source. However, strings are awkward to work with in C since they cannot be directly used in switch statements leading to less efficient if-else structures. A StringId encodes a short string into an integer so it can be used in switch statements but may also be readily converted back into a string for debugging purposes. StringIds may also be suitable for matching user input providing the strings are short enough. This patch includes a sample of StringId usage by converting protocol commands to StringIds. There are many other possible use cases. To list a few: * All "types" in storage, filters. IO , etc. These types are primarily for identification and debugging so they fit well with this model. * MemContext names would work well as StringIds since these are entirely for debugging. * Option values could be represented as StringIds which would mean we could remove the functions that convert strings to enums, e.g. CipherType. * There are a number of places where enums need to be converted back to strings for logging/debugging purposes. An example is protocolParallelJobToConstZ. If ProtocolParallelJobState were defined as: typedef enum { protocolParallelJobStatePending = STRID5("pend", ...), protocolParallelJobStateRunning = STRID5("run", ...), protocolParallelJobStateDone = STRID5("done", ...), } ProtocolParallelJobState; then protocolParallelJobToConstZ() could be replaced with strIdToZ(). This also applies to many enums that we don't covert to strings for logging, such as CipherMode. As an example of usage, convert all protocol commands from strings to StringIds.