Skip to content
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

Redis stub #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
103 changes: 96 additions & 7 deletions Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,68 @@
* Platform : Codeforces/Codejam
* Ref : N/A
*/
class Entry {
String value;
long expiredAt;

public Entry(String value, long expiredAt){
this.value = value;
this.expiredAt = expiredAt;
}

@Override
public String toString(){
return "Entry{ value:" + value + ", expiredAt: " + expiredAt + "}";
}
}

class InMemoryDB {

HashMap<String, HashMap<String, Entry>> cache = new HashMap<>();

InMemoryDB(){
}

void addEntry(long timestamp, String key, String field, String value, long ttl){
long expiredAt = timestamp + ttl;
var entries = new HashMap<String, Entry>();
if (cache.containsKey(key)) {
entries = (HashMap<String, Entry>)cache.get(key);
}
entries.put(field, new Entry(value, expiredAt));
cache.put(key, entries);
}

void evictIfExpired(long timestamp, String key, String field) {
var entries = (HashMap<String, Entry>)cache.get(key);
Entry entry = (Entry) entries.get(field);
if (entry.expiredAt <= timestamp){
entries.remove(field);
cache.put(key, entries);
}
}

boolean deleteEntry(long timestamp, String key, String field){
if (cache.containsKey(key)) {
var entries = (HashMap<String, Entry>)cache.get(key);
evictIfExpired(timestamp, key, field);
if(entries.containsKey(field)){
entries.remove(field);
cache.put(key, entries);
return true;
}else {
return false;
}
}
return false;
}

List<Entry> scan(long timestamp, String key){
// similarly O(N) iterate and evictIfExpired and return values
return null;
}

}

public class Solution{

Expand All @@ -17,13 +79,13 @@ public class Solution{
private FastReader in ;
private PrintWriter out ;
private final String PROBLEM_ID = "1039-A";
private final long MOD = 1000000000+7;
private final long MOD = (long)1e9 + 7;
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 2;

public Solution(){}

public Solution(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
outputStream = System.out;
Expand All @@ -34,15 +96,42 @@ public Solution(boolean stdIO)throws FileNotFoundException{
in = new FastReader(inputStream);
out = new PrintWriter(outputStream);
}

void run()throws Exception {
int tests = i();
test:
for(int testId = 1; testId <= tests; testId++){
// out.write("Case #"+testId+": ");
long ans = 0;
out.write(ans + "\n");

// Codejam / Hackercup formatting
// out.write("Case #" + testId + ": ");
Random rng = new Random();
InMemoryDB database = new InMemoryDB();

for(int operations = 1; operations <= 1000; operations++){
long timestamp = operations * operations;
switch(rng.nextInt(4)){
// add
case 0: {
long ttl = rng.nextLong((int)1e9);
String key = "key_" + rng.nextInt(100);// random key
String field = "field_" + rng.nextInt(100);// random field
String value = "value_" + rng.nextInt(100);// random field

database.addEntry(timestamp, key, field, value, ttl);
break;
}
// delete
case 1: {
String key = "key_" + rng.nextInt(100);// random key
String field = "field_" + rng.nextInt(100);// random field
database.deleteEntry(timestamp, key, field);
break;
}
// TODO: Scan disctionary
default: {
break;
}
}
}
// out.write(ans + "\n");
}
}

Expand Down