安卓(分布式应用程序)的主键策略 [英] Android (distributed application) primary key strategy

查看:212
本文介绍了安卓(分布式应用程序)的主键策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现分布式应用程序使用多个移动客户端和基于Web的服务器应用程序。所以每个客户端和还服务器允许生成表条目。因此,我需要的唯一主对所有参与者键和我希望能够为生成密钥离线

I am going to implement a distributed application with multiple mobile clients and a web based server application. So each client and also the server are allowed to generate table entries. Therefore I need unique primary keys over all participants AND I want to be able to generate keys offline.

什么是生成主键使用的是分布式环境中的最佳方法?对于类似的问题,请参见<一href="http://stackoverflow.com/questions/16153741/what-is-the-best-primary-key-strategy-for-an-online-offline-multi-client-mobile?lq=1">What是一个在线/离线多客户端的移动应用使用SQLite和SQL Azure的数据库作为中央存储的最佳主键的策略是什么?

What is the best approach for generating primary keys you are using on distributed environments? For a similar question see What is the best primary key strategy for an online/offline multi-client mobile application with SQLite and Azure SQL database as the central store?

我知道 UUID的密钥生成是该方案一个好办法,但我想坚持到名为_id键和所建议的Andr​​oid平台型长。

I am aware that UUID key generation is a good approach for that scenario, but I want to stick to a key with name _id and type long as suggested by the Android platform.

我不希望有一个复合ID 与设备(也服务器是设备)的ID和本地ID。这种方法是行不通的很好反正自服务器应该能够生成条目某个客户。在这种情况下,我将不得不使用的设备ID也服务器上。

I don't want to have a composite id with device (also server is a device) id and local id. This approach wouldn't work well anyway since the server should be able to generate entries for a certain client. In that case I would have to use the device id also on the server.

因此​​我目前最喜欢的是建立自己的密钥数据类型长(我在另一个项目之前这样做)。我想我会使用高/低办法(这里例如见什么是高/低位算法),并有一键其中包括:

Therefore my current favorite is to build my key with datatype long (I did this before in another project). I think I will use the high/low approach (see for example here What's the Hi/Lo algorithm?) and have a key which consists of:

  • 在客户端ID(如〜28位)由服务器生成
  • 在低值(例如〜4位)递增的客户,从来没有坚持
  • 在高价值(例如〜32位)加在客户端,坚持对客户端只

客户端ID必须从服务器上的移动应用程序首次启动进账。因此,第一个起始需要的网络连接。这可能是这种方法的一个缺点。在设备上具有客户端ID当我可以生成钥匙没有网络连接。

The client id must be fetched from the server at first start of the mobile application. So the first start needs a network connection. This might be a downside of this approach. When having the client id on the device I can generate keys without a network connection.

一般情况下,高ID是在数据库中的独特价值。当用户deinstalls应用程序并安装一遍我必须把他当作一个新的客户,并有给他一个新的客户端ID。否则,我会保存当前高ID在服务器上,以便能够对损失或重新安装恢复 - 不值得

Normally the high id is a unique value over the database. When a user deinstalls the application and installs it again I have to treat him as a new client and have to give him a new client id. Otherwise I would have to save the current high id on the server to be able to restore it on loss or on reinstallation - not worth the effort.

什么是获得高ID在Android?自动增量的关键,最好的办法是不是一个解决方案。我需要的东西就像一个发生器功能。它有它自己的事务(而不是用户交易)内执行。有没有人遇到与Android上的做法,任何人都可以点我在正确的方向? (我只发现这个回答)。

What is the best approach for getting the high id on Android? An autoincrement key is not a solution. I need something like a generator function. And it has to be executed inside its own transaction (not the "user" transaction). Has anyone experiences with that approach on Android and can anyone point me in the right direction? (I only found this answer).

您使用了哪些主要战略是为您的多客​​户端应用程序(在线和离线)?

推荐答案

这是更多的问题,那么答案...

This is more questions then answers...

它确实使事情如果你能自动生成所有的ID更容易,所以你不必从服务器获取他们担心你是否有一个连接。你提到,你不能拿常用的方法(UUID或ANDROID_ID),因为你会用很长的所建议的Andr​​oid平台。

It does make things easier if you can auto-generate all your id's, so you don't have to fetch them from the server and worry about whether you have a connection. You mention that you can't take the common approach (UUID or ANDROID_ID) because you will be using a long "as suggested by the Android platform".

您指的是Android的假定你的SQLite表将有很长的_id主键的事实?

Are you referring to the fact that Android assumes that your SQLite tables will have a long _id primary key?

您使用服务器上的数据存储或SQL数据库?

Are you using a datastore or an SQL database on your server?

如果您使用的是带有分级密钥的数据存储(例如,谷歌的数据存储),那么怎么样,如果你使用的UUID / ANDROID_ID作为客户端ID,然后只要数据项ID。然后在客户端上你刚才存放长,并在服​​务器上的实体存储的UUID的关键路径/长。

If you are using a datastore with hierarchical keys (e.g. google datastore) then how about if you use UUID/ANDROID_ID as client id, and then a long as data item id. Then on the client you just store the long, and on the server your entities are stored with a key path of UUID/long.

为什么你写的高ID必须在数据库中的独特价值?既然是prepended与客户端ID,也许你的意思是,它必须是在本地数据库中唯一?

Why do you write that the "high id must be a unique value over the database"? Since it is prepended with the client id, perhaps you mean that it must be unique on the local database?

要处理你的问题,用户可以卸载和重新安装应用程序,为什么不追究你的想法在服务器上保存当前高ID,以便能够在丢失或重新安装恢复。既然你已经打算检索第一次运行的客户端ID(直到你拥有它不能分配的ID),您不妨也向服务器请求下一个可用的高ID。

To handle your problem that the user could uninstall and reinstall the app, why not pursue your idea of "save the current high id on the server to be able to restore it on loss or on reinstallation". Since you already plan to retrieve the client id on first run (and can't assign id's until you have it) you might as well also ask the server for the next available high id.

做你的实体有这样的,你可以生成从材料的32位散列的高ID等重点材料?假设高ID只需要一个特定的客户端上唯一的(假设你不会有实体的大规模#在客户端上),那么我想你永远不会得到一个碰撞,如果你有体面的关键材料,并使用哈希功能,最大限度地减少冲突。

Do your entities have some other key material such that you could generate a 32bit hash from that material for your high id? Assuming that the high id only need to be unique on a particular client (and assuming you won't have a massive # of entities on a client) then I think you would never get a collision if you have decent key material and use a hash function that minimizes collisions.

这篇关于安卓(分布式应用程序)的主键策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