Add NoOp configuration. Split classes into packages.
This commit is contained in:
@@ -2,6 +2,7 @@ package io.kamax.mxisd.hash;
|
|||||||
|
|
||||||
import io.kamax.matrix.codec.MxSha256;
|
import io.kamax.matrix.codec.MxSha256;
|
||||||
import io.kamax.mxisd.config.HashingConfig;
|
import io.kamax.mxisd.config.HashingConfig;
|
||||||
|
import io.kamax.mxisd.hash.storage.HashStorage;
|
||||||
import io.kamax.mxisd.lookup.ThreePidMapping;
|
import io.kamax.mxisd.lookup.ThreePidMapping;
|
||||||
import io.kamax.mxisd.lookup.provider.IThreePidProvider;
|
import io.kamax.mxisd.lookup.provider.IThreePidProvider;
|
||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
package io.kamax.mxisd.hash;
|
package io.kamax.mxisd.hash;
|
||||||
|
|
||||||
import io.kamax.mxisd.config.HashingConfig;
|
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 io.kamax.mxisd.lookup.provider.IThreePidProvider;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public class HashManager {
|
public class HashManager {
|
||||||
|
|
||||||
@@ -15,15 +22,18 @@ public class HashManager {
|
|||||||
private HashRotationStrategy rotationStrategy;
|
private HashRotationStrategy rotationStrategy;
|
||||||
private HashStorage hashStorage;
|
private HashStorage hashStorage;
|
||||||
private HashingConfig config;
|
private HashingConfig config;
|
||||||
|
private AtomicBoolean configured = new AtomicBoolean(false);
|
||||||
|
|
||||||
public void init(HashingConfig config, List<? extends IThreePidProvider> providers) {
|
public void init(HashingConfig config, List<? extends IThreePidProvider> providers) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
initStorage();
|
initStorage();
|
||||||
hashEngine = new HashEngine(providers, getHashStorage(), config);
|
hashEngine = new HashEngine(providers, getHashStorage(), config);
|
||||||
initRotationStrategy();
|
initRotationStrategy();
|
||||||
|
configured.set(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initStorage() {
|
private void initStorage() {
|
||||||
|
if (config.isEnabled()) {
|
||||||
switch (config.getHashStorageType()) {
|
switch (config.getHashStorageType()) {
|
||||||
case IN_MEMORY:
|
case IN_MEMORY:
|
||||||
this.hashStorage = new InMemoryHashStorage();
|
this.hashStorage = new InMemoryHashStorage();
|
||||||
@@ -31,9 +41,13 @@ public class HashManager {
|
|||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unknown storage type: " + config.getHashStorageType());
|
throw new IllegalArgumentException("Unknown storage type: " + config.getHashStorageType());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this.hashStorage = new EmptyStorage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initRotationStrategy() {
|
private void initRotationStrategy() {
|
||||||
|
if (config.isEnabled()) {
|
||||||
switch (config.getRotationPolicy()) {
|
switch (config.getRotationPolicy()) {
|
||||||
case PER_REQUESTS:
|
case PER_REQUESTS:
|
||||||
this.rotationStrategy = new RotationPerRequests();
|
this.rotationStrategy = new RotationPerRequests();
|
||||||
@@ -41,6 +55,10 @@ public class HashManager {
|
|||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unknown rotation type: " + config.getHashStorageType());
|
throw new IllegalArgumentException("Unknown rotation type: " + config.getHashStorageType());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this.rotationStrategy = new NoOpRotationStrategy();
|
||||||
|
}
|
||||||
|
|
||||||
this.rotationStrategy.register(getHashEngine());
|
this.rotationStrategy.register(getHashEngine());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
package io.kamax.mxisd.hash;
|
package io.kamax.mxisd.hash.rotation;
|
||||||
|
|
||||||
|
import io.kamax.mxisd.hash.HashEngine;
|
||||||
|
|
||||||
public interface HashRotationStrategy {
|
public interface HashRotationStrategy {
|
||||||
|
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
23
src/main/java/io/kamax/mxisd/hash/storage/EmptyStorage.java
Normal file
23
src/main/java/io/kamax/mxisd/hash/storage/EmptyStorage.java
Normal 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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
import io.kamax.mxisd.lookup.ThreePidMapping;
|
||||||
|
|
||||||
public interface HashStorage {
|
public interface HashStorage {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package io.kamax.mxisd.hash;
|
package io.kamax.mxisd.hash.storage;
|
||||||
|
|
||||||
import io.kamax.mxisd.lookup.ThreePidMapping;
|
import io.kamax.mxisd.lookup.ThreePidMapping;
|
||||||
|
|
||||||
Reference in New Issue
Block a user