-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongList.java
More file actions
187 lines (166 loc) · 6.95 KB
/
Copy pathLongList.java
File metadata and controls
187 lines (166 loc) · 6.95 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright 2021 Nandan Desai
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* */
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
public class LongList<T> {
public static String storageRootDir = Paths.get(".").toAbsolutePath().normalize().toString();
private final String id;
private long totalSize = 0L;
private int blockSize;
private int recentBlockIndex = -1;
private ArrayList<T> recentBlockList = null;
private String dirPath;
public LongList(String id) throws IOException {
this.id = id;
blockSize = 500;
init();
}
public LongList(String id, int blockSize) throws IOException {
this.id = id;
this.blockSize = blockSize;
init();
}
public long size() {
return totalSize;
}
public int getBlockSize() {
return blockSize;
}
private void writeObject(String path, Object obj){
try {
FileOutputStream file = new FileOutputStream(path);
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(obj);
out.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private Object readObject(String path){
Object obj = null;
try {
FileInputStream file = new FileInputStream(path);
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
obj = in.readObject();
in.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
private void init() throws IOException {
dirPath = storageRootDir + "/longlist_data/" + id;
Path path = Paths.get(dirPath);
if (Files.exists(path)) {
ArrayList<Object> attributes = (ArrayList<Object>) readObject(dirPath + "/attributes");
totalSize = (long) attributes.get(0);
blockSize = (int) attributes.get(1);
} else {
Files.createDirectories(Paths.get(dirPath));
ArrayList<T> blockList = new ArrayList<>();
writeObject(dirPath + "/block-0", blockList);
persistSizeAttribute();
}
}
private void persistSizeAttribute() throws IOException {
ArrayList<Object> attributes = new ArrayList<>();
attributes.add(totalSize);
attributes.add(blockSize);
writeObject(dirPath + "/attributes", attributes);
}
//initialize the LongList with an ArrayList
public void initialize(ArrayList<T> inputList) throws IOException {
ArrayList<T> blockList;
int blockIndex = 0;
for (int i = 0; i < inputList.size(); i = i + blockSize) {
int currentBlockSize = 0;
int j = i;
blockList = new ArrayList<>();
while (currentBlockSize < blockSize && j < inputList.size()) {
blockList.add(inputList.get(j));
j++;
currentBlockSize++;
}
writeObject(dirPath + "/block-" + blockIndex, blockList);
blockIndex++;
}
totalSize = inputList.size();
persistSizeAttribute();
}
public T get(int index) throws IOException {
if (index >= totalSize || index < 0) {
throw new IndexOutOfBoundsException("Index " + index + " out of bounds for length " + totalSize);
}
double i = index / blockSize;
int blockIndex = 0;
if (i >= 1) {
blockIndex = (int) Math.ceil(i);
}
if (recentBlockList == null || blockIndex != recentBlockIndex) {
ArrayList<T> blockList = (ArrayList<T>) readObject(dirPath + "/block-" + blockIndex);
recentBlockList = blockList;
recentBlockIndex = blockIndex;
}
int blockListIndex = index - (recentBlockIndex * blockSize);
return recentBlockList.get(blockListIndex);
}
public void add(T element) throws IOException {
double i = (totalSize - 1) / blockSize;
int blockIndex = 0;
if (i >= 1) {
blockIndex = (int) Math.ceil(i);
}
if (recentBlockList == null || blockIndex != recentBlockIndex) {
//read the block from filesystem if it is already not present in the memory
ArrayList<T> blockList = (ArrayList<T>) readObject(dirPath + "/block-" + blockIndex);
recentBlockIndex = blockIndex;
recentBlockList = blockList;
}
if (recentBlockList.size() == blockSize) {
//create new block
recentBlockList = new ArrayList<>();
recentBlockList.add(element);
recentBlockIndex++;
} else {
//use existing block
recentBlockList.add(element);
}
totalSize++;
writeObject(dirPath + "/block-" + recentBlockIndex, recentBlockList);
persistSizeAttribute();
}
private static void deleteDir(String path) throws IOException {
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
// delete directories or folders
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
// delete files
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
}
);
}
public void clear() throws IOException {
deleteDir(dirPath);
}
public static void clear(String id) throws IOException {
String path = storageRootDir + "/longlist_data/" + id;
deleteDir(path);
}
}