Created
January 21, 2017 16:19
-
-
Save dramaticlly/56aaecb71e8d87013c4a09709c77ae6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter; | |
import java.io.BufferedReader; | |
import java.io.FileWriter; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.Scanner; | |
public class storage{ | |
public class record{ | |
String key; | |
String val; | |
record(String key_, String val_){ | |
key = key_; | |
val = val_; | |
} | |
} | |
public static String hashing(String key){ | |
int hash = 7; | |
for (int i = 0; i < key.length(); i++) { | |
hash = (hash*31 + key.charAt(i))%1000000; | |
} | |
return Integer.toString(hash); | |
} | |
public static String get(String key_){ | |
String target_line = "# key_start:"+hashing(key_); | |
BufferedReader br = null; | |
FileReader fr = null; | |
String val = ""; | |
try { | |
fr = new FileReader("data.txt"); | |
br = new BufferedReader(fr); | |
String sCurrentLine; | |
br = new BufferedReader(new FileReader("data.txt")); | |
while ((sCurrentLine = br.readLine()) != null) { | |
if (sCurrentLine.equals(target_line)) { | |
String tmp = ""; | |
sCurrentLine = br.readLine(); | |
while (!sCurrentLine.equals("# key_end:")) { | |
tmp += sCurrentLine; | |
sCurrentLine = br.readLine(); | |
} | |
if (!tmp.equals(key_)) | |
continue; | |
sCurrentLine = br.readLine(); | |
sCurrentLine = br.readLine(); | |
while (!sCurrentLine.equals("# val_end:")) { | |
val += sCurrentLine; | |
sCurrentLine = br.readLine(); | |
} | |
return val; | |
} | |
} | |
} | |
catch(IOException e){ | |
e.printStackTrace(); | |
System.out.println(e); | |
} | |
System.out.println("key not founded"); | |
return "failed"; | |
} | |
public static String insert(String key_, String val_){ | |
String hash_string = hashing(key_); | |
try { | |
FileWriter writer = new FileWriter("data.txt", true); | |
writer.write("# key_start:"+hash_string+"\n"); | |
writer.write(key_); | |
writer.write("\n# key_end:\n"); | |
writer.write("# val_start:\n"); | |
writer.write(val_); | |
writer.write("\n# val_end:\n"); | |
writer.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
System.out.println(e); | |
return "False"; | |
} | |
return "True"; | |
} | |
public static void main(String []args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print(">(type quit for key or value to quit)\n"); | |
System.out.print(">insert a key:"); | |
String key = scanner.nextLine(); | |
System.out.print(">insert a val:"); | |
String value = scanner.nextLine(); | |
while (!value.equals("quit") && !key.equals("quit")){ | |
insert(key,value); | |
System.out.print(">insert a key:"); | |
key = scanner.nextLine(); | |
System.out.print(">insert a val:"); | |
value = scanner.nextLine(); | |
} | |
System.out.print("type a key to get a value back or type quit to quit:"); | |
key = scanner.nextLine(); | |
while (!key.equals("quit")){ | |
String tmp = get(key); | |
if (!tmp.equals("failed")) { | |
System.out.println("key:" + key + "\n"); | |
System.out.println("val:" + tmp + "\n"); | |
} | |
System.out.print("type a key to get a value back or type quit to quit:"); | |
key = scanner.nextLine(); | |
} | |
} | |
} | |
/* Sample output | |
key_start:314616 | |
key | |
# key_end: | |
# val_start: | |
valeu | |
# val_end: | |
# key_start:257227 | |
123 | |
# key_end: | |
# val_start: | |
456 | |
# val_end: | |
# key_start:249 | |
# key_end: | |
# val_start: | |
' | |
# val_end: | |
# key_start:857550 | |
null | |
# key_end: | |
# val_start: | |
null | |
# val_end: | |
# key_start:752597 | |
keey | |
# key_end: | |
# val_start: | |
valeu | |
# val_end: | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Overall looks good and working. But there are a few points I wanted to mention to @bryan
LINE27/73, for both function return type, you can use
boolean
implement inserts
adding timestamp (I suggest to store in long type, ref https://www.tutorialspoint.com/java/util/date_gettime.htm since we need to use FIFO strategy
adding frequency since we need to use LFU strategy
I found out you never use your record classes, Do you really needed it? Also btw the java convention will capitalize the 1st letter for class name so if you are keeping it, could you rename to
Record
?Let me know if you have any more problems :) Keep it on!