<commit_before>// Copyright (c) 2017, All Contributors (see CONTRIBUTORS file)
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//!
//! This program is used to run doctests defined in doc/script
//!
#![feature(slice_patterns)]
extern crate glob;
extern crate pumpkindb;
extern crate regex;
extern crate crossbeam;
extern crate tempdir;
extern crate lmdb_zero as lmdb;
extern crate nom;

use std::io::prelude::*;
use std::fs;
use std::fs::File;
use std::sync::mpsc;

use regex::Regex;
use glob::glob;

use pumpkindb::script::{RequestMessage, ResponseMessage, EnvId, Env, Scheduler};
use pumpkindb::script::{textparser, binparser};
use pumpkindb::pubsub;
use pumpkindb::storage;
use pumpkindb::timestamp;
use std::sync::Arc;
use tempdir::TempDir;

fn eval(name: &[u8], script: &[u8]) {
    let dir = TempDir::new("pumpkindb").unwrap();
    let path = dir.path().to_str().unwrap();
    fs::create_dir_all(path).expect("can't create directory");
    let env = unsafe {
        lmdb::EnvBuilder::new()
            .expect("can't create env builder")
            .open(path, lmdb::open::NOTLS, 0o600)
            .expect("can't open env")
    };
    let name = String::from(std::str::from_utf8(name).unwrap());
    let db = storage::Storage::new(&env);
    crossbeam::scope(|scope| {
        let timestamp = Arc::new(timestamp::Timestamp::new(None));
        let mut publisher = pubsub::Publisher::new();
        let publisher_accessor = publisher.accessor();
        let publisher_thread = scope.spawn(move || publisher.run());
        let publisher_clone = publisher_accessor.clone();
        let timestamp_clone = timestamp.clone();
        let (sender, receiver) = Scheduler::create_sender();
        let handle = scope.spawn(move || {
            let mut scheduler = Scheduler::new(
                &db,
                publisher_clone,
                timestamp_clone,
                receiver,
            );
            scheduler.run()
        });
        let (callback, receiver) = mpsc::channel::<ResponseMessage>();
        let _ = sender.send(RequestMessage::ScheduleEnv(EnvId::new(), Vec::from(script), callback));
        match receiver.recv() {
            Ok(ResponseMessage::EnvTerminated(_, stack, stack_size)) => {
                let _ = sender.send(RequestMessage::Shutdown);
                publisher_accessor.shutdown();
                let mut stack_ = Vec::with_capacity(stack.len());
                for i in 0..(&stack).len() {
                    stack_.push((&stack[i]).as_slice());
                }
                let mut script_env = Env::new_with_stack(stack_, stack_size).unwrap();
                let val = script_env.pop().unwrap();
                assert_eq!(Vec::from(val), vec![1], "{} was expected to succeeed", &name);
                println!(" * {}", &name);
            }
            Ok(ResponseMessage::EnvFailed(_, err, _, _)) => {
                let _ = sender.send(RequestMessage::Shutdown);
                publisher_accessor.shutdown();
                panic!("Error while executing {:?}: {:?}", &name, err)
            }
            Err(err) => {
               let _ = sender.send(RequestMessage::Shutdown);
               publisher_accessor.shutdown();
               panic!("recv error: {:?}", err);
            }
        }
        let _ = handle.join();
        let _ = publisher_thread.join();
    });
}

fn main() {
    let re = Regex::new(r"```test\r?\n((.+\r?\n?)+)```").unwrap();
    for entry in glob("doc/script/**/*.md").expect("Failed to read glob pattern") {
        match entry {
            Ok(path) => {
                println!("{}:", path.to_str().unwrap());
                let mut f = File::open(path).expect("can't open file");
                let mut s = String::new();
                f.read_to_string(&mut s).expect("can't read file");
                for cap in re.captures_iter(s.as_ref()) {
                    let programs = textparser::programs(cap[1].as_ref()).unwrap().1;
                    for program in programs {
                        if program.len() > 0 {
                            match binparser::word(program.as_slice()) {
                                nom::IResult::Done(&[0x81, b':', ref rest..], program) => {
                                    eval(&program[1..], rest);
                                },
                                other => panic!("test definition parse error {:?}", other)
                            }
                        }
                    }

                }
            },
            Err(_) => (),
        }
    }
}
<commit_msg>Problem: Windows clock is failing HLC guarantees<commit_after>// Copyright (c) 2017, All Contributors (see CONTRIBUTORS file)
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//!
//! This program is used to run doctests defined in doc/script
//!
#![feature(slice_patterns)]
extern crate glob;
extern crate pumpkindb;
extern crate regex;
extern crate crossbeam;
extern crate tempdir;
extern crate lmdb_zero as lmdb;
extern crate nom;

use std::io::prelude::*;
use std::fs;
use std::fs::File;
use std::sync::mpsc;

use regex::Regex;
use glob::glob;

use pumpkindb::script::{RequestMessage, ResponseMessage, EnvId, Env, Scheduler};
use pumpkindb::script::{textparser, binparser};
use pumpkindb::pubsub;
use pumpkindb::storage;
use pumpkindb::timestamp;
use std::sync::Arc;
use tempdir::TempDir;

fn eval(name: &[u8], script: &[u8], timestamp: Arc<timestamp::Timestamp>) {
    let dir = TempDir::new("pumpkindb").unwrap();
    let path = dir.path().to_str().unwrap();
    fs::create_dir_all(path).expect("can't create directory");
    let env = unsafe {
        lmdb::EnvBuilder::new()
            .expect("can't create env builder")
            .open(path, lmdb::open::NOTLS, 0o600)
            .expect("can't open env")
    };
    let name = String::from(std::str::from_utf8(name).unwrap());
    let db = storage::Storage::new(&env);
    crossbeam::scope(|scope| {
        let mut publisher = pubsub::Publisher::new();
        let publisher_accessor = publisher.accessor();
        let publisher_thread = scope.spawn(move || publisher.run());
        let publisher_clone = publisher_accessor.clone();
        let timestamp_clone = timestamp.clone();
        let (sender, receiver) = Scheduler::create_sender();
        let handle = scope.spawn(move || {
            let mut scheduler = Scheduler::new(
                &db,
                publisher_clone,
                timestamp_clone,
                receiver,
            );
            scheduler.run()
        });
        let (callback, receiver) = mpsc::channel::<ResponseMessage>();
        let _ = sender.send(RequestMessage::ScheduleEnv(EnvId::new(), Vec::from(script), callback));
        match receiver.recv() {
            Ok(ResponseMessage::EnvTerminated(_, stack, stack_size)) => {
                let _ = sender.send(RequestMessage::Shutdown);
                publisher_accessor.shutdown();
                let mut stack_ = Vec::with_capacity(stack.len());
                for i in 0..(&stack).len() {
                    stack_.push((&stack[i]).as_slice());
                }
                let mut script_env = Env::new_with_stack(stack_, stack_size).unwrap();
                let val = script_env.pop().unwrap();
                assert_eq!(Vec::from(val), vec![1], "{} was expected to succeeed", &name);
                println!(" * {}", &name);
            }
            Ok(ResponseMessage::EnvFailed(_, err, _, _)) => {
                let _ = sender.send(RequestMessage::Shutdown);
                publisher_accessor.shutdown();
                panic!("Error while executing {:?}: {:?}", &name, err)
            }
            Err(err) => {
               let _ = sender.send(RequestMessage::Shutdown);
               publisher_accessor.shutdown();
               panic!("recv error: {:?}", err);
            }
        }
        let _ = handle.join();
        let _ = publisher_thread.join();
    });
}

fn main() {
    let timestamp = Arc::new(timestamp::Timestamp::new(None));
    let re = Regex::new(r"```test\r?\n((.+\r?\n?)+)```").unwrap();
    for entry in glob("doc/script/**/*.md").expect("Failed to read glob pattern") {
        match entry {
            Ok(path) => {
                println!("{}:", path.to_str().unwrap());
                let mut f = File::open(path).expect("can't open file");
                let mut s = String::new();
                f.read_to_string(&mut s).expect("can't read file");
                for cap in re.captures_iter(s.as_ref()) {
                    let programs = textparser::programs(cap[1].as_ref()).unwrap().1;
                    for program in programs {
                        if program.len() > 0 {
                            match binparser::word(program.as_slice()) {
                                nom::IResult::Done(&[0x81, b':', ref rest..], program) => {
                                    eval(&program[1..], rest, timestamp.clone());
                                },
                                other => panic!("test definition parse error {:?}", other)
                            }
                        }
                    }

                }
            },
            Err(_) => (),
        }
    }
}
<|endoftext|>


<commit_before>use data::packet::Packet;
use data::rational::Rational64;
use stream::Stream;
use error::*;

use buffer::Buffered;
use std::io::SeekFrom;

#[derive(Clone, Debug, PartialEq)]
pub struct GlobalInfo {
    pub duration: Option<u64>,
    pub timebase: Option<Rational64>,
    pub streams: Vec<Stream>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Event {
    NewPacket(Packet),
    NewStream(Stream),
    MoreDataNeeded(usize),
}

pub trait Demuxer {
    fn read_headers(&mut self, buf: &Box<Buffered>, info: &mut GlobalInfo) -> Result<SeekFrom>;
    fn read_event(&mut self, buf: &Box<Buffered>) -> Result<(SeekFrom, Event)>;
}

#[derive(Clone, Debug, PartialEq)]
pub struct Descr {
    pub name: &'static str,
    pub demuxer: &'static str,
    pub description: &'static str,
    pub extensions: &'static [&'static str],
    pub mime: &'static [&'static str],
}

pub trait Descriptor {
    fn create(&self) -> Box<Demuxer>;
    fn describe<'a>(&'a self) -> &'a Descr;

    fn probe(&self, data: &[u8]) -> u8;
}

pub struct Context {
    demuxer: Box<Demuxer>,
    reader: Box<Buffered>,
    pub info: GlobalInfo,
}

