Skip to content

LLD for In Memor key-val store #8

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

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 84 additions & 43 deletions .idea/workspace.xml

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions data-structures/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.thealgorithm</groupId>
<artifactId>TheAlgorithm</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.thealgorithm</groupId>
<artifactId>data-structures</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.thealgorithm.miscelleneous;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author: Subham Santra
*/
public class GenerateParenthesis {

public List<String> generate(int c1, int c2, int c3) {
Set<String> result = new HashSet<>();
generate(c1, 0, 0, c2, 0, 0, c3, 0, 0, result, new StringBuilder());
return new ArrayList<>(result);
}

private void generate(
int c1,
int o1,
int e1,
int c2,
int o2,
int e2,
int c3,
int o3,
int e3,
Set<String> result,
StringBuilder comb) {

if (e1 > o1 || e2 > o2 || e3 > o3) {
return;
}

// if (o1 == c1 && o1 == e1 && o2 == c2 && o2 == e2 && o3 == c3 && o3 == e3) {
if (o1 == c1 && o2 == c2 && o3 == c3) {
// if (o1 == e1 && o2 == e2 && o3 == e3) {
result.add(comb.toString());
return;
}

if (o1 < c1) {
comb.append('(');
generate(c1, o1 + 1, e1, c2, o2, e2, c3, o3, e3, result, comb);
comb.deleteCharAt(comb.length() - 1);

comb.append(')');
generate(c1, o1, e1 + 1, c2, o2, e2, c3, o3, e3, result, comb);
comb.deleteCharAt(comb.length() - 1);
}

if (o2 < c2) {
comb.append('{');
generate(c1, o1, e1, c2, o2 + 1, e2, c3, o3, e3, result, comb);
comb.deleteCharAt(comb.length() - 1);

comb.append('}');
generate(c1, o1, e1, c2, o2, e2 + 1, c3, o3, e3, result, comb);
comb.deleteCharAt(comb.length() - 1);
}

if (o3 < c3) {
comb.append('[');
generate(c1, o1, e1, c2, o2, e2, c3, o3 + 1, e3, result, comb);
comb.deleteCharAt(comb.length() - 1);

comb.append(']');
generate(c1, o1 + 1, e1, c2, o2, e2, c3, o3, e3 + 1, result, comb);
comb.deleteCharAt(comb.length() - 1);
}
}

public static void main(String[] args) {
System.out.println(new GenerateParenthesis().generate(2, 2, 2));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithm.dp.miscelleneous;
package com.thealgorithm.miscelleneous;

/**
* @see <a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithm.dp.miscelleneous;
package com.thealgorithm.miscelleneous;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithm.dp.miscelleneous;
package com.thealgorithm.miscelleneous;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thealgorithm.dp.miscelleneous;
package com.thealgorithm.miscelleneous;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down
14 changes: 14 additions & 0 deletions low-level-design/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.thealgorithm</groupId>
<artifactId>TheAlgorithm</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.thealgorithm</groupId>
<artifactId>low-level-design</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.thealgorithm.caching;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
* @author: Subham Santra
*/
public class CachingLocalTest {
public static void main(String[] args) {
InMemoryKeyValStore keyValStore = InMemoryKeyValStore.create();
try (Scanner scanner = new Scanner(System.in)) {

do {
String line = scanner.nextLine();
StringTokenizer tokenizer = new StringTokenizer(line);

String var1 = tokenizer.nextToken();
String var2 = tokenizer.nextToken();
String var3 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "";
String var4 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "";

try {
if (var1.equalsIgnoreCase("put")) {
if (var4.isBlank()) {
keyValStore.put(var2, var3.getBytes(StandardCharsets.UTF_8), 60_000L);
} else {
keyValStore.put(var2, var3.getBytes(StandardCharsets.UTF_8), Long.parseLong(var4));
}
} else if (var1.equalsIgnoreCase("get")) {
System.out.println(new String(keyValStore.get(var2)));
} else if (var1.equalsIgnoreCase("rem")) {
keyValStore.rem(var2);
}
} catch (DataNotPresentException | DataExpiredException e) {
System.err.println(e.getMessage());
}
} while (scanner.hasNextLine());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.thealgorithm.caching;

/**
* @author: Subham Santra
*/
public class DataExpiredException extends Exception {
public DataExpiredException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.thealgorithm.caching;

/**
* @author: Subham Santra
*/
public class DataNotPresentException extends Exception {
public DataNotPresentException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.thealgorithm.caching;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.Value;

/**
* @author: Subham Santra
* @apiNote
* <li>$ put key val
* <li>$ put key val ttl_in_seconds
* <li>$ get key
* <li>$ rem key
*/
public class InMemoryKeyValStore {
@Value
private static class Entry {
byte[] val;
long created;
long ttl_millis;
}

private final Map<String, Entry> kvStore;

private InMemoryKeyValStore() {
kvStore = new ConcurrentHashMap<>();
}

public static InMemoryKeyValStore create() {
return new InMemoryKeyValStore();
}

void put(String key, byte[] val, long ttl) {
kvStore.put(key, new Entry(val, System.currentTimeMillis(), ttl));
}

byte[] get(String key) throws DataNotPresentException, DataExpiredException {
if (!kvStore.containsKey(key)) {
throw new DataNotPresentException("'key' : " + key + " do not present");
}

Entry e = kvStore.get(key);
if (System.currentTimeMillis() - e.created > e.ttl_millis) {
kvStore.remove(key);
throw new DataExpiredException("'key' : " + key + " expired");
}

return e.val;
}

boolean rem(String key) throws DataNotPresentException, DataExpiredException {
if (!kvStore.containsKey(key)) {
throw new DataNotPresentException("'key' : " + key + " do not present");
}

Entry e = kvStore.get(key);
kvStore.remove(key);
return (System.currentTimeMillis() - e.created <= e.ttl_millis);
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<groupId>com.thealgorithm</groupId>
<artifactId>TheAlgorithm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>data-structures</module>
<module>low-level-design</module>
</modules>

<properties>
<maven.compiler.source>21</maven.compiler.source>
Expand Down