From 4b57d9a820aacb38bc3be39903812bf233ad1097 Mon Sep 17 00:00:00 2001 From: stash93 Date: Thu, 8 Jan 2015 01:03:51 +0300 Subject: [PATCH] Fixed negative numbers comparison in DocumentDB --- utilities/document/document_db.cc | 7 ++-- utilities/document/document_db_test.cc | 44 ++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/utilities/document/document_db.cc b/utilities/document/document_db.cc index 6540c2d8c..04d88714b 100644 --- a/utilities/document/document_db.cc +++ b/utilities/document/document_db.cc @@ -312,8 +312,11 @@ bool EncodeJSONPrimitive(const JSONDocument& json, std::string* dst) { break; case JSONDocument::kInt64: dst->push_back(kInt64); - // TODO(icanadi) oops, this will not work correctly for negative numbers - PutFixed64(dst, static_cast(json.GetInt64())); + { + auto val = json.GetInt64(); + dst->push_back((val < 0) ? '0' : '1'); + PutFixed64(dst, static_cast(val)); + } break; case JSONDocument::kString: dst->push_back(kString); diff --git a/utilities/document/document_db_test.cc b/utilities/document/document_db_test.cc index 5b36a2060..bacef9a50 100644 --- a/utilities/document/document_db_test.cc +++ b/utilities/document/document_db_test.cc @@ -164,7 +164,9 @@ TEST(DocumentDBTest, ComplexQueryTest) { "{'_id': 8, 'job_name': 'rock', 'priority': 3, 'progress': 93.24}", "{'_id': 9, 'job_name': 'steady', 'priority': 3, 'progress': 9.1}", "{'_id': 10, 'job_name': 'white', 'priority': 1, 'progress': 61.4}", - "{'_id': 11, 'job_name': 'who', 'priority': 4, 'progress': 39.41}", }; + "{'_id': 11, 'job_name': 'who', 'priority': 4, 'progress': 39.41}", + "{'_id': 12, 'job_name': 'who', 'priority': -1, 'progress': 39.42}", + "{'_id': 13, 'job_name': 'who', 'priority': -2, 'progress': 39.42}", }; // add index on the fly! CreateIndexes({job_name_index}); @@ -185,6 +187,15 @@ TEST(DocumentDBTest, ComplexQueryTest) { AssertCursorIDs(cursor.get(), {4, 8}); } + // -1 <= priority <= 1, index priority + { + std::unique_ptr query(Parse( + "[{'$filter': {'priority': {'$lte': 1, '$gte': -1}," + " '$index': 'priority'}}]")); + std::unique_ptr cursor(db_->Query(ReadOptions(), *query)); + AssertCursorIDs(cursor.get(), {6, 10, 12}); + } + // 2 < priority < 4 AND progress > 10.0, index progress { std::unique_ptr query(Parse( @@ -209,7 +220,7 @@ TEST(DocumentDBTest, ComplexQueryTest) { "[{'$filter': {'progress': {'$gt': 5.0, '$gte': 35.0, '$lt': 65.5}, " "'$index': 'progress'}}]")); std::unique_ptr cursor(db_->Query(ReadOptions(), *query)); - AssertCursorIDs(cursor.get(), {2, 5, 10, 11}); + AssertCursorIDs(cursor.get(), {2, 5, 10, 11, 12, 13}); } // 2 < priority <= 4, index priority @@ -244,6 +255,35 @@ TEST(DocumentDBTest, ComplexQueryTest) { ASSERT_OK(db_->Update(ReadOptions(), WriteOptions(), *query, *update)); } + // priority < 0 + { + std::unique_ptr query( + Parse("[{'$filter': {'priority': {'$lt': 0}, '$index': 'priority'}}]")); + std::unique_ptr cursor(db_->Query(ReadOptions(), *query)); + ASSERT_OK(cursor->status()); + AssertCursorIDs(cursor.get(), {12, 13}); + } + + // -2 < priority < 0 + { + std::unique_ptr query( + Parse("[{'$filter': {'priority': {'$gt': -2, '$lt': 0}," + " '$index': 'priority'}}]")); + std::unique_ptr cursor(db_->Query(ReadOptions(), *query)); + ASSERT_OK(cursor->status()); + AssertCursorIDs(cursor.get(), {12}); + } + + // -2 <= priority < 0 + { + std::unique_ptr query( + Parse("[{'$filter': {'priority': {'$gte': -2, '$lt': 0}," + " '$index': 'priority'}}]")); + std::unique_ptr cursor(db_->Query(ReadOptions(), *query)); + ASSERT_OK(cursor->status()); + AssertCursorIDs(cursor.get(), {12, 13}); + } + // 4 < priority { std::unique_ptr query(