99dfe48a0c2b96ebcd50d78b193649123c9f92db
[sixth-data.git] / src / test / java / eu / svjatoslav / sixth / datastore / DataStoreImplTest.java
1 /*
2  * Sixth - System for data storage, computation, exploration and interaction.
3  * Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.sixth.datastore;
11
12 import eu.svjatoslav.sixth.data.store.DataStore;
13 import eu.svjatoslav.sixth.data.store.file.FileDataStore;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21
22 public class DataStoreImplTest {
23
24     public static final String UTF_8 = "UTF-8";
25     File dataSourceFile;
26
27     DataStore dataStore;
28
29     @Before
30     public void setUp() throws Exception {
31         dataSourceFile = new File("test.byar");
32         if (dataSourceFile.exists())
33             if (!dataSourceFile.delete())
34                 throw new RuntimeException("Cannot delete file: " + dataSourceFile.getCanonicalPath());
35
36         dataStore = new FileDataStore(dataSourceFile);
37     }
38
39     @Test
40     public void testBasicStorageAndRetrieval() throws IOException {
41
42         final int record1 = dataStore.createRecord("See on test".getBytes(UTF_8));
43         final int record2 = dataStore.createRecord("another test".getBytes(UTF_8));
44
45         Assert.assertEquals(0, record1);
46         Assert.assertEquals(1, record2);
47
48         Assert.assertEquals("another test", new String(dataStore.readRecord(record2),
49                 UTF_8));
50         Assert.assertEquals("See on test", new String(dataStore.readRecord(record1),
51                 UTF_8));
52
53         dataStore.updateRecord(record1, "!!!".getBytes(UTF_8));
54
55         Assert.assertEquals("!!!", new String(dataStore.readRecord(record1), UTF_8));
56
57         dataStore.updateRecord(record1, dataStore.readRecord(record2));
58
59         Assert.assertEquals("another test", new String(dataStore.readRecord(record1),
60                 UTF_8));
61
62         dataStore.close();
63     }
64
65     @Test
66     public void testDefragmentation() throws IOException {
67         final int record1 = dataStore.createRecord("1111".getBytes(UTF_8));
68         final int record2 = dataStore.createRecord("22".getBytes(UTF_8));
69         final int record3 = dataStore.createRecord("3333333".getBytes(UTF_8));
70         final int record4 = dataStore.createRecord("4".getBytes(UTF_8));
71
72         ((FileDataStore) dataStore).vacuum(0);
73
74         final int record5 = dataStore.createRecord("555".getBytes());
75
76         dataStore.deleteRecord(record1);
77
78         ((FileDataStore) dataStore).vacuum(0);
79
80         final int record6 = dataStore.createRecord("66".getBytes());
81
82         dataStore.close();
83     }
84
85     @Test
86     public void testEntriesTableIncreasing() throws IOException {
87         // dataStore.createRecord(String.valueOf("lalalala").getBytes());
88
89         for (int j = 0; j < 10; j++) {
90             final ArrayList<Integer> ids = new ArrayList<>();
91
92             for (int i = 0; i < 100; i++) {
93                 final int id = dataStore.createRecord(String.valueOf(
94                         "____________" + i).getBytes());
95
96                 ids.add(id);
97             }
98
99             for (final int id : ids)
100                 dataStore.deleteRecord(id);
101
102             ((FileDataStore) dataStore).vacuum(0);
103         }
104     }
105
106 }