From 4588afe0984c72178e8ba745efbd33272133c808 Mon Sep 17 00:00:00 2001 From: Matthieu Dorier Date: Tue, 30 May 2023 11:49:52 +0000 Subject: [PATCH] fixed value of last_id for collection mixin --- include/yokan/doc-mixin.hpp | 15 ++++++++------- tests/test-coll-create.cpp | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/yokan/doc-mixin.hpp b/include/yokan/doc-mixin.hpp index 2384d0f..9b996f7 100644 --- a/include/yokan/doc-mixin.hpp +++ b/include/yokan/doc-mixin.hpp @@ -7,6 +7,7 @@ #define __YOKAN_DOC_MIXIN_H #include +#include #include #include #include @@ -25,7 +26,7 @@ class DocumentStoreMixin : public DB { struct CollectionMetadata { yk_id_t size = 0; - yk_id_t last_id = 0; + yk_id_t next_id = 0; }; ABT_rwlock m_lock = ABT_RWLOCK_NULL; @@ -85,7 +86,7 @@ class DocumentStoreMixin : public DB { CollectionMetadata metadata; status = _collGetMetadata(collection, name_len, &metadata); if(status != Status::OK) return status; - std::vector ids(metadata.last_id); + std::vector ids(metadata.next_id); for(yk_id_t id = 0; id < ids.size(); id++) { ids[id] = id; } @@ -110,7 +111,7 @@ class DocumentStoreMixin : public DB { ScopedReadLock lock(m_lock); auto status = _collGetMetadata(collection, strlen(collection), &metadata); if(status == Status::OK) - *id = metadata.last_id; + *id = metadata.next_id-1; return status; } @@ -149,9 +150,9 @@ class DocumentStoreMixin : public DB { auto status = _collGetMetadata(collection, name_len, &metadata); if(status != Status::OK) return status; for(uint64_t i = 0; i < count; i++) { - ids[i] = metadata.last_id + i; + ids[i] = metadata.next_id + i; } - metadata.last_id += count; + metadata.next_id += count; status = _collPutMetadata(collection, name_len, &metadata, false); if(status != Status::OK) return status; } @@ -180,7 +181,7 @@ class DocumentStoreMixin : public DB { auto status = _collGetMetadata(collection, name_len, &metadata); if(status != Status::OK) return status; for(unsigned i=0; i < ids.size; i++) { - if(ids[i] >= metadata.last_id) + if(ids[i] >= metadata.next_id) return Status::InvalidID; } return put(mode, keys, ksizes, documents, sizes); @@ -283,7 +284,7 @@ class DocumentStoreMixin : public DB { yk_id_t id = from_id; size_t docs_offset = 0; size_t i = 0; - while(i != count && id <= metadata.last_id && docs_offset < documents.size) { + while(i != count && id < metadata.next_id && docs_offset < documents.size) { auto key = _keyFromId(collection, name_len, id); auto ksize = key.size(); auto doc_umem = UserMem{ diff --git a/tests/test-coll-create.cpp b/tests/test-coll-create.cpp index ace45f6..dd9f9db 100644 --- a/tests/test-coll-create.cpp +++ b/tests/test-coll-create.cpp @@ -70,7 +70,7 @@ static MunitResult test_coll_create_store_size_last_id(const MunitParameter para ret = yk_collection_last_id(dbh, "abcd", 0, &last_id); SKIP_IF_NOT_IMPLEMENTED(ret); munit_assert_int(ret, ==, YOKAN_SUCCESS); - munit_assert_long(last_id, ==, 0); + munit_assert_long(last_id, ==, -1); for(size_t i=0; i < context->reference.size(); i++) { yk_id_t id; @@ -90,7 +90,7 @@ static MunitResult test_coll_create_store_size_last_id(const MunitParameter para ret = yk_collection_last_id(dbh, "abcd", 0, &last_id); SKIP_IF_NOT_IMPLEMENTED(ret); munit_assert_int(ret, ==, YOKAN_SUCCESS); - munit_assert_long(last_id, ==, context->reference.size()); + munit_assert_long(last_id, ==, context->reference.size()-1); ret = yk_collection_drop(dbh, "abcd", 0); SKIP_IF_NOT_IMPLEMENTED(ret);