File: 4s-reverse-bind.c

package info (click to toggle)
4store 1.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 82,064 kB
  • sloc: ansic: 47,682; sh: 10,894; perl: 593; makefile: 217
file content (250 lines) | stat: -rw-r--r-- 6,079 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
    4store - a clustered RDF storage and query engine

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
 *  Copyright 2006 Nick Lamb for Garlik.com
 */

/* Sample usage
 *
 * 4s-reverse-bind sparql_test many FS_BIND_SUBJECT FS_BIND_BY_SUBJECT /dev/null /dev/null predicates objects
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "../common/4store.h"
#include "../common/server.h"
#include "../common/error.h"

static int  segments;

static char *flag_name[] = {
  "FS_BIND_MODEL",
  "FS_BIND_SUBJECT",
  "FS_BIND_PREDICATE",
  "FS_BIND_OBJECT",
  "FS_BIND_DISTINCT",
  "FS_BIND_OPTIONAL",
  "FS_BIND_UNION",
  "FS_BIND_SAME_XXXX",
  "FS_BIND_SAME_XXAA",
  "FS_BIND_SAME_XAXA",
  "FS_BIND_SAME_XAAX",
  "FS_BIND_SAME_XAAA",
  "FS_BIND_SAME_AXXA",
  "FS_BIND_SAME_AXAX",
  "FS_BIND_SAME_AXAA",
  "FS_BIND_SAME_AAXX",
  "FS_BIND_SAME_AAXA",
  "FS_BIND_SAME_AAAX",
  "FS_BIND_SAME_AAAA",
  "FS_BIND_SAME_AABB",
  "FS_BIND_SAME_ABAB",
  "FS_BIND_SAME_ABBA",

  "FS_BIND_BY_SUBJECT",
  "FS_BIND_BY_OBJECT",
};

static int flag_value[] = {
  FS_BIND_MODEL,
  FS_BIND_SUBJECT,
  FS_BIND_PREDICATE,
  FS_BIND_OBJECT,
  FS_BIND_DISTINCT,
  FS_BIND_OPTIONAL,
  FS_BIND_UNION,
  FS_BIND_SAME_XXXX,
  FS_BIND_SAME_XXAA,
  FS_BIND_SAME_XAXA,
  FS_BIND_SAME_XAAX,
  FS_BIND_SAME_XAAA,
  FS_BIND_SAME_AXXA,
  FS_BIND_SAME_AXAX,
  FS_BIND_SAME_AXAA,
  FS_BIND_SAME_AAXX,
  FS_BIND_SAME_AAXA,
  FS_BIND_SAME_AAAX,
  FS_BIND_SAME_AAAA,
  FS_BIND_SAME_AABB,
  FS_BIND_SAME_ABAB,
  FS_BIND_SAME_ABBA,

  FS_BIND_BY_SUBJECT,
  FS_BIND_BY_OBJECT,
};

static fs_rid_vector *rid_file(char *filename)
{
  fs_rid_vector *rids = fs_rid_vector_new(0);
  FILE *fp = fopen(filename, "r");

  if (!fp) {
    fs_error(LOG_ERR, "could not open ā€œ%sā€: %s", filename, strerror(errno));
    return rids;
  }

  while (!feof(fp) && !ferror(fp)) {
    char ridstr[21];
    fs_rid rid;
    if (fscanf(fp, "%20s", ridstr) < 1) break;

    rid = strtoull(ridstr, NULL, 16);
    fs_rid_vector_append(rids, rid);
  }

  fclose(fp);

  return rids;
}

int main(int argc, char *argv[])
{
  char *password = fsp_argv_password(&argc, argv);

  int flags = 0;
  int seg = 0; /* deliberately using signed type */
  fs_rid_vector *mrids= NULL, *srids= NULL, *prids= NULL, *orids= NULL;
  fs_rid_vector **result = NULL;

  if (argc < 7) {
    fprintf(stderr, "%s revision %s\n", argv[0], FS_FRONTEND_VER);
    fprintf(stderr, "Usage: %s <kbname> <flags>\n", argv[0]);
    fprintf(stderr, " mrid-file srid-file prid-file orid-file [offset limit]\n");
    fprintf(stderr, "For flags use FS_BIND_... symbols or a numeric value\n");
    fprintf(stderr, "RID files are one RID per line\n");
    exit(1);
  }

  char *kbname = argv[1];

  seg = atoi(argv[2]);

  int param = 2;

  flags = strtol(argv[param], NULL, 0);

  if (flags == 0) { /* symbolic flags, hopefully */
    while (param < argc) {
      const int len = sizeof(flag_name) / sizeof(char *);
      int k;
      for (k = 0; k < len; ++k) {
        if (!strcmp(flag_name[k], argv[param])) {
          flags |= flag_value[k];
          break;
        }
      }
      if (k == len) break;
      param ++;
    }
  } else {
    param ++; /* done with the numeric flags then */
  }

  if (argc < param + 4) {
    fprintf(stderr, "Wrong number of arguments\n");
    exit(1);
  }

  mrids = rid_file(argv[param++]);
  srids = rid_file(argv[param++]);
  prids = rid_file(argv[param++]);
  orids = rid_file(argv[param++]);

  int limit, offset;

  if (argc == param ) {
    /* defaults */
    limit = -1;
    offset = -1;
  } else if (argc > param + 2) {
    fprintf(stderr, "Wrong number of arguments\n");
    exit(1);
  } else if (argc < param + 2) {
    fprintf(stderr, "Wrong number of arguments\n");
    exit(1);
  } else {
    offset = atoi(argv[param]);
    limit = atoi(argv[param + 1]);
  }

  fsp_link *link = fsp_open_link(kbname, password, FS_OPEN_HINT_RO);

  if (!link) {
    fs_error (LOG_ERR, "couldn't connect to ā€œ%sā€", argv[1]);
    exit(2);
  }

  segments = fsp_link_segments(link);

  if (seg < 0 || seg > segments) {
    fs_error (LOG_ERR, "Segment %d out of range (0-%u)", seg, segments);
    exit(1);
  }

  double then = fs_time();
  int ans = fsp_reverse_bind_all(link, flags, mrids, srids, prids, orids, &result, offset, limit);

  double time_binding = fs_time() - then;
  if (ans != 0) {
    fs_error (LOG_ERR, "error during reverse bind");
    exit(1);
  }

  /* print results */

  int cols = 0;
  for (int k = 0; k < 4; ++k) {
    if (flags & (1 << k)) cols++;
  }

  if (!result) {
    printf("NO MATCH found.\n");
  } else if (cols == 0) {
    printf("MATCH found.\n");
  } else if (result[0]) {
    int length = result[0]->length;

    if (flags & FS_BIND_MODEL) printf("-----Model------  ");
    if (flags & FS_BIND_SUBJECT) printf("----Subject-----  ");
    if (flags & FS_BIND_PREDICATE) printf("----Predicate---  ");
    if (flags & FS_BIND_OBJECT) printf("-----Object-----");
    putchar('\n');

    for (int k = 0; k < length; ++k) {
      for (int c = 0; c < cols; ++c) {
        printf("%016llX  ", result[c]->data[k]);
      }
      putchar('\n');
    }
  }

  fprintf(stderr, "bind took %f seconds on client\n", time_binding);

  fs_query_timing times;
  for (int s = 0; s < segments; ++s) {
    fsp_get_query_times(link, s, &times);
    if (times.bind > 0.0f) {
      fprintf(stderr, "%d: %f\n", s, times.bind);
    }
  }
  fputc('\n', stderr);

  fsp_close_link(link);
}