-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add doc store * feat: add java sdk * feat: add java test * fix: update pom group id * feat: add add doc * feat: add add doc * fix: update readme * feat: add doc query * fix: remove the main * feat: add usage * feat: remove the english * feat: add Readme
- Loading branch information
Showing
19 changed files
with
764 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea/modules.xml | ||
.idea/jarRepositories.xml | ||
.idea/compiler.xml | ||
.idea/libraries/ | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Quick Started | ||
|
||
the java sdk is under active development, please feel free to ask for help if you have some problems with it. | ||
|
||
## DB3 SDK | ||
|
||
```xml | ||
<dependency> | ||
<groupId>network.db3</groupId> | ||
<artifactId>sdk</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</dependency> | ||
``` | ||
|
||
## How to use | ||
|
||
```java | ||
// from web3j | ||
ECKeyPair keyPair = Keys.createEcKeyPair(); | ||
Client client = new Client("https://rollup.cloud.db3.network", "https://index.cloud.db3.network", keyPair); | ||
// update the nonce for the first time | ||
client.updateNonce(); | ||
String db = "0x6ef32f0d8fc6bc872ffa977eb80920a0a75d0206"; | ||
String col = "book"; | ||
String doc = """{ | ||
"name":"The Three-Body Problem", | ||
"author":"Cixin-Liu", | ||
"rate":"4.8" | ||
}"""; | ||
AddDocResult addDocResult = client.addDoc(db, col, doc); | ||
ResultSet resultSet = client.runQuery(db, col, """/[author=Cixin-Liu]"""); | ||
``` | ||
you can the the db3 [console](https://console.cloud.db3.network/console/database/) to create a database | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>network.db3</groupId> | ||
<artifactId>sdk</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>2.0.7</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-all</artifactId> | ||
<version>1.56.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>bson</artifactId> | ||
<version>4.9.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.web3j</groupId> | ||
<artifactId>core</artifactId> | ||
<version>4.9.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.web3j</groupId> | ||
<artifactId>crypto</artifactId> | ||
<version>4.9.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-codec</groupId> | ||
<artifactId>commons-codec</artifactId> | ||
<version>1.15</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.10</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>javax.annotation</groupId> | ||
<artifactId>javax.annotation-api</artifactId> | ||
<version>1.2</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
|
||
<build> | ||
<extensions> | ||
<extension> | ||
<groupId>kr.motd.maven</groupId> | ||
<artifactId>os-maven-plugin</artifactId> | ||
</extension> | ||
</extensions> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.xolstice.maven.plugins</groupId> | ||
<artifactId>protobuf-maven-plugin</artifactId> | ||
<configuration> | ||
<!-- | ||
The version of protoc must match protobuf-java. If you don't depend on | ||
protobuf-java directly, you will be transitively depending on the | ||
protobuf-java version that grpc depends on. | ||
--> | ||
<protocArtifact> | ||
com.google.protobuf:protoc:3.21.10:exe:${os.detected.classifier} | ||
</protocArtifact> | ||
<pluginId>grpc-java</pluginId> | ||
<pluginArtifact> | ||
io.grpc:protoc-gen-grpc-java:1.51.0:exe:${os.detected.classifier} | ||
</pluginArtifact> | ||
<protoSourceRoot> | ||
${basedir}/../src/proto/proto | ||
</protoSourceRoot> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>compile</goal> | ||
<goal>compile-custom</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
<distributionManagement> | ||
<snapshotRepository> | ||
<id>ossrh</id> | ||
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url> | ||
</snapshotRepository> | ||
<repository> | ||
<id>ossrh</id> | ||
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url> | ||
</repository> | ||
</distributionManagement> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package network.db3.client; | ||
|
||
public class AddDocResult { | ||
private String mutationId; | ||
private long docId; | ||
|
||
public AddDocResult(String mutationId, long docId) { | ||
this.mutationId = mutationId; | ||
this.docId = docId; | ||
} | ||
|
||
public long getDocId() { | ||
return docId; | ||
} | ||
|
||
public String getMutationId() { | ||
return mutationId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package network.db3.client; | ||
|
||
import com.google.protobuf.ByteString; | ||
import db3_database_v2_proto.Db3DatabaseV2; | ||
import db3_indexer_proto.Db3Indexer; | ||
import db3_indexer_proto.IndexerNodeGrpc; | ||
import db3_mutation_v2_proto.Db3MutationV2; | ||
import db3_storage_proto.Db3Storage; | ||
import db3_storage_proto.StorageNodeGrpc; | ||
import io.grpc.Grpc; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import io.grpc.TlsChannelCredentials; | ||
import io.grpc.netty.GrpcSslContexts; | ||
import io.netty.handler.ssl.SslContext; | ||
import network.db3.provider.IndexProvider; | ||
import network.db3.provider.StorageProvider; | ||
import network.db3.store.ResultSet; | ||
import org.bson.ByteBuf; | ||
import org.bson.RawBsonDocument; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.web3j.crypto.ECKeyPair; | ||
import org.web3j.crypto.Keys; | ||
import org.web3j.utils.Numeric; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public class Client { | ||
private static final Logger logger = LoggerFactory.getLogger(Client.class); | ||
private final ECKeyPair keyPair; | ||
private final StorageProvider storageProvider; | ||
private final IndexProvider indexProvider; | ||
private final AtomicLong nonce; | ||
|
||
public Client(String rollupUrl, String indexUrl, | ||
ECKeyPair keyPair) throws MalformedURLException { | ||
ManagedChannel rollupChannel = Client.buildFrom(rollupUrl); | ||
ManagedChannel indexChannel = Client.buildFrom(indexUrl); | ||
StorageNodeGrpc.StorageNodeBlockingStub rollupStub = StorageNodeGrpc.newBlockingStub(rollupChannel); | ||
IndexerNodeGrpc.IndexerNodeBlockingStub indexStub = IndexerNodeGrpc.newBlockingStub(indexChannel); | ||
this.storageProvider = new StorageProvider(rollupStub, keyPair); | ||
this.indexProvider = new IndexProvider(indexStub); | ||
this.keyPair = keyPair; | ||
this.nonce = new AtomicLong(0); | ||
} | ||
|
||
private static ManagedChannel buildFrom(String url) throws MalformedURLException { | ||
URL uri = new URL(url); | ||
if (uri.getProtocol().equals("https")){ | ||
TlsChannelCredentials.Builder tlsBuilder = TlsChannelCredentials.newBuilder(); | ||
return Grpc.newChannelBuilderForAddress(uri.getHost(), uri.getPort() == 0 ? 443: uri.getPort(), | ||
tlsBuilder.build()).build(); | ||
} else { | ||
return ManagedChannelBuilder.forTarget(uri.getHost() + ":" + uri.getPort()).usePlaintext().build(); | ||
} | ||
} | ||
|
||
public void updateNonce() { | ||
long nonce = this.storageProvider.getNonce(Keys.getAddress(keyPair)); | ||
this.nonce.set(nonce); | ||
logger.info("the new nonce {} for address {}", nonce, Keys.getAddress(keyPair)); | ||
} | ||
|
||
public CreateDBResult createDocDatabase(String desc) throws IOException { | ||
Db3MutationV2.DocumentDatabaseMutation docMutation = Db3MutationV2.DocumentDatabaseMutation.newBuilder().setDbDesc(desc).build(); | ||
Db3MutationV2.Mutation.BodyWrapper body = Db3MutationV2.Mutation.BodyWrapper.newBuilder().setDocDatabaseMutation(docMutation).setDbAddress(ByteString.copyFromUtf8("")).build(); | ||
Db3MutationV2.Mutation mutation = Db3MutationV2.Mutation.newBuilder().setAction(Db3MutationV2.MutationAction.CreateDocumentDB).addBodies(body).build(); | ||
byte[] data = mutation.toByteArray(); | ||
long nonce = this.nonce.getAndIncrement(); | ||
Db3Storage.SendMutationResponse response = this.storageProvider.sendMutation(data, nonce); | ||
return new CreateDBResult(response.getId(), response.getItems(0).getValue()); | ||
} | ||
|
||
public Db3DatabaseV2.DatabaseMessage getDatabase(String db) { | ||
Db3Storage.GetDatabaseResponse response = this.storageProvider.getDatabase(db); | ||
return response.getDatabase(); | ||
} | ||
|
||
public CreateCollectonResult createCollection(String db, String col) throws IOException { | ||
byte[] address = Numeric.hexStringToByteArray(db); | ||
Db3MutationV2.CollectionMutation collectionMutation = Db3MutationV2.CollectionMutation.newBuilder().setCollectionName(col).build(); | ||
Db3MutationV2.Mutation.BodyWrapper body = Db3MutationV2.Mutation.BodyWrapper.newBuilder().setCollectionMutation(collectionMutation).setDbAddress(ByteString.copyFrom(address)).build(); | ||
Db3MutationV2.Mutation mutation = Db3MutationV2.Mutation.newBuilder().setAction(Db3MutationV2.MutationAction.AddCollection).addBodies(body).build(); | ||
byte[] data = mutation.toByteArray(); | ||
long nonce = this.nonce.getAndIncrement(); | ||
Db3Storage.SendMutationResponse response = this.storageProvider.sendMutation(data, nonce); | ||
return new CreateCollectonResult(response.getId()); | ||
} | ||
|
||
public Optional<Db3DatabaseV2.Collection> getCollection(String db, String col) { | ||
return this.storageProvider.getCollection(db, col); | ||
} | ||
|
||
public AddDocResult addDoc(String db, String col, String json) throws IOException { | ||
RawBsonDocument rawBsonDocument = RawBsonDocument.parse(json); | ||
ByteBuf buf = rawBsonDocument.getByteBuffer(); | ||
byte[] data = new byte[buf.remaining()]; | ||
buf.get(data); | ||
Db3MutationV2.DocumentMutation documentMutation = Db3MutationV2.DocumentMutation.newBuilder().addDocuments(ByteString.copyFrom(data)).setCollectionName(col).build(); | ||
Db3MutationV2.Mutation.BodyWrapper body = Db3MutationV2.Mutation.BodyWrapper.newBuilder().setDbAddress(ByteString.copyFrom(Numeric.hexStringToByteArray(db))).setDocumentMutation(documentMutation).build(); | ||
Db3MutationV2.Mutation mutation = Db3MutationV2.Mutation.newBuilder().setAction(Db3MutationV2.MutationAction.AddDocument).addBodies(body).build(); | ||
byte[] buffer = mutation.toByteArray(); | ||
long nonce = this.nonce.getAndIncrement(); | ||
Db3Storage.SendMutationResponse response = this.storageProvider.sendMutation(buffer, nonce); | ||
return new AddDocResult(response.getId(), Long.parseLong(response.getItems(0).getValue())); | ||
} | ||
|
||
public ResultSet runQuery(String db, String col, String query) { | ||
Db3Indexer.RunQueryResponse response = this.indexProvider.runQuery(db, col, query); | ||
ResultSet resultSet = new ResultSet(); | ||
resultSet.setDocs(response.getDocumentsList()); | ||
resultSet.setCount(response.getCount()); | ||
return resultSet; | ||
} | ||
} |
Oops, something went wrong.