30
30
import org .apache .baremaps .data .type .DataType ;
31
31
32
32
/**
33
- * A log of records backed by a {@link DataType} and a {@link Memory}. Elements are appended to the
34
- * log and can be accessed by their position in the {@link Memory}. Appending elements to the log is
35
- * thread-safe.
33
+ * A log of elements that can only be appended to. Elements are stored in memory and can be accessed
34
+ * by their position. Append operations are thread-safe.
36
35
*
37
- * @param <E> The type of the data.
36
+ * @param <E> The type of elements in the log
38
37
*/
39
38
public class AppendOnlyLog <E > implements DataCollection <E > {
40
39
@@ -47,21 +46,84 @@ public class AppendOnlyLog<E> implements DataCollection<E> {
47
46
private final Lock lock = new ReentrantLock ();
48
47
49
48
/**
50
- * Constructs an {@link AppendOnlyLog} .
49
+ * Creates a new builder for an AppendOnlyLog.
51
50
*
52
- * @param dataType the data type
51
+ * @param <E> the type of elements
52
+ * @return a new builder
53
53
*/
54
- public AppendOnlyLog ( DataType <E > dataType ) {
55
- this ( dataType , new OffHeapMemory () );
54
+ public static <E > Builder < E > builder ( ) {
55
+ return new Builder <>( );
56
56
}
57
57
58
58
/**
59
- * Constructs an append only log.
59
+ * Builder for AppendOnlyLog.
60
+ *
61
+ * @param <E> the type of elements
62
+ */
63
+ public static class Builder <E > {
64
+ private DataType <E > dataType ;
65
+ private Memory <?> memory ;
66
+
67
+ /**
68
+ * Sets the data type for the log.
69
+ *
70
+ * @param dataType the data type
71
+ * @return this builder
72
+ */
73
+ public Builder <E > dataType (DataType <?> dataType ) {
74
+ @ SuppressWarnings ("unchecked" )
75
+ DataType <E > castedDataType = (DataType <E >) dataType ;
76
+ this .dataType = castedDataType ;
77
+ return this ;
78
+ }
79
+
80
+ /**
81
+ * Sets the memory for the log.
82
+ *
83
+ * @param memory the memory
84
+ * @return this builder
85
+ */
86
+ public Builder <E > memory (Memory <?> memory ) {
87
+ this .memory = memory ;
88
+ return this ;
89
+ }
90
+
91
+ /**
92
+ * Sets the memory for the log values.
93
+ *
94
+ * @param memory the memory
95
+ * @return this builder
96
+ */
97
+ public Builder <E > values (Memory <?> memory ) {
98
+ return memory (memory );
99
+ }
100
+
101
+ /**
102
+ * Builds a new AppendOnlyLog.
103
+ *
104
+ * @return a new AppendOnlyLog
105
+ * @throws IllegalStateException if required parameters are missing
106
+ */
107
+ public AppendOnlyLog <E > build () {
108
+ if (dataType == null ) {
109
+ throw new IllegalStateException ("Data type must be specified" );
110
+ }
111
+
112
+ if (memory == null ) {
113
+ memory = new OffHeapMemory ();
114
+ }
115
+
116
+ return new AppendOnlyLog <>(dataType , memory );
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Constructs an AppendOnlyLog.
60
122
*
61
123
* @param dataType the data type
62
124
* @param memory the memory
63
125
*/
64
- public AppendOnlyLog (DataType <E > dataType , Memory <?> memory ) {
126
+ private AppendOnlyLog (DataType <E > dataType , Memory <?> memory ) {
65
127
this .dataType = dataType ;
66
128
this .memory = memory ;
67
129
this .segmentSize = memory .segmentSize ();
@@ -70,10 +132,18 @@ public AppendOnlyLog(DataType<E> dataType, Memory<?> memory) {
70
132
}
71
133
72
134
/**
73
- * Appends the value to the log and returns its position in the memory.
135
+ * Persists the current size to memory.
136
+ */
137
+ public void persistSize () {
138
+ memory .segment (0 ).putLong (0 , size );
139
+ }
140
+
141
+ /**
142
+ * Appends an element to the log and returns its position in memory.
74
143
*
75
- * @param value the value
76
- * @return the position of the value in the memory.
144
+ * @param value the element to add
145
+ * @return the position of the element in memory
146
+ * @throws DataCollectionException if the element is too large for a segment
77
147
*/
78
148
public long addPositioned (E value ) {
79
149
int valueSize = dataType .size (value );
@@ -102,10 +172,10 @@ public long addPositioned(E value) {
102
172
}
103
173
104
174
/**
105
- * Returns a values at the specified position in the memory.
175
+ * Returns the element at the specified position in memory.
106
176
*
107
- * @param position the position of the value
108
- * @return the value
177
+ * @param position the position of the element
178
+ * @return the element
109
179
*/
110
180
public E getPositioned (long position ) {
111
181
long segmentIndex = position / segmentSize ;
@@ -130,32 +200,39 @@ public long size() {
130
200
public void clear () {
131
201
try {
132
202
memory .clear ();
203
+ this .size = 0 ;
204
+ persistSize ();
133
205
} catch (IOException e ) {
134
206
throw new DataCollectionException (e );
135
207
}
136
208
}
137
209
138
210
/**
139
- * Returns an iterator over the values of the log, starting at the beginning of the log. The
140
- * iterator allows to get the current position in the memory.
211
+ * Returns an iterator over the elements of this log.
141
212
*
142
- * @return an iterator over the values of the log
213
+ * @return an iterator over the elements
143
214
*/
144
215
@ Override
145
216
public AppendOnlyLogIterator iterator () {
146
217
return new AppendOnlyLogIterator (size );
147
218
}
148
219
220
+ @ Override
221
+ public void close () throws Exception {
222
+ try {
223
+ memory .close ();
224
+ } catch (IOException e ) {
225
+ throw new DataCollectionException (e );
226
+ }
227
+ }
228
+
149
229
/**
150
- * An iterator over the values of the log that can be used to iterate over the values of the log
151
- * and to get the current position in the memory.
230
+ * An iterator over the elements in this log.
152
231
*/
153
232
public class AppendOnlyLogIterator implements Iterator <E > {
154
233
155
234
private final long size ;
156
-
157
235
private long index ;
158
-
159
236
private long position ;
160
237
161
238
private AppendOnlyLogIterator (long size ) {
@@ -199,9 +276,13 @@ public E next() {
199
276
return dataType .read (segment , (int ) segmentOffset );
200
277
}
201
278
279
+ /**
280
+ * Returns the current position in memory.
281
+ *
282
+ * @return the current position
283
+ */
202
284
public long getPosition () {
203
285
return position ;
204
286
}
205
-
206
287
}
207
288
}
0 commit comments