英文:
using two separate keys in BoltDB
问题
我有一个User
结构体,其中包含ID
和LoginName
字段,我希望可以通过这两个字段之一来访问该结构体,并且只需一次调用数据库。我知道BoltDB不像SQL那样处理任意字段索引等(不像SQL),但这种情况有些不同,因为我事先知道要用作索引的附加字段。
所以是否有某种次要键或多键索引的方法?或者可能有一些我没有看到的策略吗?如果没有,那么我将使用两次调用来实现它,我只是更喜欢一个“更简洁”的解决方案...
谢谢!
英文:
I have a User
struct with ID
and LoginName
fields and I want this struct to be accessible by either of these fields with single call to the DB. I know BoltDB is not supposed to handle arbitrary field indexing etc. (unlike SQL) but this case is a little different as I happen to know in advance the additional field to b used as index.
So is there some kind of secondary key or multiple key indexing? or maybe some strategy that I fail to see? If not then I'll just implement it with two calls, I just prefer a "cleaner" solution...
Thanks!
答案1
得分: 2
在BoltDB中没有二级键索引,但你可以自己实现。
你可以在另一个桶中存储ID到LoginName的映射,这在技术上就是你的结构体的“二级键”。也就是说,首先从二级键获取主键值,然后获取User结构体。
如果大部分操作都是基于LoginName键,可以使用LoginName到ID的映射,在LoginName键下存储User结构体,反之亦然。
要小心:你必须自己维护一致性,记住这一点。
英文:
There is no secondary key indexing in BoltDB, but you can implement it.
You can store ID to LoginName mapping in another bucket, and it will be technically the "secondary key" for your struct. That is, first obtain the primary key value from the secondary key, and then the User struct.
If most of your calls are on LoginName key, use LoginName to ID mapping and store User struct under LoginName key and vice versa.
Be careful: you have to maintain consistency by your own, remember it.
答案2
得分: 2
不,它不在那里。BoltDB很像Go语言,简洁而简单。在其之上构建一个层也很容易。BoltDB甚至允许轻松实现更新事务,以便原子地更新两个或更多的桶。因此,创建一个保持两个或更多桶同步的更新事务很容易。但听起来你已经知道这一点,只是想确认你没有遗漏什么。
英文:
No, it's not there. BoltDB is a lot like Go. Clean and simple. And building a layer on top is easy. BoltDB even allows update transactions to be trivially implemented so two more more buckets can be updated, or not, atomically. So creating an update transaction that keeps two or more buckets in sync is easy. But it sounds like you know that and just wanted to check that you aren't missing something.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论