Skip to content

Commit

Permalink
KAA-1279: Fixed about 100 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sashadidukh committed Sep 30, 2016
1 parent 5b15c93 commit 3da8493
Show file tree
Hide file tree
Showing 28 changed files with 311 additions and 239 deletions.
Expand Up @@ -46,15 +46,24 @@ public AndroidKaaPlatformContext(Context context, KaaClientProperties properties
this(context, properties, new SimpleExecutorContext());
}

public AndroidKaaPlatformContext(Context context, KaaClientProperties properties, ExecutorContext executorContext) {
/**
* Instantiates a new AndroidKaaPlatformContext.
*
* @param context the context
* @param properties the Kaa client properties
* @param executorContext the executor context
*/
public AndroidKaaPlatformContext(Context context, KaaClientProperties properties,
ExecutorContext executorContext) {
super();
this.context = context;
this.properties = properties;
this.executorContext = executorContext;
}

@Override
public AbstractHttpClient createHttpClient(String url, PrivateKey privateKey, PublicKey publicKey, PublicKey remotePublicKey) {
public AbstractHttpClient createHttpClient(String url, PrivateKey privateKey,
PublicKey publicKey, PublicKey remotePublicKey) {
return new AndroidHttpClient(url, privateKey, publicKey, remotePublicKey);
}

Expand Down
Expand Up @@ -30,9 +30,9 @@
import java.util.List;
import java.util.Map;

public class AndroidSQLiteDBLogStorage implements LogStorage, LogStorageStatus {
public class AndroidSqLiteDqLogStorage implements LogStorage, LogStorageStatus {

private static final String TAG = "AndroidSQLiteDBLogStorage";
private static final String TAG = "AndroidSqLiteDqLogStorage";

private static final String CHANGES_QUERY_RESULT = "affected_row_count";
private static final String GET_CHANGES_QUERY = "SELECT changes() AS " + CHANGES_QUERY_RESULT;
Expand All @@ -58,13 +58,14 @@ public class AndroidSQLiteDBLogStorage implements LogStorage, LogStorageStatus {
private SQLiteStatement updateBucketStateStatement;
private SQLiteStatement resetBucketIdStatement;

public AndroidSQLiteDBLogStorage(Context context, long maxBucketSize, int maxRecordCount) {
public AndroidSqLiteDqLogStorage(Context context, long maxBucketSize, int maxRecordCount) {
this(context, PersistentLogStorageConstants.DEFAULT_DB_NAME, maxBucketSize, maxRecordCount);
}

public AndroidSQLiteDBLogStorage(Context context, String dbName, long bucketSize, int recordCount) {
public AndroidSqLiteDqLogStorage(Context context, String dbName, long bucketSize,
int recordCount) {
Log.i(TAG, "Connecting to db with name: " + dbName);
dbHelper = new DataCollectionDBHelper(context, dbName);
dbHelper = new DataCollectionDbHelper(context, dbName);
database = dbHelper.getWritableDatabase();
this.maxRecordCount = recordCount;
this.maxBucketSize = bucketSize;
Expand All @@ -82,10 +83,11 @@ public BucketInfo addLogRecord(LogRecord record) {
Log.d(TAG, "Adding a new log record...");
if (insertStatement == null) {
try {
insertStatement = database.compileStatement(PersistentLogStorageConstants.KAA_INSERT_NEW_RECORD);
} catch (SQLiteException e) {
Log.e(TAG, "Can't create row insert statement", e);
throw new RuntimeException(e);
insertStatement = database.compileStatement(
PersistentLogStorageConstants.KAA_INSERT_NEW_RECORD);
} catch (SQLiteException ex) {
Log.e(TAG, "Can't create row insert statement", ex);
throw new RuntimeException(ex);
}
}
long leftConsumedSize = maxBucketSize - currentBucketSize;
Expand All @@ -106,13 +108,14 @@ public BucketInfo addLogRecord(LogRecord record) {
unmarkedConsumedSize += record.getSize();
totalRecordCount++;
unmarkedRecordCount++;
Log.i(TAG, "Added a new log record, total record count: " + totalRecordCount + ", data: " + Arrays.toString(record.getData()) +
"unmarked record count: " + unmarkedRecordCount);
Log.i(TAG, "Added a new log record, total record count: " + totalRecordCount
+ ", data: " + Arrays.toString(record.getData())
+ "unmarked record count: " + unmarkedRecordCount);
} else {
Log.w(TAG, "No log record was added");
}
} catch (SQLiteException e) {
Log.e(TAG, "Can't add a new record", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Can't add a new record", ex);
}
}
return new BucketInfo(currentBucketId, currentRecordCount);
Expand All @@ -136,20 +139,21 @@ public LogBucket getNextBucket() {
if (cursor.moveToFirst()) {
bucketId = cursor.getInt(0);
}
} catch (SQLiteException e) {
Log.e(TAG, "Can't retrieve min bucket ID", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Can't retrieve min bucket ID", ex);
} finally {
try {
tryCloseCursor(cursor);
} catch (SQLiteException e) {
Log.e(TAG, "Unable to close cursor", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Unable to close cursor", ex);
}
}

try {
long leftBucketSize = maxBucketSize;
if (bucketId > 0) {
cursor = database.rawQuery(PersistentLogStorageConstants.KAA_SELECT_LOG_RECORDS_BY_BUCKET_ID,
cursor = database.rawQuery(
PersistentLogStorageConstants.KAA_SELECT_LOG_RECORDS_BY_BUCKET_ID,
new String[]{String.valueOf(bucketId)});
while (cursor.moveToNext()) {
byte[] recordData = cursor.getBlob(0);
Expand All @@ -170,21 +174,21 @@ public LogBucket getNextBucket() {
if (currentBucketId == bucketId) {
moveToNextBucket();
}
Log.i(TAG, "Created log block: id [" + logBlock.getBucketId() + "], size: " +
logBlockSize + ". Log block record count: " +
logBlock.getRecords().size() + ", total record count: " + totalRecordCount +
", unmarked record count: " + unmarkedRecordCount);
Log.i(TAG, "Created log block: id [" + logBlock.getBucketId() + "], size: "
+ logBlockSize + ". Log block record count: "
+ logBlock.getRecords().size() + ", total record count: " + totalRecordCount
+ ", unmarked record count: " + unmarkedRecordCount);
} else {
Log.i(TAG, "No unmarked log records found");
}
}
} catch (SQLiteException e) {
Log.e(TAG, "Can't retrieve unmarked records from storage", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Can't retrieve unmarked records from storage", ex);
} finally {
try {
tryCloseCursor(cursor);
} catch (SQLiteException e) {
Log.e(TAG, "Unable to close cursor", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Unable to close cursor", ex);
}
}
return logBlock;
Expand All @@ -197,21 +201,24 @@ private void updateBucketState(int bucketId) {

try {
if (updateBucketStateStatement == null) {
updateBucketStateStatement = database.compileStatement(PersistentLogStorageConstants.KAA_UPDATE_BUCKET_ID);
updateBucketStateStatement = database.compileStatement(
PersistentLogStorageConstants.KAA_UPDATE_BUCKET_ID);
}
updateBucketStateStatement.bindString(1, PersistentLogStorageConstants.BUCKET_STATE_COLUMN);
updateBucketStateStatement.bindString(
1, PersistentLogStorageConstants.BUCKET_STATE_COLUMN);
updateBucketStateStatement.bindLong(2, bucketId);
updateBucketStateStatement.execute();

long affectedRows = getAffectedRowCount();
if (affectedRows > 0) {
Log.i(TAG, "Successfully updated state for bucket ID [" + bucketId + "] for log records: " + affectedRows);
Log.i(TAG, "Successfully updated state for bucket ID [" + bucketId
+ "] for log records: " + affectedRows);
} else {
Log.w(TAG, "No log records were updated");
}

} catch (SQLiteException e) {
Log.e(TAG, "Failed to update state for bucket [" + bucketId + "]", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Failed to update state for bucket [" + bucketId + "]", ex);
}
}
}
Expand All @@ -222,10 +229,11 @@ public void removeBucket(int recordBlockId) {
Log.d(TAG, "Removing record block with id [" + recordBlockId + "] from storage");
if (deleteByBucketIdStatement == null) {
try {
deleteByBucketIdStatement = database.compileStatement(PersistentLogStorageConstants.KAA_DELETE_BY_BUCKET_ID);
} catch (SQLiteException e) {
Log.e(TAG, "Can't create record block deletion statement", e);
throw new RuntimeException(e);
deleteByBucketIdStatement = database.compileStatement(
PersistentLogStorageConstants.KAA_DELETE_BY_BUCKET_ID);
} catch (SQLiteException ex) {
Log.e(TAG, "Can't create record block deletion statement", ex);
throw new RuntimeException(ex);
}
}

Expand All @@ -237,12 +245,13 @@ public void removeBucket(int recordBlockId) {

if (removedRecordsCount > 0) {
totalRecordCount -= removedRecordsCount;
Log.i(TAG, "Removed " + removedRecordsCount + " records from storage. Total log record count: " + totalRecordCount);
Log.i(TAG, "Removed " + removedRecordsCount +
" records from storage. Total log record count: " + totalRecordCount);
} else {
Log.i(TAG, "No records were removed from storage");
}
} catch (SQLiteException e) {
Log.e(TAG, "Failed to remove record block with id [" + recordBlockId + "]", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Failed to remove record block with id [" + recordBlockId + "]", ex);
}
}
}
Expand All @@ -253,10 +262,11 @@ public void rollbackBucket(int bucketId) {
Log.d(TAG, "Notifying upload fail for bucket id: " + bucketId);
if (resetBucketIdStatement == null) {
try {
resetBucketIdStatement = database.compileStatement(PersistentLogStorageConstants.KAA_RESET_BY_BUCKET_ID);
} catch (SQLiteException e) {
Log.e(TAG, "Can't create bucket id reset statement", e);
throw new RuntimeException(e);
resetBucketIdStatement = database.compileStatement(
PersistentLogStorageConstants.KAA_RESET_BY_BUCKET_ID);
} catch (SQLiteException ex) {
Log.e(TAG, "Can't create bucket id reset statement", ex);
throw new RuntimeException(ex);
}
}

Expand All @@ -266,16 +276,17 @@ public void rollbackBucket(int bucketId) {

long affectedRows = getAffectedRowCount();
if (affectedRows > 0) {
Log.i(TAG, "Total " + affectedRows + " log records reset for bucket id: [" + bucketId + "]");
Log.i(TAG, "Total " + affectedRows + " log records reset for bucket id: ["
+ bucketId + "]");
long previouslyConsumedSize = consumedMemoryStorage.remove(bucketId);
unmarkedConsumedSize += previouslyConsumedSize;
unmarkedRecordCount += affectedRows;
} else {
Log.i(TAG, "No log records for bucket with id: [" + bucketId + "]");
}

} catch (SQLiteException e) {
Log.e(TAG, "Failed to reset bucket with id [" + bucketId + "]", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Failed to reset bucket with id [" + bucketId + "]", ex);
}
}
}
Expand Down Expand Up @@ -325,14 +336,14 @@ private void retrieveBucketId() {

this.currentBucketId = ++currentBucketId;
}
} catch (SQLiteException e) {
Log.e(TAG, "Can't create select max bucket ID statement", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Can't create select max bucket ID statement", ex);
throw new RuntimeException("Can't create select max bucket ID statement");
} finally {
try {
tryCloseCursor(cursor);
} catch (SQLiteException e) {
Log.e(TAG, "Unable to close cursor", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Unable to close cursor", ex);
}
}
}
Expand All @@ -345,7 +356,8 @@ private void retrieveConsumedSizeAndVolume() {
if (cursor.moveToFirst()) {
unmarkedRecordCount = totalRecordCount = cursor.getLong(0);
unmarkedConsumedSize = cursor.getLong(1);
Log.i(TAG, "Retrieved record count: " + totalRecordCount + ", consumed size: " + unmarkedConsumedSize);
Log.i(TAG, "Retrieved record count: " + totalRecordCount + ","
+ " consumed size: " + unmarkedConsumedSize);
} else {
Log.e(TAG, "Unable to retrieve consumed size and volume");
throw new RuntimeException("Unable to retrieve consumed size and volume");
Expand All @@ -366,8 +378,8 @@ private void truncateIfBucketSizeIncompatible() {
if (cursor.moveToFirst()) {
lastSavedBucketSize = cursor.getInt(0);
}
} catch (SQLiteException e) {
Log.e(TAG, "Cannot retrieve storage param: bucketSize", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Cannot retrieve storage param: bucketSize", ex);
throw new RuntimeException("Cannot retrieve storage param: bucketSize");
} finally {
tryCloseCursor(cursor);
Expand All @@ -379,8 +391,8 @@ private void truncateIfBucketSizeIncompatible() {
if (cursor.moveToFirst()) {
lastSavedRecordCount = cursor.getInt(0);
}
} catch (SQLiteException e) {
Log.e(TAG, "Cannot retrieve storage param: recordCount", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Cannot retrieve storage param: recordCount", ex);
throw new RuntimeException("Cannot retrieve storage param: recordCount");
} finally {
tryCloseCursor(cursor);
Expand All @@ -390,8 +402,8 @@ private void truncateIfBucketSizeIncompatible() {
if (lastSavedBucketSize != maxBucketSize || lastSavedRecordCount != maxRecordCount) {
database.execSQL(PersistentLogStorageConstants.KAA_DELETE_ALL_DATA);
}
} catch (SQLiteException e) {
Log.e(TAG, "Can't prepare delete statement", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Can't prepare delete statement", ex);
throw new RuntimeException("Can't prepare delete statement");
} finally {
tryCloseCursor(cursor);
Expand All @@ -403,16 +415,17 @@ private void truncateIfBucketSizeIncompatible() {
private void updateStorageParams() {
SQLiteStatement updateInfoStatement = null;
try {
updateInfoStatement = database.compileStatement(PersistentLogStorageConstants.KAA_UPDATE_STORAGE_INFO);
updateInfoStatement = database.compileStatement(
PersistentLogStorageConstants.KAA_UPDATE_STORAGE_INFO);
updateInfoStatement.bindString(1, PersistentLogStorageConstants.STORAGE_BUCKET_SIZE);
updateInfoStatement.bindLong(2, maxBucketSize);
updateInfoStatement.execute();

updateInfoStatement.bindString(1, PersistentLogStorageConstants.STORAGE_RECORD_COUNT);
updateInfoStatement.bindLong(2, maxRecordCount);
updateInfoStatement.execute();
} catch (SQLiteException e) {
Log.e(TAG, "Can't prepare update storage info statement", e);
} catch (SQLiteException ex) {
Log.e(TAG, "Can't prepare update storage info statement", ex);
throw new RuntimeException("Can't prepare update storage info statement");
} finally {
tryCloseStatement(updateInfoStatement);
Expand Down
Expand Up @@ -21,10 +21,10 @@
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataCollectionDBHelper extends SQLiteOpenHelper {
private static final String TAG = "DataCollectionDBHelper";
public class DataCollectionDbHelper extends SQLiteOpenHelper {
private static final String TAG = "DataCollectionDbHelper";

public DataCollectionDBHelper(Context context, String name) {
public DataCollectionDbHelper(Context context, String name) {
super(context, name, null, PersistentLogStorageConstants.DB_VERSION);
}

Expand All @@ -46,7 +46,8 @@ public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVers
Log.i(TAG, "Database was upgraded. Dropping its contents");

sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + PersistentLogStorageConstants.LOG_TABLE_NAME);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + PersistentLogStorageConstants.STORAGE_INFO_TABLE_NAME);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "
+ PersistentLogStorageConstants.STORAGE_INFO_TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
Expand Up @@ -35,17 +35,17 @@ public AndroidInternalPersistentStorage(Context context) {

@Override
public InputStream openForRead(String path) throws IOException {
File f = new File(context.getFilesDir(), path);
return new FileInputStream(f);
File file = new File(context.getFilesDir(), path);
return new FileInputStream(file);
}

@Override
public OutputStream openForWrite(String path) throws IOException {
File f = new File(context.getFilesDir(), path);
if (f.getParentFile() != null && !f.getParentFile().exists()) {
f.getParentFile().mkdirs();
File file = new File(context.getFilesDir(), path);
if (file.getParentFile() != null && !file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
return new FileOutputStream(f);
return new FileOutputStream(file);
}

@Override
Expand Down

0 comments on commit 3da8493

Please sign in to comment.