impl Context {
    pub fn new<R: Buffered + 'static>(demuxer: Box<Demuxer>, reader: Box<R>) -> Self {
        Context {
            demuxer: demuxer,
            reader: reader,
            info: GlobalInfo {
                duration: None,
                timebase: None,
                streams: Vec::with_capacity(2),
            },
        }
    }

    fn read_headers_internal(&mut self) -> Result<()> {
        let ref mut demux = self.demuxer;
        let res = demux.read_headers(&self.reader, &mut self.info);
        match res {
            Err(e) => Err(e),
            Ok(seek) => {
                //TODO: handle seeking here
                let res = self.reader.seek(seek);
                println!("stream now at index: {:?}", res);
                Ok(())
            }
        }
    }

    pub fn read_headers(&mut self) -> Result<()> {
        loop {
            println!("Filling");
            try!(self.reader.fill_buf());
            match self.read_headers_internal() {
                Err(e) => match e {
                    Error(ErrorKind::MoreDataNeeded(needed), _) => {
                        self.reader.grow(needed);
                    },
                    _ => return Err(e)
                },
                Ok(_) => return Ok(()),
            }
        }
    }

    fn read_event_internal(&mut self) -> Result<Event> {
        let ref mut demux = self.demuxer;

        let res = demux.read_event(&self.reader);
        match res {
            Err(e) => Err(e),
            Ok((seek, event)) => {
                //TODO: handle seeking here
                let res = self.reader.seek(seek);
                println!("stream now at index: {:?}", res);
                try!(self.reader.fill_buf());
                if let Event::NewStream(ref st) = event {
                    self.info.streams.push(st.clone());
                }
                if let Event::MoreDataNeeded(size) = event {
                    return Err(ErrorKind::MoreDataNeeded(size).into());
                }
                Ok(event)
            }
        }
    }

    pub fn read_event(&mut self) -> Result<Event> {
        // TODO: guard against infiniloops and maybe factor the loop.
        loop {
            try!(self.reader.fill_buf());
            match self.read_event_internal() {
                Err(e) => match e {
                    Error(ErrorKind::MoreDataNeeded(needed), _) => {
                        self.reader.grow(needed);
                    },
                    _ => return Err(e)
                },
                Ok(ev) => return Ok(ev),
            }
        }
    }
}

pub const PROBE_DATA: usize = 4 * 1024;
pub const PROBE_SCORE_EXTENSION: u8 = 50;

// TODO:
// IntoIterator<Item = &'static Descriptor> is confusing

pub trait Probe {
    fn probe(&self, data: &[u8]) -> Option<&'static Descriptor>;
}

impl<'a> Probe for [&'static Descriptor] {
    fn probe(&self, data: &[u8]) -> Option<&'static Descriptor> {
        let mut max = u8::min_value();
        let mut candidate: Option<&'static Descriptor> = None;
        for desc in self {
            let score = desc.probe(data);

            if score > max {
                max = score;
                candidate = Some(*desc);
            }
        }

        if max > PROBE_SCORE_EXTENSION {
            candidate
        } else {
            None
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::io::SeekFrom;
    use data::packet::Packet;

    struct DummyDes {
        d: Descr,
    }

    struct DummyDemuxer {}

    impl Demuxer for DummyDemuxer {
        fn read_headers(&mut self, buf: &Box<Buffered>, _info: &mut GlobalInfo) -> Result<SeekFrom> {
            let len = buf.data().len();
            if 9 > len {
                let needed = 9 - len;
                Err(ErrorKind::MoreDataNeeded(needed).into())
            } else {
                Ok(SeekFrom::Current(9))
            }
        }
        fn read_event(&mut self, buf: &Box<Buffered>) -> Result<(SeekFrom, Event)> {
            let size = 2;
            let len = buf.data().len();
            if size > len {
                Err(ErrorKind::MoreDataNeeded(size - len).into())
            } else {
                println!("{:?}", buf.data());
                match &buf.data()[..2] {
                    b"p1" => {
                        Ok((SeekFrom::Current(3), Event::NewPacket(Packet::new())))
                    },
                    b"e1" => {
                        Ok((SeekFrom::Current(3), Event::MoreDataNeeded(0)))
                    }
                    _ => Err(ErrorKind::InvalidData.into())
                }
            }
        }
    }

    impl Descriptor for DummyDes {
        fn create(&self) -> Box<Demuxer> {
            Box::new(DummyDemuxer {})
        }
        fn describe<'a>(&'a self) -> &'a Descr {
            &self.d
        }
        fn probe(&self, data: &[u8]) -> u8 {
            match data {
                b"dummy" => 100,
                _ => 0,
            }
        }
    }

    const DUMMY_DES: &Descriptor = &DummyDes {
        d: Descr {
            name: "dummy",
            demuxer: "dummy",
            description: "Dummy dem",
            extensions: &["dm", "dum"],
            mime: &["application/dummy"],
        },
    };

    #[test]
    fn probe() {
        let demuxers: &[&'static Descriptor] = &[DUMMY_DES];

        demuxers.probe(b"dummy").unwrap();
    }

    use std::io::Cursor;
    use buffer::*;

    #[test]
    fn read_headers() {
        let buf = b"dummy header";
        let r = AccReader::with_capacity(4, Cursor::new(buf));
        let d = DUMMY_DES.create();
        let mut c = Context::new(d, Box::new(r));

        c.read_headers().unwrap();
    }

    #[test]
    fn read_event() {
        let buf = b"dummy header p1 e1 p1 ";

        let r = AccReader::with_capacity(4, Cursor::new(buf));
        let d = DUMMY_DES.create();
        let mut c = Context::new(d, Box::new(r));

        c.read_headers().unwrap();

        println!("{:?}", c.read_event());
        println!("{:?}", c.read_event());
        println!("{:?}", c.read_event());
        println!("{:?}", c.read_event());
    }
}
<commit_msg>format: Fill only when strictly needed<commit_after>use data::packet::Packet;
use data::rational::Rational64;
use stream::Stream;
use error::*;

use buffer::Buffered;
use std::io::SeekFrom;

#[derive(Clone, Debug, PartialEq)]
pub struct GlobalInfo {
    pub duration: Option<u64>,
    pub timebase: Option<Rational64>,
    pub streams: Vec<Stream>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Event {
    NewPacket(Packet),
    NewStream(Stream),
    MoreDataNeeded(usize),
}

pub trait Demuxer {
    fn read_headers(&mut self, buf: &Box<Buffered>, info: &mut GlobalInfo) -> Result<SeekFrom>;
    fn read_event(&mut self, buf: &Box<Buffered>) -> Result<(SeekFrom, Event)>;
}

#[derive(Clone, Debug, PartialEq)]
pub struct Descr {
    pub name: &'static str,
    pub demuxer: &'static str,
    pub description: &'static str,
    pub extensions: &'static [&'static str],
    pub mime: &'static [&'static str],
}

pub trait Descriptor {
    fn create(&self) -> Box<Demuxer>;
    fn describe<'a>(&'a self) -> &'a Descr;

    fn probe(&self, data: &[u8]) -> u8;
}

pub struct Context {
    demuxer: Box<Demuxer>,
    reader: Box<Buffered>,
    pub info: GlobalInfo,
}

impl Context {
    pub fn new<R: Buffered + 'static>(demuxer: Box<Demuxer>, reader: Box<R>) -> Self {
        Context {
            demuxer: demuxer,
            reader: reader,
            info: GlobalInfo {
                duration: None,
                timebase: None,
                streams: Vec::with_capacity(2),
            },
        }
    }

    fn read_headers_internal(&mut self) -> Result<()> {
        let ref mut demux = self.demuxer;
        let res = demux.read_headers(&self.reader, &mut self.info);
        match res {
            Err(e) => Err(e),
            Ok(seek) => {
                //TODO: handle seeking here
                let res = self.reader.seek(seek);
                println!("stream now at index: {:?}", res);
                Ok(())
            }
        }
    }

    pub fn read_headers(&mut self) -> Result<()> {
        loop {
            println!("Filling");
            try!(self.reader.fill_buf());
            match self.read_headers_internal() {
                Err(e) => match e {
                    Error(ErrorKind::MoreDataNeeded(needed), _) => {
                        self.reader.grow(needed);
                    },
                    _ => return Err(e)
                },
                Ok(_) => return Ok(()),
            }
        }
    }

    fn read_event_internal(&mut self) -> Result<Event> {
        let ref mut demux = self.demuxer;

        let res = demux.read_event(&self.reader);
        match res {
            Err(e) => Err(e),
            Ok((seek, event)) => {
                //TODO: handle seeking here
                let res = self.reader.seek(seek);
                if let Event::NewStream(ref st) = event {
                    self.info.streams.push(st.clone());
                }
                if let Event::MoreDataNeeded(size) = event {
                    return Err(ErrorKind::MoreDataNeeded(size).into());
                }
                Ok(event)
            }
        }
    }

    pub fn read_event(&mut self) -> Result<Event> {
        // TODO: guard against infiniloops and maybe factor the loop.
        loop {
            match self.read_event_internal() {
                Err(e) => match e {
                    Error(ErrorKind::MoreDataNeeded(needed), _) => {
                        self.reader.grow(needed);
                        try!(self.reader.fill_buf());
                    },
                    _ => return Err(e)
                },
                Ok(ev) => return Ok(ev),
            }
        }
    }
}

pub const PROBE_DATA: usize = 4 * 1024;
pub const PROBE_SCORE_EXTENSION: u8 = 50;

// TODO:
// IntoIterator<Item = &'static Descriptor> is confusing

pub trait Probe {
    fn probe(&self, data: &[u8]) -> Option<&'static Descriptor>;
}

impl<'a> Probe for [&'static Descriptor] {
    fn probe(&self, data: &[u8]) -> Option<&'static Descriptor> {
        let mut max = u8::min_value();
        let mut candidate: Option<&'static Descriptor> = None;
        for desc in self {
            let score = desc.probe(data);

            if score > max {
                max = score;
                candidate = Some(*desc);
            }
        }

        if max > PROBE_SCORE_EXTENSION {
            candidate
        } else {
            None
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::io::SeekFrom;
    use data::packet::Packet;

    struct DummyDes {
        d: Descr,
    }

    struct DummyDemuxer {}

    impl Demuxer for DummyDemuxer {
        fn read_headers(&mut self, buf: &Box<Buffered>, _info: &mut GlobalInfo) -> Result<SeekFrom> {
            let len = buf.data().len();
            if 9 > len {
                let needed = 9 - len;
                Err(ErrorKind::MoreDataNeeded(needed).into())
            } else {
                Ok(SeekFrom::Current(9))
            }
        }
        fn read_event(&mut self, buf: &Box<Buffered>) -> Result<(SeekFrom, Event)> {
            let size = 2;
            let len = buf.data().len();
            if size > len {
                Err(ErrorKind::MoreDataNeeded(size - len).into())
            } else {
                println!("{:?}", buf.data());
                match &buf.data()[..2] {
                    b"p1" => {
                        Ok((SeekFrom::Current(3), Event::NewPacket(Packet::new())))
                    },
                    b"e1" => {
                        Ok((SeekFrom::Current(3), Event::MoreDataNeeded(0)))
                    }
                    _ => Err(ErrorKind::InvalidData.into())
                }
            }
        }
    }

    impl Descriptor for DummyDes {
        fn create(&self) -> Box<Demuxer> {
            Box::new(DummyDemuxer {})
        }
        fn describe<'a>(&'a self) -> &'a Descr {
            &self.d
        }
        fn probe(&self, data: &[u8]) -> u8 {
            match data {
                b"dummy" => 100,
                _ => 0,
            }
        }
    }

    const DUMMY_DES: &Descriptor = &DummyDes {
        d: Descr {
            name: "dummy",
            demuxer: "dummy",
            description: "Dummy dem",
            extensions: &["dm", "dum"],
            mime: &["application/dummy"],
        },
    };

    #[test]
    fn probe() {
        let demuxers: &[&'static Descriptor] = &[DUMMY_DES];

        demuxers.probe(b"dummy").unwrap();
    }

    use std::io::Cursor;
    use buffer::*;

    #[test]
    fn read_headers() {
        let buf = b"dummy header";
        let r = AccReader::with_capacity(4, Cursor::new(buf));
        let d = DUMMY_DES.create();
        let mut c = Context::new(d, Box::new(r));

        c.read_headers().unwrap();
    }

    #[test]
    fn read_event() {
        let buf = b"dummy header p1 e1 p1 ";

        let r = AccReader::with_capacity(4, Cursor::new(buf));
        let d = DUMMY_DES.create();
        let mut c = Context::new(d, Box::new(r));

        c.read_headers().unwrap();

        println!("{:?}", c.read_event());
        println!("{:?}", c.read_event());
        println!("{:?}", c.read_event());
        println!("{:?}", c.read_event());
    }
}
<|endoftext|>


