Skip to content

Commit

Permalink
KAA-537: change AndroidSQLDbLogStorage and DesktopSQLiteDBLogStorage …
Browse files Browse the repository at this point in the history
…to correspond to the new LogStorage interface && fix tests compilation
  • Loading branch information
batytskyy committed Jun 15, 2015
1 parent e9a24ba commit 274310a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ public LogStorageStatus getStatus() {
}

@Override
public LogBlock getRecordBlock(long blockSize) {
public LogBlock getRecordBlock(long blockSize, int batchSize) {
synchronized (database) {
Log.d(TAG, "Creating a new record block, needed size: " + blockSize);
Log.d(TAG, "Creating a new record block, needed size: " + blockSize + ", batchSize: " + batchSize);
LogBlock logBlock = null;
Cursor cursor = null;
List<String> unmarkedRecordIds = new LinkedList<>();
List<LogRecord> logRecords = new LinkedList<>();
long leftBlockSize = blockSize;
try {
cursor = database.rawQuery(PersistentLogStorageConstants.KAA_SELECT_UNMARKED_RECORDS, null);
cursor = database.rawQuery(PersistentLogStorageConstants.KAA_SELECT_UNMARKED_RECORDS,
new String[] {String.valueOf(batchSize)});
while (cursor.moveToNext()) {
int recordId = cursor.getInt(0);
byte[] recordData = cursor.getBlob(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public interface PersistentLogStorageConstants {
"SELECT " + RECORD_ID_COLUMN + ", " + LOG_DATA_COLUMN + " " +
"FROM " + TABLE_NAME + " " +
"WHERE " + BUCKET_ID_COLUMN + " IS NULL " +
"ORDER BY " + RECORD_ID_COLUMN + " ASC;";
"ORDER BY " + RECORD_ID_COLUMN + " ASC " +
"LIMIT ?;";

String KAA_DELETE_BY_RECORD_ID =
"DELETE FROM " + TABLE_NAME + " " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ public void notifyUploadFailed(int id) {
buckets.get(id).setState(MemBucketState.FULL);
}
}


@Override
public void close() {
// automatically done by GC
}

@Override
public LogStorageStatus getStatus() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.List;

public abstract class AbstractLogStorageTest {
private void testAddHelper(int addedN, int blockSize, int expectedN) {
private void testAddHelper(int addedN, int blockSize, int batchSize, int expectedN) {
LogStorage storage = (LogStorage) getStorage(blockSize);
List<LogRecord> expectedList = new LinkedList<>();
LogRecord record = new LogRecord();
Expand All @@ -37,7 +37,7 @@ private void testAddHelper(int addedN, int blockSize, int expectedN) {
expectedList.add(record);
}

LogBlock group = storage.getRecordBlock(blockSize);
LogBlock group = storage.getRecordBlock(blockSize, batchSize);
List<LogRecord> actualList = group.getRecords();

Assert.assertTrue("Expected: " + expectedList.size() + ", actual: " + actualList.size()
Expand All @@ -61,7 +61,7 @@ private void testAddHelper(int addedN, int blockSize, int expectedN) {
public void testEmptyLogRecord() {
long bucketSize = 3;
LogStorage storage = (LogStorage) getStorage(bucketSize);
LogBlock group = storage.getRecordBlock(5);
LogBlock group = storage.getRecordBlock(5, 1);
Assert.assertTrue(group == null);
storage.close();
}
Expand Down Expand Up @@ -104,8 +104,8 @@ public void testUniqueIdGeneration() {
storage.addLogRecord(record);
}

LogBlock group1 = storage.getRecordBlock(6);
LogBlock group2 = storage.getRecordBlock(6);
LogBlock group1 = storage.getRecordBlock(6, 2);
LogBlock group2 = storage.getRecordBlock(6, 2);

Assert.assertNotEquals(group1.getBlockId(), group2.getBlockId());
storage.close();
Expand All @@ -116,10 +116,10 @@ public void testLogRecordAdding() {
/*
* Size of each record is 3B
*/
testAddHelper(1, 3, 1);
testAddHelper(4, 3, 1);
testAddHelper(3, 9, 3);
testAddHelper(5, 5, 1);
testAddHelper(1, 3, 1, 1);
testAddHelper(4, 3, 2, 1);
testAddHelper(3, 9, 4, 3);
testAddHelper(5, 5, 2, 1);
}

@Test
Expand All @@ -138,11 +138,11 @@ public void testGetSameLogBlock() {
storage.addLogRecord(record);
}

LogBlock group1 = storage.getRecordBlock(7);
LogBlock group1 = storage.getRecordBlock(7, 2);

storage.notifyUploadFailed(group1.getBlockId());

LogBlock group2 = storage.getRecordBlock(7);
LogBlock group2 = storage.getRecordBlock(7, 2);

Assert.assertTrue("Expected: " + group1.getRecords().size() + ", actual: " + group2.getRecords().size()
, group1.getRecords().size() == group2.getRecords().size());
Expand Down Expand Up @@ -176,17 +176,17 @@ public void testLogRecordRemoval() {
storage.addLogRecord(record);
}

LogBlock removingBlock = storage.getRecordBlock(7);
LogBlock removingBlock = storage.getRecordBlock(7, 2);

insertionCount -= removingBlock.getRecords().size();
storage.removeRecordBlock(removingBlock.getBlockId());

removingBlock = storage.getRecordBlock(9);
removingBlock = storage.getRecordBlock(9, 3);

insertionCount -= removingBlock.getRecords().size();
storage.removeRecordBlock(removingBlock.getBlockId());

LogBlock leftBlock = storage.getRecordBlock(50);
LogBlock leftBlock = storage.getRecordBlock(50, 50);
Assert.assertTrue(leftBlock.getRecords().size() == insertionCount);
storage.close();
}
Expand All @@ -207,20 +207,20 @@ public void testComplexLogRemoval() {
storage.addLogRecord(record);
}

LogBlock removingBlock1 = storage.getRecordBlock(7);
LogBlock removingBlock1 = storage.getRecordBlock(7, 2);
insertionCount -= removingBlock1.getRecords().size();

LogBlock removingBlock2 = storage.getRecordBlock(9);
LogBlock removingBlock2 = storage.getRecordBlock(9, 3);
insertionCount -= removingBlock2.getRecords().size();

LogBlock removingBlock3 = storage.getRecordBlock(6);
LogBlock removingBlock3 = storage.getRecordBlock(6, 2);
insertionCount -= removingBlock3.getRecords().size();

storage.removeRecordBlock(removingBlock2.getBlockId());
storage.notifyUploadFailed(removingBlock1.getBlockId());
insertionCount += removingBlock1.getRecords().size();

LogBlock leftBlock = storage.getRecordBlock(50);
LogBlock leftBlock = storage.getRecordBlock(50, 50);
Assert.assertTrue("Ac: " + leftBlock.getRecords().size() + ", ex: " + insertionCount
, leftBlock.getRecords().size() == insertionCount);
storage.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public void testPersistDBState() {
while (iter-- > 0) {
storage.addLogRecord(record);
}
LogBlock beforePersist = storage.getRecordBlock(15);
LogBlock beforePersist = storage.getRecordBlock(15, 2);
storage.close();

storage = (LogStorage) getStorage(BUCKET_SIZE);
LogStorageStatus storageStatus = (LogStorageStatus) storage;
Assert.assertEquals(insertionCount, storageStatus.getRecordCount());
Assert.assertEquals(insertionCount * 3, storageStatus.getConsumedVolume());
LogBlock afterPersist = storage.getRecordBlock(15);
LogBlock afterPersist = storage.getRecordBlock(15, 2);

Assert.assertEquals(beforePersist.getRecords().size(), afterPersist.getRecords().size());

Expand All @@ -63,7 +63,7 @@ public void testGetBigRecordBlock() {
storage.addLogRecord(record);
}

LogBlock logBlock = storage.getRecordBlock(8192);
LogBlock logBlock = storage.getRecordBlock(8192, 1000);
Assert.assertEquals(insertionCount, logBlock.getRecords().size());
storage.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,199 +17,11 @@
package org.kaaproject.kaa.client.logging;


public class MemLogStorageTest {

// TODO: Add
import org.kaaproject.kaa.client.logging.memory.MemLogStorage;

// private void testAddHelper(int addedN, int blockSize, int expectedN) {
// MemoryLogStorage storage = new MemoryLogStorage(blockSize, 0);
// List<LogRecord> expectedList = new LinkedList<>();
// LogRecord record = new LogRecord();
//
// while (addedN-- > 0) {
// storage.addLogRecord(record);
// }
//
// while (expectedN-- > 0) {
// expectedList.add(record);
// }
//
// LogBlock group = storage.getRecordBlock(blockSize, 0);
// List<LogRecord> actualList = group.getRecords();
//
// Assert.assertTrue("Expected: " + expectedList.size() + ", actual: " + actualList.size()
// , expectedList.size() == actualList.size());
//
// Iterator<LogRecord> expectedIt = expectedList.iterator();
// Iterator<LogRecord> actualIt = actualList.iterator();
//
// while (expectedIt.hasNext()) {
// LogRecord expected = expectedIt.next();
// LogRecord actual = actualIt.next();
//
// Assert.assertTrue(expected.getSize() == actual.getSize());
// Assert.assertArrayEquals(expected.getData(), actual.getData());
// }
// }
//
// @Test
// public void testEmptyLogRecord() {
// long bucketSize = 3;
// MemoryLogStorage storage = new MemoryLogStorage(bucketSize, 0);
// LogBlock group = storage.getRecordBlock(5, 0);
// Assert.assertTrue(group == null);
// }
//
// @Test
// public void testRecordCountAndConsumedBytes() {
// long bucketSize = 3;
// MemoryLogStorage storage = new MemoryLogStorage(bucketSize, 0);
// LogRecord record = new LogRecord();
// int insertionCount = 3;
//
// /*
// * Size of each record is 3B
// */
// int iter = insertionCount;
// while (iter-- > 0) {
// storage.addLogRecord(record);
// }
//
// Assert.assertTrue(storage.getRecordCount() == insertionCount);
// Assert.assertTrue(storage.getConsumedVolume() == (insertionCount * record.getSize()));
// }
//
// @Test
// public void testUniqueIdGeneration() {
// long bucketSize = 3;
// MemoryLogStorage storage = new MemoryLogStorage(bucketSize, 0);
// LogRecord record = new LogRecord();
//
// int insertionCount = 3;
//
// /*
// * Size of each record is 3B
// */
// int iter = insertionCount;
// while (iter-- > 0) {
// storage.addLogRecord(record);
// }
//
// LogBlock group1 = storage.getRecordBlock(6, 0);
// LogBlock group2 = storage.getRecordBlock(6, 0);
//
// Assert.assertNotEquals(group1.getBlockId(), group2.getBlockId());
// }
//
// @Test
// public void testLogRecordAdding() {
// /*
// * Size of each record is 3B
// */
// testAddHelper(1, 3, 1);
// testAddHelper(4, 3, 1);
// testAddHelper(3, 9, 3);
// testAddHelper(5, 5, 1);
// }
//
// @Test
// public void testGetSameLogBlock() {
// long bucketSize = 3;
// MemoryLogStorage storage = new MemoryLogStorage(bucketSize, 0);
// LogRecord record = new LogRecord();
//
// int insertionCount = 3;
//
// /*
// * Size of each record is 3B
// */
// int iter = insertionCount;
// while (iter-- > 0) {
// storage.addLogRecord(record);
// }
//
// LogBlock group1 = storage.getRecordBlock(7, 0);
//
// storage.notifyUploadFailed(group1.getBlockId());
//
// LogBlock group2 = storage.getRecordBlock(7, 0);
//
// Assert.assertTrue("Expected: " + group1.getRecords().size() + ", actual: " + group2.getRecords().size()
// , group1.getRecords().size() == group2.getRecords().size());
//
// Iterator<LogRecord> expectedIt = group1.getRecords().iterator();
// Iterator<LogRecord> actualIt = group2.getRecords().iterator();
//
// while (expectedIt.hasNext()) {
// LogRecord expected = expectedIt.next();
// LogRecord actual = actualIt.next();
//
// Assert.assertTrue(expected.getSize() == actual.getSize());
// Assert.assertArrayEquals(expected.getData(), actual.getData());
// }
// }
//
// @Test
// public void testLogRecordRemoval() {
// long bucketSize = 3;
// MemoryLogStorage storage = new MemoryLogStorage(bucketSize, 0);
// LogRecord record = new LogRecord();
//
// int insertionCount = 7;
//
// /*
// * Size of each record is 3B
// */
// int iter = insertionCount;
// while (iter-- > 0) {
// storage.addLogRecord(record);
// }
//
// LogBlock removingBlock = storage.getRecordBlock(7, 0);
//
// insertionCount -= removingBlock.getRecords().size();
// storage.removeRecordBlock(removingBlock.getBlockId());
//
// removingBlock = storage.getRecordBlock(9, 0);
//
// insertionCount -= removingBlock.getRecords().size();
// storage.removeRecordBlock(removingBlock.getBlockId());
//
// LogBlock leftBlock = storage.getRecordBlock(50, 0);
// Assert.assertTrue(leftBlock.getRecords().size() == insertionCount);
// }
//
// @Test
// public void testComplexLogRemoval() {
// long bucketSize = 3;
// MemoryLogStorage storage = new MemoryLogStorage(bucketSize, 0);
// LogRecord record = new LogRecord();
//
// int insertionCount = 8;
//
// /*
// * Size of each record is 3B
// */
// int iter = insertionCount;
// while (iter-- > 0) {
// storage.addLogRecord(record);
// }
//
// LogBlock removingBlock1 = storage.getRecordBlock(7, 0);
// insertionCount -= removingBlock1.getRecords().size();
//
// LogBlock removingBlock2 = storage.getRecordBlock(9, 0);
// insertionCount -= removingBlock2.getRecords().size();
//
// LogBlock removingBlock3 = storage.getRecordBlock(6, 0);
// insertionCount -= removingBlock3.getRecords().size();
//
// storage.removeRecordBlock(removingBlock2.getBlockId());
// storage.notifyUploadFailed(removingBlock1.getBlockId());
// insertionCount += removingBlock1.getRecords().size();
//
// LogBlock leftBlock = storage.getRecordBlock(50, 0);
// Assert.assertTrue("Ac: " + leftBlock.getRecords().size() + ", ex: " + insertionCount
// , leftBlock.getRecords().size() == insertionCount);
// }
}
public class MemLogStorageTest extends AbstractLogStorageTest {
@Override
protected Object getStorage(long bucketSize) {
return new MemLogStorage(bucketSize, 5);
}
}
Loading

0 comments on commit 274310a

Please sign in to comment.