我正在尝试创建(如果可能的话)一个类似数据库的 LinkedList。
这个想法是,我在表中添加的数据需要具有复杂的订单结构,因为当我添加新项目时,我可以将其添加到列表中的任何位置。
例如,如果我有一个包含项目 (1)、(2)、(3)、(4)、(5) 的列表。我可以直接在位置 (2) 上添加新项目,从而更改整个列表(或至少更改位置 (2) 上的项目后面的元素)。
为此我必须考虑的另一件事是每个项目都必须知道其前一个项目的 ID(不是索引,而是从服务器获取的一些自定义 ID)。当我还必须在服务器上重新排序列表时,实际上需要此 ID(当我上传项目时,还必须发送具有先前 ID 的订单请求)。
此时,我的文档模型看起来像这样:
@DatabaseTable(tableName = DocumentsTable.TABLE_NAME, daoClass = DocumentDao.class)
public class Document implements Parcelable {
@DatabaseField(columnName = DocumentsTable.LOCAL_ID_COLUMN, generatedId = true, dataType = DataType.INTEGER)
int localId;
@DatabaseField(columnName = DocumentsTable.SERVER_ID_COLUMN, unique = true, dataType = DataType.STRING, useGetSet = true)
String serverId;
@DatabaseField(columnName = DocumentsTable.ALIAS_COLUMN, dataType = DataType.STRING, useGetSet = true)
String alias;
@DatabaseField(columnName = DocumentsTable.NAME_COLUMN, dataType = DataType.STRING, useGetSet = true)
String name;
@DatabaseField(columnName = DocumentsTable.DESCRIPTION_COLUMN, dataType = DataType.STRING, useGetSet = true)
String description;
@DatabaseField(columnName = DocumentsTable.OWNER_COLUMN, dataType = DataType.STRING, useGetSet = true)
String owner;
...
我正在考虑添加两个新列,我们将其称为 prevDoc
和 nextDoc
并将它们设置为 Foreig 键。像这样的事情:
@DatabaseField(columnName = DocumentsTable.PREVIOUS_DOC, foreign = true, canBeNull = true)
Document prevDoc;
@DatabaseField(columnName = DocumentsTable.NEXT_DOC, foreign = true, canBeNull = true)
Document nextDoc;
这样,我表中的每个项目都会知道它的前一个项目应该是什么以及它的下一个项目应该是什么。
但是
现在我陷入困境,此时,我不知道如何才能以正确的顺序对其进行排序。另外,如何正确添加新项目?
新想法?
此时我认为我走错了路,而且我似乎找不到好的解决方案。我搜索了 ORMLite 文档,但没有找到任何可以帮助我的内容。数据库等基本链表的示例也对我没有帮助。我找到了一些图形数据库,但我无法在 Android 中使用它们,而且它们对于我的需要来说太过分了。
最后的机会
作为最终的解决方案,我添加了一个新列 orderPos
,我手动更新了该列:
@DatabaseField(columnName = DocumentsTable.ORDER_POS, dataType = DataType.INTEGER, useGetSet = true)
int orderPos;
这是这样使用的: 对于每个项目,我设置一个 orderPos 值,该值在每个步骤中递增。 当我需要将一个项目添加到特定位置时,我会获取该项目后面的所有项目,并将其 orderPos 增加 1,然后将项目添加到我想要的位置的 orderPos 中。 我知道这将涉及 O(n-pos) + 1 的复杂性,但我目前想不出其他任何东西。
请告诉我您的想法以及如何使用 ORMLite 在 Android 上正确实现类似的功能。
最佳答案
This way, every item from my table will know what it's previous item should be and what it's next item should be.
是的,这有点奇怪。通常,字段将确定顺序,并且由于您要存储字段,因此您只需使用 qb.orderBy(...)
方法以任意顺序输出列表。
For example if I have an list with items (1), (2), (3), (4), (5). I can add a new item directly on position (2), thus altering the whole list (or at least the elements following the item on the position (2)).
所以我假设您将一个项目插入位置 2,但是如果您在位置 2 插入另一个项目,则先前的 #2 项目将变为 #3。是的,这很难。
For each item, I set an orderPos value which is incremented at each step. When I need to add an item to an specific position, I get all the items following that item and increment their orderPos with 1, after which I add the item with the orderPos of the position that I want. I know this will involve an complexity of O(n-pos) + 1,
是的。听起来不错。您可以在 1 个 SQL 表达式中执行更新,因此每次插入将需要 2 个 SQL 操作。除非表中有很多行,否则这不会太痛苦。
跳出框框思考,您可以做的一件事是使用 Long
的宽度值(value)。列表中的第一项获取 Long.MAX_VALUE / 2
。每当您插入列表时,您都会获取左侧排序值和右侧排序值,并取它们的平均值。您必须在要插入的值的两侧找到两个值,这将需要两次查询,但不需要更新所有行。
- 查找行
>=
我要插入的行。如果没有则Long.MAX_VALUE
. - 查找行
<
我要插入的行。如果没有则 0。 - 对每行的顺序列求平均值。
- 插入新行。
但我认为你的职位想法可能更适合你的情况。
关于java - LinkedList 类似数据库 ORM lite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28323152/