forked from LinkedInAttic/indextank-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
KratiStorage.java
90 lines (69 loc) · 2.44 KB
/
KratiStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.flaptor.indextank.index.storage;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import com.flaptor.indextank.index.Document;
import com.flaptor.indextank.storage.alternatives.DocumentStorage;
import com.flaptor.indextank.storage.alternatives.DocumentStorageFactory;
import com.google.common.collect.Maps;
import com.google.common.base.Preconditions;
import krati.core.StoreConfig;
import krati.core.StoreFactory;
import krati.core.segment.MemorySegmentFactory;
import krati.store.DataStore;
public class KratiStorage extends DocumentBinaryStorage {
private File cacheDirectory;
private Charset UTF8_CHARSET ;
private DataStore<byte[], byte[]> store;
public KratiStorage(File cacheDirectory) throws Exception {
this.cacheDirectory = cacheDirectory;
StoreConfig config = new StoreConfig(cacheDirectory, 1024);
config.setSegmentFactory(new MemorySegmentFactory());
config.setSegmentFileSizeMB(64);
this.store = StoreFactory.createStaticDataStore(config);
this.UTF8_CHARSET = Charset.forName("UTF-8");
}
@Override
public byte[] getBinaryDoc(String docId) {
return this.store.get(docId.getBytes(UTF8_CHARSET));
}
@Override
public void saveBinaryDoc(String docId, byte[] bytes){
try {
this.store.put(docId.getBytes(UTF8_CHARSET), bytes);
} catch (Exception e) {
throw new RuntimeException("while savingBinaryDoc:" + e.getMessage(), e);
}
}
@Override
public void deleteBinaryDoc(String docId) {
try {
this.store.delete(docId.getBytes(UTF8_CHARSET));
} catch (Exception e) {
throw new RuntimeException("while deleting BinaryDoc:" + e.getMessage(), e);
}
}
@Override
public void dump() throws IOException {
this.store.persist();
}
@Override
public Map<String, String> getStats() {
return Maps.newHashMap();
}
public class Factory implements DocumentStorageFactory {
public static final String DIR = "dir";
@Override
public DocumentStorage fromConfiguration(Map<?, ?> config) {
Preconditions.checkNotNull(config);
Preconditions.checkNotNull(config.get(DIR), "config needs '" + DIR + "' value");
File backupDir = new File(config.get(DIR).toString());
try {
return new KratiStorage(backupDir);
} catch (Exception e) {
throw new RuntimeException("while creating a KratiStorage: " + e.getMessage(), e);
}
}
}
}