/
ChunkedStorageCollectionTest.java
212 lines (184 loc) · 5.6 KB
/
ChunkedStorageCollectionTest.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package de.enough.polish.io;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import junit.framework.TestCase;
public class ChunkedStorageCollectionTest extends TestCase
{
public void testChunking()
{
TestChunkedStorageCollection collection = new TestChunkedStorageCollection();
int testSize = 1003;
for (int i=0; i<testSize; i++)
{
collection.add( new TestData(Integer.toString(i)));
}
assertEquals(testSize, collection.size());
assertEquals(testSize % 20 + 20, collection.sizeTail());
ArrayList list = (ArrayList) ChunkedStorageMemorySystem.listsPerIdentifier.get("test");
assertNotNull(list);
assertTrue(list.size() > 0);
assertEquals(list.size(), testSize / 20);
assertTrue(collection.isDirty());
try {
collection.saveCollection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
fail("for " + e.toString());
}
assertFalse(collection.isDirty());
for (int i=testSize-1; i>=0; i--)
{
//System.out.println("testing " + i);
assertEquals(Integer.toString(i), ((TestData)collection.get(i)).data);
}
collection.remove(1000);
assertTrue(collection.isDirty());
assertEquals(testSize - 1, collection.size());
int toAdd = 23;
for (int i=0; i<toAdd; i++)
{
collection.add( new TestData(Integer.toString(i + testSize)));
}
assertEquals(testSize + toAdd - 1, collection.size() );
assertEquals( (testSize + toAdd - 1) % 20 + 20, collection.sizeTail() );
}
public void testRemove()
{
TestChunkedStorageCollection collection = new TestChunkedStorageCollection();
int testSize = 1003;
for (int i=0; i<testSize; i++)
{
collection.add( new TestData(Integer.toString(i)));
}
assertEquals(testSize, collection.size());
System.out.println("------ before removal ------");
//System.out.println("now: internal of 9: " + collection.getInternalIndex(9) + " -> " + collection.get(9));
for (int i=0; i<30; i++)
{
System.out.print( collection.getInternalIndex(i) + ": " + collection.get(i) + ", ");
}
System.out.println();
int[] removeIndeces = new int[]{ 10, 0, 0, 999, 0, 26, 1, 767, 400, 0, 14, 7 };
for (int i = 0; i < removeIndeces.length; i++)
{
int removeIndex = removeIndeces[i];
System.out.print("removing " + removeIndex + ", internal=" + collection.getInternalIndex(removeIndex) + " -> " );
Object previous = collection.remove(removeIndex);
System.out.println(previous);
System.out.println("now: internal of 2: " + collection.getInternalIndex(2) + " -> " + collection.get(2));
System.out.println("now: internal of 3: " + collection.getInternalIndex(3) + " -> " + collection.get(3));
System.out.println("now: internal of 4: " + collection.getInternalIndex(4) + " -> " + collection.get(4));
assertNotNull(previous);
testSize--;
assertEquals( testSize, collection.size() );
}
System.out.println("------ after removal ------");
for (int i=0; i<30; i++)
{
System.out.print( i + "(" + collection.getInternalIndex(i) + "): " + collection.get(i) + ", ");
}
System.out.println();
for (int i=0; i<collection.size(); i++)
{
if (collection.get(i) == null)
{
fail("got null for index " + i + "->" + collection.getInternalIndex(i));
}
}
}
static class TestData implements Mutable
{
private String data;
private boolean isDirty;
public TestData(String data)
{
this.data = data;
}
public void write(DataOutputStream out) throws IOException {
Serializer.writeUtfNullable(this.data, out);
this.isDirty = false;
}
public void read(DataInputStream in) throws IOException {
this.data = Serializer.readUtfNullable(in);
}
public void setData(String data)
{
this.data = data;
this.isDirty = true;
}
public boolean isDirty() {
return this.isDirty;
}
public String toString()
{
return this.data;
}
}
static class TestChunkedStorageCollection extends ChunkedStorageCollection
{
public TestChunkedStorageCollection()
{
super("test", 20, new ChunkedStorageMemorySystem(), ChunkedStorageCollection.STORAGE_STRATEGY_CHUNKED);
}
public Mutable createCollectionObject() {
return new TestData(null);
}
}
static class ChunkedStorageMemorySystem implements ChunkedStorageSystem
{
private static Hashtable listsPerIdentifier;
public ChunkedStorageMemorySystem()
{
listsPerIdentifier = new Hashtable();
}
public byte[] loadTailData(String identifier) throws IOException {
return loadData(-1, identifier);
}
public byte[] loadData(int chunkIndex, String identifier)
throws IOException
{
ArrayList list = (ArrayList) listsPerIdentifier.get(identifier);
if (list == null)
{
throw new IOException();
}
int index = chunkIndex + 1;
return (byte[]) list.get(index);
}
public void saveTailData(String identifier, byte[] data)
throws IOException
{
saveChunkData(-1, identifier, data);
}
public void saveChunkData(int chunkIndex, String identifier, byte[] data)
throws IOException
{
int index = chunkIndex + 1;
ArrayList list = (ArrayList) listsPerIdentifier.get(identifier);
if (list == null)
{
list = new ArrayList();
listsPerIdentifier.put(identifier, list);
}
if (index < list.size())
{
list.set(index, data);
}
else
{
list.add(data);
if (list.size() < index + 1)
{
throw new IOException("inconsistent state: expected size=" + (index + 1) + ", actual size=" + list.size());
}
}
}
public void delete(String identifier) throws IOException {
listsPerIdentifier.remove(identifier);
}
}
}