On Saturday, November 7, 2015 at 10:01:34 AM UTC-8, Giorgio Robino wrote: > > I'd like to use a Sqlite3 database also as key/value storage (programming > in Ruby). I want to use the sqlite3 database: > > - both in as usual way (relational tables managemen with sequel orm) > > - and also to have a table just to store that key/value storage (call it > nosql if you like), where "value" is a free format [Ruby hash]( > http://docs.ruby-lang.org/en/2.0.0/Hash.html). > > Imagine a sqlite3 table with two columns: > > chat_id | data > ------------+------------------------------------------------------ > 1276262 | { state: :add_item, list: ['2 pizze 4 Stagioni con > peperoncino', '2 mezze minerali gassate] } > 1276263 | { state: :new_address, address: '...', location: '..-' } > 1276264 | { profile: '...', tastes: {...}, tags: [...] } > ... | ... > > > > `data` column must contain a free format hash, varying from record to > record with differents hash fields composition. > > Let say that I do not need to do any complex search inside the `data` > column, but I only need to persist in the DB getting a record (load to RAM) > and the putting back the record (dump to DB for persitence). **In the more > performant way!**. Let consider the code: > > > require 'sequel' # the great Sequel ( > https://github.com/jeremyevans/sequel) ORM > require 'sqlite3' # for accessing Sqlite3 (https://www.sqlite.org/) > database > > DB = Sequel.connect('sqlite://chat.db') > > DB.create_table :sessions do > string :chat_id, primary_key: true > blob :data # case 2, see below > # or text :data, case 1, see below > end > > sessions = DB[:sessions] > hash = { state: :add_item, list: ['2 pizze 4 Stagioni con peperoncino' > , '2 mezze minerali gassate] } > > > > > Now focus on inserting some hash data in `sessions` table. A possible > solutions in my mind to set the `data` column: > > 1. [JSON serialize](https://github.com/intridea/multi_json) to a `TEXT` > type column ? > > sessions.insert(chat_id: id, data: Multi_json.dump(hash) > > > > 2. [Marshal serialize](http://ruby-doc.org/core-2.2.0/Marshal.html) in a > `BLOB` type colum ? > > sessions.insert(chat_id: id, data: Sequel.blob(Marshal.dump(hash)) > > > If you are using SQLite 3.9+, you could look into using SQLite's native json support: http://sqlite.org/json1.html
Even if not, I would recommend the json approach, as it is easier to interoperate with. Marshal format ties you to ruby. > BTW: > > - I'm aware by the fact that Ruby Marshalling could be not portable data > solution, but maybe more performant; isnt'it ? > Marshalling is probably faster, but unless it is the bottleneck, don't worry about it. There are fast json parsers for ruby. Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
