[][src]Struct no_proto::NP_Factory

pub struct NP_Factory<'fact> {
    pub schema: NP_Schema,
    // some fields omitted
}

Factories are created from schemas. Once you have a factory you can use it to create new buffers or open existing ones.

The easiest way to create a factory is to pass a JSON string schema into the static new method. Learn about schemas here.

You can also create a factory with a compiled byte schema using the static new_compiled method.

Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
 
let user_factory = NP_Factory::new(r#"{
    "type": "table",
    "columns": [
        ["name",   {"type": "string"}],
        ["pass",   {"type": "string"}],
        ["age",    {"type": "uint16"}],
        ["todos",  {"type": "list", "of": {"type": "string"}}]
    ]
}"#)?;
 
// user_factory can now be used to make or open buffers that contain the data in the schema.
 
// create new buffer
let mut user_buffer = user_factory.empty_buffer(None); // optional capacity, optional address size
    
// set the "name" column of the table
user_buffer.set(&["name"], "Billy Joel")?;
 
// set the first todo
user_buffer.set(&["todos", "0"], "Write a rust library.")?;
 
// close buffer 
let user_vec:Vec<u8> = user_buffer.close();
 
// open existing buffer for reading
let user_buffer_2 = user_factory.open_buffer(user_vec);
 
// read column value
let name_column = user_buffer_2.get::<&str>(&["name"])?;
assert_eq!(name_column, Some("Billy Joel"));
 
 
// read first todo
let todo_value = user_buffer_2.get::<&str>(&["todos", "0"])?;
assert_eq!(todo_value, Some("Write a rust library."));
 
// read second todo
let todo_value = user_buffer_2.get::<&str>(&["todos", "1"])?;
assert_eq!(todo_value, None);
 
 
// close buffer again
let user_vec: Vec<u8> = user_buffer_2.close();
// user_vec is a Vec<u8> with our data
 

Next Step

Read about how to use buffers to access, mutate and compact data.

Go to NP_Buffer docs

Fields

schema: NP_Schema

schema data used by this factory

Implementations

impl<'fact> NP_Factory<'fact>[src]

pub fn new<S>(json_schema: S) -> Result<Self, NP_Error> where
    S: Into<String>, 
[src]

Generate a new factory from the given schema.

This operation will fail if the schema provided is invalid or if the schema is not valid JSON. If it fails you should get a useful error message letting you know what the problem is.

pub fn new_compiled(schema_bytes: &'fact [u8]) -> Self[src]

Create a new factory from a compiled schema byte array. The byte schemas are at least an order of magnitude faster to parse than JSON schemas.

pub fn compile_schema(&self) -> &[u8][src]

Get a copy of the compiled schema byte array

pub fn export_schema(&self) -> Result<NP_JSON, NP_Error>[src]

Exports this factorie's schema to JSON. This works regardless of wether the factory was created with NP_Factory::new or NP_Factory::new_compiled.

pub fn open_sortable_buffer<'buffer>(
    &'buffer self,
    bytes: Vec<u8>
) -> Result<NP_Buffer<'buffer>, NP_Error>
[src]

Open existing Vec sortable buffer that was closed with .close_sortable()

There is typically 10 bytes or more in front of every sortable buffer that is identical between all sortable buffers for a given schema.

This method is used to open buffers that have had the leading identical bytes trimmed from them using .close_sortale().

This operation fails if the buffer is not sortable.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new(r#"{
   "type": "tuple",
   "sorted": true,
   "values": [
        {"type": "u8"},
        {"type": "string", "size": 6}
    ]
}"#)?;
 
let mut new_buffer = factory.empty_buffer(None);
// set initial value
new_buffer.set(&["0"], 55u8)?;
new_buffer.set(&["1"], "hello")?;
 
// the buffer with it's vtables take up 20 bytes!
assert_eq!(new_buffer.read_bytes().len(), 20usize);
 
// close buffer and get sortable bytes
let bytes: Vec<u8> = new_buffer.close_sortable()?;
// with close_sortable() we only get the bytes we care about!
assert_eq!([55, 104, 101, 108, 108, 111, 32].to_vec(), bytes);
 
// you can always re open the sortable buffers with this call
let new_buffer = factory.open_sortable_buffer(bytes)?;
assert_eq!(new_buffer.get(&["0"])?, Some(55u8));
assert_eq!(new_buffer.get(&["1"])?, Some("hello "));
 

pub fn open_buffer<'buffer>(&'buffer self, bytes: Vec<u8>) -> NP_Buffer<'buffer>[src]

Open existing Vec as buffer for this factory.

pub fn open_buffer_ro<'buffer>(
    &'buffer self,
    bytes: &'buffer [u8]
) -> NP_Buffer_RO<'buffer>
[src]

Open existing buffer as ready only, much faster if you don't need to mutate anything

pub fn empty_buffer<'buffer>(
    &'buffer self,
    capacity: Option<usize>
) -> NP_Buffer<'buffer>
[src]

Generate a new empty buffer from this factory.

The first opional argument, capacity, can be used to set the space of the underlying Vec when it's created. If you know you're going to be putting lots of data into the buffer, it's a good idea to set this to a large number comparable to the amount of data you're putting in. The default is 1,024 bytes.

The second optional argument, ptr_size, controls how much address space you get in the buffer and how large the addresses are. Every value in the buffer contains at least one address, sometimes more. NP_Size::U16 (the default) gives you an address space of just over 16KB but is more space efficeint since the address pointers are only 2 bytes each. NP_Size::U32 gives you an address space of just over 4GB, but the addresses take up twice as much space in the buffer compared to NP_Size::U16. You can change the address size through compaction after the buffer is created, so it's fine to start with a smaller address space and convert it to a larger one later as needed. It's also possible to go the other way, you can convert larger address space down to a smaller one durring compaction.

Trait Implementations

impl<'fact> Debug for NP_Factory<'fact>[src]

Auto Trait Implementations

impl<'fact> Send for NP_Factory<'fact>[src]

impl<'fact> Sync for NP_Factory<'fact>[src]

impl<'fact> Unpin for NP_Factory<'fact>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.