Created
May 8, 2014 18:07
-
-
Save gilfernandes/145046a9f0b349c078b4 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
/* | |
OSSCUBE 2014 | |
*/ | |
package com.onepointltd.file.operations; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.URL; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Scanner; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import net.openhft.collections.HugeConfig; | |
import net.openhft.collections.HugeHashMap; | |
import org.junit.Test; | |
/** | |
* Tests the behaviour of huge hash map compared. | |
* | |
* | |
* | |
* @author onepoint | |
*/ | |
public class HugeMapTest { | |
/** | |
* The dictionary file. | |
*/ | |
public static final String DICT = "src/test/resources/dict.out"; | |
/** | |
* Writes the content of a dictionary into HugeMap. | |
*/ | |
@Test | |
public void testHugeMap() { | |
System.out.println("Started hugemap ..."); | |
HugeConfig config = HugeConfig.DEFAULT.clone(); | |
final Map<CharSequence, CharSequence> map | |
= new HugeHashMap<>( | |
config, CharSequence.class, CharSequence.class); | |
try { | |
putAndDump(map, new File("hugeMap.txt")); | |
} catch (IOException ex) { | |
Logger.getLogger(HugeMapTest.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
/** | |
* Writes the content of a dictionary into a normal hash map. | |
*/ | |
@Test | |
public void testNormalMap() { | |
System.out.println("Started normal map ..."); | |
final Map<CharSequence, CharSequence> map = new HashMap<>(); | |
try { | |
putAndDump(map, new File("normalMap.txt")); | |
} catch (IOException ex) { | |
Logger.getLogger(HugeMapTest.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
/** | |
* Puts a dictionary into a map and dumps it to a file. | |
* | |
* @param map The map to test. | |
* @param dumpFile The dictionary file. | |
* @throws IOException | |
*/ | |
public void putAndDump(final Map<CharSequence, CharSequence> map, File dumpFile) throws IOException { | |
if (!Files.exists(Paths.get(DICT))) { | |
downloadDict(); | |
} | |
try (PrintWriter writer = new PrintWriter(dumpFile)) { | |
try (Scanner scanner = new Scanner(Paths.get(DICT))) { | |
while (scanner.hasNext()) { | |
String term = scanner.next(); | |
map.put(term, term); | |
} | |
} catch (IOException ex) { | |
Logger.getLogger(HugeMapTest.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
System.out.printf("#### %s%n", map.size()); | |
for (Map.Entry<CharSequence, CharSequence> entry : map.entrySet()) { | |
writer.println(":: " + entry); | |
} | |
} | |
} | |
/** | |
* Downloads the dictionary. | |
*/ | |
private void downloadDict() { | |
try (BufferedReader reader = new BufferedReader(new InputStreamReader( | |
new URL("https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt").openStream())); | |
PrintWriter writer = new PrintWriter("dict.out")) { | |
String line; | |
while ((line = reader.readLine()) != null) { | |
writer.println(line); | |
} | |
} catch (IOException ex) { | |
Logger.getLogger(HugeMapTest.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment