【问题标题】:How to resolve huge byte[] issue in OrmLite如何解决 OrmLite 中的大字节 [] 问题
【发布时间】:2015-08-13 13:42:06
【问题描述】:

我有 2 个表 Header 和 Binary

标题有点像:

@DatabaseTable(tableName = Header.TABLE_NAME)
public class Header extends Table {
    @DatabaseField(
            generatedId = true,
            columnName = HEADER_ID,
            dataType = DataType.LONG_OBJ
    )
    private Long id;

    @ForeignCollectionField(orderColumnName = Binary.BINARY_ORDER, orderAscending = true)
    private Collection<Binary> binaries=new ArrayList<Binary>();
}

二进制是:

@DatabaseTable(tableName = Binary.TABLE_NAME)
public class Binary {
    @DatabaseField(
            generatedId = true,
            columnName = BINARY_ID,
            dataType = DataType.LONG_OBJ)
    private Long id;

    @DatabaseField(
            columnName = BINARY_HEADER_ID,
            foreign = true,
            foreignAutoCreate = true,
            foreignAutoRefresh = true,
            //uniqueCombo = true,
            canBeNull = false, //there always must be a link to Item._ID
            columnDefinition = "integer constraint fk_4 references `"+ Header.TABLE_NAME+"`(`"+ Header.HEADER_ID+"`) on delete cascade"
    )
    private Header header=null;

    @DatabaseField(
            columnName = BINARY_ORDER,
            //uniqueCombo = true,
            canBeNull = false,
            dataType = DataType.INTEGER_OBJ,
            defaultValue = "0"
    )
    private Integer order =0;

    @DatabaseField(
            columnName = BINARY_CHUNK,
            dataType = DataType.BYTE_ARRAY)
    private byte[] chunk=null;
}

我的问题出在byte[] chunk 字段中,因为每个块的大小是 1 mb 并且它们的数量几乎是无限的,所以当我阅读 Header 记录时,OrmLite 会隐式读取 Collection&lt;Binary&gt; 具有大量内容的列表 -所以可能会导致内存耗尽。

我应该如何声明我的表来解决这个问题?

【问题讨论】:

  • 如何解决 OrmLite 中的大字节 [] 问题:不使用大字节 []
  • 请阅读问题。我没有使用巨大的 byte[] - 而是 byte[] 的列表,这可能是巨大的
  • 仍然不使用它......根本不存储 BLOB,而是引用文件......如果 OrmLite 支持延迟加载 - 使用它......无论如何,这是众多原因之一为什么ORMs are bad ...
  • 通过设置eager=false会使数据检索变得懒惰,但这取决于你如何处理Collection&lt;Binary&gt;中的数据
  • 我要遍历他们:for(Binary binary : header.getBinaries()) {} - 如果eager=false 有意义吗?

标签: java android sqlite ormlite


【解决方案1】:

首先将你的集合声明为惰性集合并使用外部集合:

 @ForeignCollectionField(orderColumnName = Binary.BINARY_ORDER, orderAscending = true, eager=false)
 private ForeignCollection<Binary> binaries; //to be able to retrieve CloseableIterator 

如何迭代数据:延迟加载数据意味着保持连接以确保获取数据,因此iterator.close();

    CloseableIterator<Binary> iterator = binaries.closeableIterator();
    try {
        while(iterator.hasNext()){
        Binary bin= iterator.next();
        //do stuff
        bin.setChunk(null) //discard if not needed any more
       }
    } finally {
        // must always close our iterators otherwise connections to the database are held open
        iterator.close();
    }

这只是一个提示,因为我不知道你要对数据做什么,但你的代码应该是这样的。

【讨论】:

  • Thanx - 您的回答非常有用。此外,我会自己做:)
猜你喜欢
  • 2012-11-06
  • 2020-06-28
  • 2021-10-05
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
相关资源
最近更新 更多