<commit_before>pub trait Handler {
    type Sample;

    #[allow(unused_variables)]
    fn handle_sample(&mut self, sample: &Self::Sample);

    #[allow(unused_variables)]
    fn handle_terminal(&mut self, sample: &Self::Sample) {}
}

pub trait BatchHandler: Handler {
    fn handle_batch(&mut self, batch: &Vec<Self::Sample>) {
        for sample in batch.into_iter() {
            self.handle_sample(sample);
        }
    }
}
<commit_msg>refactor(core): make implementation of handle_sample optional<commit_after>pub trait Handler {
    type Sample;

    #[allow(unused_variables)]
    fn handle_sample(&mut self, sample: &Self::Sample) {}

    #[allow(unused_variables)]
    fn handle_terminal(&mut self, sample: &Self::Sample) {}
}

pub trait BatchHandler: Handler {
    fn handle_batch(&mut self, batch: &Vec<Self::Sample>) {
        for sample in batch.into_iter() {
            self.handle_sample(sample);
        }
    }
}
<|endoftext|>


<commit_before>
use libc::c_void;
use libc::funcs::c95::stdlib::free;
use std::slice;
use std::mem::transmute;

/// Horrible kludge to free memory allocated by lodepng
pub struct CVec<T> {
    elements: usize,
    ptr: *mut T,
}

impl<T> CVec<T> {
    pub fn new(ptr: *mut T, elements: usize) -> CVec<T> {
        CVec {ptr:ptr, elements:elements}
    }

    /// Number of elements (pixels for pixel type, bytes for u8 type only)
    pub fn len(&self) -> usize {
        self.elements
    }

    /// *Copies* elements into a Vec
    pub fn to_vec(&self) -> Vec<T> {
        unsafe {
            Vec::from_raw_buf(self.ptr, self.elements)
        }
    }

    /// Exposes memory as slice without copying
    pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
        unsafe {
            slice::from_raw_mut_buf(&mut self.ptr, self.elements)
        }
    }

    /// Exposes memory as slice without copying
    pub fn as_slice<'a>(&'a self) -> &'a [T] {
        unsafe {
            slice::from_raw_buf(transmute(&self.ptr), self.elements)
        }
    }

    /// Exposes memory as slice without copying
    pub fn as_u8_slice<'a>(&'a self) -> &'a [u8] {
        unsafe {
            slice::from_raw_buf(transmute(&self.ptr), self.elements * ::std::mem::size_of::<T>())
        }
    }
}

#[unsafe_destructor]
impl<T> Drop for CVec<T> {
    fn drop(&mut self) {
        unsafe {
            free(self.ptr as *mut c_void);
        }
    }
}
<commit_msg>It's unsafe<commit_after>
use libc::c_void;
use libc::funcs::c95::stdlib::free;
use std::slice;
use std::mem::transmute;

/// Horrible kludge to free memory allocated by lodepng
pub struct CVec<T> {
    elements: usize,
    ptr: *mut T,
}

impl<T> CVec<T> {
    pub unsafe fn new(ptr: *mut T, elements: usize) -> CVec<T> {
        CVec {ptr:ptr, elements:elements}
    }

    /// Number of elements (pixels for pixel type, bytes for u8 type only)
    pub fn len(&self) -> usize {
        self.elements
    }

    /// *Copies* elements into a Vec
    pub fn to_vec(&self) -> Vec<T> {
        unsafe {
            Vec::from_raw_buf(self.ptr, self.elements)
        }
    }

    /// Exposes memory as slice without copying
    pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
        unsafe {
            slice::from_raw_mut_buf(&mut self.ptr, self.elements)
        }
    }

    /// Exposes memory as slice without copying
    pub fn as_slice<'a>(&'a self) -> &'a [T] {
        unsafe {
            slice::from_raw_buf(transmute(&self.ptr), self.elements)
        }
    }

    /// Exposes memory as slice without copying
    pub fn as_u8_slice<'a>(&'a self) -> &'a [u8] {
        unsafe {
            slice::from_raw_buf(transmute(&self.ptr), self.elements * ::std::mem::size_of::<T>())
        }
    }
}

#[unsafe_destructor]
impl<T> Drop for CVec<T> {
    fn drop(&mut self) {
        unsafe {
            free(self.ptr as *mut c_void);
        }
    }
}
<|endoftext|>


<commit_before>use master_smasher::drawable::{Animation, AnimationData};

use glm;
use moho::errors::*;
use moho::renderer::Renderer;
use moho::resource_manager::{ResourceManager, Texture};
use num_traits::One;

#[derive(Clone)]
pub struct WorldAssets {
    pub red_planet: Texture,
    pub white_planet: Texture,
    pub blue_planet: Texture,
    pub red_ring: Texture,
    pub white_ring: Texture,
    pub blue_ring: Texture,
    pub dead_planet: Texture,

    pub star: AnimationData,
    pub explosion: AnimationData,
    pub enemy: AnimationData,
}

impl WorldAssets {
    pub fn new<R: Renderer>(resource_manager: &ResourceManager<R>) -> Result<Self> {
        let red_planet = resource_manager.load_texture("resources/red_planet.png")?;
        let white_planet = resource_manager.load_texture("resources/white_planet.png")?;
        let blue_planet = resource_manager.load_texture("resources/blue_planet.png")?;
        let dead_planet = resource_manager.load_texture("resources/dead_planet.png")?;
        let red_ring = resource_manager.load_texture("resources/red_ring.png")?;
        let white_ring = resource_manager.load_texture("resources/white_ring.png")?;
        let blue_ring = resource_manager.load_texture("resources/blue_ring.png")?;

        let star_path = "resources/star.png";
        let enemy_path = "resources/spaceship.png";
        let explosion_path = "resources/explosion_small.png";
        let star = AnimationData::new(star_path, 2, 150, true, resource_manager)?;
        let enemy = AnimationData::new(enemy_path, 2, 100, true, resource_manager)?;
        let explosion = AnimationData::new(explosion_path, 10, 100, false, resource_manager)?;
        let assets = WorldAssets {
            red_planet: red_planet,
            white_planet: white_planet,
            blue_planet: blue_planet,
            dead_planet: dead_planet,
            red_ring: red_ring,
            white_ring: white_ring,
            blue_ring: blue_ring,
            star: star,
            enemy: enemy,
            explosion: explosion,
        };
        Ok(assets)
    }

    pub fn star(&self, center: glm::IVec2) -> Animation {
        Animation::start(&self.star, center, glm::DVec2::one())
    }

    pub fn enemy(&self, center: glm::IVec2) -> Animation {
        Animation::start(&self.enemy, center, glm::DVec2::one())
    }
}
<commit_msg>make enemy smaller<commit_after>use master_smasher::drawable::{Animation, AnimationData};

use glm::{self, GenNum};
use moho::errors::*;
use moho::renderer::Renderer;
use moho::resource_manager::{ResourceManager, Texture};
use num_traits::One;

#[derive(Clone)]
pub struct WorldAssets {
    pub red_planet: Texture,
    pub white_planet: Texture,
    pub blue_planet: Texture,
    pub red_ring: Texture,
    pub white_ring: Texture,
    pub blue_ring: Texture,
    pub dead_planet: Texture,

    pub star: AnimationData,
    pub explosion: AnimationData,
    pub enemy: AnimationData,
}

impl WorldAssets {
    pub fn new<R: Renderer>(resource_manager: &ResourceManager<R>) -> Result<Self> {
        let red_planet = resource_manager.load_texture("resources/red_planet.png")?;
        let white_planet = resource_manager.load_texture("resources/white_planet.png")?;
        let blue_planet = resource_manager.load_texture("resources/blue_planet.png")?;
        let dead_planet = resource_manager.load_texture("resources/dead_planet.png")?;
        let red_ring = resource_manager.load_texture("resources/red_ring.png")?;
        let white_ring = resource_manager.load_texture("resources/white_ring.png")?;
        let blue_ring = resource_manager.load_texture("resources/blue_ring.png")?;

        let star_path = "resources/star.png";
        let enemy_path = "resources/spaceship.png";
        let explosion_path = "resources/explosion_small.png";
        let star = AnimationData::new(star_path, 2, 150, true, resource_manager)?;
        let enemy = AnimationData::new(enemy_path, 2, 100, true, resource_manager)?;
        let explosion = AnimationData::new(explosion_path, 10, 100, false, resource_manager)?;
        let assets = WorldAssets {
            red_planet: red_planet,
            white_planet: white_planet,
            blue_planet: blue_planet,
            dead_planet: dead_planet,
            red_ring: red_ring,
            white_ring: white_ring,
            blue_ring: blue_ring,
            star: star,
            enemy: enemy,
            explosion: explosion,
        };
        Ok(assets)
    }

    pub fn star(&self, center: glm::IVec2) -> Animation {
        Animation::start(&self.star, center, glm::DVec2::one())
    }

    pub fn enemy(&self, center: glm::IVec2) -> Animation {
        Animation::start(&self.enemy, center, glm::DVec2::from_s(0.75))
    }
}
<|endoftext|>


<commit_before>use std::error::Error;

use conduit::{Request, Response, Handler};
use conduit_static::Static;
use conduit_middleware::AroundMiddleware;

use util::RequestProxy;

pub struct Middleware {
    handler: Option<Box<Handler>>,
    dist: Static,
}

