-
Notifications
You must be signed in to change notification settings - Fork 485
Ensure TreeMapMetaStore range reads return copies #5027
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
Changes from 2 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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
|
|
||
| import jakarta.annotation.Nonnull; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.TreeMap; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
@@ -63,8 +64,6 @@ public String buildKey(T value) { | |
| /** | ||
| * read a value in the slice, will return null if not found | ||
| * | ||
| * <p>TODO: return a copy of each object to avoid mutating the records | ||
| * | ||
| * @param key key for that value | ||
| */ | ||
| public T read(String key) { | ||
|
|
@@ -81,7 +80,7 @@ public T read(String key) { | |
| public List<T> readRange(String prefix) { | ||
| ensureReadTr(); | ||
| if (prefix.isEmpty()) { | ||
| return new ArrayList<>(this.slice.values()); | ||
| return copyValues(this.slice.values()); | ||
| } | ||
|
|
||
| // end of the key | ||
|
|
@@ -90,7 +89,26 @@ public List<T> readRange(String prefix) { | |
| + (char) (prefix.charAt(prefix.length() - 1) + 1); | ||
|
|
||
| // Get the sub-map with keys in the range [prefix, endKey) | ||
| return new ArrayList<>(slice.subMap(prefix, true, endKey, false).values()); | ||
| return copyValues(slice.subMap(prefix, true, endKey, false).values()); | ||
| } | ||
|
|
||
| private List<T> copyValues(Collection<T> values) { | ||
| List<T> copied = new ArrayList<>(values.size()); | ||
| for (T value : values) { | ||
| copied.add(this.copyRecord.apply(value)); | ||
| } | ||
| return copied; | ||
| } | ||
|
|
||
| private List<String> copyKeysInRange(String prefix) { | ||
| if (prefix.isEmpty()) { | ||
| return new ArrayList<>(this.slice.keySet()); | ||
| } | ||
|
|
||
| String endKey = | ||
| prefix.substring(0, prefix.length() - 1) | ||
| + (char) (prefix.charAt(prefix.length() - 1) + 1); | ||
|
Contributor
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. nit: The endKey prefix-successor computation is now duplicated verbatim between
Contributor
Author
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. Thanks for your feedback! I just fixed it! |
||
| return new ArrayList<>(slice.subMap(prefix, true, endKey, false).keySet()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -132,9 +150,9 @@ public void delete(String key) { | |
| */ | ||
| public void deleteRange(String prefix) { | ||
| ensureReadWriteTr(); | ||
| List<T> elements = this.readRange(prefix); | ||
| for (T element : elements) { | ||
| this.delete(element); | ||
| List<String> keys = this.copyKeysInRange(prefix); | ||
| for (String key : keys) { | ||
| this.delete(key); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.core.persistence.transactional; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.List; | ||
| import org.apache.polaris.core.PolarisDefaultDiagServiceImpl; | ||
| import org.apache.polaris.core.PolarisDiagnostics; | ||
| import org.apache.polaris.core.entity.PolarisGrantRecord; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class TreeMapMetaStoreTest { | ||
|
|
||
| private final PolarisDiagnostics diagnostics = new PolarisDefaultDiagServiceImpl(); | ||
|
|
||
| @Test | ||
| void readRangeReturnsCopiesForEmptyPrefix() { | ||
| TreeMapMetaStore store = new TreeMapMetaStore(diagnostics); | ||
| PolarisGrantRecord grantRecord = new PolarisGrantRecord(1L, 2L, 3L, 4L, 5); | ||
|
|
||
| store.runActionInTransaction( | ||
| diagnostics, () -> store.getSliceGrantRecords().write(grantRecord)); | ||
|
|
||
| List<PolarisGrantRecord> range = | ||
| store.runInReadTransaction(diagnostics, () -> store.getSliceGrantRecords().readRange("")); | ||
| range.get(0).setPrivilegeCode(99); | ||
|
|
||
| PolarisGrantRecord stored = | ||
| store.runInReadTransaction( | ||
| diagnostics, () -> store.getSliceGrantRecords().readRange("").get(0)); | ||
|
|
||
| assertThat(stored.getPrivilegeCode()).isEqualTo(5); | ||
| } | ||
|
|
||
| @Test | ||
| void readRangeReturnsCopiesForNonEmptyPrefix() { | ||
| TreeMapMetaStore store = new TreeMapMetaStore(diagnostics); | ||
| PolarisGrantRecord grantRecord = new PolarisGrantRecord(1L, 2L, 3L, 4L, 5); | ||
| String prefix = store.buildPrefixKeyComposite(1L, 2L); | ||
|
|
||
| store.runActionInTransaction( | ||
| diagnostics, () -> store.getSliceGrantRecords().write(grantRecord)); | ||
|
|
||
| List<PolarisGrantRecord> range = | ||
| store.runInReadTransaction( | ||
| diagnostics, () -> store.getSliceGrantRecords().readRange(prefix)); | ||
| range.get(0).setPrivilegeCode(99); | ||
|
|
||
| PolarisGrantRecord stored = | ||
| store.runInReadTransaction( | ||
| diagnostics, () -> store.getSliceGrantRecords().readRange(prefix).get(0)); | ||
|
|
||
| assertThat(stored.getPrivilegeCode()).isEqualTo(5); | ||
| } | ||
| } |
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.
@iting0321 : WDYT?
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 updated
deleteRangeto avoid calling readRange now thatreadRangedefensively copies returned records.I also saw the dev discussion about eventually deprecating/removing
TreeMapMetaStore. So I kept this change narrow.Thanks you!