• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 ///
2 /// Fills database with a set of football table records
3 /// then traverse records according to club name in ascending and descending orders.
4 ///
5 
6 #include "iwkv.h"
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 
11 static struct data_s {
12   const char *club;
13   uint8_t     points;
14 } _points[] = {
15 
16   { "Aston Villa",              25  },
17   { "Manchester City",          57  },
18   { "Arsenal",                  40  },
19   { "Everton",                  37  },
20   { "West Ham United",          27  },
21   { "Tottenham Hotspur",        41  },
22   { "Wolverhampton Wanderers",  43  },
23   { "Norwich City",             21  },
24   { "Leicester City",           53  },
25   { "Manchester United",        45  },
26   { "Newcastle United",         35  },
27   { "Brighton & Hove Albion",   29  },
28   { "AFC Bournemouth",          27  },
29   { "Crystal Palace",           39  },
30   { "Sheffield United",         43  },
31   { "Burnley",                  39  },
32   { "Southampton",              34  },
33   { "Watford",                  27  },
34   { "Chelsea",                  48  },
35   { "Liverpool",                82  },
36 };
37 
run(void)38 static iwrc run(void) {
39   IWKV_OPTS opts = {
40     .path = "cursor1.db",
41     .oflags = IWKV_TRUNC // Cleanup database before open
42   };
43   IWKV iwkv;
44   IWDB db;
45   IWKV_cursor cur = 0;
46   iwrc rc = iwkv_open(&opts, &iwkv);
47   RCRET(rc);
48 
49   rc = iwkv_db(iwkv, 1, 0, &db);
50   RCGO(rc, finish);
51 
52   for (int i = 0; i < sizeof(_points) / sizeof(_points[0]); ++i) {
53     struct data_s *n = &_points[i];
54     IWKV_val key = { .data = (void *) n->club, .size = strlen(n->club) };
55     IWKV_val val = { .data = &n->points, .size = sizeof(n->points) };
56     RCC(rc, finish, iwkv_put(db, &key, &val, 0));
57   }
58 
59   fprintf(stdout, ">>>> Traverse in descending order\n");
60   RCC(rc, finish, iwkv_cursor_open(db, &cur, IWKV_CURSOR_BEFORE_FIRST, 0));
61   while ((rc = iwkv_cursor_to(cur, IWKV_CURSOR_NEXT)) == 0) {
62     IWKV_val key, val;
63     RCC(rc, finish, iwkv_cursor_get(cur, &key, &val));
64     fprintf(stdout, "%.*s: %u\n",
65             (int) key.size, (char *) key.data,
66             *(uint8_t *) val.data);
67     iwkv_kv_dispose(&key, &val);
68   }
69   rc = 0;
70   iwkv_cursor_close(&cur);
71 
72   fprintf(stdout, "\n>>>> Traverse in ascending order\n");
73   RCC(rc, finish, iwkv_cursor_open(db, &cur, IWKV_CURSOR_AFTER_LAST, 0));
74   while ((rc = iwkv_cursor_to(cur, IWKV_CURSOR_PREV)) == 0) {
75     IWKV_val key, val;
76     RCC(rc, finish, iwkv_cursor_get(cur, &key, &val));
77     fprintf(stdout, "%.*s: %u\n",
78             (int) key.size, (char *) key.data,
79             *(uint8_t *) val.data);
80     iwkv_kv_dispose(&key, &val);
81   }
82   rc = 0;
83   iwkv_cursor_close(&cur);
84 
85   // Select all keys greater or equal than: Manchester United
86   {
87     fprintf(stdout, "\n>>>> Records GE: %s\n", _points[9].club);
88     IWKV_val key = { .data = (void *) _points[9].club, .size = strlen(_points[9].club) }, val;
89     RCC(rc, finish, iwkv_cursor_open(db, &cur, IWKV_CURSOR_GE, &key));
90     do {
91       RCC(rc, finish, iwkv_cursor_get(cur, &key, &val));
92       fprintf(stdout, "%.*s: %u\n",
93               (int) key.size, (char *) key.data,
94               *(uint8_t *) val.data);
95       iwkv_kv_dispose(&key, &val);
96     } while ((rc = iwkv_cursor_to(cur, IWKV_CURSOR_NEXT)) == 0);
97     rc = 0;
98   }
99   iwkv_cursor_close(&cur);
100 
101 finish:
102   if (cur) {
103     iwkv_cursor_close(&cur);
104   }
105   iwkv_close(&iwkv);
106   return rc;
107 }
108 
main()109 int main() {
110   iwrc rc = run();
111   if (rc) {
112     iwlog_ecode_error3(rc);
113     return 1;
114   }
115   return 0;
116 }
117