impl Middleware {
    pub fn new() -> Middleware {
        Middleware {
            handler: None,
            dist: Static::new("dist"),
        }
    }
}

impl AroundMiddleware for Middleware {
    fn with_handler(&mut self, handler: Box<Handler>) {
        self.handler = Some(handler);
    }
}

impl Handler for Middleware {
    fn call(&self, req: &mut Request) -> Result<Response, Box<Error+Send>> {
        // First, attempt to serve a static file. If we're missing a static
        // file, then keep going.
        match self.dist.call(req) {
            Ok(ref resp) if resp.status.0 == 404 => {}
            ret => return ret,
        }

        // Second, if we're requesting html, then we've only got one page so
        // serve up that page. Otherwise proxy on to the rest of the app.
        let wants_html = req.headers().find("Accept").map(|accept| {
            accept.iter().any(|s| s.contains("html"))
        }).unwrap_or(false);
        if wants_html {
            self.dist.call(&mut RequestProxy {
                other: req,
                path: Some("/index.html"),
                method: None,
            })
        } else {
            self.handler.as_ref().unwrap().call(req)
        }
    }
}
<commit_msg>Assume html if no Accept header is presented<commit_after>use std::error::Error;

use conduit::{Request, Response, Handler};
use conduit_static::Static;
use conduit_middleware::AroundMiddleware;

use util::RequestProxy;

pub struct Middleware {
    handler: Option<Box<Handler>>,
    dist: Static,
}

impl Middleware {
    pub fn new() -> Middleware {
        Middleware {
            handler: None,
            dist: Static::new("dist"),
        }
    }
}

impl AroundMiddleware for Middleware {
    fn with_handler(&mut self, handler: Box<Handler>) {
        self.handler = Some(handler);
    }
}

impl Handler for Middleware {
    fn call(&self, req: &mut Request) -> Result<Response, Box<Error+Send>> {
        // First, attempt to serve a static file. If we're missing a static
        // file, then keep going.
        match self.dist.call(req) {
            Ok(ref resp) if resp.status.0 == 404 => {}
            ret => return ret,
        }

        // Second, if we're requesting html, then we've only got one page so
        // serve up that page. Otherwise proxy on to the rest of the app.
        let wants_html = req.headers().find("Accept").map(|accept| {
            accept.iter().any(|s| s.contains("html"))
        }).unwrap_or(true); // If no Accept header is specified, serve up html.
        if wants_html {
            self.dist.call(&mut RequestProxy {
                other: req,
                path: Some("/index.html"),
                method: None,
            })
        } else {
            self.handler.as_ref().unwrap().call(req)
        }
    }
}
<|endoftext|>


<commit_before>use proc_macro2::{self, Ident, Span};
use syn;

use field::*;
use model::*;
use util::*;

pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
    let model = Model::from_item(&item)?;

    let struct_name = &item.ident;
    let fields = model.fields().iter().map(get_ident).collect::<Vec<_>>();
    let field_names = model.fields().iter().map(|f| &f.name).collect::<Vec<_>>();

    let initial_field_expr = model
        .fields()
        .iter()
        .map(|f| {
            if f.has_flag("embed") {
                let field_ty = &f.ty;
                Ok(quote!(<#field_ty as QueryableByName<__DB>>::build(
                    row,
                )?))
            } else {
                let field_ty = &f.ty;
                let deserialize_ty = f.ty_for_deserialize()?;
                let name = f.column_name_str();
                Ok(quote!(
                   {
                       let field = diesel::row::NamedRow::get(row, #name)?;
                       <#deserialize_ty as Into<#field_ty>>::into(field)
                   }
                ))
            }
        })
        .collect::<Result<Vec<_>, Diagnostic>>()?;

    let (_, ty_generics, ..) = item.generics.split_for_impl();
    let mut generics = item.generics.clone();
    generics
        .params
        .push(parse_quote!(__DB: diesel::backend::Backend));

    for field in model.fields() {
        let where_clause = generics.where_clause.get_or_insert(parse_quote!(where));
        let field_ty = field.ty_for_deserialize()?;
        if field.has_flag("embed") {
            where_clause
                .predicates
                .push(parse_quote!(#field_ty: QueryableByName<__DB>));
        } else {
            let st = sql_type(field, &model);
            where_clause
                .predicates
                .push(parse_quote!(#field_ty: diesel::deserialize::FromSql<#st, __DB>));
        }
    }

    let (impl_generics, _, where_clause) = generics.split_for_impl();

    Ok(wrap_in_dummy_mod(quote! {
        use diesel::deserialize::{self, QueryableByName};
        use diesel::row::{NamedRow};
        use diesel::sql_types::Untyped;

        impl #impl_generics QueryableByName<__DB>
            for #struct_name #ty_generics
        #where_clause
        {
            fn build<'__a>(row: &impl NamedRow<'__a, __DB>) -> deserialize::Result<Self>
            {

                #(
                    let mut #fields = #initial_field_expr;
                )*
                deserialize::Result::Ok(Self {
                    #(
                        #field_names: #fields,
                    )*
                })
            }
        }
    }))
}

fn get_ident(field: &Field) -> Ident {
    match &field.name {
        FieldName::Named(n) => n.clone(),
        FieldName::Unnamed(i) => Ident::new(&format!("field_{}", i.index), Span::call_site()),
    }
}

