Skip to content

Commit

Permalink
Fixed a bug where a document with a duplicate ID was being inserted w…
Browse files Browse the repository at this point in the history
…ithout an error.
  • Loading branch information
kishorenc committed Jan 26, 2018
1 parent ba49971 commit 85f9887
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/collection.cpp
Expand Up @@ -82,6 +82,12 @@ Option<nlohmann::json> Collection::add(const std::string & json_str) {
}

std::string doc_id = document["id"];
Option<nlohmann::json> doc_option = get(doc_id);

// we need to check if document ID already exists before attempting to index
if(doc_option.ok()) {
return Option<nlohmann::json>(400, std::string("A document with id ") + doc_id + " already exists.");
}

const Option<uint32_t> & index_memory_op = index_in_memory(document, seq_id);

Expand Down
9 changes: 9 additions & 0 deletions test/collection_test.cpp
Expand Up @@ -1479,6 +1479,15 @@ TEST_F(CollectionTest, IndexingWithBadData) {
ASSERT_FALSE(bad_token_ranking_field_op4.ok());
ASSERT_STREQ("Bad JSON.", bad_token_ranking_field_op4.error().c_str());

// should return an error when a document with pre-existing id is being added
std::string doc = "{\"id\": \"100\", \"name\": \"foo\", \"age\": 29, \"tags\": [], \"average\": 78}";
Option<nlohmann::json> add_op = sample_collection->add(doc);
ASSERT_TRUE(add_op.ok());
add_op = sample_collection->add(doc);
ASSERT_FALSE(add_op.ok());
ASSERT_EQ(400, add_op.code());
ASSERT_STREQ("A document with id 100 already exists.", add_op.error().c_str());

collectionManager.drop_collection("sample_collection");
}

Expand Down

0 comments on commit 85f9887

Please sign in to comment.