/
HeapBlobStore.java
115 lines (96 loc) · 3.35 KB
/
HeapBlobStore.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Copyright (c) 2015-2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan (Boundless) - initial implementation
*/
package org.locationtech.geogig.storage.memory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jdt.annotation.Nullable;
import org.locationtech.geogig.storage.impl.TransactionBlobStore;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams;
public class HeapBlobStore implements TransactionBlobStore {
private ConcurrentHashMap<String, byte[]> blobs = new ConcurrentHashMap<>();
@Override
public Optional<byte[]> getBlob(String path) {
return getBlob("", path);
}
@Override
public Optional<InputStream> getBlobAsStream(String path) {
return getBlobAsStream("", path);
}
@Override
public void putBlob(String path, byte[] blob) {
putBlob("", path, blob);
}
@Override
public void putBlob(String path, InputStream blob) {
putBlob("", path, blob);
}
@Override
public void removeBlob(String path) {
removeBlob("", path);
}
@Override
public Optional<byte[]> getBlob(String namespace, String path) {
return Optional.fromNullable(blobs.get(key(namespace, path)));
}
private String key(@Nullable String ns, String path) {
return Strings.isNullOrEmpty(ns) ? path
: new StringBuilder(ns).append('/').append(path).toString();
}
@Override
public Optional<InputStream> getBlobAsStream(String namespace, String path) {
Optional<byte[]> blob = getBlob(namespace, path);
InputStream in = null;
if (blob.isPresent()) {
in = new ByteArrayInputStream(blob.get());
}
return Optional.fromNullable(in);
}
@Override
public void putBlob(String namespace, String path, byte[] blob) {
blobs.put(key(namespace, path), blob);
}
@Override
public void putBlob(String namespace, String path, InputStream blob) {
byte[] bytes;
try {
bytes = ByteStreams.toByteArray(blob);
} catch (IOException e) {
throw Throwables.propagate(e);
}
putBlob(namespace, path, bytes);
}
@Override
public void removeBlob(String namespace, String path) {
blobs.remove(key(namespace, path));
}
@Override
public void removeBlobs(final String namespace) {
Set<String> all = ImmutableSet.copyOf(blobs.keySet());
Set<String> filtered = Sets.filter(all, new Predicate<String>() {
final String prefix = namespace + "/";
@Override
public boolean apply(String key) {
return key.startsWith(prefix);
}
});
for (String k : filtered) {
blobs.remove(k);
}
}
}