Compare commits
2 commits
4ca7f19464
...
7741d47714
Author | SHA1 | Date | |
---|---|---|---|
asonix | 7741d47714 | ||
asonix | 205eb30e12 |
|
@ -4,8 +4,9 @@ mod object;
|
|||
mod subject;
|
||||
|
||||
use bonsaidb::core::{
|
||||
document::Emit,
|
||||
schema::{Collection, CollectionMapReduce, Schema, View, ViewSchema},
|
||||
document::{BorrowedDocument, Emit},
|
||||
key::Key,
|
||||
schema::{Collection, CollectionMapReduce, MapReduce, Schema, View, ViewSchema},
|
||||
};
|
||||
|
||||
pub(crate) use self::{object::Object, subject::Subject};
|
||||
|
@ -30,21 +31,24 @@ pub(crate) struct Triple {
|
|||
pub(crate) object: Object,
|
||||
}
|
||||
|
||||
#[derive(Clone, Collection, Debug, serde::Deserialize, serde::Serialize)]
|
||||
#[collection(name = "Iri", authority = "asonix/triplestore", primary_key = u128, views = [IriUniqueness])]
|
||||
#[derive(Clone, Collection, Debug, Key)]
|
||||
#[collection(name = "Iri", authority = "asonix/triplestore", primary_key = u128, views = [IriUniqueness], serialization = Key)]
|
||||
pub(crate) struct Iri {
|
||||
#[key]
|
||||
pub(crate) iri: self::iri::OwnedIri,
|
||||
}
|
||||
|
||||
#[derive(Clone, Collection, Debug, serde::Deserialize, serde::Serialize)]
|
||||
#[collection(name = "LiteralString", authority = "asonix/triplestore", primary_key = u128, views = [LiteralStringUniqueness])]
|
||||
#[serde(transparent)]
|
||||
pub(crate) struct LiteralString {
|
||||
pub(crate) string: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Collection, Debug, serde::Deserialize, serde::Serialize)]
|
||||
#[collection(name = "LiteralLanguageTag", authority = "asonix/triplestore", primary_key = u128, views = [LiteralLanguageTagUniqueness])]
|
||||
#[derive(Clone, Collection, Debug, Key)]
|
||||
#[collection(name = "LiteralLanguageTag", authority = "asonix/triplestore", primary_key = u128, views = [LiteralLanguageTagUniqueness], serialization = Key)]
|
||||
pub(crate) struct LiteralLanguageTag {
|
||||
#[key]
|
||||
pub(crate) langtag: self::langtag::OwnedLanguageTag,
|
||||
}
|
||||
|
||||
|
@ -76,22 +80,22 @@ pub(crate) struct TriplesBySubjectPredicateAndObject;
|
|||
// Iri views
|
||||
|
||||
#[derive(Clone, Debug, View, ViewSchema)]
|
||||
#[view(collection = Iri, key = self::iri::OwnedIri)]
|
||||
#[view_schema(policy = Unique)]
|
||||
#[view(collection = Iri, key = Vec<u8>)]
|
||||
#[view_schema(mapped_key = std::borrow::Cow<'doc, [u8]>, policy = Unique)]
|
||||
pub(crate) struct IriUniqueness;
|
||||
|
||||
// LiteralString views
|
||||
|
||||
#[derive(Clone, Debug, View, ViewSchema)]
|
||||
#[view(collection = LiteralString, key = String)]
|
||||
#[view_schema(policy = Unique)]
|
||||
#[view(collection = LiteralString, key = Vec<u8>)]
|
||||
#[view_schema(mapped_key = std::borrow::Cow<'doc, [u8]>, policy = Unique)]
|
||||
pub(crate) struct LiteralStringUniqueness;
|
||||
|
||||
// LiteralLanguageTag views
|
||||
|
||||
#[derive(Clone, Debug, View, ViewSchema)]
|
||||
#[view(collection = LiteralLanguageTag, key = self::langtag::OwnedLanguageTag)]
|
||||
#[view_schema(policy = Unique)]
|
||||
#[view(collection = LiteralLanguageTag, key = Vec<u8>)]
|
||||
#[view_schema(mapped_key = std::borrow::Cow<'doc, [u8]>, policy = Unique)]
|
||||
pub(crate) struct LiteralLanguageTagUniqueness;
|
||||
|
||||
// MapReduces
|
||||
|
@ -154,42 +158,34 @@ impl CollectionMapReduce for TriplesBySubjectPredicateAndObject {
|
|||
|
||||
// Iri MapReduces
|
||||
|
||||
impl CollectionMapReduce for IriUniqueness {
|
||||
impl MapReduce for IriUniqueness {
|
||||
fn map<'doc>(
|
||||
&self,
|
||||
document: bonsaidb::core::document::CollectionDocument<<Self::View as View>::Collection>,
|
||||
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self>
|
||||
where
|
||||
bonsaidb::core::document::CollectionDocument<<Self::View as View>::Collection>: 'doc,
|
||||
{
|
||||
document.header.emit_key(document.contents.iri)
|
||||
document: &'doc BorrowedDocument<'_>,
|
||||
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> {
|
||||
|
||||
document.header.emit_key_and_value(std::borrow::Cow::Borrowed(&document.contents), ())
|
||||
}
|
||||
}
|
||||
|
||||
// LiteralString MapReduces
|
||||
|
||||
impl CollectionMapReduce for LiteralStringUniqueness {
|
||||
impl MapReduce for LiteralStringUniqueness {
|
||||
fn map<'doc>(
|
||||
&self,
|
||||
document: bonsaidb::core::document::CollectionDocument<<Self::View as View>::Collection>,
|
||||
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self>
|
||||
where
|
||||
bonsaidb::core::document::CollectionDocument<<Self::View as View>::Collection>: 'doc,
|
||||
{
|
||||
document.header.emit_key(document.contents.string)
|
||||
document: &'doc BorrowedDocument<'doc>,
|
||||
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> {
|
||||
document.header.emit_key_and_value(std::borrow::Cow::Borrowed(&document.contents), ())
|
||||
}
|
||||
}
|
||||
|
||||
// LiteralLanguageTag MapReduces
|
||||
|
||||
impl CollectionMapReduce for LiteralLanguageTagUniqueness {
|
||||
impl MapReduce for LiteralLanguageTagUniqueness {
|
||||
fn map<'doc>(
|
||||
&self,
|
||||
document: bonsaidb::core::document::CollectionDocument<<Self::View as View>::Collection>,
|
||||
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self>
|
||||
where
|
||||
bonsaidb::core::document::CollectionDocument<<Self::View as View>::Collection>: 'doc,
|
||||
{
|
||||
document.header.emit_key(document.contents.langtag)
|
||||
document: &'doc BorrowedDocument<'_>,
|
||||
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> {
|
||||
document.header.emit_key_and_value(std::borrow::Cow::Borrowed(&document.contents), ())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,9 +9,7 @@ pub(crate) enum Iri<'a> {
|
|||
Owned(iref::IriBuf),
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
|
||||
)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
|
||||
#[serde(transparent)]
|
||||
pub(crate) struct OwnedIri(pub(crate) Iri<'static>);
|
||||
|
||||
|
@ -190,15 +188,35 @@ impl<'a> serde::Serialize for Iri<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for Iri<'static> {
|
||||
impl<'de> serde::Deserialize<'de> for Iri<'de> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
iref::IriBuf::new(&s)
|
||||
.map_err(D::Error::custom)
|
||||
.map(|iri| Self::Owned(iri))
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum Either<'a> {
|
||||
Str(&'a str),
|
||||
String(String),
|
||||
}
|
||||
|
||||
match Either::deserialize(deserializer)? {
|
||||
Either::Str(s) => iref::Iri::new(s).map_err(D::Error::custom).map(Self::Ref),
|
||||
Either::String(s) => iref::IriBuf::new(&s)
|
||||
.map_err(D::Error::custom)
|
||||
.map(Self::Owned),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for OwnedIri {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
Iri::deserialize(deserializer)
|
||||
.map(Iri::into_owned)
|
||||
.map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,9 +9,7 @@ pub(crate) enum LanguageTag<'a> {
|
|||
Owned(langtag::LanguageTagBuf),
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
|
||||
)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
|
||||
#[serde(transparent)]
|
||||
pub(crate) struct OwnedLanguageTag(pub(crate) LanguageTag<'static>);
|
||||
|
||||
|
@ -192,15 +190,37 @@ impl<'a> serde::Serialize for LanguageTag<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for LanguageTag<'static> {
|
||||
impl<'de> serde::Deserialize<'de> for LanguageTag<'de> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
langtag::LanguageTagBuf::parse_copy(&s)
|
||||
.map_err(D::Error::custom)
|
||||
.map(|iri| Self::Owned(iri))
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum Either<'a> {
|
||||
Str(&'a str),
|
||||
String(String),
|
||||
}
|
||||
|
||||
match Either::deserialize(deserializer)? {
|
||||
Either::Str(s) => langtag::LanguageTag::parse(s)
|
||||
.map_err(D::Error::custom)
|
||||
.map(Self::Ref),
|
||||
Either::String(s) => langtag::LanguageTagBuf::parse_copy(&s)
|
||||
.map_err(D::Error::custom)
|
||||
.map(Self::Owned),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for OwnedLanguageTag {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
LanguageTag::deserialize(deserializer)
|
||||
.map(LanguageTag::into_owned)
|
||||
.map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue