-
Notifications
You must be signed in to change notification settings - Fork 23
/
DAO.java
98 lines (86 loc) · 3.07 KB
/
DAO.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
/*
* Copyright 2018 (c) Vadim Tsesko <incubos@yandex.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ru.mail.polis;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Storage interface.
*
* @author Vadim Tsesko
* @author Dmitry Schitinin
*/
public interface DAO extends Closeable {
/**
* Provides iterator (possibly empty) over {@link Record}s starting at "from" key (inclusive)
* in <b>ascending</b> order according to {@link Record#compareTo(Record)}.
* N.B. The iterator should be obtained as fast as possible, e.g.
* one should not "seek" to start point ("from" element) in linear time ;)
*/
@NotNull
Iterator<Record> iterator(@NotNull ByteBuffer from) throws IOException;
/**
* Provides iterator (possibly empty) over {@link Record}s starting at "from" key (inclusive)
* until given "to" key (exclusive) in <b>ascending</b> order according to {@link Record#compareTo(Record)}.
* N.B. The iterator should be obtained as fast as possible, e.g.
* one should not "seek" to start point ("from" element) in linear time ;)
*/
@NotNull
default Iterator<Record> range(
@NotNull ByteBuffer from,
@Nullable ByteBuffer to) throws IOException {
if (to == null) {
return iterator(from);
}
if (from.compareTo(to) > 0) {
return Iters.empty();
}
final Record bound = new Record(to, ByteBuffer.allocate(0));
return Iters.until(iterator(from), bound);
}
/**
* Obtains {@link Record} corresponding to given key.
*
* @throws NoSuchElementException if no such record
*/
@NotNull
default ByteBuffer get(@NotNull ByteBuffer key) throws IOException, NoSuchElementException {
final Iterator<Record> iter = iterator(key);
if (!iter.hasNext()) {
throw new NoSuchElementException("Not found");
}
final Record next = iter.next();
if (next.getKey().equals(key)) {
return next.getValue();
} else {
throw new NoSuchElementException("Not found");
}
}
/**
* Inserts or updates value by given key.
*/
void upsert(
@NotNull ByteBuffer key,
@NotNull ByteBuffer value) throws IOException;
/**
* Removes value by given key.
*/
void remove(@NotNull ByteBuffer key) throws IOException;
}