package com.tencent.mm.plugin.sns.storage;

import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.cd.h;
import com.tencent.mm.protocal.protobuf.axc;
import com.tencent.mm.protocal.protobuf.cbd;
import com.tencent.mm.sdk.e.j;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.ttpic.cache.VideoMemoryManager;
import com.tencent.ytcommon.BuildConfig;

public final class q extends j<p> {
    public static final String[] fnj = new String[]{j.a(p.ccO, "SnsReportKv")};
    public h fni;

    static {
        AppMethodBeat.i(37975);
        AppMethodBeat.o(37975);
    }

    public q(h hVar) {
        super(hVar, p.ccO, "SnsReportKv", p.diI);
        this.fni = hVar;
    }

    public final int a(cbd cbd) {
        AppMethodBeat.i(37972);
        cbd cbd2 = new cbd();
        int i = 0;
        int i2 = 0;
        for (int i3 = 0; i3 < cbd.vDW.size(); i3++) {
            axc axc = (axc) cbd.vDW.get(i3);
            if (axc.wBe.wR.length + i2 > VideoMemoryManager.VIDEO_CACHE_MEM_CACHE_MIN_SIZE_IN_KB) {
                a(cbd2, i2);
                i++;
                cbd2.vDW.clear();
                i2 = 0;
            } else {
                i2 += axc.wBe.wR.length;
                cbd2.vDW.add(axc);
            }
        }
        if (cbd2.vDW.size() > 0) {
            i++;
            a(cbd2, i2);
        }
        AppMethodBeat.o(37972);
        return i;
    }

