Compare commits

..

10 Commits

9 changed files with 154 additions and 80 deletions

View File

@@ -67,6 +67,8 @@ public class BaseAndroidTest
protected ModelFactory modelFactory; protected ModelFactory modelFactory;
private boolean isDone = false;
@Before @Before
public void setUp() public void setUp()
{ {
@@ -117,6 +119,25 @@ public class BaseAndroidTest
assertTrue(latch.await(60, TimeUnit.SECONDS)); assertTrue(latch.await(60, TimeUnit.SECONDS));
} }
protected void runConcurrently(Runnable... runnableList) throws Exception
{
isDone = false;
ExecutorService executor = Executors.newFixedThreadPool(100);
List<Future> futures = new LinkedList<>();
for (Runnable r : runnableList)
futures.add(executor.submit(() ->
{
while (!isDone) r.run();
return null;
}));
Thread.sleep(3000);
isDone = true;
executor.shutdown();
for(Future f : futures) f.get();
while (!executor.isTerminated()) Thread.sleep(50);
}
protected void setTheme(@StyleRes int themeId) protected void setTheme(@StyleRes int themeId)
{ {
targetContext.setTheme(themeId); targetContext.setTheme(themeId);

View File

@@ -61,29 +61,6 @@ public class SQLiteScoreListTest extends BaseAndroidTest
day = DateUtils.millisecondsInOneDay; day = DateUtils.millisecondsInOneDay;
} }
@Test
public void testGetAll()
{
List<Score> list = scores.toList();
assertThat(list.size(), equalTo(121));
assertThat(list.get(0).getTimestamp(), equalTo(today));
assertThat(list.get(10).getTimestamp(), equalTo(today - 10 * day));
}
@Test
public void testInvalidateNewerThan()
{
scores.getTodayValue(); // force recompute
List<ScoreRecord> records = getAllRecords();
assertThat(records.size(), equalTo(121));
scores.invalidateNewerThan(today - 10 * day);
records = getAllRecords();
assertThat(records.size(), equalTo(110));
assertThat(records.get(0).timestamp, equalTo(today - 11 * day));
}
@Test @Test
public void testAdd() public void testAdd()
{ {
@@ -101,6 +78,15 @@ public class SQLiteScoreListTest extends BaseAndroidTest
assertThat(records.get(0).timestamp, equalTo(today)); assertThat(records.get(0).timestamp, equalTo(today));
} }
@Test
public void testGetAll()
{
List<Score> list = scores.toList();
assertThat(list.size(), equalTo(121));
assertThat(list.get(0).getTimestamp(), equalTo(today));
assertThat(list.get(10).getTimestamp(), equalTo(today - 10 * day));
}
@Test @Test
public void testGetByInterval() public void testGetByInterval()
{ {
@@ -115,6 +101,16 @@ public class SQLiteScoreListTest extends BaseAndroidTest
assertThat(list.get(7).getTimestamp(), equalTo(today - 10 * day)); assertThat(list.get(7).getTimestamp(), equalTo(today - 10 * day));
} }
@Test
public void testGetByInterval_concurrent() throws Exception
{
Runnable block1 = () -> scores.invalidateNewerThan(0);
Runnable block2 =
() -> assertThat(scores.getByInterval(today, today).size(),
equalTo(1));
runConcurrently(block1, block2);
}
@Test @Test
public void testGetByInterval_withLongInterval() public void testGetByInterval_withLongInterval()
{ {
@@ -125,6 +121,30 @@ public class SQLiteScoreListTest extends BaseAndroidTest
assertThat(list.size(), equalTo(201)); assertThat(list.size(), equalTo(201));
} }
@Test
public void testGetTodayValue_concurrent() throws Exception
{
Runnable block1 = () -> scores.invalidateNewerThan(0);
Runnable block2 =
() -> assertThat(scores.getTodayValue(), equalTo(18407827));
runConcurrently(block1, block2);
}
@Test
public void testInvalidateNewerThan()
{
scores.getTodayValue(); // force recompute
List<ScoreRecord> records = getAllRecords();
assertThat(records.size(), equalTo(121));
scores.invalidateNewerThan(today - 10 * day);
records = getAllRecords();
assertThat(records.size(), equalTo(110));
assertThat(records.get(0).timestamp, equalTo(today - 11 * day));
}
private List<ScoreRecord> getAllRecords() private List<ScoreRecord> getAllRecords()
{ {
return new Select() return new Select()

View File

@@ -21,8 +21,8 @@
<manifest <manifest
package="org.isoron.uhabits" package="org.isoron.uhabits"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="30" android:versionCode="32"
android:versionName="1.7.3"> android:versionName="1.7.5">
<uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.VIBRATE"/>

View File

@@ -256,7 +256,7 @@ public class FrequencyChart extends ScrollableChart
float scale = 1.0f/maxFreq * value; float scale = 1.0f/maxFreq * value;
float radius = maxRadius * scale; float radius = maxRadius * scale;
int colorIndex = Math.round((colors.length-1) * scale); int colorIndex = Math.min(colors.length - 1, Math.round((colors.length - 1) * scale));
pGraph.setColor(colors[colorIndex]); pGraph.setColor(colors[colorIndex]);
canvas.drawCircle(rect.centerX(), rect.centerY(), radius, pGraph); canvas.drawCircle(rect.centerX(), rect.centerY(), radius, pGraph);
} }

View File

@@ -81,7 +81,7 @@ public abstract class ScoreList implements Iterable<Score>
* @param timestamp the timestamp of a day * @param timestamp the timestamp of a day
* @return score value for that day * @return score value for that day
*/ */
public final int getValue(long timestamp) public synchronized final int getValue(long timestamp)
{ {
compute(timestamp, timestamp); compute(timestamp, timestamp);
Score s = getComputedByTimestamp(timestamp); Score s = getComputedByTimestamp(timestamp);

View File

@@ -38,6 +38,12 @@ import java.util.*;
public class SQLiteCheckmarkList extends CheckmarkList public class SQLiteCheckmarkList extends CheckmarkList
{ {
private static final String ADD_QUERY =
"insert into Checkmarks(habit, timestamp, value) values (?,?,?)";
private static final String INVALIDATE_QUERY =
"delete from Checkmarks where habit = ? and timestamp >= ?";
@Nullable @Nullable
private HabitRecord habitRecord; private HabitRecord habitRecord;
@@ -45,7 +51,7 @@ public class SQLiteCheckmarkList extends CheckmarkList
private final SQLiteUtils<CheckmarkRecord> sqlite; private final SQLiteUtils<CheckmarkRecord> sqlite;
@Nullable @Nullable
private Integer todayValue; private CachedData cache;
@NonNull @NonNull
private final SQLiteStatement invalidateStatement; private final SQLiteStatement invalidateStatement;
@@ -56,12 +62,6 @@ public class SQLiteCheckmarkList extends CheckmarkList
@NonNull @NonNull
private final SQLiteDatabase db; private final SQLiteDatabase db;
private static final String ADD_QUERY =
"insert into Checkmarks(habit, timestamp, value) values (?,?,?)";
private static final String INVALIDATE_QUERY =
"delete from Checkmarks where habit = ? and timestamp >= ?";
public SQLiteCheckmarkList(Habit habit) public SQLiteCheckmarkList(Habit habit)
{ {
super(habit); super(habit);
@@ -102,8 +102,7 @@ public class SQLiteCheckmarkList extends CheckmarkList
check(habit.getId()); check(habit.getId());
compute(fromTimestamp, toTimestamp); compute(fromTimestamp, toTimestamp);
String query = "select habit, timestamp, value " + String query = "select habit, timestamp, value from checkmarks " +
"from checkmarks " +
"where habit = ? and timestamp >= ? and timestamp <= ? " + "where habit = ? and timestamp >= ? and timestamp <= ? " +
"order by timestamp desc"; "order by timestamp desc";
@@ -127,10 +126,19 @@ public class SQLiteCheckmarkList extends CheckmarkList
return toCheckmarks(records); return toCheckmarks(records);
} }
@Override
public int getTodayValue()
{
if (cache == null || cache.expired())
cache = new CachedData(super.getTodayValue());
return cache.todayValue;
}
@Override @Override
public void invalidateNewerThan(long timestamp) public void invalidateNewerThan(long timestamp)
{ {
todayValue = null; cache = null;
invalidateStatement.bindLong(1, habit.getId()); invalidateStatement.bindLong(1, habit.getId());
invalidateStatement.bindLong(2, timestamp); invalidateStatement.bindLong(2, timestamp);
invalidateStatement.execute(); invalidateStatement.execute();
@@ -142,10 +150,8 @@ public class SQLiteCheckmarkList extends CheckmarkList
protected Checkmark getNewestComputed() protected Checkmark getNewestComputed()
{ {
check(habit.getId()); check(habit.getId());
String query = "select habit, timestamp, value " + String query = "select habit, timestamp, value from checkmarks " +
"from checkmarks " + "where habit = ? " + "order by timestamp desc " +
"where habit = ? " +
"order by timestamp desc " +
"limit 1"; "limit 1";
String params[] = { Long.toString(habit.getId()) }; String params[] = { Long.toString(habit.getId()) };
@@ -157,10 +163,8 @@ public class SQLiteCheckmarkList extends CheckmarkList
protected Checkmark getOldestComputed() protected Checkmark getOldestComputed()
{ {
check(habit.getId()); check(habit.getId());
String query = "select habit, timestamp, value " + String query = "select habit, timestamp, value from checkmarks " +
"from checkmarks " + "where habit = ? " + "order by timestamp asc " +
"where habit = ? " +
"order by timestamp asc " +
"limit 1"; "limit 1";
String params[] = { Long.toString(habit.getId()) }; String params[] = { Long.toString(habit.getId()) };
@@ -194,10 +198,21 @@ public class SQLiteCheckmarkList extends CheckmarkList
return checkmarks; return checkmarks;
} }
@Override private static class CachedData
public int getTodayValue()
{ {
if(todayValue == null) todayValue = super.getTodayValue(); int todayValue;
return todayValue;
private long today;
CachedData(int todayValue)
{
this.todayValue = todayValue;
this.today = DateUtils.getStartOfToday();
}
boolean expired()
{
return today != DateUtils.getStartOfToday();
}
} }
} }

View File

@@ -270,9 +270,7 @@ public class SQLiteHabitList extends HabitList
for (HabitRecord record : recordList) for (HabitRecord record : recordList)
{ {
Habit habit = getById(record.getId()); Habit habit = getById(record.getId());
if (habit == null) if (habit == null) continue;
throw new RuntimeException("habit not in database");
if (!filter.matches(habit)) continue; if (!filter.matches(habit)) continue;
habits.add(habit); habits.add(habit);
} }

View File

@@ -27,6 +27,7 @@ import com.activeandroid.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.models.sqlite.records.*; import org.isoron.uhabits.models.sqlite.records.*;
import org.isoron.uhabits.utils.*;
import org.jetbrains.annotations.*; import org.jetbrains.annotations.*;
import java.util.*; import java.util.*;
@@ -36,6 +37,11 @@ import java.util.*;
*/ */
public class SQLiteScoreList extends ScoreList public class SQLiteScoreList extends ScoreList
{ {
public static final String ADD_QUERY =
"insert into Score(habit, timestamp, score) values (?,?,?)";
public static final String INVALIDATE_QUERY =
"delete from Score where habit = ? and timestamp >= ?";
@Nullable @Nullable
private HabitRecord habitRecord; private HabitRecord habitRecord;
@@ -43,23 +49,17 @@ public class SQLiteScoreList extends ScoreList
@NonNull @NonNull
private final SQLiteUtils<ScoreRecord> sqlite; private final SQLiteUtils<ScoreRecord> sqlite;
@Nullable
private Integer todayValue;
@NonNull @NonNull
private final SQLiteStatement invalidateStatement; private final SQLiteStatement invalidateStatement;
@NonNull @NonNull
private final SQLiteStatement addStatement; private final SQLiteStatement addStatement;
public static final String ADD_QUERY =
"insert into Score(habit, timestamp, score) values (?,?,?)";
public static final String INVALIDATE_QUERY =
"delete from Score where habit = ? " + "and timestamp >= ?";
private final SQLiteDatabase db; private final SQLiteDatabase db;
@Nullable
private CachedData cache = null;
/** /**
* Constructs a new ScoreList associated with the given habit. * Constructs a new ScoreList associated with the given habit.
* *
@@ -100,20 +100,20 @@ public class SQLiteScoreList extends ScoreList
@NonNull @NonNull
@Override @Override
public List<Score> getByInterval(long fromTimestamp, long toTimestamp) public synchronized List<Score> getByInterval(long fromTimestamp,
long toTimestamp)
{ {
check(habit.getId()); check(habit.getId());
compute(fromTimestamp, toTimestamp); compute(fromTimestamp, toTimestamp);
String query = "select habit, timestamp, score " + String query = "select habit, timestamp, score from Score " +
"from Score " + "where habit = ? and timestamp >= ? and timestamp <= ? " +
"where habit = ? and timestamp >= ? and timestamp <= ? " + "order by timestamp desc";
"order by timestamp desc";
String params[] = { String params[] = {
Long.toString(habit.getId()), Long.toString(habit.getId()),
Long.toString(fromTimestamp), Long.toString(fromTimestamp),
Long.toString(toTimestamp) Long.toString(toTimestamp)
}; };
List<ScoreRecord> records = sqlite.query(query, params); List<ScoreRecord> records = sqlite.query(query, params);
@@ -138,9 +138,18 @@ public class SQLiteScoreList extends ScoreList
} }
@Override @Override
public void invalidateNewerThan(long timestamp) public synchronized int getTodayValue()
{ {
todayValue = null; if (cache == null || cache.expired())
cache = new CachedData(super.getTodayValue());
return cache.todayValue;
}
@Override
public synchronized void invalidateNewerThan(long timestamp)
{
cache = null;
invalidateStatement.bindLong(1, habit.getId()); invalidateStatement.bindLong(1, habit.getId());
invalidateStatement.bindLong(2, timestamp); invalidateStatement.bindLong(2, timestamp);
invalidateStatement.execute(); invalidateStatement.execute();
@@ -171,8 +180,7 @@ public class SQLiteScoreList extends ScoreList
{ {
check(habit.getId()); check(habit.getId());
String query = "select habit, timestamp, score from Score " + String query = "select habit, timestamp, score from Score " +
"where habit = ? order by timestamp desc " + "where habit = ? order by timestamp desc limit 1";
"limit 1";
String params[] = { Long.toString(habit.getId()) }; String params[] = { Long.toString(habit.getId()) };
return getScoreFromQuery(query, params); return getScoreFromQuery(query, params);
@@ -184,8 +192,7 @@ public class SQLiteScoreList extends ScoreList
{ {
check(habit.getId()); check(habit.getId());
String query = "select habit, timestamp, score from Score " + String query = "select habit, timestamp, score from Score " +
"where habit = ? order by timestamp asc " + "where habit = ? order by timestamp asc limit 1";
"limit 1";
String params[] = { Long.toString(habit.getId()) }; String params[] = { Long.toString(habit.getId()) };
return getScoreFromQuery(query, params); return getScoreFromQuery(query, params);
@@ -217,10 +224,21 @@ public class SQLiteScoreList extends ScoreList
return scores; return scores;
} }
@Override private static class CachedData
public int getTodayValue()
{ {
if (todayValue == null) todayValue = super.getTodayValue(); int todayValue;
return todayValue;
private long today;
CachedData(int todayValue)
{
this.todayValue = todayValue;
this.today = DateUtils.getStartOfToday();
}
boolean expired()
{
return today != DateUtils.getStartOfToday();
}
} }
} }

View File

@@ -28,6 +28,8 @@ import org.isoron.uhabits.utils.*;
import javax.inject.*; import javax.inject.*;
import static org.isoron.uhabits.utils.DateUtils.*;
@ReceiverScope @ReceiverScope
public class ReminderController public class ReminderController
{ {
@@ -66,7 +68,7 @@ public class ReminderController
{ {
long snoozeInterval = preferences.getSnoozeInterval(); long snoozeInterval = preferences.getSnoozeInterval();
long now = DateUtils.getLocalTime(); long now = applyTimezone(getLocalTime());
long reminderTime = now + snoozeInterval * 60 * 1000; long reminderTime = now + snoozeInterval * 60 * 1000;
reminderScheduler.schedule(habit, reminderTime); reminderScheduler.schedule(habit, reminderTime);