Skip to content

Commit

Permalink
Version 1.4.0
Browse files Browse the repository at this point in the history
1. New method for storing files through network URL.
2. Renamed interface class and method names.
3. Optimize code.
  • Loading branch information
artbits committed Mar 24, 2024
1 parent 6a69bed commit 3959c26
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 92 deletions.
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ QuickIO is a Java embedded database. The underlying layer is based on the ``Leve
+ Fast reading and writing to meet the use scenarios of small and medium-sized data.


## Discover
[SQLite-Java](https://github.com/artbits/sqlite-java) is a Java ORM for SQLite databases. Written by the author with reference to the ``QuickIO`` project. It is currently in the development stage, welcome to watch.


## Download
Gradle:
```groovy
Expand All @@ -28,7 +24,7 @@ repositories {
}
dependencies {
implementation 'com.github.artbits:quickio:1.3.6-beta'
implementation 'com.github.artbits:quickio:1.4.0'
}
```
Maven:
Expand All @@ -41,15 +37,15 @@ Maven:
<dependency>
<groupId>com.github.artbits</groupId>
<artifactId>quickio</artifactId>
<version>1.3.6-beta</version>
<version>1.4.0</version>
</dependency>
```


## Usage
Store data of document type.
```java
DB db = QuickIO.usingDB("example_db")
JDB db = QuickIO.db("example_db");
Collection<Document> collection = db.collection(Document.class);

collection.save(new Document().put("city", "Canton").put("area", 7434.4));
Expand All @@ -72,7 +68,7 @@ public class Book extends IOEntity {
}


DB db = QuickIO.usingDB("example_db")
JDB db = QuickIO.db("example_db");
Collection<Book> collection = db.collection(Book.class);

collection.save(Book.of(b -> {
Expand All @@ -86,17 +82,17 @@ books.forEach(IOEntity::printJson);
```
Store data of Key-Value type, and support any key and value that can be serialized and deserialized.
```java
KV kv = QuickIO.usingKV("example_kv")
kv.write("Pi", 3.14);
kv.write(3.14, "Pi");
JKV kv = QuickIO.kv("example_kv");
kv.set("Pi", 3.14);
kv.set(3.14, "Pi");

double d = kv.read("Pi", Double.class);
String s = kv.read(3.14, String.class);
double d = kv.get("Pi", Double.class);
String s = kv.get(3.14, String.class);
QuickIO.println("%s = %f", s, d);
```
Stores data for file types.
```java
Tin tin = QuickIO.usingTin("example_tin")
JTin tin = QuickIO.tin("example_tin");
tin.put("photo.png", new File("..."));

File file = tin.get("photo.png");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.github.artbits.quickio.core.IOEntity;

public interface DB extends AutoCloseable {
public interface JDB extends AutoCloseable {
@Override
void close();
void destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@

package com.github.artbits.quickio.api;

import com.github.artbits.quickio.core.Config;

import java.util.function.BiConsumer;
import java.util.function.BiFunction;

public interface KV extends AutoCloseable {
public interface JKV extends AutoCloseable {
@Override
void close();
void destroy();
<K, V> void write(K key, V value);
<K, V> V read(K key, V defaultValue);
<K, V> V read(K key, Class<V> clazz);
<K, V> void foreach(Class<K> kClass, Class<V> vClass, BiConsumer<K, V> consumer);
<K, V> void foreach(Class<K> kClass, Class<V> vClass, BiFunction<K, V, Boolean> function);
<K> boolean erase(K key);
<K> boolean contains(K key);
<K, V> void set(K key, V value);
<K, V> V get(K key, V defaultValue);
<K, V> V get(K key, Class<V> clazz);
<K> boolean del(K key);
<K> boolean exists(K key);
<K> void rename(K oldKey, K newKey);
<K> String type(K key);
<K, V> void foreach(Class<K> kClass, Class<V> vClass, BiConsumer<K, V> consumer);
<K, V> void foreach(Class<K> kClass, Class<V> vClass, BiFunction<K, V, Boolean> function);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import java.util.List;
import java.util.function.Predicate;

public interface Tin extends AutoCloseable {
public interface JTin extends AutoCloseable {
@Override
void close();
void destroy();
void put(String filename, File file);
void put(String filename, String url);
void put(String filename, byte[] bytes);
File get(String filename);
void remove(String filename);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/artbits/quickio/core/QDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package com.github.artbits.quickio.core;

import com.github.artbits.quickio.api.Collection;
import com.github.artbits.quickio.api.DB;
import com.github.artbits.quickio.api.JDB;

import java.nio.file.Paths;

import static com.github.artbits.quickio.core.Constants.DB_PATH;

final class QDB implements DB {
final class QDB implements JDB {

private final EngineIO engine;
private final Indexer indexer;
Expand Down
66 changes: 33 additions & 33 deletions src/main/java/com/github/artbits/quickio/core/QKV.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.github.artbits.quickio.core;

import com.github.artbits.quickio.api.KV;
import com.github.artbits.quickio.api.JKV;
import com.github.artbits.quickio.exception.QIOException;

import java.nio.file.Paths;
Expand All @@ -27,7 +27,7 @@

import static com.github.artbits.quickio.core.Constants.KV_PATH;

final class QKV implements KV {
final class QKV implements JKV {

private final EngineIO engine;

Expand Down Expand Up @@ -60,14 +60,14 @@ public void destroy() {


@Override
public <K, V> void write(K key, V value) {
public <K, V> void set(K key, V value) {
engine.put(Codec.encode(key), Codec.encode(value));
}


@SuppressWarnings("unchecked")
@Override
public <K, V> V read(K key, V defaultValue) {
public <K, V> V get(K key, V defaultValue) {
byte[] bytes = engine.get(Codec.encode(key));
if (bytes != null) {
Object object = Codec.decode(bytes, defaultValue.getClass());
Expand All @@ -78,7 +78,7 @@ public <K, V> V read(K key, V defaultValue) {


@Override
public <K, V> V read(K key, Class<V> clazz) {
public <K, V> V get(K key, Class<V> clazz) {
byte[] bytes = engine.get(Codec.encode(key));
if (bytes != null) {
Object object = Codec.decode(bytes, clazz);
Expand All @@ -89,40 +89,14 @@ public <K, V> V read(K key, Class<V> clazz) {


@Override
public <K, V> void foreach(Class<K> kClass, Class<V> vClass, BiConsumer<K, V> consumer) {
engine.iteration((k, v) -> {
K key = Codec.decode(k, kClass);
V value = Codec.decode(v, vClass);
if (key != null && value != null) {
consumer.accept(key, value);
}
});
}


@Override
public <K, V> void foreach(Class<K> kClass, Class<V> vClass, BiFunction<K, V, Boolean> function) {
engine.iteration((k, v) -> {
K key = Codec.decode(k, kClass);
V value = Codec.decode(v, vClass);
if (key != null && value != null) {
Boolean b = function.apply(key, value);
return b ? null : b;
}
return null;
});
}


@Override
public <K> boolean erase(K key) {
public <K> boolean del(K key) {
engine.delete(Codec.encode(key));
return true;
}


@Override
public <K> boolean contains(K key) {
public <K> boolean exists(K key) {
byte[] bytes = engine.get(Codec.encode(key));
return bytes != null;
}
Expand Down Expand Up @@ -152,4 +126,30 @@ public <K> String type(K key) {
return (bytes != null) ? Codec.getClassName(bytes) : null;
}


@Override
public <K, V> void foreach(Class<K> kClass, Class<V> vClass, BiConsumer<K, V> consumer) {
engine.iteration((k, v) -> {
K key = Codec.decode(k, kClass);
V value = Codec.decode(v, vClass);
if (key != null && value != null) {
consumer.accept(key, value);
}
});
}


@Override
public <K, V> void foreach(Class<K> kClass, Class<V> vClass, BiFunction<K, V, Boolean> function) {
engine.iteration((k, v) -> {
K key = Codec.decode(k, kClass);
V value = Codec.decode(v, vClass);
if (key != null && value != null) {
Boolean b = function.apply(key, value);
return b ? null : b;
}
return null;
});
}

}
21 changes: 19 additions & 2 deletions src/main/java/com/github/artbits/quickio/core/QTin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

package com.github.artbits.quickio.core;

import com.github.artbits.quickio.api.Tin;
import com.github.artbits.quickio.api.JTin;
import com.github.artbits.quickio.exception.QIOException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Comparator;
Expand All @@ -33,7 +36,7 @@

import static com.github.artbits.quickio.core.Constants.TIN_PATH;

final class QTin implements Tin {
final class QTin implements JTin {

private final String LOCK_FILE_NAME = ".LOCK";

Expand Down Expand Up @@ -127,6 +130,20 @@ public void put(String filename, File file) {
}


@Override
public void put(String filename, String url) {
if (!LOCK_FILE_NAME.equals(filename)) {
String outPath = path + "/" + filename;
try (FileOutputStream stream = new FileOutputStream(outPath); FileChannel fileChannel = stream.getChannel()) {
ReadableByteChannel readableByteChannel = Channels.newChannel(new URL(url).openStream());
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
} catch (IOException e) {
throw new QIOException(e);
}
}
}


@Override
public void put(String filename, byte[] bytes) {
if (!LOCK_FILE_NAME.equals(filename)) {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/github/artbits/quickio/core/QuickIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@

package com.github.artbits.quickio.core;

import com.github.artbits.quickio.api.DB;
import com.github.artbits.quickio.api.KV;
import com.github.artbits.quickio.api.Tin;
import com.github.artbits.quickio.api.JDB;
import com.github.artbits.quickio.api.JKV;
import com.github.artbits.quickio.api.JTin;

public final class QuickIO extends Plugin {

public static DB usingDB(String name) {
public static JDB db(String name) {
return new QDB(name);
}


public static DB usingDB(Config config) {
public static JDB db(Config config) {
return new QDB(config);
}


public static KV usingKV(String name) {
public static JKV kv(String name) {
return new QKV(name);
}


public static KV usingKV(Config config) {
public static JKV kv(Config config) {
return new QKV(config);
}


public static Tin usingTin(String name) {
public static JTin tin(String name) {
return new QTin(name);
}


public static Tin usingTin(Config config) {
public static JTin tin(Config config) {
return new QTin(config);
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/apis/DBExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.artbits.quickio.annotations.Index;
import com.github.artbits.quickio.api.Collection;
import com.github.artbits.quickio.api.DB;
import com.github.artbits.quickio.api.JDB;
import com.github.artbits.quickio.core.Config;
import com.github.artbits.quickio.core.IOEntity;
import com.github.artbits.quickio.core.QuickIO;
Expand All @@ -15,7 +15,7 @@
final class DBExample {

//A static DB object. When the program ends running, the JVM automatically closes the object.
private final static DB db = QuickIO.usingDB("example_db");
private final static JDB db = QuickIO.db("example_db");


//Custom Entity Class.
Expand All @@ -42,7 +42,7 @@ void config() {
c.cache(16L * 1024 * 1024); //Set cache size.
});

try (DB db1 = QuickIO.usingDB(config)) {
try (JDB db1 = QuickIO.db(config)) {
//DB operation.
}
}
Expand Down
Loading

0 comments on commit 3959c26

Please sign in to comment.