Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #62 from mynameisfiber/master

Made simpleleveldb responses more uniform
  • Loading branch information...
commit dc8918a9e9da9ebbc4afb3f33030f875ae7369ba 2 parents 489aa9c + a2cf434
Pierce Lopez authored
30 simpleleveldb/simpleleveldb.c
@@ -18,7 +18,7 @@
18 18
19 19 // defined values
20 20 #define NAME "simpleleveldb"
21   -#define VERSION "0.8"
  21 +#define VERSION "0.9"
22 22
23 23 #define DUMP_CSV_ITERS_CHECK 10
24 24 #define DUMP_CSV_MSECS_WORK 10
@@ -431,7 +431,7 @@ void mget_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
431 431
432 432 void range_match_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
433 433 {
434   - char *start_key, *end_key, *key_clean;
  434 + char *start_key, *end_key;
435 435 const char *key, *value;
436 436 char sep;
437 437 size_t key_len, value_len;
@@ -477,10 +477,8 @@ void range_match_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
477 477
478 478 while (leveldb_iter_valid(bt_iter) && (result_limit == 0 || result_count < result_limit)) {
479 479 key = leveldb_iter_key(bt_iter, &key_len);
480   - key_clean = strndup(key, key_len);
481 480
482   - if (strcmp(key_clean, end_key) > 0) {
483   - free(key_clean);
  481 + if (strncmp(key, end_key, key_len) > 0) {
484 482 break;
485 483 }
486 484
@@ -488,18 +486,18 @@ void range_match_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
488 486
489 487 if (format == json_format) {
490 488 tmp_obj = json_object_new_object();
491   - json_object_object_add(tmp_obj, key_clean, json_object_new_string_len(value, value_len));
  489 + json_object_object_add(tmp_obj, "key", json_object_new_string_len(key, key_len));
  490 + json_object_object_add(tmp_obj, "value", json_object_new_string_len(value, value_len));
492 491 json_object_array_add(result_array, tmp_obj);
493 492 } else {
494   - evbuffer_add_printf(evb, "%s%c", key_clean, sep);
  493 + evbuffer_add(evb, key, key_len);
  494 + evbuffer_add_printf(evb, "%c", sep);
495 495 evbuffer_add(evb, value, value_len);
496 496 evbuffer_add_printf(evb, "\n");
497 497 }
498 498
499 499 leveldb_iter_next(bt_iter);
500 500 result_count ++;
501   -
502   - free(key_clean);
503 501 }
504 502 if (format == json_format) {
505 503 json_object_object_add(jsobj, "status", json_object_new_string(result_count ? "ok" : "no results"));
@@ -514,7 +512,7 @@ void range_match_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
514 512
515 513 void fwmatch_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
516 514 {
517   - char *fw_key, *key_clean;
  515 + char *fw_key;
518 516 const char *key, *value;
519 517 char sep;
520 518 size_t key_len, value_len;
@@ -555,30 +553,28 @@ void fwmatch_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
555 553
556 554 while (leveldb_iter_valid(fw_iter) && (result_limit == 0 || result_count < result_limit)) {
557 555 key = leveldb_iter_key(fw_iter, &key_len);
558   - key_clean = strndup(key, key_len);
559 556
560 557 // this is the case where we are only fwing keys of this prefix
561 558 // so we need to break out of the loop at the last key
562   - if (strlen(fw_key) > key_len || strncmp(key_clean, fw_key, strlen(fw_key)) != 0 ) {
563   - free(key_clean);
  559 + if (strlen(fw_key) > key_len || strncmp(key, fw_key, strlen(fw_key)) != 0 ) {
564 560 break;
565 561 }
566 562 value = leveldb_iter_value(fw_iter, &value_len);
567 563
568 564 if (format == json_format) {
569 565 tmp_obj = json_object_new_object();
570   - json_object_object_add(tmp_obj, key_clean, json_object_new_string_len(value, value_len));
  566 + json_object_object_add(tmp_obj, "key", json_object_new_string_len(key, key_len));
  567 + json_object_object_add(tmp_obj, "value", json_object_new_string_len(value, value_len));
571 568 json_object_array_add(result_array, tmp_obj);
572 569 } else {
573   - evbuffer_add_printf(evb, "%s%c", key_clean, sep);
  570 + evbuffer_add(evb, key, key_len);
  571 + evbuffer_add_printf(evb, "%c", sep);
574 572 evbuffer_add(evb, value, value_len);
575 573 evbuffer_add_printf(evb, "\n");
576 574 }
577 575
578 576 leveldb_iter_next(fw_iter);
579 577 result_count ++;
580   -
581   - free(key_clean);
582 578 }
583 579 if (format == json_format) {
584 580 json_object_object_add(jsobj, "status", json_object_new_string(result_count ? "ok" : "no results"));
6 simpleleveldb/test_simpleleveldb.py
@@ -74,7 +74,11 @@ def test_multikey(self):
74 74 data = http_fetch('/mget', dict(key=['test_multikey_1', 'test_multikey_2', 'test_multikey_3'], format='txt'))
75 75 assert data == 'test_multikey_1,asdf1\ntest_multikey_2,asdf2\n'
76 76 data = http_fetch_json("/fwmatch", dict(key="test_multikey"))
77   - assert data == [{'test_multikey_1': 'asdf1'}, {'test_multikey_2': 'asdf2'}]
  77 + assert data == [{'key':'test_multikey_1', 'value':'asdf1'}, {'key':'test_multikey_2', 'value':'asdf2'}]
  78 + data = http_fetch_json("/range_match", dict(start="test_multikey_1", end="test_multikey_2"))
  79 + assert data == [{'key':'test_multikey_1', 'value':'asdf1'}, {'key':'test_multikey_2', 'value':'asdf2'}]
  80 + data = http_fetch("/range_match", dict(start="test_multikey_1", end="test_multikey_2", format='txt'))
  81 + assert data == "test_multikey_1,asdf1\ntest_multikey_2,asdf2\n"
78 82 data = http_fetch("/fwmatch", dict(key="test_multikey", format="txt"))
79 83 assert data == "test_multikey_1,asdf1\ntest_multikey_2,asdf2\n"
80 84 http_fetch_json('/mput', body='test_multikey_3,mv1a;mv1b\ntest_multikey_4,mv2a;mv2b\ntest_multikey_5,mv3a')

0 comments on commit dc8918a

Please sign in to comment.
Something went wrong with that request. Please try again.