-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to insert list in ets:insert, ets:lookup refactor #1405
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,15 +252,9 @@ static void ets_delete_all_tables(struct Ets *ets, GlobalContext *global) | |
ets_delete_tables_internal(ets, true_pred, NULL, global); | ||
} | ||
|
||
EtsErrorCode ets_insert(term ref, term entry, Context *ctx) | ||
EtsErrorCode ets_table_insert(struct EtsTable *ets_table, term entry, Context *ctx) | ||
{ | ||
struct EtsTable *ets_table = term_is_atom(ref) ? ets_get_table_by_name(&ctx->global->ets, ref, TableAccessWrite) : ets_get_table_by_ref(&ctx->global->ets, term_to_ref_ticks(ref), TableAccessWrite); | ||
if (ets_table == NULL) { | ||
return EtsTableNotFound; | ||
} | ||
|
||
if (ets_table->access_type != EtsAccessPublic && ets_table->owner_process_id != ctx->process_id) { | ||
SMP_UNLOCK(ets_table); | ||
return EtsPermissionDenied; | ||
} | ||
|
||
|
@@ -271,39 +265,66 @@ EtsErrorCode ets_insert(term ref, term entry, Context *ctx) | |
|
||
Heap *heap = malloc(sizeof(Heap)); | ||
if (IS_NULL_PTR(heap)) { | ||
SMP_UNLOCK(ets_table); | ||
return EtsAllocationFailure; | ||
} | ||
size_t size = (size_t) memory_estimate_usage(entry); | ||
if (memory_init_heap(heap, size) != MEMORY_GC_OK) { | ||
free(heap); | ||
SMP_UNLOCK(ets_table); | ||
return EtsAllocationFailure; | ||
} | ||
|
||
term new_entry = memory_copy_term_tree(heap, entry); | ||
term key = term_get_tuple_element(new_entry, (int) ets_table->keypos); | ||
|
||
EtsErrorCode ret = EtsOk; | ||
EtsErrorCode result = EtsOk; | ||
EtsHashtableErrorCode res = ets_hashtable_insert(ets_table->hashtable, key, new_entry, EtsHashtableAllowOverwrite, heap, ctx->global); | ||
if (UNLIKELY(res != EtsHashtableOk)) { | ||
ret = EtsAllocationFailure; | ||
result = EtsAllocationFailure; | ||
} | ||
|
||
SMP_UNLOCK(ets_table); | ||
return result; | ||
} | ||
|
||
return ret; | ||
EtsErrorCode ets_table_insert_list(struct EtsTable *ets_table, term list, Context *ctx) | ||
{ | ||
while (term_is_nonempty_list(list)) { | ||
term tuple = term_get_list_head(list); | ||
if (!term_is_tuple(tuple) && term_get_tuple_arity(tuple) < 1) { | ||
return EtsBadEntry; | ||
} | ||
EtsErrorCode result = ets_table_insert(ets_table, tuple, ctx); | ||
if (result != EtsOk) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like an unlikely branch, so we can use |
||
AVM_ABORT(); // Abort because operation might not be atomic. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually don't do VM abort: calling AVM_ABORT() means that an unrecoverable happened, such as memory corruption, a bad internal bug and any other kind of situation that required an entire VM crash and reboot. |
||
} | ||
|
||
list = term_get_list_tail(list); | ||
} | ||
|
||
return EtsOk; | ||
} | ||
|
||
EtsErrorCode ets_lookup(term ref, term key, term *ret, Context *ctx) | ||
EtsErrorCode ets_insert(term ref, term entry, Context *ctx) | ||
{ | ||
struct EtsTable *ets_table = term_is_atom(ref) ? ets_get_table_by_name(&ctx->global->ets, ref, TableAccessRead) : ets_get_table_by_ref(&ctx->global->ets, term_to_ref_ticks(ref), TableAccessRead); | ||
struct EtsTable *ets_table = term_is_atom(ref) ? ets_get_table_by_name(&ctx->global->ets, ref, TableAccessWrite) : ets_get_table_by_ref(&ctx->global->ets, term_to_ref_ticks(ref), TableAccessWrite); | ||
if (ets_table == NULL) { | ||
return EtsTableNotFound; | ||
} | ||
EtsErrorCode result = EtsBadEntry; | ||
|
||
if (term_is_tuple(entry) && term_get_tuple_arity(entry) > 0) { | ||
result = ets_table_insert(ets_table, entry, ctx); | ||
} else if (term_is_list(entry)) { | ||
result = ets_table_insert_list(ets_table, entry, ctx); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My personal opinion: } else {
result = EtsBadEntry;
} Looks slightly more readable. |
||
|
||
SMP_UNLOCK(ets_table); | ||
|
||
return result; | ||
} | ||
|
||
EtsErrorCode ets_table_lookup(struct EtsTable *ets_table, term key, term *ret, Context *ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, let's mark with |
||
{ | ||
if (ets_table->access_type == EtsAccessPrivate && ets_table->owner_process_id != ctx->process_id) { | ||
SMP_UNLOCK(ets_table); | ||
return EtsPermissionDenied; | ||
} | ||
|
||
|
@@ -316,17 +337,28 @@ EtsErrorCode ets_lookup(term ref, term key, term *ret, Context *ctx) | |
size_t size = (size_t) memory_estimate_usage(res); | ||
// allocate [object] | ||
if (UNLIKELY(memory_ensure_free_opt(ctx, size + CONS_SIZE, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) { | ||
SMP_UNLOCK(ets_table); | ||
return EtsAllocationFailure; | ||
} | ||
term new_res = memory_copy_term_tree(&ctx->heap, res); | ||
*ret = term_list_prepend(new_res, term_nil(), &ctx->heap); | ||
} | ||
SMP_UNLOCK(ets_table); | ||
|
||
return EtsOk; | ||
} | ||
|
||
EtsErrorCode ets_lookup(term ref, term key, term *ret, Context *ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are in the middle of a refactor, it might be a good idea to rename |
||
{ | ||
struct EtsTable *ets_table = term_is_atom(ref) ? ets_get_table_by_name(&ctx->global->ets, ref, TableAccessRead) : ets_get_table_by_ref(&ctx->global->ets, term_to_ref_ticks(ref), TableAccessRead); | ||
if (ets_table == NULL) { | ||
return EtsTableNotFound; | ||
} | ||
|
||
EtsErrorCode result = ets_table_lookup(ets_table, key, ret, ctx); | ||
SMP_UNLOCK(ets_table); | ||
|
||
return result; | ||
} | ||
|
||
EtsErrorCode ets_lookup_element(term ref, term key, size_t pos, term *ret, Context *ctx) | ||
{ | ||
if (UNLIKELY(pos == 0)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ start() -> | |
ok = test_protected_access(), | ||
ok = test_public_access(), | ||
ok = test_lookup_element(), | ||
|
||
ok = test_insert_list(), | ||
0. | ||
|
||
test_basic() -> | ||
|
@@ -352,3 +352,10 @@ test_lookup_element() -> | |
expect_failure(fun() -> ets:lookup_element(Tid, foo, 3) end), | ||
expect_failure(fun() -> ets:lookup_element(Tid, foo, 0) end), | ||
ok. | ||
|
||
test_insert_list() -> | ||
Tid = ets:new(test_insert_list, []), | ||
true = ets:insert(Tid, [{foo, tapas}, {batat, batat}, {patat, patat}]), | ||
[{patat, patat}] = ets:lookup(Tid, patat), | ||
[{batat, batat}] = ets:lookup(Tid, batat), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is really important to test also different error situations, so we can spot bugs such as the one at line In addition let's also test special cases, such as: ets:insert(Tid, [])
ets:insert(Tid, [{foo, tapas} | {patat, patat}]) |
||
ok. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this line cannot work:
!term_is_tuple(tuple) && term_get_tuple_arity(tuple) < 1
. I think||
should fix this.