|
11 | 11 | import com.simperium.client.GhostStorageProvider;
|
12 | 12 | import com.simperium.util.Logger;
|
13 | 13 |
|
| 14 | +import org.json.JSONException; |
14 | 15 | import org.json.JSONObject;
|
15 | 16 |
|
16 | 17 | public class GhostStore implements GhostStorageProvider {
|
@@ -119,21 +120,21 @@ public void saveGhost(Bucket bucket, Ghost ghost) {
|
119 | 120 |
|
120 | 121 | @Override
|
121 | 122 | public Ghost getGhost(Bucket bucket, String key) throws GhostMissingException {
|
122 |
| - // public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) |
123 | 123 | String[] columns = { BUCKET_NAME_FIELD, OBJECT_KEY_FIELD, VERSION_FIELD, PAYLOAD_FIELD };
|
124 | 124 | String where = "bucketName=? AND simperiumKey=?";
|
125 | 125 | String[] args = { bucket.getName(), key };
|
126 |
| - Cursor cursor = database.query(GHOSTS_TABLE_NAME, columns, where, args, null, null, null); |
127 |
| - Ghost ghost = null; |
128 |
| - if (cursor.getCount() > 0) { |
129 |
| - cursor.moveToFirst(); |
130 |
| - ghost = new Ghost(cursor.getString(1), cursor.getInt(2), deserializeGhostData(cursor.getString(3))); |
131 |
| - } |
132 |
| - cursor.close(); |
133 |
| - if (ghost == null) { |
134 |
| - throw(new GhostMissingException(String.format("Ghost %s does not exist for bucket %s", bucket.getName(), key))); |
| 126 | + |
| 127 | + try (Cursor cursor = database.query(GHOSTS_TABLE_NAME, columns, where, args, null, null, null)) { |
| 128 | + if (cursor.moveToFirst()) { |
| 129 | + JSONObject ghostData = new JSONObject(cursor.getString(3)); |
| 130 | + return new Ghost(cursor.getString(1), cursor.getInt(2), ghostData); |
| 131 | + } |
| 132 | + } catch (org.json.JSONException e){ |
| 133 | + // a corrupted ghost is effectively equal to a missing ghost |
| 134 | + // so pass through here and let the library request a new copy |
135 | 135 | }
|
136 |
| - return ghost; |
| 136 | + |
| 137 | + throw(new GhostMissingException(String.format("Ghost %s does not exist for bucket %s", bucket.getName(), key))); |
137 | 138 | }
|
138 | 139 |
|
139 | 140 | @Override
|
|
0 commit comments