Skip to content

Commit

Permalink
Merge pull request #258 from chef/repos
Browse files Browse the repository at this point in the history
Merged change c4cc03c4-a0fa-4bca-a3a9-deb7fbb1c2de

From review branch repos into master

Signed-off-by: adam <[email protected]>
  • Loading branch information
chef-delivery committed Mar 8, 2016
2 parents 0846e31 + 11d769e commit cab0b11
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/bldr/command/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use std::fs;

use fs::PACKAGE_CACHE;
use error::BldrResult;
use package::{Package, PackageIdent};
use package::PackageIdent;
use repo::{self, data_object};

static LOGKEY: &'static str = "CI";
Expand Down
3 changes: 2 additions & 1 deletion src/bldr/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl PackageIdent {
self.version.is_some() && self.release.is_some()
}

pub fn satisfies(&self, other: &Self) -> bool {
pub fn satisfies<T: AsRef<Self>>(&self, ident: T) -> bool {
let other = ident.as_ref();
if self.origin != other.origin || self.name != other.name {
return false;
}
Expand Down
23 changes: 5 additions & 18 deletions src/bldr/repo/data_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// this file ("Licensee") apply to Licensee's use of the Software until such time that the Software
// is made available under an open source license such as the Apache 2.0 License.

use std::collections::HashSet;
use std::fmt;

use rustc_serialize::{Encoder, Decoder, Encodable, Decodable};
Expand All @@ -30,6 +29,10 @@ impl PackageIdent {
PackageIdent(ident, string_id)
}

pub fn fully_qualified(&self) -> bool {
self.0.fully_qualified()
}

pub fn len(&self) -> u8 {
let mut len = 2;
if self.0.version.is_some() {
Expand Down Expand Up @@ -135,20 +138,11 @@ impl From<package::PackageIdent> for PackageIdent {
#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)]
pub struct View {
pub ident: String,
pub packages: HashSet<<Package as DataObject>::Key>,
}

impl View {
pub fn new(name: &str) -> Self {
View {
ident: String::from(name),
packages: HashSet::new(),
}
}

pub fn add_package(&mut self, package: <Package as DataObject>::Key) -> &mut Self {
self.packages.insert(package);
self
View { ident: String::from(name) }
}
}

Expand All @@ -175,7 +169,6 @@ pub struct Package {
pub tdeps: Vec<PackageIdent>,
pub exposes: Vec<u16>,
pub config: Option<String>,
pub views: HashSet<<View as DataObject>::Key>,
}

impl Package {
Expand All @@ -196,14 +189,8 @@ impl Package {
tdeps: try!(archive.tdeps()).into_iter().map(|d| d.into()).collect(),
exposes: try!(archive.exposes()),
config: try!(archive.config()),
views: HashSet::new(),
})
}

pub fn add_view(&mut self, view: <View as DataObject>::Key) -> &mut Self {
self.views.insert(view);
self
}
}

impl Into<package::Package> for Package {
Expand Down
168 changes: 158 additions & 10 deletions src/bldr/repo/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use rustc_serialize::{Encodable, Decodable};

use error::{BldrResult, ErrorKind};
use super::data_object::{self, DataObject};
use package::PackageIdent;

static LOGKEY: &'static str = "DS";

Expand Down Expand Up @@ -86,10 +85,14 @@ pub const PACKAGE_DB: &'static str = "packages";
pub const PACKAGE_INDEX: &'static str = "package-index";
/// Name of the views database
pub const VIEW_DB: &'static str = "views";
/// Name of the package to views index database
pub const PACKAGE_VIEW_INDEX: &'static str = "package-view-index";
/// Name of the view to packages index database
pub const VIEW_PACKAGE_INDEX: &'static str = "view-package-index";
/// Value for how many databases can be opened within a DataStore's environment. Increase this
/// count for each new database added and decrease this count if databases are removed from the
/// DataStore.
pub const MAX_DBS: u32 = 3;
pub const MAX_DBS: u32 = 5;

macro_rules! try_mdb {
($e: expr) => (
Expand Down Expand Up @@ -370,8 +373,8 @@ impl DataStore {
let env1 = Arc::new(env);
let env2 = env1.clone();
let env3 = env1.clone();
let pkg_database = try!(PkgDatabase::new().create(env2));
let view_database = try!(ViewDatabase::new().create(env3));
let pkg_database = try!(PkgDatabase::new().create(env2));
Ok(DataStore {
env: env1,
packages: pkg_database,
Expand Down Expand Up @@ -604,6 +607,9 @@ pub trait Cursor<'a, 'd, D: 'a + 'd + Database, T: Transaction<'a, D>> {
/// Returns the cursor's database handle.
fn database(&self) -> lmdb_sys::MDB_dbi;

/// Close an open cursor.
fn close(self) -> ();

/// Return count of duplicates for current key.
///
/// This call is only valid on databases that support sorted duplicate items by the
Expand Down Expand Up @@ -700,6 +706,30 @@ pub trait Cursor<'a, 'd, D: 'a + 'd + Database, T: Transaction<'a, D>> {
}
}

fn prev(&mut self) -> BldrResult<(&'a <D::Object as DataObject>::Key, D::Object)> {
assert_txn_state_eq!(self.state(), TxnState::Normal);
match cursor_get::<<D::Object as DataObject>::Key, D::Object>(self.handle(),
None,
None,
CursorOp::Prev) {
Ok((Some(key), value)) => Ok((key, value)),
Ok(_) => unreachable!(),
Err(e) => Err(e),
}
}

fn prev_dup(&mut self) -> BldrResult<(&'a <D::Object as DataObject>::Key, D::Object)> {
assert_txn_state_eq!(self.state(), TxnState::Normal);
match cursor_get::<<D::Object as DataObject>::Key, D::Object>(self.handle(),
None,
None,
CursorOp::PrevDup) {
Ok((Some(key), value)) => Ok((key, value)),
Ok(_) => unreachable!(),
Err(e) => Err(e),
}
}

/// Position the cursor at the specified key and return the key and data.
fn set_key(&mut self,
key: &<D::Object as DataObject>::Key)
Expand Down Expand Up @@ -745,6 +775,10 @@ impl<'a, 'b, D, T> Cursor<'a, 'b, D, T> for RoCursor<'a, D, T>
where D: 'a + 'b + Database,
T: 'a + Transaction<'a, D>
{
fn close(self) {
unsafe { lmdb_sys::mdb_cursor_close(self.cursor) }
}

fn handle(&self) -> *mut lmdb_sys::MDB_cursor {
self.cursor
}
Expand Down Expand Up @@ -797,6 +831,10 @@ impl<'a, 'b, D, T> Cursor<'a, 'b, D, T> for RwCursor<'a, D>
where D: 'a + 'b + Database,
T: 'a + Transaction<'a, D>
{
fn close(self) {
unsafe { lmdb_sys::mdb_cursor_close(self.cursor) }
}

fn handle(&self) -> *mut lmdb_sys::MDB_cursor {
self.cursor
}
Expand Down Expand Up @@ -1347,6 +1385,8 @@ impl Drop for PkgIndex {
///
/// This is how packages will be "promoted" between environments without duplicating data on disk.
pub struct ViewDatabase {
pub pkg_view_idx: PkgViewIndex,
pub view_pkg_idx: ViewPkgIndex,
env: Arc<Environment>,
handle: lmdb_sys::MDB_dbi,
}
Expand All @@ -1355,15 +1395,30 @@ impl ViewDatabase {
pub fn new() -> DatabaseBuilder<Self> {
DatabaseBuilder::default()
}

// Associate the given package to the given view
pub fn associate<'a, T: Database>(&self, txn: &RwTransaction<'a, T>, view: &<Self as Database>::Object, pkg: &<PkgDatabase as Database>::Object) -> BldrResult<()> {
let nested = try!(txn.new_child_rw(&self.pkg_view_idx));
try!(nested.put(pkg.ident(), view));
let nested2 = try!(nested.new_child_rw(&self.view_pkg_idx));
try!(nested2.put(view.ident(), &pkg.ident));
Ok(())
}
}

impl Database for ViewDatabase {
type Object = data_object::View;

fn open(env: Arc<Environment>, handle: lmdb_sys::MDB_dbi) -> BldrResult<Self> {
let env2 = env.clone();
let env3 = env.clone();
let pkg_view_idx = try!(PkgViewIndex::new().create(env2));
let view_pkg_idx = try!(ViewPkgIndex::new().create(env3));
Ok(ViewDatabase {
env: env,
handle: handle,
pkg_view_idx: pkg_view_idx,
view_pkg_idx: view_pkg_idx,
})
}

Expand Down Expand Up @@ -1393,10 +1448,107 @@ impl Drop for ViewDatabase {
}
}

pub struct PkgViewIndex {
env: Arc<Environment>,
handle: lmdb_sys::MDB_dbi,
}

impl PkgViewIndex {
pub fn new() -> DatabaseBuilder<Self> {
DatabaseBuilder::default()
}
}

impl Database for PkgViewIndex {
type Object = data_object::View;

fn open(env: Arc<Environment>, handle: lmdb_sys::MDB_dbi) -> BldrResult<Self> {
Ok(PkgViewIndex {
env: env,
handle: handle,
})
}

fn env(&self) -> &Environment {
&self.env
}

fn handle(&self) -> lmdb_sys::MDB_dbi {
self.handle
}
}

impl Default for DatabaseBuilder<PkgViewIndex> {
fn default() -> DatabaseBuilder<PkgViewIndex> {
let mut flags = DatabaseFlags::empty();
flags.toggle(DB_ALLOW_DUPS);
DatabaseBuilder {
name: Some(PACKAGE_VIEW_INDEX.to_string()),
flags: flags,
txn_flags: 0,
_marker: PhantomData,
}
}
}

impl Drop for PkgViewIndex {
fn drop(&mut self) {
unsafe { lmdb_sys::mdb_dbi_close(self.env.handle, self.handle()) }
}
}

pub struct ViewPkgIndex {
env: Arc<Environment>,
handle: lmdb_sys::MDB_dbi,
}

impl ViewPkgIndex {
pub fn new() -> DatabaseBuilder<Self> {
DatabaseBuilder::default()
}
}

impl Database for ViewPkgIndex {
type Object = data_object::PackageIdent;

fn open(env: Arc<Environment>, handle: lmdb_sys::MDB_dbi) -> BldrResult<Self> {
Ok(ViewPkgIndex {
env: env,
handle: handle,
})
}

fn env(&self) -> &Environment {
&self.env
}

fn handle(&self) -> lmdb_sys::MDB_dbi {
self.handle
}
}

impl Default for DatabaseBuilder<ViewPkgIndex> {
fn default() -> DatabaseBuilder<ViewPkgIndex> {
let mut flags = DatabaseFlags::empty();
flags.toggle(DB_ALLOW_DUPS);
DatabaseBuilder {
name: Some(VIEW_PACKAGE_INDEX.to_string()),
flags: flags,
txn_flags: 0,
_marker: PhantomData,
}
}
}

impl Drop for ViewPkgIndex {
fn drop(&mut self) {
unsafe { lmdb_sys::mdb_dbi_close(self.env.handle, self.handle()) }
}
}

#[cfg(test)]
mod tests {
use std::path::Path;
use std::collections::HashSet;
use super::*;
use super::super::data_object::*;
use error::{BldrError, ErrorKind};
Expand All @@ -1420,7 +1572,6 @@ mod tests {
tdeps: vec![],
exposes: vec![],
config: Some("configuration".to_string()),
views: HashSet::new(),
};
let txn = ds.packages.txn_rw().unwrap();
txn.put(&pkg.ident(), &pkg).unwrap();
Expand All @@ -1441,8 +1592,7 @@ mod tests {
fn transaction_read_write() {
let ds = open_datastore();
{
let mut view = View::new("my-view");
view.add_package("my-package".to_string());
let view = View::new("my-view");
let txn = ds.views.txn_rw().unwrap();
txn.put(view.ident(), &view).unwrap();
txn.commit().unwrap();
Expand All @@ -1451,7 +1601,6 @@ mod tests {
let saved = txn.get(&"my-view".to_string()).unwrap();
txn.abort();
assert_eq!(saved.ident(), "my-view");
assert_eq!(saved.packages.len(), 1);
}

// JW TODO: This test is ignored while I track down a bug preventing multiple transactions
Expand All @@ -1461,8 +1610,7 @@ mod tests {
fn transaction_delete() {
let ds = open_datastore();
{
let mut view = View::new("my-view");
view.add_package("my-package".to_string());
let view = View::new("my-view");
let txn = ds.views.txn_rw().unwrap();
txn.put(view.ident(), &view).unwrap();
txn.commit().unwrap();
Expand Down
Loading

0 comments on commit cab0b11

Please sign in to comment.