Skip to content

Commit

Permalink
Formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
vbotka committed Jan 2, 2018
1 parent 2924f94 commit 214aad9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
22 changes: 12 additions & 10 deletions docs/source/code/create-graph-01.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* Task: Read /var/log/dpkg.log and create a graph to visualize how
often packages are installed, upgraded and removed.
Tested with: gcc 7.2, hiredis 0.13 and redis 4.0.1 */
/*
* Task: Read /var/log/dpkg.log and create a graph to visualize how
* often packages are installed, upgraded and removed.
* Tested with: gcc 7.2, hiredis 0.13 and redis 4.0.1
*/

#include <assert.h>
#include <stdio.h>
Expand All @@ -14,7 +16,7 @@ const char *LOG_FILES[] = { "dpkg.log", };

redisContext *c;

/* This function reads log_file and puts the status into the database */
// This function reads log_file and puts the status into the database.
int read_log(const char *log_file, redisContext *c) {
redisReply *reply;
FILE *fp;
Expand All @@ -40,7 +42,7 @@ int read_log(const char *log_file, redisContext *c) {
return(0);
}

/* This function reads the database and writes the status CSV file */
// This function reads the database and writes the status CSV file.
int write_csv(char *status, redisContext *c) {
redisReply *reply;
FILE *fp;
Expand All @@ -53,10 +55,10 @@ int write_csv(char *status, redisContext *c) {
reply = redisCommand(c, "ZRANGE %s 0 -1 WITHSCORES", status);
assert(reply != NULL);
n = reply->elements-1;
for(i=0; i<n; i=i+2) {
for (i=0; i<n; i=i+2) {
fprintf(fp, "%s %c %s\n", reply->element[i]->str, \
CSV_SEPARATOR, \
reply->element[i+1]->str);
CSV_SEPARATOR, \
reply->element[i+1]->str);
}
freeReplyObject(reply);
fclose(fp);
Expand Down Expand Up @@ -89,14 +91,14 @@ int main(int argc, char **argv) {
freeReplyObject(reply);

n = sizeof(LOG_FILES)/sizeof(LOG_FILES[0]);
for(i = 0; i < n; i++) {
for (i = 0; i < n; i++) {
read_log(LOG_FILES[i], c);
}

reply = redisCommand(c, "SCAN 0 COUNT 10");
assert(reply != NULL);
n = reply->element[1]->elements;
for(i=0; i<n; i++) {
for (i=0; i<n; i++) {
status = reply->element[1]->element[i]->str;
write_csv(status, c);
}
Expand Down
10 changes: 6 additions & 4 deletions docs/source/code/create-topchart.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* Task: Read text from a file and list 10 most frequently used words
in it.
Tested with: gcc 7.2, hiredis 0.13 and redis 4.0.1 */
/*
* Task: Read text from a file and list 10 most frequently used words
* in it.
* Tested with: gcc 7.2, hiredis 0.13 and redis 4.0.1
*/

#include <assert.h>
#include <stdio.h>
Expand Down Expand Up @@ -56,7 +58,7 @@ int main(int argc, char **argv) {
reply = redisCommand(c, "ZRANGE \"topchart\" -10 -1 WITHSCORES");
assert(reply != NULL);
n = reply->elements-1;
for(i=0; i<n; i=i+2) {
for (i=0; i<n; i=i+2) {
printf("%s %s\n", reply->element[i+1]->str, reply->element[i]->str);
}
freeReplyObject(reply);
Expand Down
8 changes: 4 additions & 4 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ Create frequency graph from a log

**Task:** Read /var/log/dpkg.log and create a graph to visualize how often packages are installed, upgraded and removed.

**Solution:** The loop (92) calls function *read_log* which reads the log line by line (25) and splits the fields date and time in minutes (27.31) and status (32,34). Third field of the log *status* is status of the dpkg operation(install, upgrade, remove ...). Method *zincrby* (35) increments by 1 the score of *date* in the key *status*. As a result the database contains keys(install, upgrade, remove ...) and associated lists of *dates* sorted by score. Next loop (99) calls the function *write_csv* with all keys. As a result *status.csv* files are created in the current directory with the *(date;score)* pairs.
**Solution:** The loop (94) calls function *read_log* which reads the log line by line (27) and splits the fields date and time in minutes (29.33) and status (34,36). Third field of the log *status* is status of the dpkg operation(install, upgrade, remove ...). Method *zincrby* (37) increments by 1 the score of *date* in the key *status*. As a result the database contains keys(install, upgrade, remove ...) and associated lists of *dates* sorted by score. Next loop (101) calls the function *write_csv* with all keys. As a result *status.csv* files are created in the current directory with the *(date;score)* pairs.

.. `[source] <../../code/create-graph-01.c>`_
.. literalinclude:: code/create-graph-01.c
:linenos:
:emphasize-lines: 35, 53, 72, 83, 87, 96, 105
:emphasize-lines: 37, 55, 74, 85, 89, 98, 107

**Result:** The *status.csv* files can be used to create a graph with *gnuplot*.

Expand All @@ -31,13 +31,13 @@ List 10 most used words in a text
.. literalinclude:: code/create-topchart-text.bash

*zincrby* (47) increments by 1 the score of *word* in the key *topchart* and *zrange* (56) returns top 10 words with scores.
*zincrby* (49) increments by 1 the score of *word* in the key *topchart* and *zrange* (58) returns top 10 words with scores.

.. `[source] <../../source/code/create-topchart.c>`_
.. literalinclude:: code/create-topchart.c
:linenos:
:emphasize-lines: 23, 34, 38, 47, 56, 64
:emphasize-lines: 25, 36, 40, 49, 58, 66

**Result:**

Expand Down
2 changes: 2 additions & 0 deletions docs/source/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Introduction
`Redis <https://redis.io/>`_ is popular in-memory data structure store, used as a database, cache and message broker. `Hiredis <https://github.com/redis/hiredis>`_ is a minimalistic C client library for the Redis database documented at `RedisLabs <https://redislabs.com/lp/hiredis/>`_ .

Intention of this documentation is to create examples how to use Hiredis.

Ultimate guide to Redis commands is available at `redis.io <https://redis.io/commands/>`_.

0 comments on commit 214aad9

Please sign in to comment.