    private int a(cbd cbd, int i) {
        AppMethodBeat.i(37973);
        try {
            byte[] toByteArray = cbd.toByteArray();
            p pVar = new p();
            pVar.field_value = toByteArray;
            pVar.field_logtime = System.currentTimeMillis();
            pVar.field_logsize = i;
            pVar.field_offset = 0;
            int insert = (int) this.fni.insert("SnsReportKv", BuildConfig.FLAVOR, pVar.Hl());
            ab.d("MicroMsg.SnsKvReportStg", "SnsKvReport Insert result ".concat(String.valueOf(insert)));
            AppMethodBeat.o(37973);
            return insert;
        } catch (Exception e) {
            AppMethodBeat.o(37973);
            return 0;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:38:0x01a5  */
    /* JADX WARNING: Removed duplicated region for block: B:44:0x0115 A:{SYNTHETIC, EDGE_INSN: B:44:0x0115->B:24:0x0115 ?: BREAK  } */
    /* JADX WARNING: Removed duplicated region for block: B:28:0x013c A:{LOOP_END, LOOP:2: B:26:0x0136->B:28:0x013c} */
    /* JADX WARNING: Removed duplicated region for block: B:45:0x0115 A:{SYNTHETIC} */
    public final com.tencent.mm.protocal.protobuf.cbd fr(int r14, int r15) {
        /*
        r13 = this;
        r12 = 37974; // 0x9456 float:5.3213E-41 double:1.87616E-319;
        r1 = 0;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r12);
        r0 = "select rowid, *  from SnsReportKv";
        r4 = new java.lang.StringBuffer;
        r4.<init>();
        if (r15 <= 0) goto L_0x0029;
    L_0x0011:
        r2 = new java.lang.StringBuilder;
        r2.<init>();
        r0 = r2.append(r0);
        r2 = " where rowid <= ";
        r0 = r0.append(r2);
        r0 = r0.append(r15);
        r0 = r0.toString();
    L_0x0029:
        r2 = r13.fni;
        r3 = 0;
        r5 = r2.a(r0, r3, r1);
        r6 = new com.tencent.mm.protocal.protobuf.cbd;
        r6.<init>();
        r7 = new java.util.ArrayList;
        r7.<init>();
        r0 = new java.lang.StringBuilder;
        r2 = "target size ";
        r0.<init>(r2);
        r0 = r0.append(r14);
        r2 = " current maxcolid ";
        r0 = r0.append(r2);
        r0 = r0.append(r15);
        r0 = r0.toString();
        r4.append(r0);
        r0 = r5.moveToFirst();
        if (r0 == 0) goto L_0x012f;
    L_0x005e:
        r0 = r1;
    L_0x005f:
        r8 = new com.tencent.mm.plugin.sns.storage.p;
        r8.<init>();
        r8.d(r5);
        r3 = r8.field_offset;
        r2 = "|offset: ";
        r9 = java.lang.String.valueOf(r3);
        r2 = r2.concat(r9);
        r4.append(r2);
        r9 = new com.tencent.mm.protocal.protobuf.cbd;
        r9.<init>();
        r2 = r8.field_value;	 Catch:{ Exception -> 0x01ab }
        r9.parseFrom(r2);	 Catch:{ Exception -> 0x01ab }
        r2 = r0;
    L_0x0082:
        r0 = r9.vDW;	 Catch:{ Exception -> 0x016f }
        r0 = r0.size();	 Catch:{ Exception -> 0x016f }
        if (r3 >= r0) goto L_0x01ae;
    L_0x008a:
        r0 = r9.vDW;	 Catch:{ Exception -> 0x016f }
        r0 = r0.get(r3);	 Catch:{ Exception -> 0x016f }
        r0 = (com.tencent.mm.protocal.protobuf.axc) r0;	 Catch:{ Exception -> 0x016f }
        r10 = r0.wBe;	 Catch:{ Exception -> 0x016f }
        r10 = r10.wR;	 Catch:{ Exception -> 0x016f }
        r10 = r10.length;	 Catch:{ Exception -> 0x016f }
        r10 = r10 + r2;
        if (r10 <= r14) goto L_0x014b;
    L_0x009a:
        if (r2 != 0) goto L_0x00cb;
    L_0x009c:
        r3 = r8.reX;	 Catch:{ Exception -> 0x016f }
        r3 = java.lang.Integer.valueOf(r3);	 Catch:{ Exception -> 0x016f }
        r7.add(r3);	 Catch:{ Exception -> 0x016f }
        r3 = "MicroMsg.SnsKvReportStg";
        r10 = new java.lang.StringBuilder;	 Catch:{ Exception -> 0x016f }
        r11 = "error by server for the mini size ";
        r10.<init>(r11);	 Catch:{ Exception -> 0x016f }
        r10 = r10.append(r14);	 Catch:{ Exception -> 0x016f }
        r11 = " vlauesize ";
        r10 = r10.append(r11);	 Catch:{ Exception -> 0x016f }
        r0 = r0.wBe;	 Catch:{ Exception -> 0x016f }
        r0 = r0.wR;	 Catch:{ Exception -> 0x016f }
        r0 = r0.length;	 Catch:{ Exception -> 0x016f }
        r0 = r10.append(r0);	 Catch:{ Exception -> 0x016f }
        r0 = r0.toString();	 Catch:{ Exception -> 0x016f }
        com.tencent.mm.sdk.platformtools.ab.i(r3, r0);	 Catch:{ Exception -> 0x016f }
    L_0x00cb:
        r0 = 1;
    L_0x00cc:
        r3 = new java.lang.StringBuilder;	 Catch:{ Exception -> 0x016f }
        r10 = "|read end on ";
        r3.<init>(r10);	 Catch:{ Exception -> 0x016f }
        r10 = r8.reX;	 Catch:{ Exception -> 0x016f }
        r3 = r3.append(r10);	 Catch:{ Exception -> 0x016f }
        r10 = " and get size ";
        r3 = r3.append(r10);	 Catch:{ Exception -> 0x016f }
        r3 = r3.append(r2);	 Catch:{ Exception -> 0x016f }
        r3 = r3.toString();	 Catch:{ Exception -> 0x016f }
        r4.append(r3);	 Catch:{ Exception -> 0x016f }
        if (r0 == 0) goto L_0x015f;
    L_0x00ee:
        r3 = r8.field_offset;	 Catch:{ Exception -> 0x016f }
        r9 = r9.vDW;	 Catch:{ Exception -> 0x016f }
        r9 = r9.size();	 Catch:{ Exception -> 0x016f }
        if (r3 > r9) goto L_0x015f;
    L_0x00f8:
        r3 = r8.reX;	 Catch:{ Exception -> 0x016f }
        r10 = (long) r3;	 Catch:{ Exception -> 0x016f }
        r13.a(r10, r8);	 Catch:{ Exception -> 0x016f }
        r3 = new java.lang.StringBuilder;	 Catch:{ Exception -> 0x016f }
        r9 = "|update new offset ";
        r3.<init>(r9);	 Catch:{ Exception -> 0x016f }
        r9 = r8.field_offset;	 Catch:{ Exception -> 0x016f }
        r3 = r3.append(r9);	 Catch:{ Exception -> 0x016f }
        r3 = r3.toString();	 Catch:{ Exception -> 0x016f }
        r4.append(r3);	 Catch:{ Exception -> 0x016f }
    L_0x0113:
        if (r0 == 0) goto L_0x01a5;
    L_0x0115:
        r0 = "MicroMsg.SnsKvReportStg";
        r1 = new java.lang.StringBuilder;
        r2 = "read info ";
        r1.<init>(r2);
        r2 = r4.toString();
        r1 = r1.append(r2);
        r1 = r1.toString();
        com.tencent.mm.sdk.platformtools.ab.i(r0, r1);
    L_0x012f:
        r5.close();
        r1 = r7.iterator();
    L_0x0136:
        r0 = r1.hasNext();
        if (r0 == 0) goto L_0x01a7;
    L_0x013c:
        r0 = r1.next();
        r0 = (java.lang.Integer) r0;
        r0 = r0.intValue();
        r2 = (long) r0;
        r13.delete(r2);
        goto L_0x0136;
    L_0x014b:
        r10 = r3 + 1;
        r8.field_offset = r10;	 Catch:{ Exception -> 0x016f }
        r10 = r6.vDW;	 Catch:{ Exception -> 0x016f }
        r10.add(r0);	 Catch:{ Exception -> 0x016f }
        r0 = r0.wBe;	 Catch:{ Exception -> 0x016f }
        r0 = r0.wR;	 Catch:{ Exception -> 0x016f }
        r0 = r0.length;	 Catch:{ Exception -> 0x016f }
        r2 = r2 + r0;
        r0 = r3 + 1;
        r3 = r0;
        goto L_0x0082;
    L_0x015f:
        r3 = "|read full ";
        r4.append(r3);	 Catch:{ Exception -> 0x016f }
        r3 = r8.reX;	 Catch:{ Exception -> 0x016f }
        r3 = java.lang.Integer.valueOf(r3);	 Catch:{ Exception -> 0x016f }
        r7.add(r3);	 Catch:{ Exception -> 0x016f }
        goto L_0x0113;
    L_0x016f:
        r3 = move-exception;
        r0 = r2;
    L_0x0171:
        r2 = "MicroMsg.SnsKvReportStg";
        r9 = "";
        r10 = new java.lang.Object[r1];
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r2, r3, r9, r10);
        r2 = r8.reX;
        r2 = java.lang.Integer.valueOf(r2);
        r7.add(r2);
        r2 = "MicroMsg.SnsKvReportStg";
        r3 = new java.lang.StringBuilder;
        r9 = "error paser then delete ";
        r3.<init>(r9);
        r8 = r8.reX;
        r3 = r3.append(r8);
        r3 = r3.toString();
        com.tencent.mm.sdk.platformtools.ab.i(r2, r3);
    L_0x019d:
        r2 = r5.moveToNext();
        if (r2 != 0) goto L_0x005f;
    L_0x01a3:
        goto L_0x0115;
    L_0x01a5:
        r0 = r2;
        goto L_0x019d;
    L_0x01a7:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r12);
        return r6;
    L_0x01ab:
        r2 = move-exception;
        r3 = r2;
        goto L_0x0171;
    L_0x01ae:
        r0 = r1;
        goto L_0x00cc;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.plugin.sns.storage.q.fr(int, int):com.tencent.mm.protocal.protobuf.cbd");
    }
}