fn sql_type(field: &Field, model: &Model) -> syn::Type {
    let table_name = model.table_name();

    match field.sql_type {
        Some(ref st) => st.clone(),
        None => {
            if model.has_table_name_attribute() {
                let column_name = field.column_name_ident();
                parse_quote!(diesel::dsl::SqlTypeOf<#table_name::#column_name>)
            } else {
                let field_name = match field.name {
                    FieldName::Named(ref x) => x.clone(),
                    _ => Ident::new("field", Span::call_site()),
                };
                field
                    .span
                    .error(format!("Cannot determine the SQL type of {}", field_name))
                    .help(
                        "Your struct must either be annotated with `#[table_name = \"foo\"]` \
                         or have all of its fields annotated with `#[sql_type = \"Integer\"]`",
                    )
                    .emit();
                parse_quote!(())
            }
        }
    }
}
<commit_msg>Extracting the same statement from different logical branches<commit_after>use proc_macro2::{self, Ident, Span};
use syn;

use field::*;
use model::*;
use util::*;

pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagnostic> {
    let model = Model::from_item(&item)?;

    let struct_name = &item.ident;
    let fields = model.fields().iter().map(get_ident).collect::<Vec<_>>();
    let field_names = model.fields().iter().map(|f| &f.name).collect::<Vec<_>>();

    let initial_field_expr = model
        .fields()
        .iter()
        .map(|f| {
            let field_ty = &f.ty;

            if f.has_flag("embed") {
                Ok(quote!(<#field_ty as QueryableByName<__DB>>::build(
                    row,
                )?))
            } else {
                let deserialize_ty = f.ty_for_deserialize()?;
                let name = f.column_name_str();
                Ok(quote!(
                   {
                       let field = diesel::row::NamedRow::get(row, #name)?;
                       <#deserialize_ty as Into<#field_ty>>::into(field)
                   }
                ))
            }
        })
        .collect::<Result<Vec<_>, Diagnostic>>()?;

    let (_, ty_generics, ..) = item.generics.split_for_impl();
    let mut generics = item.generics.clone();
    generics
        .params
        .push(parse_quote!(__DB: diesel::backend::Backend));

    for field in model.fields() {
        let where_clause = generics.where_clause.get_or_insert(parse_quote!(where));
        let field_ty = field.ty_for_deserialize()?;
        if field.has_flag("embed") {
            where_clause
                .predicates
                .push(parse_quote!(#field_ty: QueryableByName<__DB>));
        } else {
            let st = sql_type(field, &model);
            where_clause
                .predicates
                .push(parse_quote!(#field_ty: diesel::deserialize::FromSql<#st, __DB>));
        }
    }

    let (impl_generics, _, where_clause) = generics.split_for_impl();

    Ok(wrap_in_dummy_mod(quote! {
        use diesel::deserialize::{self, QueryableByName};
        use diesel::row::{NamedRow};
        use diesel::sql_types::Untyped;

        impl #impl_generics QueryableByName<__DB>
            for #struct_name #ty_generics
        #where_clause
        {
            fn build<'__a>(row: &impl NamedRow<'__a, __DB>) -> deserialize::Result<Self>
            {

                #(
                    let mut #fields = #initial_field_expr;
                )*
                deserialize::Result::Ok(Self {
                    #(
                        #field_names: #fields,
                    )*
                })
            }
        }
    }))
}

fn get_ident(field: &Field) -> Ident {
    match &field.name {
        FieldName::Named(n) => n.clone(),
        FieldName::Unnamed(i) => Ident::new(&format!("field_{}", i.index), Span::call_site()),
    }
}

fn sql_type(field: &Field, model: &Model) -> syn::Type {
    let table_name = model.table_name();

    match field.sql_type {
        Some(ref st) => st.clone(),
        None => {
            if model.has_table_name_attribute() {
                let column_name = field.column_name_ident();
                parse_quote!(diesel::dsl::SqlTypeOf<#table_name::#column_name>)
            } else {
                let field_name = match field.name {
                    FieldName::Named(ref x) => x.clone(),
                    _ => Ident::new("field", Span::call_site()),
                };
                field
                    .span
                    .error(format!("Cannot determine the SQL type of {}", field_name))
                    .help(
                        "Your struct must either be annotated with `#[table_name = \"foo\"]` \
                         or have all of its fields annotated with `#[sql_type = \"Integer\"]`",
                    )
                    .emit();
                parse_quote!(())
            }
        }
    }
}
<|endoftext|>


<commit_before>use client::AsyncClient;
use dovahkiin::types::*;
use futures::prelude::*;
use index::btree::external::ExtNode;
use index::btree::external::*;
use index::btree::internal::InNode;
use index::btree::node::{write_node, Node, NodeWriteGuard};
use index::btree::remove::SubNodeStatus::InNodeEmpty;
use index::btree::{external, max_entry_key, BPlusTree, DeletionSetInneer, NodeCellRef};
use index::{EntryKey, Slice};
use parking_lot::RwLock;
use ram::cell::Cell;
use std::cell::RefCell;
use std::cmp::max;
use std::collections::btree_set::BTreeSet;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::mem;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;

pub struct TreeConstructor<KS, PS>
where
    KS: Slice<EntryKey> + Debug + 'static,
    PS: Slice<NodeCellRef> + 'static,
{
    level_guards: Vec<Rc<RefCell<NodeWriteGuard<KS, PS>>>>,
}

impl<KS, PS> TreeConstructor<KS, PS>
where
    KS: Slice<EntryKey> + Debug + 'static,
    PS: Slice<NodeCellRef> + 'static,
{
    pub fn new() -> Self {
        TreeConstructor {
            level_guards: vec![],
        }
    }

    pub fn push_extnode(&mut self, node: &NodeCellRef, first_key: EntryKey) {
        self.push(0, node, first_key);
    }

    fn push(&mut self, level: usize, node: &NodeCellRef, first_key: EntryKey) {
        let mut new_level = false;
        if self.level_guards.len() < level + 1 {
            let mut new_root_innode = InNode::<KS, PS>::new(0, max_entry_key());
            let new_root_ref = NodeCellRef::new(Node::with_internal(new_root_innode));
            self.level_guards
                .push(Rc::new(RefCell::new(write_node::<KS, PS>(&new_root_ref))));
            new_level = true;
        }
        let parent_page_ref = self.level_guards[level].clone();
        let mut parent_guard = parent_page_ref.borrow_mut();
        let cap = KS::slice_len();
        if parent_guard.len() >= cap {
            let mut new_innode = InNode::<KS, PS>::new(1, max_entry_key());
            let parent_right_bound = parent_guard.last_key().clone();
            let new_innode_head_ptr = {
                let mut parent_innode = parent_guard.innode_mut();
                parent_innode.len -= 1;
                parent_innode.right_bound = parent_right_bound.clone();
                mem::replace(
                    &mut parent_innode.ptrs.as_slice()[cap],
                    NodeCellRef::default(),
                )
            };
            new_innode.ptrs.as_slice()[0] = new_innode_head_ptr;
            new_innode.ptrs.as_slice()[1] = node.clone();
            new_innode.keys.as_slice()[0] = first_key;
            let new_node = NodeCellRef::new(Node::with_internal(new_innode));
            *parent_guard = write_node::<KS, PS>(&new_node);
            self.push(level + 1, &new_node, parent_right_bound)
        } else {
            let mut parent_innode = parent_guard.innode_mut();
            let new_len = if new_level {
                0
            } else {
                let len = parent_innode.len;
                parent_innode.keys.as_slice()[len] = first_key;
                len + 1
            };
            parent_innode.ptrs.as_slice()[new_len] = node.clone();
            parent_innode.len = new_len;
        }
    }

    pub fn root(&self) -> NodeCellRef {
        let last_ref = self.level_guards.last().unwrap().clone();
        let last_guard = last_ref.borrow();
        if last_guard.len() == 0 {
            last_guard.innode().ptrs.as_slice_immute()[0].clone()
        } else {
            last_guard.node_ref().clone()
        }
    }
}

pub fn reconstruct_from_head_id<KS, PS>(head_id: Id, neb: &AsyncClient) -> BPlusTree<KS, PS>
where
    KS: Slice<EntryKey> + Debug + 'static,
    PS: Slice<NodeCellRef> + 'static,
{
    let mut len = 0;
    let mut constructor = TreeConstructor::<KS, PS>::new();
    let mut prev_ref = NodeCellRef::new_none::<KS, PS>();
    let mut id = head_id;
    let mut at_end = false;
    while !at_end {
        let cell = neb.read_cell(id).wait().unwrap().unwrap();
        let page = ExtNode::<KS, PS>::from_cell(&cell);
        let next_id = page.next_id;
        let prev_id = page.prev_id;
        let mut node = page.node;
        id = next_id;
        at_end = id.is_unit_id();
        if at_end {
            node.next = NodeCellRef::new_none::<KS, PS>();
        }
        let mut prev_lock = write_node::<KS, PS>(&prev_ref);
        if node.len == 0 {
            // skip this empty node and make it deleted
            external::make_deleted(&node.id);
            if at_end {
                // if the empty node is the last node, assign the right none node to previous node
                *prev_lock.right_ref_mut().unwrap() = node.next.clone();
            }
            continue;
        }
        let first_key = node.keys.as_slice_immute()[0].clone();
        len += node.len;
        node.prev = prev_ref.clone();
        let node_ref = NodeCellRef::new(Node::with_external(box node));
        if !prev_lock.is_none() {
            *prev_lock.right_bound_mut() = first_key.clone();
            *prev_lock.right_ref_mut().unwrap() = node_ref.clone();
        } else {
            assert_eq!(prev_id, Id::unit_id());
        }
        constructor.push_extnode(&node_ref, first_key);
        prev_ref = node_ref;
    }
    let root = constructor.root();
    BPlusTree {
        root: RwLock::new(root),
        root_versioning: NodeCellRef::new(Node::<KS, PS>::with_none()),
        head_page_id: head_id,
        len: AtomicUsize::new(len),
        deleted: Arc::new(RwLock::new(DeletionSetInneer::new())),
        marker: PhantomData,
    }
}
<commit_msg>add comments<commit_after>use client::AsyncClient;
use dovahkiin::types::*;
use futures::prelude::*;
use index::btree::external::ExtNode;
use index::btree::external::*;
use index::btree::internal::InNode;
use index::btree::node::{write_node, Node, NodeWriteGuard};
use index::btree::remove::SubNodeStatus::InNodeEmpty;
use index::btree::{external, max_entry_key, BPlusTree, DeletionSetInneer, NodeCellRef};
use index::{EntryKey, Slice};
use parking_lot::RwLock;
use ram::cell::Cell;
use std::cell::RefCell;
use std::cmp::max;
use std::collections::btree_set::BTreeSet;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::mem;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;

pub struct TreeConstructor<KS, PS>
where
    KS: Slice<EntryKey> + Debug + 'static,
    PS: Slice<NodeCellRef> + 'static,
{
    level_guards: Vec<Rc<RefCell<NodeWriteGuard<KS, PS>>>>,
}

impl<KS, PS> TreeConstructor<KS, PS>
where
    KS: Slice<EntryKey> + Debug + 'static,
    PS: Slice<NodeCellRef> + 'static,
{
    pub fn new() -> Self {
        TreeConstructor {
            level_guards: vec![],
        }
    }

    pub fn push_extnode(&mut self, node: &NodeCellRef, first_key: EntryKey) {
        self.push(0, node, first_key);
    }

    fn push(&mut self, level: usize, node: &NodeCellRef, first_key: EntryKey) {
        let mut new_level = false;
        if self.level_guards.len() < level + 1 {
            let mut new_root_innode = InNode::<KS, PS>::new(0, max_entry_key());
            let new_root_ref = NodeCellRef::new(Node::with_internal(new_root_innode));
            self.level_guards
                .push(Rc::new(RefCell::new(write_node::<KS, PS>(&new_root_ref))));
            new_level = true;
        }
        let parent_page_ref = self.level_guards[level].clone();
        let mut parent_guard = parent_page_ref.borrow_mut();
        let cap = KS::slice_len();
        if parent_guard.len() >= cap {
            // current page overflowed, need a new page
            let mut new_innode = InNode::<KS, PS>::new(1, max_entry_key());
            let parent_right_bound = parent_guard.last_key().clone();
            let new_innode_head_ptr = {
                // take a key and a ptr from current page to new page
                // reset current page right bound to the taken key
                // return the taken ptr
                let mut parent_innode = parent_guard.innode_mut();
                parent_innode.len -= 1;
                parent_innode.right_bound = parent_right_bound.clone();
                mem::replace(
                    &mut parent_innode.ptrs.as_slice()[cap],
                    NodeCellRef::default(),
                )
            };
            // arrange a valid new page by putting the ptr from current page at 1st
            new_innode.ptrs.as_slice()[0] = new_innode_head_ptr;
            new_innode.ptrs.as_slice()[1] = node.clone();
            new_innode.keys.as_slice()[0] = first_key;
            let new_node = NodeCellRef::new(Node::with_internal(new_innode));
            *parent_guard = write_node::<KS, PS>(&new_node);
            self.push(level + 1, &new_node, parent_right_bound)
        } else {
            let mut parent_innode = parent_guard.innode_mut();
            let new_len = if new_level {
                0
            } else {
                let len = parent_innode.len;
                parent_innode.keys.as_slice()[len] = first_key;
                len + 1
            };
            parent_innode.ptrs.as_slice()[new_len] = node.clone();
            parent_innode.len = new_len;
        }
    }

    pub fn root(&self) -> NodeCellRef {
        let last_ref = self.level_guards.last().unwrap().clone();
        let last_guard = last_ref.borrow();
        if last_guard.len() == 0 {
            last_guard.innode().ptrs.as_slice_immute()[0].clone()
        } else {
            last_guard.node_ref().clone()
        }
    }
}

pub fn reconstruct_from_head_id<KS, PS>(head_id: Id, neb: &AsyncClient) -> BPlusTree<KS, PS>
where
    KS: Slice<EntryKey> + Debug + 'static,
    PS: Slice<NodeCellRef> + 'static,
{
    let mut len = 0;
    let mut constructor = TreeConstructor::<KS, PS>::new();
    let mut prev_ref = NodeCellRef::new_none::<KS, PS>();
    let mut id = head_id;
    let mut at_end = false;
    while !at_end {
        let cell = neb.read_cell(id).wait().unwrap().unwrap();
        let page = ExtNode::<KS, PS>::from_cell(&cell);
        let next_id = page.next_id;
        let prev_id = page.prev_id;
        let mut node = page.node;
        id = next_id;
        at_end = id.is_unit_id();
        if at_end {
            node.next = NodeCellRef::new_none::<KS, PS>();
        }
        let mut prev_lock = write_node::<KS, PS>(&prev_ref);
        if node.len == 0 {
            // skip this empty node and make it deleted
            external::make_deleted(&node.id);
            if at_end {
                // if the empty node is the last node, assign the right none node to previous node
                *prev_lock.right_ref_mut().unwrap() = node.next.clone();
            }
            continue;
        }
        let first_key = node.keys.as_slice_immute()[0].clone();
        len += node.len;
        node.prev = prev_ref.clone();
        let node_ref = NodeCellRef::new(Node::with_external(box node));
        if !prev_lock.is_none() {
            *prev_lock.right_bound_mut() = first_key.clone();
            *prev_lock.right_ref_mut().unwrap() = node_ref.clone();
        } else {
            assert_eq!(prev_id, Id::unit_id());
        }
        constructor.push_extnode(&node_ref, first_key);
        prev_ref = node_ref;
    }
    let root = constructor.root();
    BPlusTree {
        root: RwLock::new(root),
        root_versioning: NodeCellRef::new(Node::<KS, PS>::with_none()),
        head_page_id: head_id,
        len: AtomicUsize::new(len),
        deleted: Arc::new(RwLock::new(DeletionSetInneer::new())),
        marker: PhantomData,
    }
}
<|endoftext|>


<commit_before>s_no_extra_traits! {
    #[allow(missing_debug_implementations)]
    #[repr(align(16))]
    pub struct max_align_t {
        priv_: [f64; 4]
    }
}
<commit_msg>Auto merge of #2993 - zhaixiaojuan:LA-add-ucontext-clone_args, r=JohnTitor<commit_after>s_no_extra_traits! {
    #[allow(missing_debug_implementations)]
    #[repr(align(16))]
    pub struct max_align_t {
        priv_: [f64; 4]
    }
}

s! {
    pub struct ucontext_t {
        pub uc_flags: ::c_ulong,
        pub uc_link: *mut ucontext_t,
        pub uc_stack: ::stack_t,
        pub uc_sigmask: ::sigset_t,
        pub uc_mcontext: mcontext_t,
    }

    #[repr(align(16))]
    pub struct mcontext_t {
        pub sc_pc: ::c_ulonglong,
        pub sc_regs: [::c_ulonglong; 32],
        pub sc_flags: ::c_ulong,
        pub sc_extcontext: [u64; 0],
    }

    #[repr(align(8))]
    pub struct clone_args {
        pub flags: ::c_ulonglong,
        pub pidfd: ::c_ulonglong,
        pub child_tid: ::c_ulonglong,
        pub parent_tid: ::c_ulonglong,
        pub exit_signal: ::c_ulonglong,
        pub stack: ::c_ulonglong,
        pub stack_size: ::c_ulonglong,
        pub tls: ::c_ulonglong,
        pub set_tid: ::c_ulonglong,
        pub set_tid_size: ::c_ulonglong,
        pub cgroup: ::c_ulonglong,
    }
}
<|endoftext|>


<commit_before>//! A Netherrack buffer backed by a VecDeque\<u8\>

use std::collections::VecDeque;

extern crate varint;

extern crate bit_utils;
use self::bit_utils::BitInformation;

/// A buffer that is backed by a VecDeque\<u8\>
pub struct DequeBuffer {

    pub data: VecDeque<u8>

}

impl DequeBuffer {

    /// Creates a DequeBuffer from a VecDeque
    ///
    /// After this method, we own the VecDeque
    pub fn from(deque: VecDeque<u8>) -> DequeBuffer {
        DequeBuffer { data: deque }
    }

    /// Creates a new empty DequeBuffer
    pub fn new() -> DequeBuffer {
        DequeBuffer { data: VecDeque::<u8>::new() }
    }

    /// Creates a new empty DequeBuffer with the specified initial capacity
    pub fn with_capacity(capacity: usize) -> DequeBuffer {
        DequeBuffer { data: VecDeque::<u8>::with_capacity(capacity) }
    }

    /// Gets the remaining bytes in this DequeBuffer
    pub fn remaining(&self) -> usize {
        self.data.len()
    }

    /// Reads a UTF-8 string from the buffer
    pub fn read_utf8_string(&mut self) -> String {
        let size_result = self.read_unsigned_varint_32();

        if size_result.is_err() {
            return String::from("!!!BAD_SIZE!!!");
        } else {
            let mut size: u32 = size_result.unwrap();

            let mut bytes: Vec<u8> = Vec::<u8>::with_capacity(size as usize);

            while size > 0 {
                bytes.push(self.read_unsigned_byte());
                size -= 1;
            }

            let result = String::from_utf8(bytes);

            if result.is_ok() {
                return result.unwrap();
            } else {
                return String::from("!!!INVALID_UTF8!!!");
            }

        }
    }

    /// Writes a UTF-8 string into the buffer
    pub fn write_utf8_string(&mut self, value: String) {

        let mut bytes = value.as_bytes();

        self.write_unsigned_varint_32(bytes.len() as u32);

        info!("Length is {}", bytes.len() as u32);

        for &byte in bytes {
            self.write_unsigned_byte(byte);
        }

    }

    /// Reads an unsigned byte from the buffer, returning 0 if no data is available
    pub fn read_unsigned_byte(&mut self) -> u8 {
        match self.data.pop_front() {
            Some(value) => value,
            None => 0
        }
    }

    /// Reads an unsigned byte from the buffer, returning -127 if no data is available
    pub fn read_signed_byte(&mut self) -> i8 {
        match self.data.pop_front() {
            Some(value) => value as i8,
            None => -127
        }
    }

    /// Reads an unsigned short from the buffer
    pub fn read_unsigned_short(&mut self) -> u16 {

        let high: u8 = self.read_unsigned_byte();
        let low: u8 = self.read_unsigned_byte();

        ((high as u16) << 8) | low as u16

    }

    /// Writes an unsigned byte to the buffer
    pub fn write_unsigned_byte(&mut self, value: u8) {
        self.data.push_back(value);
    }

    /// Writes a signed byte to the buffer
    pub fn write_signed_byte(&mut self, value: i8) {
        self.data.push_back(value as u8);
    }

    /// Writes a signed varint 32 to the buffer
    pub fn write_signed_varint_32(&mut self, value: i32) {

        self.write_unsigned_varint_32(self::varint::zigzag_signed_int(value));

    }

    /// Writes an unsigned 32-bit Varint from the buffer
    pub fn write_unsigned_varint_32(&mut self, value: u32) {

        let mut _value: u32 = value;

        if value == 0 {
            self.write_unsigned_byte(0);
        } else {

            while _value >= 0b10000000 {

                debug!("Value is {}, writing {}", (_value & 0xff) as u8, _value as u8);

                let next_byte: u8 = ((_value & 0b01111111) as u8) | 0b10000000;

                _value = _value >> 7;

                self.write_unsigned_byte(next_byte);

            }

            debug!("Writing last byte of value: {} is {}", _value, _value & 0b01111111);
            self.write_unsigned_byte((_value & 0b01111111) as u8);

        }

    }

    /// Reads an unsigned Varint from the buffer
    pub fn read_unsigned_varint_32(&mut self) -> Result<u32, &'static str> {
        if self.remaining() == 0 {
            return Err("Cannot read a Varint with no data to read.");
        } else {
            // The number of bits to shift by. <<0, <<7, <<14, etc
            let mut shift_amount: u32 = 0;

            // The decoded value
            let mut decoded_value: u32 = 0;

            while self.remaining() >= 1 {

                let byte_value: u8 = self.read_unsigned_byte(); //Yay for this existing!

                decoded_value |= ((byte_value & 0b01111111) as u32) << shift_amount; // We're not shifting for the first byte ;)

                // See if we're still reading bytes
                if byte_value.has_most_signifigant_bit() {
                    shift_amount += 7;
                } else {
                    return Ok(decoded_value);
                }

            }

            return Err("Reached end of buffer while reading a varint32");

        }
    }

}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn new_contains_no_elements() {
        let new = DequeBuffer::new();

        assert_eq!(0, new.remaining());
    }

    #[test]
    fn new_with_capacity_contains_no_elements() {
        let new = DequeBuffer::with_capacity(25000); //WOW, MUCH BIG PACKET

        assert_eq!(0, new.remaining());
    }

    #[test]
    fn unsigned_byte() {
        let mut buffer = DequeBuffer::with_capacity(2);

        buffer.write_unsigned_byte(1);
        buffer.write_unsigned_byte(15);

        assert_eq!(2, buffer.remaining());

        assert_eq!(1, buffer.read_unsigned_byte());
        assert_eq!(15, buffer.read_unsigned_byte());
    }

    #[test]
    fn signed_byte() {
        let mut buffer = DequeBuffer::with_capacity(2);

        buffer.write_signed_byte(-50);
        buffer.write_signed_byte(0);

        assert_eq!(2, buffer.remaining());

        assert_eq!(-50, buffer.read_signed_byte());
        assert_eq!(-0, buffer.read_signed_byte());
    }

    #[test]
    fn unsigned_varint_32() {

        let mut buffer = DequeBuffer::new();

        let var1: u32 = 0;

        let var2: u32 = 4003254234;

        let var3: u32 = 200;

        buffer.write_unsigned_varint_32(var1);
        buffer.write_unsigned_varint_32(var2);
        buffer.write_unsigned_varint_32(var3);

        assert_eq!(8, buffer.remaining());

        // No need to check for okay here, we know it will be. If you're getting it from the client, always check!
        assert_eq!(var1, buffer.read_unsigned_varint_32().unwrap());
        assert_eq!(var2, buffer.read_unsigned_varint_32().unwrap());
        assert_eq!(var3, buffer.read_unsigned_varint_32().unwrap());

    }

}<commit_msg>Use Varint for writing varints. Need to fix the reference encoding in that crate<commit_after>//! A Netherrack buffer backed by a VecDeque\<u8\>

