1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use jfs;

use sda_protocol::{Id, Identified};

use errors::*;


mod agents;
mod aggregations;
mod auth_tokens;
mod clerking_jobs;

pub use self::agents::JfsAgentsStore;
pub use self::auth_tokens::JfsAuthTokensStore;
pub use self::aggregations::JfsAggregationsStore;
pub use self::clerking_jobs::JfsClerkingJobsStore;

trait JfsStoreExt {
    fn get_option_for_str<T, S>(&self, id: S) -> SdaServerResult<Option<T>>
        where T: ::serde::Serialize + ::serde::Deserialize,
              S: AsRef<str>;

    fn get_option<T, I>(&self, id: &I) -> SdaServerResult<Option<T>>
        where T: ::serde::Serialize + ::serde::Deserialize,
              I: Id;

    fn create_with_id<T, I>(&self, it: &T, id: &I) -> SdaServerResult<()>
        where T: ::serde::Serialize + ::serde::Deserialize + PartialEq,
              I: Id;

    fn create<T>(&self, it: &T) -> SdaServerResult<()>
        where T: ::serde::Serialize + ::serde::Deserialize + Identified + PartialEq {
        self.create_with_id(it, it.id())
    }

    fn update_with_id<T, I>(&self, it: &T, id: &I) -> SdaServerResult<()>
        where T: ::serde::Serialize + ::serde::Deserialize + PartialEq,
              I: Id;

    fn update<T>(&self, it: &T) -> SdaServerResult<()>
        where T: ::serde::Serialize + ::serde::Deserialize + Identified + PartialEq {
        self.update_with_id(it, it.id())
    }

    fn upsert_with_id<T, I>(&self, it: &T, id: &I) -> SdaServerResult<()>
        where T: ::serde::Serialize + ::serde::Deserialize + PartialEq,
              I: Id;

    fn upsert<T>(&self, it: &T) -> SdaServerResult<()>
        where T: ::serde::Serialize + ::serde::Deserialize + Identified + PartialEq {
        self.upsert_with_id(it, it.id())
    }
}

impl JfsStoreExt for jfs::Store {
    fn get_option_for_str<T, S>(&self, id: S) -> SdaServerResult<Option<T>>
        where T: ::serde::Serialize + ::serde::Deserialize,
              S: AsRef<str>
    {
        match self.get(id.as_ref()) {
            Ok(it) => Ok(Some(it)),
            Err(io) => {
                if io.kind() == ::std::io::ErrorKind::NotFound {
                    Ok(None)
                } else {
                    Err(io)?
                }
            }
        }
    }

    fn get_option<T, I>(&self, id: &I) -> SdaServerResult<Option<T>>
        where T: ::serde::Serialize + ::serde::Deserialize,
              I: Id
    {
        self.get_option_for_str(id.to_string())
    }

    fn create_with_id<T, I>(&self, it: &T, id: &I) -> SdaServerResult<()>
        where T: ::serde::Serialize + ::serde::Deserialize + PartialEq,
              I: Id {
        if let Some(prev) = self.get_option_for_str::<T, _>(&*id.to_string())? {
            if prev != *it {
                Err("File already exists")?
            }
        }
        self.save_with_id(it, &*id.to_string())?;
        Ok(())
    }

    fn update_with_id<T, I>(&self, it: &T, id: &I) -> SdaServerResult<()>
        where T: ::serde::Serialize + ::serde::Deserialize + PartialEq,
              I: Id {
        if self.get_option_for_str::<T, _>(&*id.to_string())?.is_none() {
            Err("File not present")?
        }
        self.save_with_id(it, &*id.to_string())?;
        Ok(())
    }

    fn upsert_with_id<T, I>(&self, it: &T, id: &I) -> SdaServerResult<()>
        where T: ::serde::Serialize + ::serde::Deserialize + PartialEq,
              I: Id {
        self.save_with_id(it, &*id.to_string())?;
        Ok(())
    }
}

#[cfg(test)]
mod test {
    extern crate tempdir;
    use jfs_stores::JfsStoreExt;

    #[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
    struct I(String);
    #[derive(Debug,PartialEq,Clone,Serialize,Deserialize)]
    struct A { id: I, int: usize}

    impl ::sda_protocol::Identified for A {
        type I=I;
        fn id(&self) -> &I {
            &self.id
        }
    }

    impl ::sda_protocol::Id for I { }

    impl ::std::str::FromStr for I {
        type Err = String;
        fn from_str(s:&str) -> ::std::result::Result<I,String> {
            Ok(I(s.to_string()))
        }
    }

    impl ::std::fmt::Display for I {
        fn fmt(&self, f:&mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
            self.0.fmt(f)
        }
    }

    #[test]
    fn create() {
        let tmpdir = tempdir::TempDir::new("sda").unwrap();
        let store = ::jfs::Store::new(&tmpdir.path().to_str().unwrap()).unwrap();
        let a = A { id: I("foo".to_string()), int: 12};
        store.create(&a).unwrap();
        store.create(&a).unwrap();
        let b = A { id: I("foo".to_string()), int: 42};
        assert!(store.create(&b).is_err());
    }

    #[test]
    fn update() {
        let tmpdir = tempdir::TempDir::new("sda").unwrap();
        let store = ::jfs::Store::new(&tmpdir.path().to_str().unwrap()).unwrap();
        let a = A { id: I("foo".to_string()), int: 12};
        store.create(&a).unwrap();
        let a = A { id: I("foo".to_string()), int: 42};
        store.update(&a).unwrap();
        let a_again = store.get_option_for_str("foo").unwrap().unwrap();
        assert_eq!(a, a_again);

        let b = A { id: I("bar".to_string()), int: 42};
        assert!(store.update(&b).is_err());
    }


    #[test]
    fn upsert() {
        let tmpdir = tempdir::TempDir::new("sda").unwrap();
        let store = ::jfs::Store::new(&tmpdir.path().to_str().unwrap()).unwrap();
        let a = A { id: I("foo".to_string()), int: 12};
        store.create(&a).unwrap();
        let a = A { id: I("foo".to_string()), int: 42};
        store.upsert(&a).unwrap();
        let a_again = store.get_option_for_str("foo").unwrap().unwrap();
        assert_eq!(a, a_again);

        let b = A { id: I("bar".to_string()), int: 42};
        store.upsert(&b).is_err();
        let b_again = store.get_option_for_str("bar").unwrap().unwrap();
        assert_eq!(b, b_again);
    }
}