Add NoOp configuration. Split classes into packages.

This commit is contained in:
Anatoly Sablin
2019-10-16 23:07:14 +03:00
parent 703044d06a
commit a0270c7d01
8 changed files with 85 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ package io.kamax.mxisd.hash;
import io.kamax.matrix.codec.MxSha256;
import io.kamax.mxisd.config.HashingConfig;
import io.kamax.mxisd.hash.storage.HashStorage;
import io.kamax.mxisd.lookup.ThreePidMapping;
import io.kamax.mxisd.lookup.provider.IThreePidProvider;
import org.apache.commons.lang3.RandomStringUtils;

View File

@@ -1,11 +1,18 @@
package io.kamax.mxisd.hash;
import io.kamax.mxisd.config.HashingConfig;
import io.kamax.mxisd.hash.rotation.HashRotationStrategy;
import io.kamax.mxisd.hash.rotation.NoOpRotationStrategy;
import io.kamax.mxisd.hash.rotation.RotationPerRequests;
import io.kamax.mxisd.hash.storage.EmptyStorage;
import io.kamax.mxisd.hash.storage.HashStorage;
import io.kamax.mxisd.hash.storage.InMemoryHashStorage;
import io.kamax.mxisd.lookup.provider.IThreePidProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class HashManager {
@@ -15,32 +22,43 @@ public class HashManager {
private HashRotationStrategy rotationStrategy;
private HashStorage hashStorage;
private HashingConfig config;
private AtomicBoolean configured = new AtomicBoolean(false);
public void init(HashingConfig config, List<? extends IThreePidProvider> providers) {
this.config = config;
initStorage();
hashEngine = new HashEngine(providers, getHashStorage(), config);
initRotationStrategy();
configured.set(true);
}
private void initStorage() {
switch (config.getHashStorageType()) {
case IN_MEMORY:
this.hashStorage = new InMemoryHashStorage();
break;
default:
throw new IllegalArgumentException("Unknown storage type: " + config.getHashStorageType());
if (config.isEnabled()) {
switch (config.getHashStorageType()) {
case IN_MEMORY:
this.hashStorage = new InMemoryHashStorage();
break;
default:
throw new IllegalArgumentException("Unknown storage type: " + config.getHashStorageType());
}
} else {
this.hashStorage = new EmptyStorage();
}
}
private void initRotationStrategy() {
switch (config.getRotationPolicy()) {
case PER_REQUESTS:
this.rotationStrategy = new RotationPerRequests();
break;
default:
throw new IllegalArgumentException("Unknown rotation type: " + config.getHashStorageType());
if (config.isEnabled()) {
switch (config.getRotationPolicy()) {
case PER_REQUESTS:
this.rotationStrategy = new RotationPerRequests();
break;
default:
throw new IllegalArgumentException("Unknown rotation type: " + config.getHashStorageType());
}
} else {
this.rotationStrategy = new NoOpRotationStrategy();
}
this.rotationStrategy.register(getHashEngine());
}

View File

@@ -1,4 +1,6 @@
package io.kamax.mxisd.hash;
package io.kamax.mxisd.hash.rotation;
import io.kamax.mxisd.hash.HashEngine;
public interface HashRotationStrategy {

View File

@@ -0,0 +1,23 @@
package io.kamax.mxisd.hash.rotation;
import io.kamax.mxisd.hash.HashEngine;
public class NoOpRotationStrategy implements HashRotationStrategy {
private HashEngine hashEngine;
@Override
public void register(HashEngine hashEngine) {
this.hashEngine = hashEngine;
}
@Override
public HashEngine getHashEngine() {
return hashEngine;
}
@Override
public void newRequest() {
// nothing to do
}
}

View File

@@ -1,4 +1,6 @@
package io.kamax.mxisd.hash;
package io.kamax.mxisd.hash.rotation;
import io.kamax.mxisd.hash.HashEngine;
import java.util.concurrent.atomic.AtomicInteger;

View File

@@ -0,0 +1,23 @@
package io.kamax.mxisd.hash.storage;
import io.kamax.mxisd.lookup.ThreePidMapping;
import java.util.Collections;
public class EmptyStorage implements HashStorage {
@Override
public Iterable<ThreePidMapping> find(Iterable<String> hashes) {
return Collections.emptyList();
}
@Override
public void add(ThreePidMapping pidMapping, String hash) {
}
@Override
public void clear() {
}
}

View File

@@ -1,6 +1,5 @@
package io.kamax.mxisd.hash;
package io.kamax.mxisd.hash.storage;
import io.kamax.matrix.ThreePid;
import io.kamax.mxisd.lookup.ThreePidMapping;
public interface HashStorage {

View File

@@ -1,4 +1,4 @@
package io.kamax.mxisd.hash;
package io.kamax.mxisd.hash.storage;
import io.kamax.mxisd.lookup.ThreePidMapping;