use std::collections::VecDeque;

extern crate varint;
use self::varint::{Varint, encode_unsigned_varint32};

extern crate bit_utils;
use self::bit_utils::BitInformation;

/// A buffer that is backed by a VecDeque\<u8\>
pub struct DequeBuffer {

    pub data: VecDeque<u8>

}

impl DequeBuffer {

    /// Creates a DequeBuffer from a VecDeque
    ///
    /// After this method, we own the VecDeque
    pub fn from(deque: VecDeque<u8>) -> DequeBuffer {
        DequeBuffer { data: deque }
    }

    /// Creates a new empty DequeBuffer
    pub fn new() -> DequeBuffer {
        DequeBuffer { data: VecDeque::<u8>::new() }
    }

    /// Creates a new empty DequeBuffer with the specified initial capacity
    pub fn with_capacity(capacity: usize) -> DequeBuffer {
        DequeBuffer { data: VecDeque::<u8>::with_capacity(capacity) }
    }

    /// Gets the remaining bytes in this DequeBuffer
    pub fn remaining(&self) -> usize {
        self.data.len()
    }

    /// Reads a UTF-8 string from the buffer
    pub fn read_utf8_string(&mut self) -> String {
        let size_result = self.read_unsigned_varint_32();

        if size_result.is_err() {
            return String::from("!!!BAD_SIZE!!!");
        } else {
            let mut size: u32 = size_result.unwrap();

            let mut bytes: Vec<u8> = Vec::<u8>::with_capacity(size as usize);

            while size > 0 {
                bytes.push(self.read_unsigned_byte());
                size -= 1;
            }

            let result = String::from_utf8(bytes);

            if result.is_ok() {
                return result.unwrap();
            } else {
                return String::from("!!!INVALID_UTF8!!!");
            }

        }
    }

    /// Writes a UTF-8 string into the buffer
    pub fn write_utf8_string(&mut self, value: String) {

        let mut bytes = value.as_bytes();

        self.write_unsigned_varint_32(bytes.len() as u32);

        info!("Length is {}", bytes.len() as u32);

        for &byte in bytes {
            self.write_unsigned_byte(byte);
        }

    }

    /// Reads an unsigned byte from the buffer, returning 0 if no data is available
    pub fn read_unsigned_byte(&mut self) -> u8 {
        match self.data.pop_front() {
            Some(value) => value,
            None => 0
        }
    }

