r/FlutterDev • u/leisim • Sep 11 '19
Plugin Hive database is stable (v1.0.0). I'm now working on query support and loading databases from assets.
https://github.com/leisim/hive10
7
5
u/wellbranding Sep 11 '19
Ia it better than sharedprefences for Android? Or is it competitor to MOOR?
4
u/leisim Sep 11 '19
It is faster than shared preferences (especially for writing) and MUCH faster than moor...
2
u/wellbranding Sep 11 '19
But it does not have capabilities like MOOR, like querying data. Is it planned to provide features or ORM?
2
u/leisim Sep 11 '19
I'm currently working on query support. You can already store regular objects...
13
u/simolus3 Sep 11 '19 edited Sep 12 '19
While performance is an important aspect for persistence libraries, and hive completely crushes moor in benchmarks, it is not the only thing to consider. Here are some other points that come to my mind:
Performance: Clearly, hive is much master. Moor has to create a SQL query from your Dart code, send that over a platform channel, have sqlite parse that SQL query, execute it, send the results back and finally parse the result into data classes. While it comes to no surprise that this process is much slower than hive, moor is still fast enough for almost all use-cases, even on slow devices. Moor is also going to become significantly faster with
dart:ffi, which lets us cut platform channels. Further, we'll be able to use prepared statements, which will amortize some costs for queries that are executed often.Queries: Hive, at the moment, doesn't support querying for a subset of data, joins, etc. While that feature is planned, sqlite (and thus, moor) has had it since the beginning and is heavily optimized for it.
Integrity: As moor uses a stable, well-supported, relational database, you can express assumptions of your data in sql (nullability,
CHECK, enforced foreign keys, etc.) and the integrity of your data is enforced early during inserts. On database systems that don't enforce this, all it takes is one buggy version of your app that breaks these assumptions.Type safety: One of moor biggest promises is that, whether you use Dart or sql, your database code is typesafe. There's no manual mapping from
Map<String, dynamic>to your objects, moor takes care of that. If you can compile it, it will (unless you're actively trying to break something) work at runtime. And even if it doesn't, moor will exactly explain which constraints where violated.Tooling integration: As moor uses sqlite, its schema and data format is well understood by a variety of tools. You can literally dump a bunch of "CREATE TABLE" statements into a file and moor will generate matching Dart code for that. Moor also "speaks" sql and contains a built-in sql parser and static analyzer. This lets moor look at your sql queries at build time and figure out potential problems. I'm also working on an analyzer plugin which will report errors while you're typing, offer fixes and even auto-complete.
I'm the author of moor, so clearly my views are biased: I wouldn't write a persistence library I don't like. Finally, hive is a great library and there are many scenarios where hive is better suited than moor: Apps with a simple data model that doesn't require cross-table lookups, apps that absolutely need to prioritize performance, etc. You should pick a persistence library that fits your project well :) You could also store some cached app state in hive and long-lived data in moor.
0
Sep 11 '19
[deleted]
2
u/simolus3 Sep 12 '19
With "buggy versions released to users" I was talking about broken versions of an app. As you already mentioned, sometimes it's necessary to denormalize a data model to be efficient with a document store. When data is duplicated for performance reasons, it can be easy to introduce inconsistencies by not updating in all locations. I didn't mean to diminish your library and I've edited the comment. But it's great to hear that hive is well-tested and has integrity checking.
Anyway, congrats on releasing 1.0!
1
u/leisim Sep 12 '19
You are certainly right, it is much harder to store relational data in Hive or any kv-store really. I would always choose a database which fits the problem.
I really look forward to FFI support in moor. Will you talk directly to SQLite or use another library?
I use moor in multiple production apps myself and I think that platform channels are quite expensive...
1
u/simolus3 Sep 12 '19
I have a version that talks to sqlite directly, without a third-party library. It's pretty much the ffi example from the sdk repo with some additions to support BLOBs and prepared statements. There are some open questions left (do we need a background isolate when talking to sqlite, what's the best way to bundle the native library with a Flutter package, ...), but the prototype seems to be working pretty well so far. Issue #76 on moor tracks the progress.
2
u/leisim Sep 12 '19
Cool! I'll keep watching this issue... I also experimented with isolates but in my tests they added quite a bit of overhead. Especially passing data between them is rather slow :/
7
u/oaga_strizzi Sep 11 '19
It's a key-value store. It's not a direct competitor to Moor. If all you need is a key-value store, Hive is a good choice and likely faster (even if the benchmarks are a bit misleading IMO, as the difference is much smaller if you use SQL properly (1 SELECT statement with a WHERE IN clause instead of 1000 queries executed sequentially, batch INSERT statements).
0
Sep 11 '19
[deleted]
8
u/oaga_strizzi Sep 11 '19 edited Sep 11 '19
It really is. You can almost always model your data to make Hive the perfect storage solution.
How would you implement a full-text search feature in Hive (e.q. like sqlite FTS5)? Or access the same data via multiple keys (e.q. for what would be an indexed join in SQL)? Do a large update that either succeeds or fails as a whole (transaction)?
Not every application needs those of course, but I wouldn't say these requirements are so uncommon that you can say that you almost never need them.
SQLite is just very slow and shared prefs are a joke.
Shared prefs are kinda bad, yes. But I never had an issue what SQLite being too slow and I maintain an app for professional use that ships with in embedded db that is several hundred megabytes big and has queries with multiple joins over huge tables. They still only take a couple of milliseconds on modern devices. As long as you set the right indices, sqlite is pretty fast.
0
Sep 11 '19
[deleted]
5
u/oaga_strizzi Sep 11 '19
Jup, both approaches have their use cases. Some apps are only possible with approaches like Hive, some are better with SQL, some could use both.
> What is the use case for that?
E.q. you have a product and a part model. You can list all parts of a product, no problem.
New requirement: Show all products, that have a specific part built-in. Well, that's gonna be difficult. With SQL, it would be no problem, with a document store it's much more work and likely even slower.
I've seen applications that have been written on the top of the NoSQL-hype in MongoDB that would have absolutely no problem with a relational database model, but choose Mongo nevertheless. They crafted the data model so that it fits the initial requirement, but then any change was very difficult to implement.
That's not to say that Key-Value databases have no use case, far from that. Just use what's appropriate.
0
Sep 11 '19
[deleted]
1
u/ech89899 Dec 23 '19
I tend to think that the "complex queries" should be done by a server anyway...
Why do we need then all those powerful devices with GBs of memory and multiple processor cores? Just to draw animations and other whistles? :)
3
3
u/merrycachemiss Sep 11 '19
For Android, at least - is it multi-process safe?
5
u/leisim Sep 11 '19
Since Flutter only uses a single process and a single thread, I didn't implement multi process support. Maybe you can elaborate the use cases a bit.
3
u/merrycachemiss Sep 11 '19
Ooh, I see. My Android-only app is multi, haven't started my next app under Flutter yet - just researching different tools. Will likely use Hive when it's time to get my hands dirty.
2
u/realni_ Sep 11 '19
I'm getting an error when running flutter pub get. It says that hive_generator depends on a lower version than hive 1.0.0
Versions:
hive: ^1.0.0
hive_generator: ^0.5.0
build_runner: ^1.6.9Do I use wrong versions?
2
2
1
u/Abion47 Sep 11 '19
How does Hive compare to Sembast?
1
u/leisim Sep 11 '19
It is a little bit faster, easier to use (in my opinion), it supports storing objects and it runs in the browser.
2
u/kgainz Sep 11 '19
Looks like an awesome library. Will definitely use it in my next project. Thank you!
2
u/_thinkdigital Sep 11 '19
WOW. This is nice! I'm going to swap out my previous solution for this one asap!
2
u/pilgr Sep 12 '19
I'm about to think the same. For now I use shared preferences for iOS/Android for simple storage for notes.
1
u/jjcat1997 Sep 12 '19
Been using hive for my current desktop app, very easy to use and understand, too bad for some reason I can’t get my desktop app started again 😭
1
0
Sep 11 '19
[deleted]
1
u/leisim Sep 11 '19
You don't have to include a native binary (which is not even possible for web) and Hive is much easier to use (easy setup and less async & await)
0
34
u/leisim Sep 11 '19 edited Sep 11 '19
For all of you who don't know what Hive is: It is a very fast key-value database which is written in dart and supports all platforms with a single codebase (iOS, Android, Web, Mac, Windows, Linux).
I want to thank all of the testers and contributors. I hope you continue to use and enjoy Hive.
If you have questions or feature requests, just post them here or open an issue ;)