|
1 | 1 | package com.scalableminds.zarrjava.store; |
2 | 2 |
|
3 | 3 | import com.scalableminds.zarrjava.utils.Utils; |
4 | | - |
5 | | -import javax.annotation.Nonnull; |
6 | | -import javax.annotation.Nullable; |
7 | 4 | import java.io.IOException; |
8 | 5 | import java.nio.ByteBuffer; |
9 | 6 | import java.nio.channels.SeekableByteChannel; |
10 | | -import java.nio.file.*; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.NoSuchFileException; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.nio.file.StandardOpenOption; |
11 | 12 | import java.util.stream.Stream; |
| 13 | +import javax.annotation.Nonnull; |
| 14 | +import javax.annotation.Nullable; |
12 | 15 |
|
13 | 16 | public class FilesystemStore implements Store, Store.ListableStore { |
14 | 17 |
|
15 | | - @Nonnull |
16 | | - private final Path path; |
| 18 | + @Nonnull |
| 19 | + private final Path path; |
17 | 20 |
|
18 | | - public FilesystemStore(@Nonnull Path path) { |
19 | | - this.path = path; |
20 | | - } |
| 21 | + public FilesystemStore(@Nonnull Path path) { |
| 22 | + this.path = path; |
| 23 | + } |
21 | 24 |
|
22 | | - public FilesystemStore(@Nonnull String path) { |
23 | | - this.path = Paths.get(path); |
24 | | - } |
| 25 | + public FilesystemStore(@Nonnull String path) { |
| 26 | + this.path = Paths.get(path); |
| 27 | + } |
25 | 28 |
|
26 | | - Path resolveKeys(String[] keys) { |
27 | | - Path newPath = path; |
28 | | - for (String key : keys) { |
29 | | - newPath = newPath.resolve(key); |
30 | | - } |
31 | | - return newPath; |
| 29 | + Path resolveKeys(String[] keys) { |
| 30 | + Path newPath = path; |
| 31 | + for (String key : keys) { |
| 32 | + newPath = newPath.resolve(key); |
32 | 33 | } |
33 | | - |
34 | | - @Override |
35 | | - public boolean exists(String[] keys) { |
36 | | - return Files.exists(resolveKeys(keys)); |
| 34 | + return newPath; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public boolean exists(String[] keys) { |
| 39 | + return Files.exists(resolveKeys(keys)); |
| 40 | + } |
| 41 | + |
| 42 | + @Nullable |
| 43 | + @Override |
| 44 | + public ByteBuffer get(String[] keys) { |
| 45 | + try { |
| 46 | + return ByteBuffer.wrap(Files.readAllBytes(resolveKeys(keys))); |
| 47 | + } catch (IOException e) { |
| 48 | + return null; |
37 | 49 | } |
38 | | - |
39 | | - @Nullable |
40 | | - @Override |
41 | | - public ByteBuffer get(String[] keys) { |
42 | | - try { |
43 | | - return ByteBuffer.wrap(Files.readAllBytes(resolveKeys(keys))); |
44 | | - } catch (IOException e) { |
45 | | - return null; |
46 | | - } |
| 50 | + } |
| 51 | + |
| 52 | + @Nullable |
| 53 | + @Override |
| 54 | + public ByteBuffer get(String[] keys, long start) { |
| 55 | + try (SeekableByteChannel byteChannel = Files.newByteChannel(resolveKeys(keys))) { |
| 56 | + long startOffset = 0; |
| 57 | + if (start >= 0) { |
| 58 | + startOffset = start; |
| 59 | + } else { |
| 60 | + startOffset = byteChannel.size() + start; |
| 61 | + } |
| 62 | + long endOffset = byteChannel.size(); |
| 63 | + ByteBuffer bytes = Utils.allocateNative((int) (endOffset - startOffset)); |
| 64 | + byteChannel.position(startOffset); |
| 65 | + byteChannel.read(bytes); |
| 66 | + bytes.rewind(); |
| 67 | + return bytes; |
| 68 | + } catch (IOException e) { |
| 69 | + return null; |
47 | 70 | } |
48 | | - |
49 | | - @Nullable |
50 | | - @Override |
51 | | - public ByteBuffer get(String[] keys, long start) { |
52 | | - try (SeekableByteChannel byteChannel = Files.newByteChannel(resolveKeys(keys))) { |
53 | | - long startOffset = 0; |
54 | | - if (start >= 0) { |
55 | | - startOffset = start; |
56 | | - } else { |
57 | | - startOffset = byteChannel.size() + start; |
58 | | - } |
59 | | - long endOffset = byteChannel.size(); |
60 | | - ByteBuffer bytes = Utils.allocateNative((int) (endOffset - startOffset)); |
61 | | - byteChannel.position(startOffset); |
62 | | - byteChannel.read(bytes); |
63 | | - bytes.rewind(); |
64 | | - return bytes; |
65 | | - } catch (IOException e) { |
66 | | - return null; |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - @Nullable |
71 | | - @Override |
72 | | - public ByteBuffer get(String[] keys, long start, long end) { |
73 | | - try (SeekableByteChannel byteChannel = Files.newByteChannel(resolveKeys(keys))) { |
74 | | - long startOffset = 0; |
75 | | - if (start >= 0) { |
76 | | - startOffset = start; |
77 | | - } else { |
78 | | - startOffset = byteChannel.size() + start; |
79 | | - } |
80 | | - ByteBuffer bytes = Utils.allocateNative((int) (end - startOffset)); |
81 | | - byteChannel.position(startOffset); |
82 | | - byteChannel.read(bytes); |
83 | | - bytes.rewind(); |
84 | | - return bytes; |
85 | | - } catch (IOException e) { |
86 | | - return null; |
87 | | - } |
| 71 | + } |
| 72 | + |
| 73 | + @Nullable |
| 74 | + @Override |
| 75 | + public ByteBuffer get(String[] keys, long start, long end) { |
| 76 | + try (SeekableByteChannel byteChannel = Files.newByteChannel(resolveKeys(keys))) { |
| 77 | + long startOffset = 0; |
| 78 | + if (start >= 0) { |
| 79 | + startOffset = start; |
| 80 | + } else { |
| 81 | + startOffset = byteChannel.size() + start; |
| 82 | + } |
| 83 | + ByteBuffer bytes = Utils.allocateNative((int) (end - startOffset)); |
| 84 | + byteChannel.position(startOffset); |
| 85 | + byteChannel.read(bytes); |
| 86 | + bytes.rewind(); |
| 87 | + return bytes; |
| 88 | + } catch (IOException e) { |
| 89 | + return null; |
88 | 90 | } |
| 91 | + } |
89 | 92 |
|
90 | 93 |
|
91 | | - @Override |
92 | | - public void set(String[] keys, ByteBuffer bytes) { |
93 | | - Path keyPath = resolveKeys(keys); |
94 | | - try { |
95 | | - Files.createDirectories(keyPath.getParent()); |
96 | | - } catch (IOException e) { |
97 | | - throw new RuntimeException(e); |
98 | | - } |
99 | | - try (SeekableByteChannel channel = Files.newByteChannel(keyPath.toAbsolutePath(), StandardOpenOption.CREATE, |
100 | | - StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { |
101 | | - channel.write(bytes); |
102 | | - } catch (IOException e) { |
103 | | - throw new RuntimeException(e); |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - @Override |
108 | | - public void delete(String[] keys) { |
109 | | - try { |
110 | | - Files.delete(resolveKeys(keys)); |
111 | | - } catch (NoSuchFileException e) { |
112 | | - // ignore |
113 | | - } catch (IOException e) { |
114 | | - throw new RuntimeException(e); |
115 | | - } |
| 94 | + @Override |
| 95 | + public void set(String[] keys, ByteBuffer bytes) { |
| 96 | + Path keyPath = resolveKeys(keys); |
| 97 | + try { |
| 98 | + Files.createDirectories(keyPath.getParent()); |
| 99 | + } catch (IOException e) { |
| 100 | + throw new RuntimeException(e); |
116 | 101 | } |
117 | | - |
118 | | - public String[] list(String[] keys) { |
119 | | - try (Stream<Path> paths = Files.list(resolveKeys(keys))) { |
120 | | - return paths.map(p -> p.toFile().getName()).toArray(String[]::new); |
121 | | - } catch (IOException e) { |
122 | | - throw new RuntimeException(e); |
123 | | - } |
| 102 | + try (SeekableByteChannel channel = Files.newByteChannel(keyPath.toAbsolutePath(), |
| 103 | + StandardOpenOption.CREATE, |
| 104 | + StandardOpenOption.TRUNCATE_EXISTING, |
| 105 | + StandardOpenOption.WRITE |
| 106 | + )) { |
| 107 | + channel.write(bytes); |
| 108 | + } catch (IOException e) { |
| 109 | + throw new RuntimeException(e); |
124 | 110 | } |
125 | | - |
126 | | - @Nonnull |
127 | | - @Override |
128 | | - public StoreHandle resolve(String... keys) { |
129 | | - return new StoreHandle(this, keys); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void delete(String[] keys) { |
| 115 | + try { |
| 116 | + Files.delete(resolveKeys(keys)); |
| 117 | + } catch (NoSuchFileException e) { |
| 118 | + // ignore |
| 119 | + } catch (IOException e) { |
| 120 | + throw new RuntimeException(e); |
130 | 121 | } |
131 | | - |
132 | | - @Override |
133 | | - public String toString() { |
134 | | - return this.path.toUri().toString().replaceAll("\\/$", ""); |
| 122 | + } |
| 123 | + |
| 124 | + public String[] list(String[] keys) { |
| 125 | + try (Stream<Path> paths = Files.list(resolveKeys(keys))) { |
| 126 | + return paths.map(p -> p.toFile() |
| 127 | + .getName()) |
| 128 | + .toArray(String[]::new); |
| 129 | + } catch (IOException e) { |
| 130 | + throw new RuntimeException(e); |
135 | 131 | } |
| 132 | + } |
| 133 | + |
| 134 | + @Nonnull |
| 135 | + @Override |
| 136 | + public StoreHandle resolve(String... keys) { |
| 137 | + return new StoreHandle(this, keys); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String toString() { |
| 142 | + return this.path.toUri().toString().replaceAll("\\/$", ""); |
| 143 | + } |
136 | 144 |
|
137 | 145 | } |
0 commit comments