    /// Reads an unsigned byte from the buffer, returning -127 if no data is available
    pub fn read_signed_byte(&mut self) -> i8 {
        match self.data.pop_front() {
            Some(value) => value as i8,
            None => -127
        }
    }

    /// Reads an unsigned short from the buffer
    pub fn read_unsigned_short(&mut self) -> u16 {

        let high: u8 = self.read_unsigned_byte();
        let low: u8 = self.read_unsigned_byte();

        ((high as u16) << 8) | low as u16

    }

    /// Writes an unsigned byte to the buffer
    pub fn write_unsigned_byte(&mut self, value: u8) {
        self.data.push_back(value);
    }

    /// Writes a signed byte to the buffer
    pub fn write_signed_byte(&mut self, value: i8) {
        self.data.push_back(value as u8);
    }

    /// Writes a signed varint 32 to the buffer
    pub fn write_signed_varint_32(&mut self, value: i32) {

        self.write_unsigned_varint_32(self::varint::zigzag_signed_int(value));

    }

    /// Writes an unsigned 32-bit Varint from the buffer
    pub fn write_unsigned_varint_32(&mut self, value: u32) {

        let mut varint = encode_unsigned_varint32(150);

        info!("Varint has {} bytes", varint.number_of_bytes());

        info!("Was {:?}", varint.data);

        //TODO: Once stable, do: self.data.append(&mut varint.data);

        while (varint.data.len() > 0) {

            match varint.data.pop_front() {
                Some(value) => {
                    self.data.push_back(value);
                }
                None => {
                    error!("No data left in Varint's buffer");
                }
            }

        }

        info!("Is now {:?}", self.data);

        /*if value == 0 {
            self.write_unsigned_byte(0);
        } else {

            while _value >= 0b10000000 {

                debug!("Value is {}, writing {}", (_value & 0xff) as u8, _value as u8);

                let next_byte: u8 = ((_value & 0b01111111) as u8) | 0b10000000;

                _value = _value >> 7;

                self.write_unsigned_byte(next_byte);

            }

            debug!("Writing last byte of value: {} is {}", _value, _value & 0b01111111);
            self.write_unsigned_byte((_value & 0b01111111) as u8);

        }*/

    }

    /// Reads an unsigned Varint from the buffer
    pub fn read_unsigned_varint_32(&mut self) -> Result<u32, &'static str> {
        if self.remaining() == 0 {
            return Err("Cannot read a Varint with no data to read.");
        } else {
            // The number of bits to shift by. <<0, <<7, <<14, etc
            let mut shift_amount: u32 = 0;

            // The decoded value
            let mut decoded_value: u32 = 0;

            while self.remaining() >= 1 {

                let byte_value: u8 = self.read_unsigned_byte(); //Yay for this existing!

                decoded_value |= ((byte_value & 0b01111111) as u32) << shift_amount; // We're not shifting for the first byte ;)

                // See if we're still reading bytes
                if byte_value.has_most_signifigant_bit() {
                    shift_amount += 7;
                } else {
                    return Ok(decoded_value);
                }

            }

            return Err("Reached end of buffer while reading a varint32");

        }
    }

}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn new_contains_no_elements() {
        let new = DequeBuffer::new();

        assert_eq!(0, new.remaining());
    }

    #[test]
    fn new_with_capacity_contains_no_elements() {
        let new = DequeBuffer::with_capacity(25000); //WOW, MUCH BIG PACKET

        assert_eq!(0, new.remaining());
    }

    #[test]
    fn unsigned_byte() {
        let mut buffer = DequeBuffer::with_capacity(2);

        buffer.write_unsigned_byte(1);
        buffer.write_unsigned_byte(15);

        assert_eq!(2, buffer.remaining());

        assert_eq!(1, buffer.read_unsigned_byte());
        assert_eq!(15, buffer.read_unsigned_byte());
    }

    #[test]
    fn signed_byte() {
        let mut buffer = DequeBuffer::with_capacity(2);

        buffer.write_signed_byte(-50);
        buffer.write_signed_byte(0);

        assert_eq!(2, buffer.remaining());

        assert_eq!(-50, buffer.read_signed_byte());
        assert_eq!(-0, buffer.read_signed_byte());
    }

    #[test]
    fn unsigned_varint_32() {

        let mut buffer = DequeBuffer::new();

        let var1: u32 = 0;

        let var2: u32 = 4003254234;

        let var3: u32 = 200;

        buffer.write_unsigned_varint_32(var1);
        buffer.write_unsigned_varint_32(var2);
        buffer.write_unsigned_varint_32(var3);

        assert_eq!(8, buffer.remaining());

        // No need to check for okay here, we know it will be. If you're getting it from the client, always check!
        assert_eq!(var1, buffer.read_unsigned_varint_32().unwrap());
        assert_eq!(var2, buffer.read_unsigned_varint_32().unwrap());
        assert_eq!(var3, buffer.read_unsigned_varint_32().unwrap());

    }

}<|endoftext|>


<commit_before>use std::str::raw;
use std::os::getenv;
use std::path::Path;
use std::libc::{c_int, c_char};
use sync::mutex::{StaticMutex, MUTEX_INIT};

static mut LOCK: StaticMutex = MUTEX_INIT;

static mut registered_drivers: bool = false;

struct Dataset {
    c_dataset: *(),
}

struct Driver {
    c_driver: *(),
}

#[link(name = "gdal")]
extern {
    fn GDALVersionInfo(key: *c_char) -> *c_char;
    fn GDALOpen(pszFilename: *c_char, eAccess: c_int) -> *();
    fn GDALClose(hDS: *());
    fn GDALGetDatasetDriver(hDataset: *()) -> *();
    fn GDALGetRasterXSize(hDataset: *()) -> c_int;
    fn GDALGetRasterYSize(hDataset: *()) -> c_int;
    fn GDALGetRasterCount(hDataset: *()) -> c_int;
    fn GDALGetProjectionRef(hDS: *()) -> *c_char;
    fn GDALSetProjection(hDS: *(), pszProjection: *c_char) -> c_int;
    fn GDALAllRegister();
    fn GDALGetDriverShortName(hDriver: *()) -> *c_char;
    fn GDALGetDriverLongName(hDriver: *()) -> *c_char;
}
static GA_ReadOnly: c_int = 0;
static GA_Update: c_int = 1;

pub fn version_info(key: &str) -> ~str {
    let info = key.with_c_str(|c_key| {
        unsafe {
            let _g = LOCK.lock();
            return raw::from_c_str(GDALVersionInfo(c_key));
        };
    });
    return info;
}

pub fn open(path: &Path) -> Option<Dataset> {
    unsafe {
        let _g = LOCK.lock();
        if ! registered_drivers {
            GDALAllRegister();
            registered_drivers = true;
        }
    }
    let filename = path.as_str().unwrap();
    let c_dataset = filename.with_c_str(|c_filename| {
        unsafe {
            let _g = LOCK.lock();
            return GDALOpen(c_filename, GA_ReadOnly);
        };
    });
    return match c_dataset.is_null() {
        true  => None,
        false => Some(Dataset{c_dataset: c_dataset}),
    };
}

impl Drop for Dataset {
    fn drop(&mut self) {
        unsafe {
            let _g = LOCK.lock();
            GDALClose(self.c_dataset);
        }
    }
}

impl Dataset {
    pub fn get_raster_size(&self) -> (int, int) {
        unsafe {
            let _g = LOCK.lock();
            let size_x = GDALGetRasterXSize(self.c_dataset) as int;
            let size_y = GDALGetRasterYSize(self.c_dataset) as int;
            return (size_x, size_y);
        }
    }

    pub fn get_driver(&self) -> Driver {
        let mut c_driver;
        unsafe {
            let _g = LOCK.lock();
            c_driver = GDALGetDatasetDriver(self.c_dataset);
        };
        return Driver{c_driver: c_driver};
    }

    pub fn get_raster_count(&self) -> int {
        unsafe {
            let _g = LOCK.lock();
            return GDALGetRasterCount(self.c_dataset) as int;
        }
    }

    pub fn get_projection(&self) -> ~str {
        unsafe {
            let _g = LOCK.lock();
            return raw::from_c_str(GDALGetProjectionRef(self.c_dataset));
        }
    }

    pub fn set_projection(&self, projection: &str) {
        projection.with_c_str(|c_projection| {
            unsafe {
                let _g = LOCK.lock();
                GDALSetProjection(self.c_dataset, c_projection);
            }
        });
    }
}

impl Driver {
    pub fn get_short_name(&self) -> ~str {
        unsafe {
            let _g = LOCK.lock();
            return raw::from_c_str(GDALGetDriverShortName(self.c_driver));
        }
    }

    pub fn get_long_name(&self) -> ~str {
        unsafe {
            let _g = LOCK.lock();
            return raw::from_c_str(GDALGetDriverLongName(self.c_driver));
        }
    }
}

#[test]
fn test_version_info() {
    let release_date = version_info("RELEASE_DATE");
    let release_name = version_info("RELEASE_NAME");
    let version_text = version_info("--version");

    let expected_text: ~str = "GDAL " + release_name + ", " +
        "released " + release_date.slice(0, 4) + "/" +
        release_date.slice(4, 6) + "/" + release_date.slice(6, 8);

    assert_eq!(version_text.into_owned(), expected_text);
}

fn fixture_path(name: &str) -> Path {
    let envvar = "RUSTILES_TEST_FIXTURES";
    let fixtures = match getenv(envvar) {
        Some(p) => Path::new(p),
        None => fail!("Environment variable {} not set", envvar)
    };
    let rv = fixtures.join(name);
    return rv;
}

#[test]
fn test_open() {
    let dataset = open(&fixture_path("tinymarble.jpeg"));
    assert!(dataset.is_some());

    let missing_dataset = open(&fixture_path("no_such_file.jpeg"));
    assert!(missing_dataset.is_none());
}

#[test]
fn test_get_raster_size() {
    let dataset = open(&fixture_path("tinymarble.jpeg")).unwrap();
    let (size_x, size_y) = dataset.get_raster_size();
    assert_eq!(size_x, 100);
    assert_eq!(size_y, 50);
}

#[test]
fn test_get_raster_count() {
    let dataset = open(&fixture_path("tinymarble.jpeg")).unwrap();
    let count = dataset.get_raster_count();
    assert_eq!(count, 3);
}

#[test]
fn test_get_projection() {
    let dataset = open(&fixture_path("tinymarble.jpeg")).unwrap();
    //dataset.set_projection("WGS84");
    let projection = dataset.get_projection();
    assert_eq!(projection.slice(0, 16), "GEOGCS[\"WGS 84\",");
}

