diff --git a/InMemoryImpl/.gitignore b/InMemoryImpl/.gitignore new file mode 100644 index 00000000..ae3c1726 --- /dev/null +++ b/InMemoryImpl/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/InMemoryImpl/src/com/attribute/AttributeKeyValueMap.java b/InMemoryImpl/src/com/attribute/AttributeKeyValueMap.java new file mode 100644 index 00000000..42e7818f --- /dev/null +++ b/InMemoryImpl/src/com/attribute/AttributeKeyValueMap.java @@ -0,0 +1,183 @@ +package com.attribute; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; + +import com.attribute.interfaces.IAttributeKeyValueMap; +import com.exceptions.DataFormatIncorrectException; + +// TODO: Auto-generated Javadoc +/** + * The Class AttributeKeyValueMap. + * + * @param the generic type + * @param the generic type + * @param the generic type + */ +public abstract class AttributeKeyValueMap implements IAttributeKeyValueMap +{ + + /** The key value map. */ + private HashMap keyValueMap; + + /** The value key map. */ + private HashMap> valueKeyMap; + + /** The t value map. */ + private HashMap tValueMap; + + + + /** + * Instantiates a new attribute key value map. + */ + public AttributeKeyValueMap() { + this.keyValueMap = new HashMap<>(); + this.valueKeyMap = new HashMap>(); + this.tValueMap = new HashMap(); + } + + /** + * Gets the value as T. + * + * @param value the value + * @return the value as T + * @throws DataFormatIncorrectException the data format incorrect exception + */ + protected abstract T getValueAsT(Value value) throws DataFormatIncorrectException; + + /** + * Checks if is valid type. + * + * @param value the value + * @return true, if is valid type + */ + @Override + public abstract boolean isValidType(Value value); + + /** + * Gets the. + * + * @param key the key + * @return the value + */ + @Override + public Value get(Key key) { + if (!keyValueMap.containsKey(key)) { + + } + return this.getValue(this.keyValueMap.get(key)); + } + + /** + * Put. + * + * @param key the key + * @param value the value + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + public void put(Key key, Value value) throws DataFormatIncorrectException { + if (!isValidType(value)) { + throw new DataFormatIncorrectException(); + } + + if (this.keyValueMap.containsKey(key)) { + this.valueKeyMap.remove(this.keyValueMap.get(key)); + } + this.keyValueMap.put(key, this.getValueAsT(value)); + + if (!this.valueKeyMap.containsKey(this.getValueAsT(value))) { + this.valueKeyMap.put(this.getValueAsT(value), new HashSet()); + } + + this.valueKeyMap.get(this.getValueAsT(value)).add(key); + this.tValueMap.put(this.getValueAsT(value), value); + } + + /** + * Contains key. + * + * @param key the key + * @return true, if successful + */ + @Override + public boolean containsKey(Key key) { + if (this.keyValueMap.containsKey(key)) { + return true; + } + + return false; + } + + + + /** + * Gets the keys with values. + * + * @param value the value + * @return the keys with values + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + public List getkeysWithValues(Value value) throws DataFormatIncorrectException { + + if (!this.isValidType(value)) { + return new ArrayList(); + } + + List result = new ArrayList(); + + if (!this.valueKeyMap.containsKey(this.getValueAsT(value))) { + return new ArrayList(); + } + + Iterator it = this.valueKeyMap.get(getValueAsT(value)).iterator(); + + while (it.hasNext()) { + result.add(it.next()); + } + + return result; + } + + + + /** + * Delete key. + * + * @param key the key + */ + @Override + public void deleteKey(Key key) { + if (!this.keyValueMap.containsKey(key)) { + return; + } + + } + + + /** + * To string. + * + * @return the string + */ + @Override + public String toString() { + return "AttributeKeyValueMap [keyValueMap=" + keyValueMap + ", valueKeyMap=" + valueKeyMap + "]"; + } + + /** + * Gets the value. + * + * @param obj the obj + * @return the value + */ + protected Value getValue(T obj) { + return this.tValueMap.get(obj); + } + +} diff --git a/InMemoryImpl/src/com/attribute/ValueAttribute.java b/InMemoryImpl/src/com/attribute/ValueAttribute.java new file mode 100644 index 00000000..6b4419f5 --- /dev/null +++ b/InMemoryImpl/src/com/attribute/ValueAttribute.java @@ -0,0 +1,116 @@ +package com.attribute; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import com.attribute.interfaces.IValueAttribute; +import com.commonClasses.Pair; + +// TODO: Auto-generated Javadoc +/** + * The Class ValueAttribute. + * + * @param the generic type + * @param the generic type + */ +public class ValueAttribute implements IValueAttribute { + + /** The attribute key value map. */ + HashMap attributeKeyValueMap; + + /** + * Instantiates a new value attribute. + */ + public ValueAttribute() { + this.attributeKeyValueMap = new HashMap(); + } + + /** + * Instantiates a new value attribute. + * + * @param attributekeyValueList the attributekey value list + */ + public ValueAttribute(List> attributekeyValueList) { + this(); + Iterator> it = attributekeyValueList.listIterator(); + + while (it.hasNext()) { + Pair next = it.next(); + this.attributeKeyValueMap.put(next.getKey(), next.getValue()); + } + } + + /** + * Sets the attribute. + * + * @param key the key + * @param value the value + */ + @Override + public void setAttribute(Key key, Value value) { + this.attributeKeyValueMap.put(key, value); + + } + + /** + * Gets the attribute. + * + * @param key the key + * @return the attribute + */ + @Override + public Value getAttribute(Key key) { + return this.attributeKeyValueMap.get(key); + } + + /** + * Gets the attribute keys. + * + * @return the attribute keys + */ + @Override + public List getAttributeKeys() { + List attributeKeys = new ArrayList(); + + Iterator it = this.attributeKeyValueMap.keySet().iterator(); + + while (it.hasNext()) { + attributeKeys.add(it.next()); + } + + return attributeKeys; + } + + /** + * To string. + * + * @return the string + */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + Iterator it = this.attributeKeyValueMap.keySet().iterator(); + + while (it.hasNext()) { + Key next = it.next(); + + Value value = this.attributeKeyValueMap.get(next); + + sb.append(next); + sb.append(": "); + sb.append(value); + + if (it.hasNext()) { + sb.append(", "); + } + } + + return sb.toString(); + } + + + +} diff --git a/InMemoryImpl/src/com/attribute/interfaces/IAttributeKeyValueMap.java b/InMemoryImpl/src/com/attribute/interfaces/IAttributeKeyValueMap.java new file mode 100644 index 00000000..7380c205 --- /dev/null +++ b/InMemoryImpl/src/com/attribute/interfaces/IAttributeKeyValueMap.java @@ -0,0 +1,64 @@ +package com.attribute.interfaces; + +import java.util.List; + +import com.exceptions.DataFormatIncorrectException; + +// TODO: Auto-generated Javadoc +/** + * The Interface IAttributeKeyValueMap. + * + * @param the key type + * @param the value type + */ +public interface IAttributeKeyValueMap { + + /** + * Gets the. + * + * @param key the key + * @return the v + */ + V get(K key); + + /** + * Put. + * + * @param key the key + * @param value the value + * @throws DataFormatIncorrectException the data format incorrect exception + */ + void put(K key, V value) throws DataFormatIncorrectException; + + /** + * Contains key. + * + * @param key the key + * @return true, if successful + */ + boolean containsKey(K key); + + /** + * Checks if is valid type. + * + * @param value the value + * @return true, if is valid type + */ + boolean isValidType(V value); + + /** + * Gets the keys with values. + * + * @param value the value + * @return the keys with values + * @throws DataFormatIncorrectException the data format incorrect exception + */ + List getkeysWithValues(V value) throws DataFormatIncorrectException; + + /** + * Delete key. + * + * @param key the key + */ + void deleteKey(K key); + } diff --git a/InMemoryImpl/src/com/attribute/interfaces/IValueAttribute.java b/InMemoryImpl/src/com/attribute/interfaces/IValueAttribute.java new file mode 100644 index 00000000..adb01898 --- /dev/null +++ b/InMemoryImpl/src/com/attribute/interfaces/IValueAttribute.java @@ -0,0 +1,43 @@ +package com.attribute.interfaces; + +import java.util.List; + +// TODO: Auto-generated Javadoc +/** + * The Interface IValueAttribute. + * + * @param the key type + * @param the value type + */ +public interface IValueAttribute { + + /** + * To string. + * + * @return the string + */ + String toString(); + + /** + * Sets the attribute. + * + * @param key the key + * @param value the value + */ + void setAttribute(K key, V value); + + /** + * Gets the attribute. + * + * @param key the key + * @return the attribute + */ + V getAttribute(K key); + + /** + * Gets the attribute keys. + * + * @return the attribute keys + */ + List getAttributeKeys(); +} diff --git a/InMemoryImpl/src/com/attribute/typeClasses/BooleanAttributeKeyValueMap.java b/InMemoryImpl/src/com/attribute/typeClasses/BooleanAttributeKeyValueMap.java new file mode 100644 index 00000000..e4e568cb --- /dev/null +++ b/InMemoryImpl/src/com/attribute/typeClasses/BooleanAttributeKeyValueMap.java @@ -0,0 +1,48 @@ +package com.attribute.typeClasses; + +import com.attribute.AttributeKeyValueMap; +import com.exceptions.DataFormatIncorrectException; + +// TODO: Auto-generated Javadoc +/** + * The Class BooleanAttributeKeyValueMap. + * + * @param the generic type + * @param the generic type + */ +public class BooleanAttributeKeyValueMap extends AttributeKeyValueMap { + + /** + * Gets the value as T. + * + * @param value the value + * @return the value as T + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + protected Boolean getValueAsT(Value value) throws DataFormatIncorrectException { + try { + return Boolean.parseBoolean(value.toString()); + } catch (Exception e) { + throw new DataFormatIncorrectException(); + } + } + + /** + * Checks if is valid type. + * + * @param value the value + * @return true, if is valid type + */ + @Override + public boolean isValidType(Value value) { + try { + Boolean.parseBoolean(value.toString()); + + return true; + } catch (Exception e) { + return false; + } + } + +} diff --git a/InMemoryImpl/src/com/attribute/typeClasses/DoubleAttributeKeyValueMap.java b/InMemoryImpl/src/com/attribute/typeClasses/DoubleAttributeKeyValueMap.java new file mode 100644 index 00000000..c4e40001 --- /dev/null +++ b/InMemoryImpl/src/com/attribute/typeClasses/DoubleAttributeKeyValueMap.java @@ -0,0 +1,50 @@ +package com.attribute.typeClasses; + +import com.attribute.AttributeKeyValueMap; +import com.exceptions.DataFormatIncorrectException; + +// TODO: Auto-generated Javadoc +/** + * The Class DoubleAttributeKeyValueMap. + * + * @param the generic type + * @param the generic type + */ +public class DoubleAttributeKeyValueMap extends AttributeKeyValueMap { + + /** + * Gets the value as T. + * + * @param value the value + * @return the value as T + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + protected Double getValueAsT(Value value) throws DataFormatIncorrectException { + if (!this.isValidType(value)) { + throw new DataFormatIncorrectException(); + } + + return Double.parseDouble(value.toString()); + } + + /** + * Checks if is valid type. + * + * @param value the value + * @return true, if is valid type + */ + @Override + public boolean isValidType(Value value) { + try { + if (value.toString().indexOf('.') == -1) { + return false; + } + Double.parseDouble(value.toString()); + return true; + } catch (Exception e) { + return false; + } + } + +} diff --git a/InMemoryImpl/src/com/attribute/typeClasses/IntegerAttributeKeyValueMap.java b/InMemoryImpl/src/com/attribute/typeClasses/IntegerAttributeKeyValueMap.java new file mode 100644 index 00000000..4d178ec4 --- /dev/null +++ b/InMemoryImpl/src/com/attribute/typeClasses/IntegerAttributeKeyValueMap.java @@ -0,0 +1,51 @@ +package com.attribute.typeClasses; + + +import com.attribute.AttributeKeyValueMap; +import com.exceptions.DataFormatIncorrectException; + +// TODO: Auto-generated Javadoc +/** + * The Class IntegerAttributeKeyValueMap. + * + * @param the generic type + * @param the generic type + */ +public class IntegerAttributeKeyValueMap extends AttributeKeyValueMap { + + /** + * Gets the value as T. + * + * @param value the value + * @return the value as T + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + protected Integer getValueAsT(Value value) throws DataFormatIncorrectException { + + try { + return Integer.parseInt(value.toString()); + } catch (Exception e) { + throw new DataFormatIncorrectException(); + } + } + + /** + * Checks if is valid type. + * + * @param value the value + * @return true, if is valid type + */ + @Override + public boolean isValidType(Value value) { + try { + + Integer.parseInt(value.toString()); + + return true; + + } catch (Exception e) { + return false; + } + } +} diff --git a/InMemoryImpl/src/com/attribute/typeClasses/StringAttributekeyValueMap.java b/InMemoryImpl/src/com/attribute/typeClasses/StringAttributekeyValueMap.java new file mode 100644 index 00000000..aafe9e95 --- /dev/null +++ b/InMemoryImpl/src/com/attribute/typeClasses/StringAttributekeyValueMap.java @@ -0,0 +1,38 @@ +package com.attribute.typeClasses; + +import com.attribute.AttributeKeyValueMap; +import com.exceptions.DataFormatIncorrectException; + +// TODO: Auto-generated Javadoc +/** + * The Class StringAttributekeyValueMap. + * + * @param the generic type + * @param the generic type + */ +public class StringAttributekeyValueMap extends AttributeKeyValueMap { + + /** + * Gets the value as T. + * + * @param value the value + * @return the value as T + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + protected String getValueAsT(Value value) throws DataFormatIncorrectException { + return value.toString(); + } + + /** + * Checks if is valid type. + * + * @param value the value + * @return true, if is valid type + */ + @Override + public boolean isValidType(Value value) { + return true; + } + +} diff --git a/InMemoryImpl/src/com/commonClasses/Pair.java b/InMemoryImpl/src/com/commonClasses/Pair.java new file mode 100644 index 00000000..7fe5814c --- /dev/null +++ b/InMemoryImpl/src/com/commonClasses/Pair.java @@ -0,0 +1,48 @@ +package com.commonClasses; + +// TODO: Auto-generated Javadoc +/** + * The Class Pair. + * + * @param the key type + * @param the value type + */ +public class Pair { + + /** The key. */ + private K key; + + /** The value. */ + private V value; + + /** + * Instantiates a new pair. + * + * @param key the key + * @param value the value + */ + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + /** + * Gets the key. + * + * @return the key + */ + public K getKey() { + return key; + } + + /** + * Gets the value. + * + * @return the value + */ + public V getValue() { + return value; + } + + +} diff --git a/InMemoryImpl/src/com/enums/Operations.java b/InMemoryImpl/src/com/enums/Operations.java new file mode 100644 index 00000000..6a4e1c93 --- /dev/null +++ b/InMemoryImpl/src/com/enums/Operations.java @@ -0,0 +1,24 @@ +package com.enums; + +// TODO: Auto-generated Javadoc +/** + * The Enum Operations. + */ +public enum Operations { + + /** The put. */ + PUT, + + /** The get. */ + GET, + + /** The keys. */ + KEYS, + + /** The delete. */ + DELETE, + + /** The search. */ + SEARCH; + +} diff --git a/InMemoryImpl/src/com/exceptions/DataFormatIncorrectException.java b/InMemoryImpl/src/com/exceptions/DataFormatIncorrectException.java new file mode 100644 index 00000000..1c36891e --- /dev/null +++ b/InMemoryImpl/src/com/exceptions/DataFormatIncorrectException.java @@ -0,0 +1,25 @@ +package com.exceptions; + +// TODO: Auto-generated Javadoc +/** + * The Class DataFormatIncorrectException. + */ +public class DataFormatIncorrectException extends KeyValueStoreException { + + /** The Constant serialVersionUID. */ + private static final long serialVersionUID = 1L; + + /** + * Gets the message. + * + * @return the message + */ + @Override + public String getMessage() { + // TODO Auto-generated method stub + return "DATA TYPE INCORRECT"; + } + + + +} diff --git a/InMemoryImpl/src/com/exceptions/InputFormatIncorrectException.java b/InMemoryImpl/src/com/exceptions/InputFormatIncorrectException.java new file mode 100644 index 00000000..b35ac078 --- /dev/null +++ b/InMemoryImpl/src/com/exceptions/InputFormatIncorrectException.java @@ -0,0 +1,24 @@ +package com.exceptions; + +// TODO: Auto-generated Javadoc +/** + * The Class InputFormatIncorrectException. + */ +public class InputFormatIncorrectException extends KeyValueStoreException { + + + /** The Constant serialVersionUID. */ + private static final long serialVersionUID = 1L; + + /** + * Gets the message. + * + * @return the message + */ + @Override + public String getMessage() { + return "INPUT FORMAT INCORRECT"; + } + + +} diff --git a/InMemoryImpl/src/com/exceptions/KeyValueStoreException.java b/InMemoryImpl/src/com/exceptions/KeyValueStoreException.java new file mode 100644 index 00000000..5bcd7762 --- /dev/null +++ b/InMemoryImpl/src/com/exceptions/KeyValueStoreException.java @@ -0,0 +1,12 @@ +package com.exceptions; + +// TODO: Auto-generated Javadoc +/** + * The Class KeyValueStoreException. + */ +public class KeyValueStoreException extends Exception { + + /** The Constant serialVersionUID. */ + private static final long serialVersionUID = 1L; + +} diff --git a/InMemoryImpl/src/com/factories/AttributeKeyValueMapFactory.java b/InMemoryImpl/src/com/factories/AttributeKeyValueMapFactory.java new file mode 100644 index 00000000..669995f3 --- /dev/null +++ b/InMemoryImpl/src/com/factories/AttributeKeyValueMapFactory.java @@ -0,0 +1,51 @@ +package com.factories; + +import com.attribute.interfaces.IAttributeKeyValueMap; +import com.attribute.typeClasses.BooleanAttributeKeyValueMap; +import com.attribute.typeClasses.DoubleAttributeKeyValueMap; +import com.attribute.typeClasses.IntegerAttributeKeyValueMap; +import com.attribute.typeClasses.StringAttributekeyValueMap; +import com.factories.interfaces.IAttributeKeyValueMapFactory; + +// TODO: Auto-generated Javadoc +/** + * A factory for creating AttributeKeyValueMap objects. + */ +public class AttributeKeyValueMapFactory implements IAttributeKeyValueMapFactory { + + /** + * Creates a new AttributeKeyValueMap object. + * + * @param the generic type + * @param the generic type + * @param value the value + * @return the i attribute key value map< key, value> + */ + @Override + public IAttributeKeyValueMap createAttributeKeyValueMap(Value value) { + try { + Integer.parseInt(value.toString()); + + return new IntegerAttributeKeyValueMap(); + } catch (Exception e) { + // do nothing + } + + try { + Double.parseDouble(value.toString()); + + return new DoubleAttributeKeyValueMap(); + } catch (Exception e) { + // Do nothing + } + + try { + Boolean.parseBoolean(value.toString()); + + return new BooleanAttributeKeyValueMap(); + } catch (Exception e) { + return new StringAttributekeyValueMap(); + } + } + +} diff --git a/InMemoryImpl/src/com/factories/KeyValueMapFactory.java b/InMemoryImpl/src/com/factories/KeyValueMapFactory.java new file mode 100644 index 00000000..caed3962 --- /dev/null +++ b/InMemoryImpl/src/com/factories/KeyValueMapFactory.java @@ -0,0 +1,26 @@ +package com.factories; + +import com.factories.interfaces.IKeyValueMapFactory; +import com.map.KeyValueMap; +import com.map.interfaces.IKeyValueMap; + +// TODO: Auto-generated Javadoc +/** + * A factory for creating KeyValueMap objects. + */ +public class KeyValueMapFactory implements IKeyValueMapFactory { + + /** + * Gets the key value map. + * + * @param the generic type + * @param the generic type + * @param the generic type + * @return the key value map + */ + @Override + public IKeyValueMap getKeyValueMap() { + return new KeyValueMap(new AttributeKeyValueMapFactory()); + } + +} diff --git a/InMemoryImpl/src/com/factories/interfaces/IAttributeKeyValueMapFactory.java b/InMemoryImpl/src/com/factories/interfaces/IAttributeKeyValueMapFactory.java new file mode 100644 index 00000000..e4928871 --- /dev/null +++ b/InMemoryImpl/src/com/factories/interfaces/IAttributeKeyValueMapFactory.java @@ -0,0 +1,20 @@ +package com.factories.interfaces; + +import com.attribute.interfaces.IAttributeKeyValueMap; + +// TODO: Auto-generated Javadoc +/** + * A factory for creating IAttributeKeyValueMap objects. + */ +public interface IAttributeKeyValueMapFactory { + + /** + * Creates a new IAttributeKeyValueMap object. + * + * @param the key type + * @param the value type + * @param value the value + * @return the i attribute key value map< k, v> + */ + IAttributeKeyValueMap createAttributeKeyValueMap(V value); +} diff --git a/InMemoryImpl/src/com/factories/interfaces/IKeyValueMapFactory.java b/InMemoryImpl/src/com/factories/interfaces/IKeyValueMapFactory.java new file mode 100644 index 00000000..cde4dd7c --- /dev/null +++ b/InMemoryImpl/src/com/factories/interfaces/IKeyValueMapFactory.java @@ -0,0 +1,20 @@ +package com.factories.interfaces; + +import com.map.interfaces.IKeyValueMap; + +// TODO: Auto-generated Javadoc +/** + * A factory for creating IKeyValueMap objects. + */ +public interface IKeyValueMapFactory { + + /** + * Gets the key value map. + * + * @param the generic type + * @param the generic type + * @param the generic type + * @return the key value map + */ + IKeyValueMap getKeyValueMap(); +} diff --git a/InMemoryImpl/src/com/ioParser/InputFormatter.java b/InMemoryImpl/src/com/ioParser/InputFormatter.java new file mode 100644 index 00000000..ca839b50 --- /dev/null +++ b/InMemoryImpl/src/com/ioParser/InputFormatter.java @@ -0,0 +1,93 @@ +package com.ioParser; + +import java.util.ArrayList; +import java.util.List; + +import com.commonClasses.Pair; +import com.exceptions.InputFormatIncorrectException; +import com.exceptions.KeyValueStoreException; +import com.ioParser.interfaces.IInputFormatter; + +// TODO: Auto-generated Javadoc +/** + * The Class InputFormatter. + */ +public class InputFormatter implements IInputFormatter { + + /** + * Parses the put operation input. + * + * @param parts the parts + * @return the pair + * @throws KeyValueStoreException the key value store exception + */ + @Override + public Pair>> parsePutOperationInput(String[] parts) throws KeyValueStoreException { + if (parts.length < 2 || parts.length % 2 != 0) { + throw new InputFormatIncorrectException(); + } + + String key = parts[1]; + + List> attributeValue = new ArrayList<>(); + + int i = 2; + + while (i < parts.length - 1) { + String att = parts[i]; + + String val = parts[i + 1]; + + attributeValue.add(new Pair(att, val)); + + i += 2; + } + + return new Pair>>(key, attributeValue); + } + + /** + * Parses the search operation input. + * + * @param parts the parts + * @return the pair + * @throws KeyValueStoreException the key value store exception + */ + @Override + public Pair parseSearchOperationInput(String[] parts) throws KeyValueStoreException { + if (parts.length < 3) { + throw new InputFormatIncorrectException(); + } + + return new Pair(parts[1], parts[2]); + } + + /** + * Parses the get operation input. + * + * @param parts the parts + * @return the string + * @throws KeyValueStoreException the key value store exception + */ + @Override + public String parseGetOperationInput(String[] parts) throws KeyValueStoreException { + if (parts.length < 2) { + throw new InputFormatIncorrectException(); + } + + return parts[1]; + } + + /** + * Parses the delete operation input. + * + * @param parts the parts + * @return the string + * @throws KeyValueStoreException the key value store exception + */ + @Override + public String parseDeleteOperationInput(String[] parts) throws KeyValueStoreException { + return this.parseGetOperationInput(parts); + } + +} diff --git a/InMemoryImpl/src/com/ioParser/ResultFormatter.java b/InMemoryImpl/src/com/ioParser/ResultFormatter.java new file mode 100644 index 00000000..aaa9f3a8 --- /dev/null +++ b/InMemoryImpl/src/com/ioParser/ResultFormatter.java @@ -0,0 +1,57 @@ +package com.ioParser; + +import java.util.List; + +import com.attribute.interfaces.IValueAttribute; +import com.ioParser.interfaces.IResultFormatter; + +// TODO: Auto-generated Javadoc +/** + * The Class ResultFormatter. + */ +public class ResultFormatter implements IResultFormatter { + + /** + * Gets the result string. + * + * @param valueAttribute the value attribute + * @return the result string + */ + @Override + public String getResultString(IValueAttribute valueAttribute) { + if (valueAttribute == null) { + return ""; + } + return valueAttribute.toString(); + } + + /** + * Gets the result string. + * + * @param keys the keys + * @return the result string + */ + @Override + public String getResultString(List keys) { + if (keys == null || keys.size() == 0) { + return ""; + } + + StringBuilder sb = new StringBuilder(); + + sb.append(keys.get(0)); + + int i = 1; + + while (i < keys.size()) { + sb.append(","); + + sb.append(keys.get(i)); + + i++; + } + + return sb.toString(); + } + +} diff --git a/InMemoryImpl/src/com/ioParser/interfaces/IInputFormatter.java b/InMemoryImpl/src/com/ioParser/interfaces/IInputFormatter.java new file mode 100644 index 00000000..89dbe154 --- /dev/null +++ b/InMemoryImpl/src/com/ioParser/interfaces/IInputFormatter.java @@ -0,0 +1,53 @@ +package com.ioParser.interfaces; + +import java.util.List; + +import com.commonClasses.Pair; +import com.exceptions.KeyValueStoreException; + +// TODO: Auto-generated Javadoc +/** + * The Interface IInputFormatter. + * + * @param the generic type + * @param the generic type + * @param the generic type + */ +public interface IInputFormatter { + + /** + * Parses the put operation input. + * + * @param input the input + * @return the pair + * @throws KeyValueStoreException the key value store exception + */ + Pair>> parsePutOperationInput(String[] input) throws KeyValueStoreException; + + /** + * Parses the search operation input. + * + * @param input the input + * @return the pair + * @throws KeyValueStoreException the key value store exception + */ + Pair parseSearchOperationInput(String[] input) throws KeyValueStoreException; + + /** + * Parses the get operation input. + * + * @param input the input + * @return the key + * @throws KeyValueStoreException the key value store exception + */ + Key parseGetOperationInput(String[] input) throws KeyValueStoreException; + + /** + * Parses the delete operation input. + * + * @param input the input + * @return the key + * @throws KeyValueStoreException the key value store exception + */ + Key parseDeleteOperationInput(String[] input) throws KeyValueStoreException; +} diff --git a/InMemoryImpl/src/com/ioParser/interfaces/IResultFormatter.java b/InMemoryImpl/src/com/ioParser/interfaces/IResultFormatter.java new file mode 100644 index 00000000..fd2ed541 --- /dev/null +++ b/InMemoryImpl/src/com/ioParser/interfaces/IResultFormatter.java @@ -0,0 +1,32 @@ +package com.ioParser.interfaces; + +import java.util.List; + +import com.attribute.interfaces.IValueAttribute; + +// TODO: Auto-generated Javadoc +/** + * The Interface IResultFormatter. + * + * @param the generic type + * @param the generic type + * @param the generic type + */ +public interface IResultFormatter { + + /** + * Gets the result string. + * + * @param valueAttribute the value attribute + * @return the result string + */ + String getResultString(IValueAttribute valueAttribute); + + /** + * Gets the result string. + * + * @param keys the keys + * @return the result string + */ + String getResultString(List keys); +} diff --git a/InMemoryImpl/src/com/keyvalueStore/KeyValueStore.java b/InMemoryImpl/src/com/keyvalueStore/KeyValueStore.java new file mode 100644 index 00000000..a27a7d03 --- /dev/null +++ b/InMemoryImpl/src/com/keyvalueStore/KeyValueStore.java @@ -0,0 +1,99 @@ +package com.keyvalueStore; + +import java.util.List; + +import com.attribute.ValueAttribute; +import com.attribute.interfaces.IValueAttribute; +import com.commonClasses.Pair; +import com.exceptions.DataFormatIncorrectException; +import com.factories.interfaces.IKeyValueMapFactory; +import com.keyvalueStore.interfaces.IKeyValueStore; +import com.map.interfaces.IKeyValueMap; + +// TODO: Auto-generated Javadoc +/** + * The Class KeyValueStore. + * + * @param the generic type + * @param the generic type + * @param the generic type + */ +public class KeyValueStore implements IKeyValueStore { + + /** The key value map. */ + private IKeyValueMap keyValueMap; + + /** The key value map factory. */ + private IKeyValueMapFactory keyValueMapFactory; + + /** + * Instantiates a new key value store. + * + * @param keyValueMapFactory the key value map factory + */ + public KeyValueStore(IKeyValueMapFactory keyValueMapFactory) { + this.keyValueMapFactory = keyValueMapFactory; + + this.keyValueMap = this.keyValueMapFactory.getKeyValueMap(); + } + + /** + * Gets the. + * + * @param key the key + * @return the i value attribute + */ + @Override + public IValueAttribute get(Key key) { + return this.keyValueMap.get(key); + } + + /** + * Search. + * + * @param attributeKey the attribute key + * @param attributeValue the attribute value + * @return the list + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + public List search(AttributeKey attributeKey, AttributeValue attributeValue) throws DataFormatIncorrectException { + // TODO Auto-generated method stub + return this.keyValueMap.getKeyContainingAttributeKeyValuePair(attributeKey, attributeValue); + } + + /** + * Put. + * + * @param key the key + * @param listOfAttributesPair the list of attributes pair + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + public void put(Key key, List> listOfAttributesPair) throws DataFormatIncorrectException { + this.keyValueMap.put(key, new ValueAttribute(listOfAttributesPair)); + } + + /** + * Delete. + * + * @param key the key + */ + @Override + public void delete(Key key) { + this.keyValueMap.deleteKey(key); + + } + + /** + * Keys. + * + * @return the list + */ + @Override + public List keys() { + return this.keyValueMap.getKeys(); + } + + +} diff --git a/InMemoryImpl/src/com/keyvalueStore/KeyValueStoreInterface.java b/InMemoryImpl/src/com/keyvalueStore/KeyValueStoreInterface.java new file mode 100644 index 00000000..2712c87a --- /dev/null +++ b/InMemoryImpl/src/com/keyvalueStore/KeyValueStoreInterface.java @@ -0,0 +1,192 @@ +package com.keyvalueStore; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import com.commonClasses.Pair; +import com.enums.Operations; +import com.exceptions.InputFormatIncorrectException; +import com.exceptions.KeyValueStoreException; +import com.factories.KeyValueMapFactory; +import com.ioParser.InputFormatter; +import com.ioParser.ResultFormatter; +import com.ioParser.interfaces.IInputFormatter; +import com.ioParser.interfaces.IResultFormatter; +import com.keyvalueStore.interfaces.IKeyValueStore; +import com.keyvalueStore.interfaces.IKeyValueStoreInterface; + +// TODO: Auto-generated Javadoc +/** + * The Class KeyValueStoreInterface. + */ +public class KeyValueStoreInterface implements IKeyValueStoreInterface { + + /** The key value store. */ + private IKeyValueStore keyValueStore; + + /** The result formatter. */ + private IResultFormatter resultFormatter; + + /** The input formatter. */ + private IInputFormatter inputFormatter; + + + /** + * Instantiates a new key value store interface. + */ + public KeyValueStoreInterface() { + this.keyValueStore = new KeyValueStore(new KeyValueMapFactory()); + this.inputFormatter = new InputFormatter(); + + this.resultFormatter = new ResultFormatter(); + } + + + /** + * Return result. + * + * @param input the input + * @return the list + */ + @Override + public List returnResult(List input) { + List result = new ArrayList(); + + Iterator it = input.listIterator(); + + while (it.hasNext()) { + try { + result.add(this.processInput(it.next())); + } catch (KeyValueStoreException e) { + result.add(e.getMessage()); + } + } + + return result; + } + + /** + * Process input. + * + * @param inputString the input string + * @return the string + * @throws KeyValueStoreException the key value store exception + */ + private String processInput(String inputString) throws KeyValueStoreException { + String[] parts = inputString.split(" "); + + if (parts.length == 0) { + throw new InputFormatIncorrectException(); + } + + try { + Operations operation = Operations.valueOf(parts[0].toUpperCase()); + + switch (operation) { + case PUT: + return this.parseAndExecutePUTOperation(parts); + case DELETE: + return this.parseAndExecuteDELETEOperation(parts); + case GET: + + return this.parseAndExecuteGETOperation(parts); + case KEYS: + return this.parseAndExecuteKEYSOperation(); + case SEARCH: + return this.parseAndExecuteSEARCHOperation(parts); + default: + return ""; + + } + } catch(KeyValueStoreException dataException) { + throw dataException; + } catch (Exception e) { + e.printStackTrace(); + throw new InputFormatIncorrectException(); + } + + + } + + + /** + * Parses the and execute KEYS operation. + * + * @return the string + * @throws KeyValueStoreException the key value store exception + */ + private String parseAndExecuteKEYSOperation() throws KeyValueStoreException { + + return this.resultFormatter.getResultString(this.keyValueStore.keys()); + } + + + /** + * Parses the and execute GET operation. + * + * @param parts the parts + * @return the string + * @throws KeyValueStoreException the key value store exception + */ + private String parseAndExecuteGETOperation(String[] parts) throws KeyValueStoreException { + + String input = this.inputFormatter.parseGetOperationInput(parts); + + + String result = this.resultFormatter.getResultString(this.keyValueStore.get(this.inputFormatter.parseGetOperationInput(parts))); + + if (result.equals("")) { + return "No entry found for " + input; + } + + return result; + } + + + /** + * Parses the and execute DELETE operation. + * + * @param parts the parts + * @return the string + * @throws KeyValueStoreException the key value store exception + */ + private String parseAndExecuteDELETEOperation(String[] parts) throws KeyValueStoreException { + + + this.keyValueStore.delete(this.inputFormatter.parseDeleteOperationInput(parts)); + + return null; + } + + + /** + * Parses the and execute PUT operation. + * + * @param parts the parts + * @return the string + * @throws KeyValueStoreException the key value store exception + */ + private String parseAndExecutePUTOperation(String[] parts) throws KeyValueStoreException { + Pair>> input = this.inputFormatter.parsePutOperationInput(parts); + + this.keyValueStore.put(input.getKey(), input.getValue()); + + return ""; + } + + /** + * Parses the and execute SEARCH operation. + * + * @param parts the parts + * @return the string + * @throws KeyValueStoreException the key value store exception + */ + private String parseAndExecuteSEARCHOperation(String[] parts) throws KeyValueStoreException { + Pair input = this.inputFormatter.parseSearchOperationInput(parts); + List result = this.keyValueStore.search(input.getKey(), input.getValue()); + + return this.resultFormatter.getResultString(result); + } + +} diff --git a/InMemoryImpl/src/com/keyvalueStore/interfaces/IKeyValueStore.java b/InMemoryImpl/src/com/keyvalueStore/interfaces/IKeyValueStore.java new file mode 100644 index 00000000..2da4ef69 --- /dev/null +++ b/InMemoryImpl/src/com/keyvalueStore/interfaces/IKeyValueStore.java @@ -0,0 +1,60 @@ +package com.keyvalueStore.interfaces; + +import java.util.List; + +import com.attribute.interfaces.IValueAttribute; +import com.commonClasses.Pair; +import com.exceptions.DataFormatIncorrectException; + + +// TODO: Auto-generated Javadoc +/** + * The Interface IKeyValueStore. + * + * @param the generic type + * @param the generic type + * @param the generic type + */ +public interface IKeyValueStore { + + /** + * Gets the. + * + * @param key the key + * @return the i value attribute + */ + IValueAttribute get(Key key); + + /** + * Search. + * + * @param attributeKey the attribute key + * @param attributeValue the attribute value + * @return the list + * @throws DataFormatIncorrectException the data format incorrect exception + */ + List search(AttributeKey attributeKey, AttributeValue attributeValue) throws DataFormatIncorrectException; + + /** + * Put. + * + * @param key the key + * @param listOfAttributesPair the list of attributes pair + * @throws DataFormatIncorrectException the data format incorrect exception + */ + void put(Key key, List> listOfAttributesPair) throws DataFormatIncorrectException; + + /** + * Delete. + * + * @param key the key + */ + void delete(Key key); + + /** + * Keys. + * + * @return the list + */ + List keys(); +} diff --git a/InMemoryImpl/src/com/keyvalueStore/interfaces/IKeyValueStoreInterface.java b/InMemoryImpl/src/com/keyvalueStore/interfaces/IKeyValueStoreInterface.java new file mode 100644 index 00000000..c0e757ea --- /dev/null +++ b/InMemoryImpl/src/com/keyvalueStore/interfaces/IKeyValueStoreInterface.java @@ -0,0 +1,19 @@ +package com.keyvalueStore.interfaces; + +import java.util.List; + +// TODO: Auto-generated Javadoc +/** + * The Interface IKeyValueStoreInterface. + */ +public interface IKeyValueStoreInterface { + + /** + * Return result. + * + * @param input the input + * @return the list + */ + List returnResult(List input); + +} diff --git a/InMemoryImpl/src/com/main/InterfaceMain.java b/InMemoryImpl/src/com/main/InterfaceMain.java new file mode 100644 index 00000000..3271475e --- /dev/null +++ b/InMemoryImpl/src/com/main/InterfaceMain.java @@ -0,0 +1,60 @@ +package com.main; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import com.keyvalueStore.KeyValueStoreInterface; +import com.keyvalueStore.interfaces.IKeyValueStoreInterface; + +// TODO: Auto-generated Javadoc +/** + * The Class InterfaceMain. + */ +public class InterfaceMain { + + /** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args) { + IKeyValueStoreInterface keyValueStoreInterface = new KeyValueStoreInterface(); + + Scanner sc = new Scanner(System.in); + + List input = new ArrayList(); + + + while (true) { + String next = sc.nextLine(); + + if (next.equals("EXIT")) { + break; + } + + input.add(next); + } + + List result = keyValueStoreInterface.returnResult(input); + +// Stream.of(result).forEach(s -> { +// System.out.println(s); +// }); +// +// System.out.println(result); + + int i = 0; + + while (i < result.size()) { + if (result.get(i) != null && !result.get(i).equals("")) { + System.out.println(result.get(i)); + } + + i++; + } + + sc.close(); + } + +} diff --git a/InMemoryImpl/src/com/main/Main.java b/InMemoryImpl/src/com/main/Main.java new file mode 100644 index 00000000..a5b2971f --- /dev/null +++ b/InMemoryImpl/src/com/main/Main.java @@ -0,0 +1,51 @@ +package com.main; + +import java.util.Arrays; + +import com.commonClasses.Pair; +import com.exceptions.DataFormatIncorrectException; +import com.factories.AttributeKeyValueMapFactory; +import com.factories.KeyValueMapFactory; +import com.keyvalueStore.KeyValueStore; +import com.keyvalueStore.interfaces.IKeyValueStore; +import com.map.KeyValueMap; + +// TODO: Auto-generated Javadoc +/** + * The Class Main. + */ +public class Main { + + /** + * The main method. + * + * @param args the arguments + * @throws DataFormatIncorrectException the data format incorrect exception + */ + public static void main(String[] args) throws DataFormatIncorrectException { + IKeyValueStore keyValueStore = new KeyValueStore<>(new KeyValueMapFactory()); + + keyValueStore.put("test1", Arrays.asList(new Pair("att1", "123.0"))); + + System.out.println(keyValueStore.get("test1")); + + System.out.println(keyValueStore.search("att1", "1231")); + + System.out.println(keyValueStore.keys()); + + keyValueStore.delete("test1"); + + System.out.println(keyValueStore.get("test1")); + + keyValueStore.put("test1", Arrays.asList(new Pair("att1", "123"))); + + keyValueStore.put("test2", Arrays.asList(new Pair("att1", "1232"))); + + System.out.println(keyValueStore.get("test1")); + + System.out.println(keyValueStore.search("att1", "1231")); + + System.out.println(keyValueStore.keys()); + } + +} diff --git a/InMemoryImpl/src/com/map/KeyValueMap.java b/InMemoryImpl/src/com/map/KeyValueMap.java new file mode 100644 index 00000000..e25ee2b5 --- /dev/null +++ b/InMemoryImpl/src/com/map/KeyValueMap.java @@ -0,0 +1,171 @@ +package com.map; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import com.attribute.ValueAttribute; +import com.attribute.interfaces.IAttributeKeyValueMap; +import com.attribute.interfaces.IValueAttribute; +import com.exceptions.DataFormatIncorrectException; +import com.factories.interfaces.IAttributeKeyValueMapFactory; +import com.map.interfaces.IKeyValueMap; + +// TODO: Auto-generated Javadoc +/** + * The Class KeyValueMap. + * + * @param the generic type + * @param the generic type + * @param the generic type + */ +public class KeyValueMap implements IKeyValueMap { + + /** The attribute map. */ + private HashMap> attributeMap; + + /** The key attribute map. */ + private HashMap> keyAttributeMap; + + /** The attribute key value map factory. */ + private IAttributeKeyValueMapFactory attributeKeyValueMapFactory; + + + + /** + * Instantiates a new key value map. + * + * @param attributeKeyValueMapFactory the attribute key value map factory + */ + public KeyValueMap(IAttributeKeyValueMapFactory attributeKeyValueMapFactory) { + this.attributeKeyValueMapFactory = attributeKeyValueMapFactory; + this.attributeMap = new HashMap>(); + + this.keyAttributeMap = new HashMap>(); + } + + /** + * Gets the. + * + * @param key the key + * @return the i value attribute + */ + @Override + public IValueAttribute get(Key key) { + IValueAttribute value = new ValueAttribute(); + + if (!this.keyAttributeMap.containsKey(key)) { + return null; + } + + List attributeList = this.keyAttributeMap.get(key); + + Iterator it = attributeList.listIterator(); + + while (it.hasNext()) { + AttributeKey next = it.next(); + + value.setAttribute(next, this.attributeMap.get(next).get(key)); + } + + return value; + } + + /** + * Put. + * + * @param key the key + * @param value the value + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + public void put(Key key, IValueAttribute value) throws DataFormatIncorrectException { + if (this.keyAttributeMap.containsKey(key)) { + this.deleteKey(key); + } + + List attributeList = new ArrayList(); + + Iterator it = value.getAttributeKeys().listIterator(); + + while (it.hasNext()) { + AttributeKey attkey = it.next(); + + AttributeValue attValue = value.getAttribute(attkey); + + if (!attributeMap.containsKey(attkey)) { + this.attributeMap.put(attkey, attributeKeyValueMapFactory.createAttributeKeyValueMap(attValue)); + } + + this.attributeMap.get(attkey).put(key, attValue); + + attributeList.add(attkey); + } + + this.keyAttributeMap.put(key, attributeList); + + } + + /** + * Gets the key containing attribute key value pair. + * + * @param attribute the attribute + * @param value the value + * @return the key containing attribute key value pair + * @throws DataFormatIncorrectException the data format incorrect exception + */ + @Override + public List getKeyContainingAttributeKeyValuePair(AttributeKey attribute, AttributeValue value) throws DataFormatIncorrectException { + + if (!this.attributeMap.containsKey(attribute)) { + return new ArrayList(); + } + return this.attributeMap.get(attribute).getkeysWithValues(value); + } + + /** + * Gets the keys. + * + * @return the keys + */ + @Override + public List getKeys() { + List keys = new ArrayList(); + + Iterator it = this.keyAttributeMap.keySet().iterator(); + + while (it.hasNext()) { + keys.add(it.next()); + } + + return keys; + } + + /** + * Delete key. + * + * @param key the key + */ + @Override + public void deleteKey(Key key) { + if (!this.keyAttributeMap.containsKey(key)) { + return; + } + + Iterator it = this.keyAttributeMap.get(key).listIterator(); + + while (it.hasNext()) { + AttributeKey attribute = it.next(); + + + this.attributeMap.get(attribute).deleteKey(key); + + } + + this.keyAttributeMap.remove(key); + + return; + } + +} diff --git a/InMemoryImpl/src/com/map/interfaces/IKeyValueMap.java b/InMemoryImpl/src/com/map/interfaces/IKeyValueMap.java new file mode 100644 index 00000000..deab708b --- /dev/null +++ b/InMemoryImpl/src/com/map/interfaces/IKeyValueMap.java @@ -0,0 +1,59 @@ +package com.map.interfaces; + +import java.util.List; + +import com.attribute.interfaces.IValueAttribute; +import com.exceptions.DataFormatIncorrectException; + +// TODO: Auto-generated Javadoc +/** + * The Interface IKeyValueMap. + * + * @param the generic type + * @param the generic type + * @param the generic type + */ +public interface IKeyValueMap { + + /** + * Gets the. + * + * @param key the key + * @return the i value attribute + */ + IValueAttribute get(Key key); + + /** + * Put. + * + * @param key the key + * @param value the value + * @throws DataFormatIncorrectException the data format incorrect exception + */ + void put(Key key, IValueAttribute value) throws DataFormatIncorrectException; + + /** + * Gets the key containing attribute key value pair. + * + * @param attribute the attribute + * @param value the value + * @return the key containing attribute key value pair + * @throws DataFormatIncorrectException the data format incorrect exception + */ + List getKeyContainingAttributeKeyValuePair(AttributeKey attribute, AttributeValue value) throws DataFormatIncorrectException; + + /** + * Gets the keys. + * + * @return the keys + */ + List getKeys(); + + /** + * Delete key. + * + * @param key the key + */ + void deleteKey(Key key); + +} diff --git a/InMemoryImpl/src/module-info.java b/InMemoryImpl/src/module-info.java new file mode 100644 index 00000000..2913a884 --- /dev/null +++ b/InMemoryImpl/src/module-info.java @@ -0,0 +1,9 @@ +/** + * + */ +/** + * + */ +module InMemoryKeyvalueStore { + +} \ No newline at end of file