2017-07-30 20 views
0

我需要存儲包含50,000到200,000個整數的對象。每天用戶可以生成0-10個這樣的對象。我的想法是使用Realm,但Realm沒有內建支持原始數組/列表。 使用領域有2種方式:如何在Android中存儲大量的整數?

1)創建RealmInt - 只有一個場域對象

2)像串100000個整型數組存儲 「1,20,41」

所以,這種情況有沒有辦法可以稱爲「最佳做法」?

P.S.如果這個問題是出於stackoverflow「主題」,我很抱歉。如果是 - 說,我會刪除這個問題

+0

這裏的答案可能會有所幫助[如何將對象存儲在sqlite數據庫?](https://stackoverflow.com/questions/1243181/how-to-store-object-in-sqlite-database),它指向[展平你的對象](http://www.javaworld.com/article/2076120/java-se/flatten-your-objects.html) – MikeT

+0

'2)存儲100,000個整數,如字符串:「1,20,41 「'。 – EpicPandaForce

回答

0

您可以將整數數組轉換爲字節數組,並將其存儲爲blob。

下面的代碼是一個測試SQliteOpenHelper子類,可以有兩個方法用於插入和取回所述整數數組沿定義了一個簡單1列的表: -

public class DBHelperObjectStore extends SQLiteOpenHelper { 

    private static final String DATABASENAME = "objects"; 
    private static final int DATABASEVERSION = 1; 

    private static final String MAINTABLE = "main"; 
    private static final String OBJCOL = "object"; 
    private static final String OBJCOLTYPE = " BLOB "; 
    private static final String MAINTABLECREATE = "CREATE TABLE " + 
      MAINTABLE + "(" + 
      OBJCOL + OBJCOLTYPE + 
      ");"; 

    private long lastinsertedrow; 

    DBHelperObjectStore(Context context) { 
     super(context,DATABASENAME,null,DATABASEVERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(MAINTABLECREATE); 

    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 

    public void insertRow(int[] intarray) { 

     ByteBuffer bb = ByteBuffer.allocate(4); 

     byte[] objasbytes = new byte[intarray.length * 4]; 
     int byteidx = 0; 
     for (int i: intarray) { 
      objasbytes[byteidx++] = (byte) (i >>> 24); 
      objasbytes[byteidx++] = (byte) (i >>> 16); 
      objasbytes[byteidx++] = (byte) (i >>> 8); 
      objasbytes[byteidx++] = (byte) i; 
     } 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues cv = new ContentValues(); 
     cv.put(OBJCOL,objasbytes); 
     lastinsertedrow = db.insert(MAINTABLE,null,cv); 
     db.close(); 
    } 

    public int[] getRow(long id) { 
     int[] rv = new int[0]; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     String[] columns = {"*"}; 
     String whereclause = "ROWID =?"; 
     String[] whereargs = {Long.toString(id)}; 
     Cursor csr = db.query(MAINTABLE,columns,whereclause,whereargs,null,null,null); 
     while (csr.moveToNext()) { 
      byte[] bytes = csr.getBlob(csr.getColumnIndex(OBJCOL)); 
      rv = new int[bytes.length/4]; 
      for (int i=0; i < bytes.length; i = i + 4) { 
       rv[i/4] = bytes[i] << 24 | 
         (bytes[i+1] & 0xff) << 16 | 
         (bytes[i+2] & 0xff) << 8 | 
         (bytes[i+3] & 0xff); 
      } 
     } 
     csr.close(); 
     return rv; 
    } 
} 

以下是從活動代碼(有一些測試代碼註釋): -

 DBHelperObjectStore dbstorehlp = new DBHelperObjectStore(this); 
     int arrayelements = 200000; 
     Random rdm = new Random(System.currentTimeMillis()); 
     int[] arraytosave = new int[arrayelements]; 
     for (int i = 0; i < arraytosave.length; i++) { 
      arraytosave[i] = rdm.nextInt(655555555); 
     } 
     dbstorehlp.insertRow(arraytosave); 
     int retrievedarray[] = dbstorehlp.getRow(1); 


     int errorcount = 0; 
     int matchcount = 0; 
     if (arraytosave.length != retrievedarray.length) { 
      Log.d("RESULTS", 
        "Array MISMATCH the number of elements of the saved array is " + 
          Integer.toString(arraytosave.length) + 
          " whilst the retrieved array has " + 
          Integer.toString(retrievedarray.length) + 
          " elements." 
      ); 
     } else { 
      for (int i=0; i < arraytosave.length; i++) { 
       if (arraytosave[i] != retrievedarray[i]) { 

        /* 
        Log.d("RESULTS","Elements with index of " + 
          Integer.toString(i) + " mismatch " + 
          Integer.toString(arraytosave[i]) + 
          " against " + 
          Integer.toString(retrievedarray[i]) 
        ); 
        */ 
        errorcount++; 
       } else { 
        /* 
        Log.d("RESULTS","Element at index " + 
          Integer.toString(i) + 
          " are both " + 
          Integer.toString(retrievedarray[i]) 
        ); 
        */ 
        matchcount++; 
       } 
      } 
      Log.d("RESULTS","Error Count =" + Integer.toString(errorcount)); 
      Log.d("RESULTS","Matched Count =" + Integer.toString(matchcount)); 

上面創建DBHelper,然後生成上述利用隨機數填充它到655555555(接近最大)指定尺寸(200000)的整數數組。

陣列經由dbstorehlp.insertRow(arraytosave);

行保存在DB與的rowid 1(顯然只適合用於測試)使用int retrievedarray[] = dbstorehlp.getRow(1);

的代碼的其餘部分相比較針對所檢索的原始檢索(再次用於測試)。

我已經用200000(每行大約800 K左右)運行了這個次數。我嘗試了2000000,它崩潰(內存),並再次減少到200000時,但刪除應用程序數據後再次運行OK。

注意!如果第二次運行上述內容,那麼您可能會得到100%的不匹配,因爲它會將第一行與另一行進行比較。