Skip to content

Commit

Permalink
feat: sql调整&代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
lemondark committed Feb 23, 2024
1 parent 7474bbe commit b558d67
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 43 deletions.
9 changes: 5 additions & 4 deletions src/main/java/com/tencent/msdk/dns/core/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ public static Cache getInstance() {
public void readFromDb() {
if (getCachedIpEnable()) {
List<LookupCache> allCache = cacheDbHelper.getAll();
ArrayList<LookupCache> expired = new ArrayList<>();
ArrayList<String> expiredList = new ArrayList<>();
for (LookupCache lookupCache : allCache) {
mHostnameIpsMap.put(lookupCache.hostname, lookupCache.lookupResult);

if (lookupCache.isExpired()) {
expired.add(lookupCache);
expiredList.add(lookupCache.hostname);
}
}
String [] expiredHosts = expiredList.toArray(new String[expiredList.size()]);
// 内存读取后,清空本地已过期的缓存
cacheDbHelper.deleteLookupCaches(expired);
cacheDbHelper.delete(expiredHosts);
}
}

Expand Down Expand Up @@ -88,7 +89,7 @@ public void add(String hostname, LookupResult lookupResult) {
DnsLog.d("Cache %s for %s", lookupResult, hostname);
mHostnameIpsMap.put(hostname, lookupResult);
if (getCachedIpEnable()) {
cacheDbHelper.insertLookupCache(new LookupCache(hostname, lookupResult));
cacheDbHelper.insert(new LookupCache(hostname, lookupResult));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.tencent.msdk.dns.core.cache;
package com.tencent.msdk.dns.core.cache.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

import com.tencent.msdk.dns.base.log.DnsLog;
import com.tencent.msdk.dns.core.cache.database.LookupCache;
import com.tencent.msdk.dns.core.cache.database.LookupResultConverter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CacheDbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;

public static final String DATABASE_NAME = "LookupCache.db";
public static final String DATABASE_NAME = "LookupResult.db";

private SQLiteDatabase mDb;
private static final Object mLock = new Object();

private final Object mLock = new Object();
private SQLiteDatabase mDb;

static class DB {
static final String TABLE_NAME = "lookupDB";
Expand All @@ -42,19 +42,18 @@ public CacheDbHelper(Context context) {

private SQLiteDatabase getDb() {
if (mDb == null) {
try{
try {
mDb = getWritableDatabase();
} catch (Exception e){
} catch (Exception e) {

}
}
return mDb;
}

public List<LookupCache> readFromDb() {
public List<LookupCache> getAll() {
synchronized (mLock) {
ArrayList<LookupCache> lists = new ArrayList<>();

SQLiteDatabase db = null;
Cursor cursor = null;

Expand All @@ -64,7 +63,6 @@ public List<LookupCache> readFromDb() {
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
do {
DnsLog.d("testt----" + cursor.getString(cursor.getColumnIndex(DB.HOST))+ "result ---" + LookupResultConverter.toLookupResult(cursor.getBlob(cursor.getColumnIndex(DB.RESULT))));
lists.add(new LookupCache(
cursor.getString(cursor.getColumnIndex(DB.HOST)),
LookupResultConverter.toLookupResult(cursor.getBlob(cursor.getColumnIndex(DB.RESULT)))
Expand All @@ -85,7 +83,7 @@ public List<LookupCache> readFromDb() {
}
}

public void insertLookupCache(LookupCache lookupCache) {
public void insert(LookupCache lookupCache) {
synchronized (mLock) {
SQLiteDatabase db = null;
try {
Expand All @@ -94,7 +92,7 @@ public void insertLookupCache(LookupCache lookupCache) {
ContentValues cv = new ContentValues();
cv.put(DB.HOST, lookupCache.hostname);
cv.put(DB.RESULT, LookupResultConverter.fromLookupResult(lookupCache.lookupResult));
db.insert(DB.TABLE_NAME, null, cv);
db.insertWithOnConflict(DB.TABLE_NAME, null, cv, SQLiteDatabase.CONFLICT_REPLACE);
db.setTransactionSuccessful();
} catch (Exception e) {
DnsLog.e("insert lookupCache fail " + e);
Expand All @@ -109,27 +107,29 @@ public void insertLookupCache(LookupCache lookupCache) {
}
}

public void deleteLookupCaches(ArrayList<LookupCache> lookupCaches) {
for(LookupCache lookupCache: lookupCaches) {
delete(lookupCache.hostname);
}
public void delete(String hostname) {
delete(new String[]{hostname});
}

public void delete(String hostname) {
synchronized (mLock) {
SQLiteDatabase db = null;
try {
db = getDb();
db.beginTransaction();
db.delete(DB.TABLE_NAME, DB.HOST + "= ? ", new String[] {hostname});
db.setTransactionSuccessful();
} catch (Exception e) {
DnsLog.e("delete by hostname fail" + e);
} finally {
if (db != null) {
try {
db.endTransaction();
} catch (Exception ignored) {
public void delete(String[] hosts) {
if (hosts.length > 0) {
synchronized (mLock) {
SQLiteDatabase db = null;
try {
db = getDb();
db.beginTransaction();
db.delete(DB.TABLE_NAME, DB.HOST + " IN (" + TextUtils.join(",", Collections.nCopies(hosts.length,
"?")) + ")",
hosts);
db.setTransactionSuccessful();
} catch (Exception e) {
DnsLog.e("delete by hostname fail" + e);
} finally {
if (db != null) {
try {
db.endTransaction();
} catch (Exception ignored) {
}
}
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public void clear() {
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DB.SQL_CREATE_ENTRIES);
}catch (Exception e) {
} catch (Exception e) {
DnsLog.e("create db fail " + e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public LookupCache(@NonNull String mHostname, LookupResult mLookupResult) {
this.lookupResult = mLookupResult;
}

public LookupCache() {
}

public boolean isExpired() {
AbsRestDns.Statistics stat = (AbsRestDns.Statistics) lookupResult.stat;
if (stat != null) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/tencent/msdk/dns/report/Session.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.tencent.msdk.dns.report;

import com.tencent.msdk.dns.base.log.DnsLog;

import java.util.Random;

public class Session {
Expand All @@ -15,7 +13,6 @@ public static void setSessionId() {
buf[i] = sampleAlphabet.charAt(random.nextInt(sampleAlphabet.length()));
}
sessionId = new String(buf);
DnsLog.d("hello sessionId: " + sessionId);
}

public static String getSessionId() {
Expand Down

0 comments on commit b558d67

Please sign in to comment.