mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-16 17:21:08 +01:00
SubkeyLookup: Switch to batch inserting
This commit is contained in:
parent
d53d62a6e9
commit
1495baaf6e
9 changed files with 110 additions and 86 deletions
|
|
@ -4,20 +4,21 @@
|
|||
|
||||
package pgp.cert_d;
|
||||
|
||||
import pgp.certificate_store.SubkeyLookup;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import pgp.certificate_store.SubkeyLookup;
|
||||
|
||||
public class InMemorySubkeyLookup implements SubkeyLookup {
|
||||
|
||||
private static final Map<Long, Set<String>> subkeyMap = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public Set<String> getIdentifiersForSubkeyId(long subkeyId) {
|
||||
public Set<String> getCertificatesForSubkeyId(long subkeyId) {
|
||||
Set<String> identifiers = subkeyMap.get(subkeyId);
|
||||
if (identifiers == null) {
|
||||
return Collections.emptySet();
|
||||
|
|
@ -26,13 +27,15 @@ public class InMemorySubkeyLookup implements SubkeyLookup {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void storeIdentifierForSubkeyId(long subkeyId, String identifier) {
|
||||
Set<String> identifiers = subkeyMap.get(subkeyId);
|
||||
if (identifiers == null) {
|
||||
identifiers = new HashSet<>();
|
||||
subkeyMap.put(subkeyId, identifiers);
|
||||
public void storeCertificateSubkeyIds(String certificate, List<Long> subkeyIds) {
|
||||
for (long subkeyId : subkeyIds) {
|
||||
Set<String> certificates = subkeyMap.get(subkeyId);
|
||||
if (certificates == null) {
|
||||
certificates = new HashSet<>();
|
||||
subkeyMap.put(subkeyId, certificates);
|
||||
}
|
||||
certificates.add(certificate);
|
||||
}
|
||||
identifiers.add(identifier);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
|
|
|
|||
|
|
@ -55,33 +55,33 @@ public class SubkeyLookupTest {
|
|||
public void testInsertGet(SubkeyLookup subject) throws IOException {
|
||||
// Initially all null
|
||||
|
||||
assertTrue(subject.getIdentifiersForSubkeyId(123).isEmpty());
|
||||
assertTrue(subject.getIdentifiersForSubkeyId(1337).isEmpty());
|
||||
assertTrue(subject.getIdentifiersForSubkeyId(420).isEmpty());
|
||||
assertTrue(subject.getCertificatesForSubkeyId(123).isEmpty());
|
||||
assertTrue(subject.getCertificatesForSubkeyId(1337).isEmpty());
|
||||
assertTrue(subject.getCertificatesForSubkeyId(420).isEmpty());
|
||||
|
||||
// Store one val, others still null
|
||||
|
||||
subject.storeIdentifierForSubkeyId(123, "d1a66e1a23b182c9980f788cfbfcc82a015e7330");
|
||||
subject.storeCertificateSubkeyIds("d1a66e1a23b182c9980f788cfbfcc82a015e7330", Collections.singletonList(123L));
|
||||
|
||||
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getIdentifiersForSubkeyId(123));
|
||||
assertTrue(subject.getIdentifiersForSubkeyId(1337).isEmpty());
|
||||
assertTrue(subject.getIdentifiersForSubkeyId(420).isEmpty());
|
||||
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getCertificatesForSubkeyId(123));
|
||||
assertTrue(subject.getCertificatesForSubkeyId(1337).isEmpty());
|
||||
assertTrue(subject.getCertificatesForSubkeyId(420).isEmpty());
|
||||
|
||||
// Store other val, first stays intact
|
||||
|
||||
subject.storeIdentifierForSubkeyId(1337, "d1a66e1a23b182c9980f788cfbfcc82a015e7330");
|
||||
subject.storeIdentifierForSubkeyId(420, "d1a66e1a23b182c9980f788cfbfcc82a015e7330");
|
||||
subject.storeCertificateSubkeyIds("d1a66e1a23b182c9980f788cfbfcc82a015e7330", Collections.singletonList(1337L));
|
||||
subject.storeCertificateSubkeyIds("d1a66e1a23b182c9980f788cfbfcc82a015e7330", Collections.singletonList(420L));
|
||||
|
||||
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getIdentifiersForSubkeyId(123));
|
||||
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getIdentifiersForSubkeyId(1337));
|
||||
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getIdentifiersForSubkeyId(420));
|
||||
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getCertificatesForSubkeyId(123));
|
||||
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getCertificatesForSubkeyId(1337));
|
||||
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getCertificatesForSubkeyId(420));
|
||||
|
||||
// add additional entry for subkey
|
||||
|
||||
subject.storeIdentifierForSubkeyId(123, "eb85bb5fa33a75e15e944e63f231550c4f47e38e");
|
||||
subject.storeCertificateSubkeyIds("eb85bb5fa33a75e15e944e63f231550c4f47e38e", Collections.singletonList(123L));
|
||||
|
||||
assertEquals(
|
||||
new HashSet<>(Arrays.asList("eb85bb5fa33a75e15e944e63f231550c4f47e38e", "d1a66e1a23b182c9980f788cfbfcc82a015e7330")),
|
||||
subject.getIdentifiersForSubkeyId(123));
|
||||
subject.getCertificatesForSubkeyId(123));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue