Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f442477a authored by Hans Boehm's avatar Hans Boehm
Browse files

Clean up database access operations

Bug: 33251173

Move write and erase operations to a background thread.

Fix interrupt handling during database initialization. Simplify
initialization synchronization.

Initialize a Cursor at startup and use it for most read operations.
Continue to perform accesses on negative indices directly, if they
would miss the CursorWindow. This avoids a somewhat likely situation
in which we're bounding between windows.

Add some code to gracefully degrade in the event of a database failure.
This is currently disabled.

Test: The alternate direct read path was minimally tested by temporarily
modifying the code. Manual tests were run.

Change-Id: I2112f73ca43cc5f855da98d7f9b1c3beaed3f610
parent 2976d21d
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -22,8 +22,10 @@ import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.TtsSpan;

import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
@@ -411,6 +413,21 @@ class CalculatorExpr {
        }
    }

    /**
     * Use write() above to generate a byte array containing a serialized representation of
     * this expression.
     */
    public byte[] toBytes() {
        ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
        try (DataOutputStream out = new DataOutputStream(byteArrayStream)) {
            write(out);
        } catch (IOException e) {
            // Impossible; No IO involved.
            throw new AssertionError("Impossible IO exception", e);
        }
        return byteArrayStream.toByteArray();
    }

    /**
     * Does this expression end with a numeric constant?
     * As opposed to an operator or preevaluated expression.
+1 −9
Original line number Diff line number Diff line
@@ -1407,15 +1407,7 @@ public class Evaluator implements CalculatorExpr.ExprResolver {
     * If in_history is true, add it with a positive index, so it will appear in the history.
     */
    private long addToDB(boolean in_history, ExprInfo ei) {
        /* TODO: Possibly do this in a different thread. */
        ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
        try (DataOutputStream out = new DataOutputStream(byteArrayStream)) {
            ei.mExpr.write(out);
        } catch (IOException e) {
            // Impossible; No IO involved.
            throw new AssertionError("Impossible IO exception", e);
        }
        byte[] serializedExpr = byteArrayStream.toByteArray();
        byte[] serializedExpr = ei.mExpr.toBytes();
        ExpressionDB.RowData rd = new ExpressionDB.RowData(serializedExpr, ei.mDegreeMode,
                ei.mLongTimeout, 0);
        long resultIndex = mExprDB.addRow(!in_history, rd);