<commit_before>package com.simperium.android;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.simperium.client.Bucket;
import com.simperium.client.Change;
import com.simperium.client.Channel;
import com.simperium.util.Logger;

import org.json.JSONException;
import org.json.JSONObject;

public class QueueSerializer implements Channel.Serializer {

    public static final String TAG = "Simperium.QueueSerializer";

    static public final String TABLE_NAME      = "queue";
    static public final String FIELD_BUCKET    = "bucket";
    static public final String FIELD_KEY       = "key";
    static public final String FIELD_STATUS    = "status";
    static public final String FIELD_VERSION   = "version";
    static public final String FIELD_OPERATION = "operation";
    static public final String FIELD_ORIGIN    = "origin";
    static public final String FIELD_TARGET    = "target";
    static public final String FIELD_CCID      = "ccid";

    protected SQLiteDatabase mDatabase;

    private enum Status {
        QUEUED("Q"), PENDING("P");

        protected String mStatus;

        Status(String status){
            mStatus = status;
        }

        @Override
        public String toString(){
            return mStatus;
        }
    }

    public QueueSerializer(SQLiteDatabase database) {
        mDatabase = database;
        prepare();
    }

    private void prepare(){
        // create the table for the database
        // bucket, key, status, version number, operation and diff
        mDatabase.execSQL(String.format("CREATE TABLE IF NOT EXISTS %s (%s, %s, %s, %s, %s, %s, %s, %s)",
            TABLE_NAME, FIELD_BUCKET, FIELD_KEY, FIELD_STATUS, FIELD_VERSION, FIELD_OPERATION,
            FIELD_ORIGIN, FIELD_TARGET, FIELD_CCID));

        // searching by bucket and key
        mDatabase.execSQL(String.format("CREATE INDEX IF NOT EXISTS queue_object_key ON %s (%s, %s)",
            TABLE_NAME, FIELD_BUCKET, FIELD_KEY));

        // searching by bucket and status
        mDatabase.execSQL(String.format("CREATE INDEX IF NOT EXISTS queue_status ON %s (%s, %s)",
            TABLE_NAME, FIELD_BUCKET, FIELD_STATUS));

        // searching by bucket and status
        mDatabase.execSQL(String.format("CREATE INDEX IF NOT EXISTS queue_ccid ON %s (%s, %s)",
            TABLE_NAME, FIELD_BUCKET, FIELD_CCID));
    }

    private static final String QUERY_CLAUSE = String.format("%s = ?", FIELD_BUCKET);
    @Override
    public Channel.SerializedQueue restore(Bucket bucket) {
        Channel.SerializedQueue queue = new Channel.SerializedQueue();
        // public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
        String[] args = new String[]{ bucket.getName() };
        Cursor items = mDatabase.query(TABLE_NAME, null, QUERY_CLAUSE, args, null, null, null);

        try {

            int bucketColumn = items.getColumnIndexOrThrow(FIELD_BUCKET);
            int keyColumn = items.getColumnIndexOrThrow(FIELD_KEY);
            int statusColumn = items.getColumnIndexOrThrow(FIELD_STATUS);
            int versionColumn = items.getColumnIndexOrThrow(FIELD_VERSION);
            int operationColumn = items.getColumnIndexOrThrow(FIELD_OPERATION);
            int originColumn = items.getColumnIndexOrThrow(FIELD_ORIGIN);
            int targetColumn = items.getColumnIndexOrThrow(FIELD_TARGET);
            int ccidColumn = items.getColumnIndexOrThrow(FIELD_CCID);

            String key = null;
            String operation = null;
            String status = null;
            JSONObject origin = null;
            JSONObject target = null;

            while(items.moveToNext()){
                try {
                    key = items.getString(keyColumn);
                    operation = items.getString(operationColumn);
                    status = items.getString(statusColumn);

                    if (operation.equals(Change.OPERATION_MODIFY)) {
                        origin = new JSONObject(items.getString(originColumn));
                        target = new JSONObject(items.getString(targetColumn));
                    } else {
                        origin = null;
                        target = null;
                    }

                    Change change = Change.buildChange(operation, items.getString(ccidColumn),
                        items.getString(bucketColumn), key, items.getInt(versionColumn), origin, target);

                    if (status.equals(Status.QUEUED.toString())) {
                        queue.queued.add(change);
                    } else if(status.equals(Status.PENDING.toString())) {
                        queue.pending.put(key, change);
                    }
                } catch (JSONException e) {
                    Logger.log(TAG, String.format("Failed to deserialize item", e));
                }
            }

        } catch (IllegalArgumentException e) {
            Logger.log(TAG, "Could not restore queue, invalid table columns", e);
            return null;
        }

        return queue;
    }

    @Override
    public void reset(Bucket bucket) {
        // delete everything from the queue for this bucket
    }

    @Override
    public void onQueueChange(Change change) {
        // insert the record into the db
        insertState(Status.QUEUED, change);
    }

    @Override
    public void onDequeueChange(Change change) {
        // change will not be sent, remove queued status
        removeState(Status.QUEUED, change);
    }

