2 * Sixth - System for data storage, computation, exploration and interaction.
3 * Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.sixth.datastore;
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;
19 import java.io.IOException;
20 import java.util.ArrayList;
22 public class DataStoreImplTest {
24 public static final String UTF_8 = "UTF-8";
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());
36 dataStore = new FileDataStore(dataSourceFile);
40 public void testBasicStorageAndRetrieval() throws IOException {
42 final int record1 = dataStore.createRecord("See on test".getBytes(UTF_8));
43 final int record2 = dataStore.createRecord("another test".getBytes(UTF_8));
45 Assert.assertEquals(0, record1);
46 Assert.assertEquals(1, record2);
48 Assert.assertEquals("another test", new String(dataStore.readRecord(record2),
50 Assert.assertEquals("See on test", new String(dataStore.readRecord(record1),
53 dataStore.updateRecord(record1, "!!!".getBytes(UTF_8));
55 Assert.assertEquals("!!!", new String(dataStore.readRecord(record1), UTF_8));
57 dataStore.updateRecord(record1, dataStore.readRecord(record2));
59 Assert.assertEquals("another test", new String(dataStore.readRecord(record1),
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));
72 ((FileDataStore) dataStore).vacuum(0);
74 final int record5 = dataStore.createRecord("555".getBytes());
76 dataStore.deleteRecord(record1);
78 ((FileDataStore) dataStore).vacuum(0);
80 final int record6 = dataStore.createRecord("66".getBytes());
86 public void testEntriesTableIncreasing() throws IOException {
87 // dataStore.createRecord(String.valueOf("lalalala").getBytes());
89 for (int j = 0; j < 10; j++) {
90 final ArrayList<Integer> ids = new ArrayList<>();
92 for (int i = 0; i < 100; i++) {
93 final int id = dataStore.createRecord(String.valueOf(
94 "____________" + i).getBytes());
99 for (final int id : ids)
100 dataStore.deleteRecord(id);
102 ((FileDataStore) dataStore).vacuum(0);