-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.c
163 lines (136 loc) · 3.71 KB
/
store.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <mongo.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
static void
delete_old (mongo_sync_connection *conn)
{
bson *b;
b = bson_build
(BSON_TYPE_TIMESTAMP, "processed", 1294860709000,
BSON_TYPE_NONE);
bson_finish (b);
//TODO How to delete all records?
if (!mongo_sync_cmd_delete(conn, "blahblah.plurals", 0, b)) {
fprintf (stderr, "Error creating index: %s\n", strerror(errno));
exit (1);
bson_free (b);
}
}
//FIXME Error creating index: Operation not supported
/* static void
create_index (mongo_sync_connection *conn)
{
bson *indexes;
indexes = bson_build (BSON_TYPE_STRING, "encoded_encrypted_data", "", -1,
BSON_TYPE_NONE);
bson_finish (indexes);
if (!mongo_sync_cmd_index_create(conn, "blahblah.plurals", indexes, MONGO_INDEX_UNIQUE | MONGO_INDEX_DROP_DUPS | MONGO_INDEX_BACKGROUND | MONGO_INDEX_SPARSE)) {
fprintf (stderr, "Error creating index: %s\n", strerror(errno));
exit (1);
bson_free (indexes);
}
}*/
static void
do_inserts (mongo_sync_connection *conn)
{
bson *data1a;
data1a = bson_build
(BSON_TYPE_TIMESTAMP, "processed", 0,
BSON_TYPE_STRING, "encoded_encrypted_data", "SGVsbG8gV29ybGQgCg==", -1,
BSON_TYPE_STRING, "user_id", "", -1,
BSON_TYPE_INT64, "word_id", 0,
BSON_TYPE_INT64, "synset_id", 0,
BSON_TYPE_INT32, "word_type", 0,
BSON_TYPE_STRING, "data", "", -1,
BSON_TYPE_NONE);
// Missing values or NULL values are simply not provided.
bson_finish (data1a);
if (!mongo_sync_cmd_insert (conn, "blahblah.plurals", data1a, NULL))
{
fprintf (stderr, "Error inserting document: %s\n", strerror(errno));
exit (1);
}
bson_free (data1a);
bson *data1b;
data1b = bson_build
(BSON_TYPE_TIMESTAMP, "processed", 1294860709000,
BSON_TYPE_STRING, "encoded_encrypted_data", "SGVsbG8gV29ybGQgCg==", -1,
BSON_TYPE_STRING, "user_id", "****", -1,
BSON_TYPE_INT64, "word_id", 41012531,
BSON_TYPE_INT64, "synset_id", 0,
BSON_TYPE_INT32, "word_type", 1,
BSON_TYPE_STRING, "data", "smörgåsbord", -1,
BSON_TYPE_NONE);
bson_finish (data1b);
if (!mongo_sync_cmd_insert (conn, "blahblah.plurals", data1b, NULL))
{
fprintf (stderr, "Error inserting document: %s\n", strerror(errno));
exit (1);
}
bson_free (data1b);
}
static void
do_query (mongo_sync_connection *conn)
{
mongo_sync_cursor *c;
bson *query;
gchar *error = NULL;
query = bson_build
(BSON_TYPE_TIMESTAMP, "processed", 1294860709000,
BSON_TYPE_NONE);
bson_finish (query);
c = mongo_sync_cursor_new (conn, "blahblah.plurals",
mongo_sync_cmd_query (conn, "blahblah.plurals", 0,
0, 10, query, NULL));
if (!c)
{
fprintf (stderr, "Error creating the query cursor: %s\n",
strerror (errno));
exit (1);
}
bson_free (query);
while (mongo_sync_cursor_next (c))
{
bson *b = mongo_sync_cursor_get_data (c);
bson_cursor *bc;
gint32 w_t;
gint64 w_i;
if (!b)
{
int e = errno;
mongo_sync_cmd_get_last_error (conn, "blahblah.plurals", &error);
fprintf (stderr, "Error retrieving cursor data: %s\n",
(error) ? error : strerror (e));
exit (1);
}
bc = bson_find (b, "word_type");
bson_cursor_get_int32 (bc, &w_t);
printf ("\rWord type: %d\n", w_t);
bc = bson_find (b, "word_id");
bson_cursor_get_int64 (bc, &w_i);
printf ("\rWord id: %d\n", (int)w_i);
bson_cursor_free (bc);
bson_free (b);
}
printf ("\n");
mongo_sync_cursor_free (c);
}
int
main (void)
{
mongo_sync_connection *conn;
conn = mongo_sync_connect ("localhost", 27017, FALSE);
if (!conn)
{
fprintf (stderr, "Connection failed: %s\n", strerror (errno));
return 1;
}
delete_old (conn);
do_inserts (conn);
// create_index (conn);
do_query (conn);
mongo_sync_disconnect (conn);
return 0;
}