forked from bulldog2011/bigqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIBigArray.java
More file actions
167 lines (143 loc) · 4.33 KB
/
Copy pathIBigArray.java
File metadata and controls
167 lines (143 loc) · 4.33 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.leansoft.bigqueue;
import java.io.Closeable;
import java.io.IOException;
/**
* Append Only Big Array ADT
*
* @author bulldog
*
*/
public interface IBigArray extends Closeable {
public static final long NOT_FOUND = -1;
/**
* Append the data into the head of the array
*
* @param data binary data to append
* @return appended index
* @throws IOException if there is any IO error
*/
long append(byte[] data) throws IOException;
/**
* Get the data at specific index
*
* @param index valid data index
* @return binary data if the index is valid
* @throws IOException if there is any IO error
*/
byte[] get(long index) throws IOException;
/**
* Get the timestamp of data at specific index,
*
* this is the timestamp when the data was appended.
*
* @param index valid data index
* @return timestamp when the data was appended
* @throws IOException if there is any IO error
*/
long getTimestamp(long index) throws IOException;
/**
* The total number of items has been appended into the array
*
* @return total number
*/
long size();
/**
* Get the back data file size per page.
*
* @return size per page
*/
int getDataPageSize();
/**
* The head of the array.
*
* This is the next to append index, the index of the last appended data
* is [headIndex - 1] if the array is not empty.
*
* @return an index
*/
long getHeadIndex();
/**
* The tail of the array.
*
* The is the index of the first appended data
*
* @return an index
*/
long getTailIndex();
/**
* Check if the array is empty or not
*
* @return true if empty false otherwise
*/
boolean isEmpty();
/**
* Check if the ring space of java long type has all been used up.
*
* can always assume false, if true, the world is end:)
*
* @return array full or not
*/
boolean isFull();
/**
* Remove all data in this array, this will empty the array and delete all back page files.
*
*/
void removeAll() throws IOException;
/**
* Remove all data before specific index, this will advance the array tail to index and
* delete back page files before index.
*
* @param index an index
* @throws IOException exception thrown if there was any IO error during the removal operation
*/
void removeBeforeIndex(long index) throws IOException;
/**
* Remove all data before specific timestamp, this will advance the array tail and delete back page files
* accordingly.
*
* @param timestamp a timestamp
* @throws IOException exception thrown if there was any IO error during the removal operation
*/
void removeBefore(long timestamp) throws IOException;
/**
* Force to persist newly appended data,
*
* normally, you don't need to flush explicitly since:
* 1.) BigArray will automatically flush a cached page when it is replaced out,
* 2.) BigArray uses memory mapped file technology internally, and the OS will flush the changes even your process crashes,
*
* call this periodically only if you need transactional reliability and you are aware of the cost to performance.
*/
void flush();
/**
* Find an index closest to the specific timestamp when the corresponding item was appended
*
* @param timestamp when the corresponding item was appended
* @return an index
* @throws IOException exception thrown if there was any IO error during the getClosestIndex operation
*/
long findClosestIndex(long timestamp) throws IOException;
/**
* Get total size of back files(index and data files) of the big array
*
* @return total size of back files
* @throws IOException exception thrown if there was any IO error during the getBackFileSize operation
*/
long getBackFileSize() throws IOException;
/**
* limit the back file size, truncate back file and advance array tail index accordingly,
* Note, this is a best effort call, exact size limit can't be guaranteed
*
* @param sizeLimit the size to limit
* @throws IOException exception thrown if there was any IO error during the limitBackFileSize operation
*/
void limitBackFileSize(long sizeLimit) throws IOException;
/**
* Get the data item length at specific index
*
* @param index valid data index
* @return the length of binary data if the index is valid
* @throws IOException if there is any IO error
*/
int getItemLength(long index) throws IOException;
}