    @Override
    public void onSendChange(Change change) {
        // change was sent, mark as pending<commit_msg>Fixing warnings<commit_after>package com.simperium.android;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.simperium.client.Bucket;
import com.simperium.client.Change;
import com.simperium.client.Channel;
import com.simperium.util.Logger;

import org.json.JSONException;
import org.json.JSONObject;

public class QueueSerializer implements Channel.Serializer {

    public static final String TAG = "Simperium.QueueSerializer";

    static public final String TABLE_NAME      = "queue";
    static public final String FIELD_BUCKET    = "bucket";
    static public final String FIELD_KEY       = "key";
    static public final String FIELD_STATUS    = "status";
    static public final String FIELD_VERSION   = "version";
    static public final String FIELD_OPERATION = "operation";
    static public final String FIELD_ORIGIN    = "origin";
    static public final String FIELD_TARGET    = "target";
    static public final String FIELD_CCID      = "ccid";

    protected SQLiteDatabase mDatabase;

    private enum Status {
        QUEUED("Q"), PENDING("P");

        protected String mStatus;

        Status(String status){
            mStatus = status;
        }

        @Override
        public String toString(){
            return mStatus;
        }
    }

    public QueueSerializer(SQLiteDatabase database) {
        mDatabase = database;
        prepare();
    }

    private void prepare(){
        // create the table for the database
        // bucket, key, status, version number, operation and diff
        mDatabase.execSQL(String.format("CREATE TABLE IF NOT EXISTS %s (%s, %s, %s, %s, %s, %s, %s, %s)",
            TABLE_NAME, FIELD_BUCKET, FIELD_KEY, FIELD_STATUS, FIELD_VERSION, FIELD_OPERATION,
            FIELD_ORIGIN, FIELD_TARGET, FIELD_CCID));

        // searching by bucket and key
        mDatabase.execSQL(String.format("CREATE INDEX IF NOT EXISTS queue_object_key ON %s (%s, %s)",
            TABLE_NAME, FIELD_BUCKET, FIELD_KEY));

        // searching by bucket and status
        mDatabase.execSQL(String.format("CREATE INDEX IF NOT EXISTS queue_status ON %s (%s, %s)",
            TABLE_NAME, FIELD_BUCKET, FIELD_STATUS));

        // searching by bucket and status
        mDatabase.execSQL(String.format("CREATE INDEX IF NOT EXISTS queue_ccid ON %s (%s, %s)",
            TABLE_NAME, FIELD_BUCKET, FIELD_CCID));
    }

    private static final String QUERY_CLAUSE = String.format("%s = ?", FIELD_BUCKET);
    @Override
    public Channel.SerializedQueue restore(Bucket bucket) {
        Channel.SerializedQueue queue = new Channel.SerializedQueue();
        // public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
        String[] args = new String[]{ bucket.getName() };
        Cursor items = mDatabase.query(TABLE_NAME, null, QUERY_CLAUSE, args, null, null, null);

        try {

            int bucketColumn = items.getColumnIndexOrThrow(FIELD_BUCKET);
            int keyColumn = items.getColumnIndexOrThrow(FIELD_KEY);
            int statusColumn = items.getColumnIndexOrThrow(FIELD_STATUS);
            int versionColumn = items.getColumnIndexOrThrow(FIELD_VERSION);
            int operationColumn = items.getColumnIndexOrThrow(FIELD_OPERATION);
            int originColumn = items.getColumnIndexOrThrow(FIELD_ORIGIN);
            int targetColumn = items.getColumnIndexOrThrow(FIELD_TARGET);
            int ccidColumn = items.getColumnIndexOrThrow(FIELD_CCID);

            String key;
            String operation;
            String status;
            JSONObject origin;
            JSONObject target;

            while(items.moveToNext()){
                try {
                    key = items.getString(keyColumn);
                    operation = items.getString(operationColumn);
                    status = items.getString(statusColumn);

                    if (operation.equals(Change.OPERATION_MODIFY)) {
                        origin = new JSONObject(items.getString(originColumn));
                        target = new JSONObject(items.getString(targetColumn));
                    } else {
                        origin = null;
                        target = null;
                    }

                    Change change = Change.buildChange(operation, items.getString(ccidColumn),
                        items.getString(bucketColumn), key, items.getInt(versionColumn), origin, target);

                    if (status.equals(Status.QUEUED.toString())) {
                        queue.queued.add(change);
                    } else if(status.equals(Status.PENDING.toString())) {
                        queue.pending.put(key, change);
                    }
                } catch (JSONException e) {
                    Logger.log(TAG, String.format("Failed to deserialize item %s", e));
                }
            }

        } catch (IllegalArgumentException e) {
            Logger.log(TAG, "Could not restore queue, invalid table columns", e);
            return null;
        }

        return queue;
    }

    @Override
    public void reset(Bucket bucket) {
        // delete everything from the queue for this bucket
    }

    @Override
    public void onQueueChange(Change change) {
        // insert the record into the db
        insertState(Status.QUEUED, change);
    }

    @Override
    public void onDequeueChange(Change change) {
        // change will not be sent, remove queued status
        removeState(Status.QUEUED, change);
    }

    @Override
    public void onSendChange(Change change) {
        // change was sent, mark as pending
{'fraction_non_alphanumeric': 0.08854166666666667, 'fraction_numerical': 0.0, 'mean_word_length': 2.2832715873468223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7321066', 'n_tokens_mistral': 3051, 'n_tokens_neox': 2950, 'n_words': 814}