package com.kryptnostic.api.v1.client;

import java.io.File;
import java.io.IOException;

import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.common.io.Files;
import com.kryptnostic.kodex.v1.storage.DataStore;

/**
 * A simple implementation of the client datastore. The keys are used to generate filenames.
 * 
 * @author Nick Hewitt &lt;nick@kryptnostic.com&gt;
 *
 */
public class FileStore implements DataStore {
    private final File rootDirectory;
    private static HashFunction hf = Hashing.murmur3_128();

    public FileStore(String name) {
        this.rootDirectory = new File(System.getProperty("user.home"), name);
        this.rootDirectory.mkdir();
    }

    @Override
    public byte[] get(byte[] key) throws IOException {
        File keyFile = keyToFile(key);
        if (keyFile.isFile()) {
            byte[] data = Files.toByteArray(keyFile);
            return data;
        }
        return null;
    }

    @Override
    public void put(byte[] key, byte[] value) throws IOException {
        File keyFile = keyToFile(key);
        Files.write(value, keyFile);
    }

    private File keyToFile(byte[] key) {
        Long longEncodedKey = hf.hashBytes(key).asLong();
        return new File(rootDirectory, longEncodedKey.toString());
    }

}

@@ -83,16 +83,61 @@
 ption;%0A%0A
+import org.apache.commons.codec.binary.Hex;%0A%0A
 import c
@@ -545,68 +545,8 @@
 ory;
-%0A    private static HashFunction hf = Hashing.murmur3_128();
 %0A%0A  
@@ -621,39 +621,22 @@
 ile(
-System.getProperty(%22user.home%22)
+%22.kryptnostic%22
 , na
@@ -1149,66 +1149,8 @@
 ) %7B%0A
-        Long longEncodedKey = hf.hashBytes(key).asLong();%0A
     
@@ -1188,33 +1188,35 @@
 ry, 
-longEncodedKey.toString()
+Hex.encodeHexString( key ) 
 );%0A 
@@ -1220,11 +1220,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Switch to hex encoding for key values