Diametric 是一个库,用于从 Ruby 对象构建 Datomic 的模式、查询和事务。
Diametric is ActiveRecord style wrapper for Datomic, and a library for building schemas, queries, and transactions for Datomic from Ruby objects. It is also used to map Ruby objects as entities into a Datomic database.
Diametric supports both CRuby and JRuby. When Diametric is used on CRuby, Diametric connects to Datomic's REST service. Using Datomic's REST API, Diametric creates schema/data and makes a queries to Datomic. When Diametric is used on JRuby, both Datomic's REST and Peer services are supported. The core parts of Peer service are implemented using Datomic API.
For Rubyists who are familiar with object oriented programming, Diametric converts:
The biggest difference between REST and Peer services is available database types. REST service can use memory database only, which means we can't save data in any file as long as using free version. While Peer service can use memory and free version of transactor, which means we can save data in a file through Datomic's transactor. Other than that, Peer service has an excellent API to use Datomic's various features.
gem install diametricDiametric is a multi-platform gem. On CRuby, the above installs the CRuby version of the gem. On JRuby, the above installs the JRuby version of gem, with a Java extension.
[note] When you install diametric on JRuby, you'll see a message, "Building native extensions. This could take a while...." Although Diametric doesn't rely on a C library, it depends on several Java libraries. While installing gem, diametric downloads those jar archives, including dependencies on your local maven repo. The message shows up because of this.
On CRuby, you need to start Datomic's REST server.
Diametric has a command to start REST server.
Type datomic-rest -p port -a db_alias -u uri, for example
datomic-rest -p 9000 -a free -u datomic:mem://When you run the command at the very first time, it takes a while to start running. This is because Diametric downloads datomic if it doesn't exist in Diametric's directory tree. If it is the second time or later, the REST server starts quickly.
To learn more about the options in the above command, please go to http://docs.datomic.com/rest.html.
Once the REST server starts running, go to localhost:9000 on your browser. You can see Datomic's REST service is running.
Alternatively, you can download Datomic archive from https://my.datomic.com/downloads/free,
and start REST server using the Datomic command, script/datomic-rest -p 9000 free datomic:mem://
You have 2 choices on JRuby, Peer and REST services. Peer service works on the same JVM as JRuby and looks like just a Java library for JRuby.
When you choose REST service, follow Preparation for CRuby section.
When you choose Peer service, you don't need to prepare anything. You even don't need to start Datomic. Diametric does everything for you. What you need to do is just coding in Ruby using Diametric API.
Although Diametric API makes coding very easy, you still have the freedom to hit Datomic's Java API directly.
Typical Diametric coding follows the steps below:
To establish a connection to Datomic REST Service, you can do as such:
require 'diametric'
Diametric::Persistence.establish_base_connection({:uri => 'http://localhost:9000', :storage => 'free', :database => 'sample'})Optionally, you can connect using datomic-client gem:
require 'datomic/client'
@datomic_uri = 'http://localhost:9000'
@storage = 'free'
@dbname = "sample"
@client = Datomic::Client.new @datomic_uri, @storage
@client.create_database(@dbname)Each parameter should be consistent to the ones used to start the REST service.
require 'diametric'
Diametric::Persistence.establish_base_connection({:uri=>'datomic:mem://sample'})Or in a peer service specific way,
datomic_uri = "datomic:mem://sample-#{SecureRandom.uuid}"
@conn = Diametric::Persistence::Peer.connect(datomic_uri)While a relational databse inserts a record, Diametric saves an Entity.
To save data by Diametric, you need to define Entity first.
Defining entities loosely corresponds to defining database tables.
Below is an example of Entity definition for Peer service.
Be aware, you should include Diametric::Entity and
either one of Diametric::Persistence::REST or Diametric::Persistence::Peerin your Entity definition.
require 'diametric'
class Person
include Diametric::Entity
include Diametric::Persistence::Peer
attribute :name, String, index: true
attribute :birthday, DateTime
attribute :awesomeness, Boolean, doc: "Is this person awesome?"
endThe attribute definition is consists of:
This is the Diametric way of converting Datomic schema to a Ruby model definition.
The attribute name goes to :db/ident and the value will be :/.
This is interpreted as :/ on Datomic.
Currently, Diametric supports the following data types:
| Diametric | Datomic |
|---|---|
| String | :db.type/string |
| Symbol | :db.type/keyword |
| Boolean | :db.type/boolean |
| Integer | :db.type/long |
| Float | :db.type/double |
| Double | :db.type/double |
| BigDecimal | :db.type/bigdec |
| Ref | :db.type/ref |
| DateTime | :db.type/instant |
| UUID | :db.type/uuid |
In addition, Diametric supports an Enum type.
Available options are:
| Diametric | Datomic |
|---|---|
:cardinality => :one (default) |
:db.cardinality/one |
:cardinality => :many |
:db.cardinality/many |
:doc => "some document here" |
:db/doc |
:unique => :identity |
:db.unique/identity |
:unique => :value |
:db.unique/value |
:index => true |
:db/index |
:fulltext => true |
:db/fulltext |
Creating schema in Datomic is similar to a migration in Ruby on Rails. However, schema creation can be done as a part of a program. For Datomic, the difference between schema and entity is quite small. Like saving entities, schemas are saved in Datomic.
Creating schema is done by calling create_schema method of the entity.
Person.create_schema.getPerson.create_schema (not followed by get) also creates a schema,
but it is a sort of piling up on the queue and not performed immediately.
If get is not specified, schema creation is done by at some point implicitly.
Adding get requests Datomic to create schema now.
This is good to know whether the entity definition is correct or not.
If some attribute definition has a problem, error will be raised.
Once the entity is defined, and schema is created on Datomic, you can create an entity instance like you create a new object in Ruby. Below is an example:
person = Person.new
person.name = "Sky"
person.birthday = DateTime.parse('2005-01-01')
person.awesomeness = true
puts "dbid (before save): #{person.dbid}"
person.save
puts "dbid (after save): #{person.dbid}"The above prints:
dbid (before save):
dbid (after save): 17592186045418The dbid is equivalent to id of ordinary ActiveModel objects.
Before save, the person object doesn't have dbid value.
But after save, it will have an auto-generated value as dbid.
Alternatively, you can create an entity in one line:
Person.new(name: "Sky", birthday: DateTime.parse('2005-01-01'), awesomeness: true).saveTo update attribue values of the instance, use update_attributes:
person.update_attributes(name: "Sky Jr.", awesomeness: false)Queries are done by Diametric::Query instance. A basic usage is:
Diametric::Query has three arguments in constructor:
Diametric::Query.new(entity_class, db_or_conn=nil, resolve=false)The second and the third options work only on Peer service.
On REST service, the query results are an array of entity objects. On Peer service, the result data type depends on the constructor arguments. In default setting, the query result is a Set of Arrays. Each array has an entity id (future Diametric version will have a couple of attributes depends on the query). This data structure is exactly the same as what Datomic returns. Also, to avoid the overhead that comes from converting to Ruby's Set and Arrays, Diametric wraps in Diametric::Persistence::Set or Diametric::Persistence::Collection. (Diametric's wrappers are not a perfect Ruby Array nor Ruby Set in this version. Those will be improved in future versions.)
The reason Diametric doesn't create entity instances by default is to save memory. When dealing with millions or billions of data, saving memory is key to running faster, and, more importantly, to avoid OutOfMemoryError. In terms of saving memory, Diametric returns minimum by default.
If you don't have a lot of data and want to get instances directly from query results,
set true to the third option, resolve.
When resolve option is set to true, the query result will be an Array of entity instances. You should be careful when setting this option to true. When it is true, Diametric tries to create instances recursively following associations. If friends or friends of friends includes self, you'll likely to get StackOverflowError.
To resolve an entity from a dbid, use Entity's reify method. The reify method creates an instance from the dbid.
The second option, db_or_conn is to specify the database state at some time in the past.
If it is not specified, Diametric uses the default database state, which means the latest state.
Query example1:
…Above prints:
name: Rain, dbid: 17592186045429, birthday: 2005-05-04 20:00:00 -0400, awesomeness: true
name: Breeze, dbid: 17592186045425, birthday: 1911-03-22 19:00:00 -0500, awesomeness: true
name: Sleet, dbid: 17592186045427, birthday: 2010-04-29 20:00:00 -0400, awesomeness: false
name: Sun, dbid: 17592186045421, birthday: 2004-12-31 19:00:00 -0500, awesomeness: true
name: Cloud, dbid: 17592186045423, birthday: 1980-02-11 19:00:00 -0500, awesomeness: falseQuery example2 (with resolve to true):
query = Diametric::Query.new(Person, nil, true)
query.each do |entity|
puts "name: #{entity.name}, dbid: #{entity.dbid}, birthday: #{entity.birthday}, awesomeness: #{entity.awesomeness}"
endThe example2 prints exactly the same as the example1.
To narrow down the query results, you can use where and filter methods.
Query example3:
query = Diametric::Query.new(Person, nil, true).where(awesomeness: true)
query.each do |entity|
puts "name: #{entity.name}, dbid: #{entity.dbid}, birthday: #{entity.birthday}, awesomeness: #{entity.awesomeness}"
endAbove print
暂无开放 Issues,或尚未同步最近议题。