Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#296 Implemented SelfJsonStorage #297

Merged
merged 2 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<name>Self XDSD Storage Module</name>
<description>Storage Module for Self XDSD</description>
<properties>
<self.core.version>0.0.72</self.core.version>
<self.core.version>0.0.81</self.core.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/selfxdsd/storage/SelfJooq.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.selfxdsd.storage;

import com.selfxdsd.api.*;
import com.selfxdsd.api.storage.JsonStorage;
import com.selfxdsd.api.storage.Storage;

/**
Expand Down Expand Up @@ -135,6 +136,11 @@ public Payments payments() {
return new SelfPayments(this, this.database);
}

@Override
public JsonStorage jsonStorage() {
return new SelfJsonStorage(this, this.database);
}

@Override
public void close() {
this.database.close();
Expand Down
116 changes: 116 additions & 0 deletions src/main/java/com/selfxdsd/storage/SelfJsonStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Copyright (c) 2020-2021, Self XDSD Contributors
* All rights reserved.
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to read the Software only. Permission is hereby NOT GRANTED to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software.
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.selfxdsd.storage;

import com.selfxdsd.api.storage.JsonStorage;
import com.selfxdsd.api.storage.Storage;
import org.jooq.Record;
import org.jooq.Result;

import java.net.URI;
import static com.selfxdsd.storage.generated.jooq.Tables.SLF_JSONSTORAGE_XDSD;

/**
* Storage for JsonResources in Self XDSD (we store JSONs received from
* providers' API for making conditional requests using E-Tag).
* @author Mihai Andronache (amihaiemil@gmail.com)
* @since 0.0.70
* @version $Id$
*/
public final class SelfJsonStorage implements JsonStorage {

/**
* Parent Storage.
*/
private final Storage storage;

/**
* Database.
*/
private final Database database;

/**
* Ctor.
* @param storage Parent Storage.
* @param database Database.
*/
public SelfJsonStorage(
final Storage storage,
final Database database
) {
this.storage = storage;
this.database = database;
}

@Override
public String getEtag(final URI uri) {
final Result<Record> result = this.database.jooq()
.select()
.from(SLF_JSONSTORAGE_XDSD)
.where(SLF_JSONSTORAGE_XDSD.URL.eq(uri.toString()))
.fetch();
if(result.size() > 0) {
return result.get(0).get(SLF_JSONSTORAGE_XDSD.ETAG);
}
return null;
}

@Override
public String getResourceBody(final URI uri) {
final Result<Record> result = this.database.jooq()
.select()
.from(SLF_JSONSTORAGE_XDSD)
.where(SLF_JSONSTORAGE_XDSD.URL.eq(uri.toString()))
.fetch();
if(result.size() > 0) {
return result.get(0).get(SLF_JSONSTORAGE_XDSD.JSONBODY);
}
return null;
}

@Override
public void store(final URI uri, final String etag, final String resource) {
if(uri.toString().length() < 1024
&& !etag.isBlank() && !resource.isBlank()) {
if(this.getEtag(uri) == null) {
this.database.jooq().insertInto(
SLF_JSONSTORAGE_XDSD,
SLF_JSONSTORAGE_XDSD.URL,
SLF_JSONSTORAGE_XDSD.ETAG,
SLF_JSONSTORAGE_XDSD.JSONBODY
).values(
uri.toString(),
etag,
resource
).execute();
} else {
this.database.jooq()
.update(SLF_JSONSTORAGE_XDSD)
.set(SLF_JSONSTORAGE_XDSD.ETAG, etag)
.set(SLF_JSONSTORAGE_XDSD.JSONBODY, resource)
.where(SLF_JSONSTORAGE_XDSD.URL.eq(uri.toString()))
.execute();
}
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/selfxdsd/storage/generated/jooq/Keys.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/main/java/com/selfxdsd/storage/generated/jooq/Tables.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading