Crate ledb_actix

source ·
Expand description

LEDB Storage actor and REST interface

An implementation of storage actor for Actix.

NOTE: Use features = ["web"] to enable an optional scoped REST-interface for actix-web.

Storage actor

Usage example:

extern crate actix;
extern crate futures;
extern crate tokio;
#[macro_use]
extern crate serde_derive;
// This allows inserting JSON documents
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate ledb;
#[macro_use]
extern crate ledb_actix;
// This allows define typed documents easy
#[macro_use]
extern crate ledb_derive;
extern crate ledb_types;

#[macro_use]
extern crate log;
extern crate pretty_env_logger;

use actix::System;
use futures::Future;
use ledb_actix::{Storage, Options, StorageAddrExt, Primary, Identifier, Document};
use serde_json::from_value;
use std::env;
use tokio::spawn;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Document)]
struct BlogPost {
    #[document(primary)]
    pub id: Option<Primary>,
    pub title: String,
    pub tags: Vec<String>,
    pub content: String,
}

fn main() {
    env::set_var("RUST_LOG", "info");
    pretty_env_logger::init().unwrap();

    let _ = std::fs::remove_dir_all("example_db");

    System::run(|| {
        let addr = Storage::new("example_db", Options::default()).unwrap().start(1);

        spawn(
            addr.clone().send_query(query!(
                insert into blog {
                    "title": "Absurd",
                    "tags": ["absurd", "psychology"],
                    "content": "Still nothing..."
                }
            )).and_then({ let addr = addr.clone(); move |id| {
                info!("Inserted document id: {}", id);
                assert_eq!(id, 1);
                
                addr.send_query(query!(
                    insert into blog {
                        "title": "Lorem ipsum",
                        "tags": ["lorem", "ipsum"],
                        "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                    }
                ))
            } }).and_then({ let addr = addr.clone(); move |id| {
                info!("Inserted document id: {}", id);
                assert_eq!(id, 2);

                addr.send_query(query!(
                    index for blog tags string
                ))
            } }).and_then({ let addr = addr.clone(); move |_| {
                info!("Indexing is ok");
                
                addr.send_query(query!(
                    find BlogPost in blog
                    where tags == "psychology"
                        order asc
                ))
            } }).map(|mut docs| {
                info!("Number of found documents: {}", docs.size_hint().0);
                
                assert_eq!(docs.size_hint(), (1, Some(1)));
                
                let doc = docs.next().unwrap().unwrap();

                info!("Found document: {:?}", doc);
                
                let doc_data: BlogPost = from_value(json!({
                    "id": 1,
                    "title": "Absurd",
                    "tags": ["absurd", "psychology"],
                    "content": "Still nothing..."
                })).unwrap();
                
                assert_eq!(&doc, &doc_data);
                assert!(docs.next().is_none());
                
                System::current().stop();
            }).map_err(|err| {
                error!("Error: {:?}", err);
            })
        );
    });
}

REST-interface

LEDB HTTP interface 0.1.0

Storage API

get database info

GET /info

get database statistics

GET /stats

Collection API

get list of collections

GET /collection

create new empty collection

POST /collection?name=$collection_name

drop collection with all documents

DELETE /collection/$collection_name

Index API

get indexes of collection

GET /collection/$collection_name/index

create new index for collection

POST /collection/$collection_name/index?path=$field_name&kind=$index_kind&key=$key_type

drop index of collection

DELETE /collection/$collection_name/document/$index_name

Document API

find documents using query

GET /collection/$collection_name/document?filter=$query&order=$ordering&offset=$skip&length=$take

GET /collection/$collection_name?filter=$query&order=$ordering&offset=$skip&length=$take

modify documents using query

PUT /collection/$collection_name/document?filter=$query&modify=$modifications

PATCH /collection/$collection_name?filter=$query&modify=$modifications

remove documents using query

DELETE /collection/$collection_name/document?filter=$query

PUT /collection/$collection_name?filter=$query

insert new document

POST /collection/$collection_name/document

POST /collection/$collection_name

get document by id

GET /collection/$collection_name/document/$document_id

GET /collection/$collection_name/$document_id

replace document

PUT /collection/$collection_name/document/$document_id

PUT /collection/$collection_name/$document_id

remove document

DELETE /collection/$collection_name/document/$document_id

DELETE /collection/$collection_name/$document_id

Macros

Unified query message macro

Structs

Delete the previously inserted document
Iterator across found documents
Drop collection from storage
Drop spicific index from collection
Ensure collection in storage
Ensure new index for collection
Find documents using filter and ordering
Get collections request
Get indexes of collection
Get database info
Get the previously inserted document by primary key
Get database stats
Storage info data
Insert new document into collection
Indexed field definition
Indexed fields definition
Modification operator
Database options
Put new version of the previously inserted document
Remove documents using filter
Set indexes for collection
Storage stats data
Storage actor
Update documents using filter and modifier

Enums

Modifier action
Comparison operator of filter
Condition operator of filter
Filter operator
Generic string indentifier
The kind of index
The data of key
The type of key
Ordering operator
The kind ot order
An enum over all possible CBOR types.

Traits

Identified document representation
Helper for sending queries

Functions

Delete the previously inserted document
Drop collection from storage
Drop spicific index from collection
Ensure collection in storage
Ensure new index for collection
Find documents using filter and ordering
Get the previously inserted document by primary key
Get indexes of collection
Insert new document into collection
Put new version of the previously inserted document
Remove documents using filter
Set indexes for collection
Update documents using filter and modifier

Type Definitions

The list of collections
Primary key (document identifier)