Ejemplo n.º 1
0
Archivo: query.c Proyecto: Mayalinux/db
/*
 * query_by_field --
 *	Query the primary database by field.
 */
static int
query_by_field(char *input)
{
	OPERATOR operator;
	size_t len;
	char *field, *op, *value;

	/*
	 * We expect to see "field [op] value" -- figure it out.
	 *
	 * Skip leading whitespace.
	 */
	while (isspace(*input))
		++input;

	/*
	 * Find an operator, and it better not start the string.
	 */
	if ((len = strcspn(field = input, "<>!=~")) == 0)
		return (query_usage());
	op = field + len;

	/* Figure out the operator, and find the start of the value. */
	switch (op[0]) {
	case '~':
		operator = WC;
		value = op + 1;
		break;
	case '!':
		if (op[1] == '=') {
			operator = NEQ;
			value = op + 2;
			break;
		}
		if (op[1] == '~') {
			operator = NWC;
			value = op + 2;
			break;
		}
		return (query_usage());
	case '<':
		if (op[1] == '=') {
			operator = LTEQ;
			value = op + 2;
		} else {
			operator = LT;
			value = op + 1;
		}
		break;
	case '=':
		operator = EQ;
		if (op[1] == '=')
			value = op + 2;
		else
			value = op + 1;
		break;
	case '>':
		if (op[1] == '=') {
			operator = GTEQ;
			value = op + 2;
		} else {
			operator = GT;
			value = op + 1;
		}
		break;
	default:
		return (query_usage());
	}

	/* Terminate the field name, and there better be a field name. */
	while (--op > input && isspace(*op))
		;
	if (op == input)
		return (query_usage());
	op[1] = '\0';

	/* Make sure there is a value field. */
	while (isspace(*value))
		++value;
	if (*value == '\0')
		return (query_usage());

	return (DbRecord_search_field_name(field, value, operator));
}
Ejemplo n.º 2
0
int bq_main (int argc, char **argv)
{
  if (argc < 3)
  {
      return query_usage();
  }
/*-------defaults for bloom filter building-------*/
  int opt;
  double tole_rate = 0, sampling_rate = 1;
  char *ref = NULL, *list = NULL, *target_path = NULL, *source = NULL, *report_fmt = "json";
  // XXX: make r and l mutually exclusive
  while ((opt = getopt (argc, argv, "s:t:r:o:q:f:h")) != -1)
  {
      switch (opt)
      {
	case 't':
	  tole_rate = atof(optarg);
	  break;
	case 's':
	  sampling_rate = atof(optarg); 
	  // Sampling rate is the partial proportion of a sample, or subsampling, i.e: 0.20 means take only 20% of the input file.
	  break;
	case 'o':
	  target_path = optarg;
	  break;
	case 'q':
	  source = optarg;
	  break;
	case 'r':
	  ref = optarg;
	  break;
	case 'f': // "json", "tsv" or none
	  (optarg) && (report_fmt = optarg, 1);
	  break;
	case 'h':
	  return query_usage();
      	  break;
	case '?':
	  printf ("Unknown option: -%c\n", (char) optopt);
	  return query_usage();
      	  break;
      }
  }
  if (!target_path && !source)
  {
  	fprintf (stderr, "\nPlease, at least specify a bloom filter (-r) and a query file (-q)\n");
      	exit (-1);
  }
  /*
  check the format for reference bloom filter by the extension name so far, can write special tag in the filter for format checking in the future.
  */
  if (strstr(ref,".bloom") == NULL)
  {
	fprintf (stderr,"\nIncorrect bloom filter\n");
	exit(-1);
  }
  if (target_path == NULL) 
  {
      	target_path = argv[0];
  }  //set default path, which is where the binary file is.
  char *result = query(source, ref, tole_rate, sampling_rate, list, target_path, report_fmt, 'c');
  printf("%s\n",result);
  return 1;
}