#[test]
fn test_get_dataset_driver() {
    let dataset = open(&fixture_path("tinymarble.jpeg")).unwrap();
    let driver = dataset.get_driver();
    assert_eq!(driver.get_short_name(), ~"JPEG");
    assert_eq!(driver.get_long_name(), ~"JPEG JFIF");
}
<commit_msg>get driver by name<commit_after>use std::str::raw;
use std::os::getenv;
use std::path::Path;
use std::libc::{c_int, c_char};
use sync::mutex::{StaticMutex, MUTEX_INIT};

static mut LOCK: StaticMutex = MUTEX_INIT;

static mut registered_drivers: bool = false;

struct Dataset {
    c_dataset: *(),
}

struct Driver {
    c_driver: *(),
}

#[link(name = "gdal")]
extern {
    fn GDALVersionInfo(key: *c_char) -> *c_char;
    fn GDALOpen(pszFilename: *c_char, eAccess: c_int) -> *();
    fn GDALClose(hDS: *());
    fn GDALGetDatasetDriver(hDataset: *()) -> *();
    fn GDALGetRasterXSize(hDataset: *()) -> c_int;
    fn GDALGetRasterYSize(hDataset: *()) -> c_int;
    fn GDALGetRasterCount(hDataset: *()) -> c_int;
    fn GDALGetProjectionRef(hDS: *()) -> *c_char;
    fn GDALSetProjection(hDS: *(), pszProjection: *c_char) -> c_int;
    fn GDALAllRegister();
    fn GDALGetDriverByName(pszName: *c_char) -> *();
    fn GDALGetDriverShortName(hDriver: *()) -> *c_char;
    fn GDALGetDriverLongName(hDriver: *()) -> *c_char;
}
static GA_ReadOnly: c_int = 0;
static GA_Update: c_int = 1;

pub fn version_info(key: &str) -> ~str {
    let info = key.with_c_str(|c_key| {
        unsafe {
            let _g = LOCK.lock();
            return raw::from_c_str(GDALVersionInfo(c_key));
        };
    });
    return info;
}

pub fn open(path: &Path) -> Option<Dataset> {
    unsafe {
        let _g = LOCK.lock();
        if ! registered_drivers {
            GDALAllRegister();
            registered_drivers = true;
        }
    }
    let filename = path.as_str().unwrap();
    let c_dataset = filename.with_c_str(|c_filename| {
        unsafe {
            let _g = LOCK.lock();
            return GDALOpen(c_filename, GA_ReadOnly);
        };
    });
    return match c_dataset.is_null() {
        true  => None,
        false => Some(Dataset{c_dataset: c_dataset}),
    };
}

impl Drop for Dataset {
    fn drop(&mut self) {
        unsafe {
            let _g = LOCK.lock();
            GDALClose(self.c_dataset);
        }
    }
}

impl Dataset {
    pub fn get_raster_size(&self) -> (int, int) {
        unsafe {
            let _g = LOCK.lock();
            let size_x = GDALGetRasterXSize(self.c_dataset) as int;
            let size_y = GDALGetRasterYSize(self.c_dataset) as int;
            return (size_x, size_y);
        }
    }

    pub fn get_driver(&self) -> Driver {
        let mut c_driver;
        unsafe {
            let _g = LOCK.lock();
            c_driver = GDALGetDatasetDriver(self.c_dataset);
        };
        return Driver{c_driver: c_driver};
    }

    pub fn get_raster_count(&self) -> int {
        unsafe {
            let _g = LOCK.lock();
            return GDALGetRasterCount(self.c_dataset) as int;
        }
    }

    pub fn get_projection(&self) -> ~str {
        unsafe {
            let _g = LOCK.lock();
            return raw::from_c_str(GDALGetProjectionRef(self.c_dataset));
        }
    }

    pub fn set_projection(&self, projection: &str) {
        projection.with_c_str(|c_projection| {
            unsafe {
                let _g = LOCK.lock();
                GDALSetProjection(self.c_dataset, c_projection);
            }
        });
    }
}

pub fn get_driver(name: &str) -> Option<Driver> {
    let c_driver = name.with_c_str(|c_name| {
        unsafe {
            let _g = LOCK.lock();
            return GDALGetDriverByName(c_name);
        }
    });
    return match c_driver.is_null() {
        true  => None,
        false => Some(Driver{c_driver: c_driver}),
    };
}

impl Driver {
    pub fn get_short_name(&self) -> ~str {
        unsafe {
            let _g = LOCK.lock();
            return raw::from_c_str(GDALGetDriverShortName(self.c_driver));
        }
    }

    pub fn get_long_name(&self) -> ~str {
        unsafe {
            let _g = LOCK.lock();
            return raw::from_c_str(GDALGetDriverLongName(self.c_driver));
        }
    }
}

#[test]
fn test_version_info() {
    let release_date = version_info("RELEASE_DATE");
    let release_name = version_info("RELEASE_NAME");
    let version_text = version_info("--version");

    let expected_text: ~str = "GDAL " + release_name + ", " +
        "released " + release_date.slice(0, 4) + "/" +
        release_date.slice(4, 6) + "/" + release_date.slice(6, 8);

    assert_eq!(version_text.into_owned(), expected_text);
}

fn fixture_path(name: &str) -> Path {
    let envvar = "RUSTILES_TEST_FIXTURES";
    let fixtures = match getenv(envvar) {
        Some(p) => Path::new(p),
        None => fail!("Environment variable {} not set", envvar)
    };
    let rv = fixtures.join(name);
    return rv;
}

#[test]
fn test_open() {
    let dataset = open(&fixture_path("tinymarble.jpeg"));
    assert!(dataset.is_some());

    let missing_dataset = open(&fixture_path("no_such_file.jpeg"));
    assert!(missing_dataset.is_none());
}

#[test]
fn test_get_raster_size() {
    let dataset = open(&fixture_path("tinymarble.jpeg")).unwrap();
    let (size_x, size_y) = dataset.get_raster_size();
    assert_eq!(size_x, 100);
    assert_eq!(size_y, 50);
}

#[test]
fn test_get_raster_count() {
    let dataset = open(&fixture_path("tinymarble.jpeg")).unwrap();
    let count = dataset.get_raster_count();
    assert_eq!(count, 3);
}

#[test]
fn test_get_projection() {
    let dataset = open(&fixture_path("tinymarble.jpeg")).unwrap();
    //dataset.set_projection("WGS84");
    let projection = dataset.get_projection();
    assert_eq!(projection.slice(0, 16), "GEOGCS[\"WGS 84\",");
}

#[test]
fn test_get_dataset_driver() {
    let dataset = open(&fixture_path("tinymarble.jpeg")).unwrap();
    let driver = dataset.get_driver();
    assert_eq!(driver.get_short_name(), ~"JPEG");
    assert_eq!(driver.get_long_name(), ~"JPEG JFIF");
}

#[test]
fn test_get_driver_by_name() {
    let missing_driver = get_driver("wtf");
    assert!(missing_driver.is_none());

    let ok_driver = get_driver("GTiff");
    assert!(ok_driver.is_some());
    let driver = ok_driver.unwrap();
    assert_eq!(driver.get_short_name(), ~"GTiff");
    assert_eq!(driver.get_long_name(), ~"GeoTIFF");
}
<|endoftext|>


<commit_before>extern crate futures;
extern crate hyper;
extern crate log;
extern crate serde;
extern crate tokio_core;

use super::config;
use futures::{Future, Stream};
use hyper::{Client, Method, Request, Uri};
use hyper::header::{ContentLength, ContentType};
use std::io::{Error, ErrorKind};
use std::str;
use tokio_core::reactor::Core;

pub struct Response {
    pub status: u16,
    pub body: String,
}

pub fn request(path: &str, request_body: Option<String>) -> Result<Response, Error> {
    let mut request = Request::new(Method::Post, uri(path));
    match request_body {
        Some(json) => {
            debug!("Request body: {}", json);
            request.headers_mut().set(ContentType::json());
            request.headers_mut().set(ContentLength(json.len() as u64));
            request.set_body(json);
        }
        _ => {
            debug!("Empty request body...");
        }
    }

    let mut status: u16 = 0;
    let mut body = String::new();
    {
        let mut core = Core::new().unwrap();
        let client = Client::new(&core.handle());
        let work = client
            .request(request)
            .and_then(|res| {
                status = res.status().clone().into();
                res.body().for_each(|chunk| {
                    body.push_str(str::from_utf8(&*chunk).unwrap());
                    futures::future::ok(())
                })
            })
            .map_err(|err| Error::new(ErrorKind::Other, err));
        try!(core.run(work));
    }

    debug!("Response status: {}", status);
    debug!("Response body: {}", body);
    Ok(Response { status, body })
}

fn uri(path: &str) -> Uri {
    let addr = config::base_url().clone();
    let uri = format!("{}{}", addr, path).parse().unwrap();
    debug!("Request URL: {}", uri);
    uri
}
<commit_msg>cleanup http construct<commit_after>extern crate futures;
extern crate hyper;
extern crate log;
extern crate serde;
extern crate tokio_core;

use super::config;
use futures::{Future, Stream};
use hyper::{Client, Method, Request, Uri};
use hyper::header::{ContentLength, ContentType};
use std::{mem, str};
use std::io::{Error, ErrorKind};
use tokio_core::reactor::Core;

pub struct Response {
    pub status: u16,
    pub body: String,
}

pub fn request(path: &str, request_body: Option<String>) -> Result<Response, Error> {
    let mut request = Request::new(Method::Post, uri(path));
    match request_body {
        Some(json) => {
            debug!("Request body: {}", json);
            request.headers_mut().set(ContentType::json());
            request.headers_mut().set(ContentLength(json.len() as u64));
            request.set_body(json);
        }
        _ => {
            debug!("Empty request body...");
        }
    }

    let mut status: u16 = 0;
    let mut body = String::new();
    {
        let mut core = Core::new().unwrap();
        let client = Client::new(&core.handle());
        let work = client
            .request(request)
            .and_then(|res| {
                status = res.status().clone().into();
                res.body().for_each(|chunk| {
                    body.push_str(str::from_utf8(&*chunk).unwrap());
                    futures::future::ok(())
                })
            })
            .map_err(|err| Error::new(ErrorKind::Other, err));
        try!(core.run(work));

        // explicit destroy http fuss
        mem::drop(client);
        mem::drop(core);
    }

    debug!("Response status: {}", status);
    debug!("Response body: {}", body);
    Ok(Response { status, body })
}

fn uri(path: &str) -> Uri {
    let addr = config::base_url().clone();
    let uri = format!("{}{}", addr, path).parse().unwrap();
    debug!("Request URL: {}", uri);
    uri
}
<|endoftext|>