File: example-matcher.c

package info (click to toggle)
syslog-ng 3.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 47,320 kB
  • ctags: 43,937
  • sloc: ansic: 159,432; yacc: 25,059; sh: 13,574; makefile: 4,669; python: 3,468; java: 3,218; xml: 2,309; perl: 318; lex: 316; awk: 184
file content (84 lines) | stat: -rw-r--r-- 1,672 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bcon.h>
#include <mongoc.h>
#include <stdio.h>

static void
log_query (const bson_t *doc,
           const bson_t *query)
{
   char *str1;
   char *str2;

   str1 = bson_as_json (doc, NULL);
   str2 = bson_as_json (query, NULL);

   printf ("Matching %s against %s\n", str2, str1);

   bson_free (str1);
   bson_free (str2);
}

static void
check_match (const bson_t *doc,
             const bson_t *query)
{
   bson_error_t error;
   mongoc_matcher_t *matcher = mongoc_matcher_new (query, &error);
   if (!matcher) {
      fprintf (stderr, "Error: %s\n", error.message);
      return;
   }

   if (mongoc_matcher_match (matcher, doc)) {
      printf ("  Document matched!\n");
   } else {
      printf ("  No match.\n");
   }

   mongoc_matcher_destroy (matcher);
}

static void
example (void)
{
   bson_t *query;
   bson_t *doc;

   doc = BCON_NEW ("hello", "[", "{", "foo", BCON_UTF8 ("bar"), "}", "]");
   query = BCON_NEW ("hello.0.foo", BCON_UTF8 ("bar"));

   log_query (doc, query);
   check_match (doc, query);

   bson_destroy (doc);
   bson_destroy (query);

   /* i is > 1 or i < -1. */
   query = BCON_NEW ("$or", "[",
                     "{", "i", "{", "$gt", BCON_INT32 (1), "}", "}",
                     "{", "i", "{", "$lt", BCON_INT32 (-1), "}", "}", "]");

   doc = BCON_NEW ("i", BCON_INT32 (2));
   log_query (doc, query);
   check_match (doc, query);

   bson_destroy (doc);

   doc = BCON_NEW ("i", BCON_INT32 (0));
   log_query (doc, query);
   check_match (doc, query);

   bson_destroy (doc);
   bson_destroy (query);
}

int
main (int argc,
      char *argv[])
{
   mongoc_init ();
   example ();
   mongoc_cleanup ();

   return 0;
}