tests: load json to ejdb
authorŁukasz Stelmach <l.stelmach@samsung.com>
Mon, 24 Apr 2017 10:46:39 +0000 (12:46 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Mon, 24 Apr 2017 10:46:39 +0000 (12:46 +0200)
make LDFLAGS=-L/tmp/ejdb/usr/local/lib\ -lejdb\ -ljson-c CFLAGS=-g\ -I/usr/include/json-c\ -I/tmp/ejdb/usr/local/include\ -DWITH_EJDB json

tests/json.c [new file with mode: 0644]

diff --git a/tests/json.c b/tests/json.c
new file mode 100644 (file)
index 0000000..ac1eab7
--- /dev/null
@@ -0,0 +1,123 @@
+#include <stdint.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <json.h>
+#include <ejdb/ejdb.h>
+
+static EJDB *jb;
+
+int main(int ac, char* av[])
+{
+  struct json_tokener *tok;
+  enum json_tokener_error jerr;
+  json_object *o;
+  struct stat s;
+  int fd;
+  const char *input, *i;
+  int input_len,il;
+  EJCOLL *coll;
+
+  jb = ejdbnew();
+  if (!ejdbopen(jb, "journal", JBOWRITER | JBOCREAT | JBOTRUNC)) {
+    perror("ejdbopen");
+    return -1;
+  }
+
+  coll = ejdbcreatecoll(jb, "log", NULL);
+
+  if (stat(av[1], &s) < 0) {
+    perror("stat");
+    return -1;
+  }
+  il = input_len = s.st_size;
+
+  if ((fd = open(av[1], O_RDONLY)) < 0) {
+    perror("open");
+    return -1;
+  }
+
+  i = input = mmap(NULL, input_len, PROT_READ, MAP_PRIVATE, fd, 0);
+  if (input == NULL) {
+    perror("mmap");
+    return -1;
+  }
+
+  tok = json_tokener_new();
+  while ((uintptr_t)i < (uintptr_t)input + input_len) {
+    int val_type;
+    char *val_type_str, *str;
+    bson bsrec;
+    bson_oid_t oid;
+
+    do {
+      o = json_tokener_parse_ex(tok, i, il);
+    } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
+
+    if (jerr != json_tokener_success) {
+      fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
+      return -1;
+    }
+    printf("c:%d i:%p t:%d\n", tok->char_offset, i, json_object_get_type(o));
+
+#ifdef WITH_EJDB
+    bson_init(&bsrec);
+#endif
+    json_object_object_foreach(o, key, val) {
+      printf("key: \"%s\", type of val: ", key);
+      val_type = json_object_get_type(val);
+
+      switch (val_type) {
+      case json_type_null:
+        val_type_str = "val is NULL";
+        break;
+
+      case json_type_boolean:
+        val_type_str = "val is a boolean";
+        break;
+
+      case json_type_double:
+        val_type_str = "val is a double";
+        break;
+
+      case json_type_int:
+        val_type_str = "val is an integer";
+        break;
+
+      case json_type_string:
+        val_type_str = "val is a string";
+        str = (char *) json_object_get_string(val);
+#ifdef WITH_EJDB
+        bson_append_string(&bsrec, key, str);
+#endif
+        break;
+
+      case json_type_object:
+        val_type_str = "val is an object";
+        break;
+
+      case json_type_array:
+        val_type_str = "val is an array";
+        break;
+      }
+
+      printf("%s", val_type_str);
+
+      if (str)
+        printf("\t->\t\"%s\"", str);
+
+      printf("\n");
+      str = NULL;
+    }
+#ifdef WITH_EJDB
+    bson_finish(&bsrec);
+    ejdbsavebson(coll, &bsrec, &oid);
+    bson_destroy(&bsrec);
+#endif
+    i += tok->char_offset;
+    il -= tok->char_offset;
+  }
+
+  return 0;
+}