-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from roots-id/21-add-external-persistence-storage
Refactor #21 - Add external persistence storage
- Loading branch information
Showing
9 changed files
with
205 additions
and
152 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
src/main/kotlin/com/rootsid/wal/library/mongoimpl/WalletDocStorage.kt
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,130 @@ | ||
package com.rootsid.wal.library.mongoimpl | ||
|
||
import com.mongodb.client.MongoCollection | ||
import com.mongodb.client.MongoDatabase | ||
import com.rootsid.wal.library.mongoimpl.config.DefaultMongoDbConn | ||
import com.rootsid.wal.library.mongoimpl.document.WalletDocument | ||
import com.rootsid.wal.library.wallet.model.Wallet | ||
import com.rootsid.wal.library.wallet.storage.WalletStorage | ||
import org.litote.kmongo.* | ||
|
||
|
||
class WalletDocStorage(db: MongoDatabase? = null, collectionName: String = "wallet") : WalletStorage { | ||
private val walletCollection: MongoCollection<WalletDocument> | ||
|
||
init { | ||
val mongoConn = db ?: DefaultMongoDbConn.open() | ||
|
||
this.walletCollection = mongoConn.getCollection<WalletDocument>(collectionName) | ||
} | ||
|
||
/** | ||
* Insert wallet | ||
* | ||
* @param wallet Wallet data object to add into the database | ||
* @return true if the operation was acknowledged | ||
*/ | ||
override fun insert(wallet: Wallet): Wallet { | ||
val result = walletCollection.insertOne(wallet as WalletDocument) | ||
|
||
if (result.wasAcknowledged()) { | ||
return wallet | ||
} | ||
|
||
throw Exception("Failed to insert wallet") | ||
} | ||
|
||
/** | ||
* Update wallet | ||
* | ||
* @param wallet updated Wallet data object | ||
* @return true if the operation was acknowledged | ||
*/ | ||
override fun update(wallet: Wallet): Boolean { | ||
val result = walletCollection.updateOne(wallet as WalletDocument, upsert()) | ||
return result.wasAcknowledged() | ||
} | ||
|
||
/** | ||
* Find wallet | ||
* | ||
* @param name name of the wallet to find | ||
* @return wallet data object | ||
*/ | ||
override fun findByName(name: String): Wallet { | ||
return walletCollection.findOne(Wallet::_id eq name) ?: throw Exception("Wallet '$name' not found.") | ||
} | ||
|
||
/** | ||
* List wallets | ||
* | ||
* @return list of stored wallet names | ||
*/ | ||
override fun list(): List<Wallet> { | ||
return walletCollection.find().toList() | ||
} | ||
|
||
/** | ||
* Wallet exists | ||
* | ||
* @param name name of the wallet to find | ||
* @return true if the wallet was found | ||
*/ | ||
override fun exists(name: String): Boolean { | ||
val wallet = walletCollection.findOne("{_id:'$name'}") | ||
return wallet != null | ||
} | ||
|
||
/** | ||
* Did alias exists | ||
* | ||
* @param db MongoDB Client | ||
* @param walletName name of the wallet storing the did | ||
* @param didAlias alias of the did | ||
* @return true if the did was found | ||
*/ | ||
fun didAliasExists(walletName: String, didAlias: String): Boolean { | ||
val wallet = walletCollection.findOne("{_id:'$walletName','dids':{${MongoOperator.elemMatch}: {'alias':'$didAlias'}}}") | ||
return wallet != null | ||
} | ||
|
||
/** | ||
* Key id exists | ||
* | ||
* @param db MongoDB Client | ||
* @param walletName name of the wallet storing the did | ||
* @param didAlias alias of the did | ||
* @param keyId key identifier | ||
* @return true if the keyId was found | ||
*/ | ||
fun keyIdExists(walletName: String, didAlias: String, keyId: String): Boolean { | ||
val wallet = | ||
walletCollection.findOne("{_id:'$walletName','dids':{${MongoOperator.elemMatch}: {'alias':'$didAlias'}}, 'dids.keyPairs.keyId':'$keyId'}") | ||
return wallet != null | ||
} | ||
|
||
/** | ||
* Issued credential alias exists | ||
* | ||
* @param db MongoDB Client | ||
* @param issuedCredentialAlias credential alias to find | ||
* @return true if the did was found | ||
*/ | ||
fun issuedCredentialAliasExists(walletName: String, issuedCredentialAlias: String): Boolean { | ||
val wallet = | ||
walletCollection.findOne("{_id:'$walletName','issuedCredentials':{${MongoOperator.elemMatch}: {'alias':'$issuedCredentialAlias'}}}") | ||
return wallet != null | ||
} | ||
|
||
/** | ||
* Credential alias exists | ||
* | ||
* @param db MongoDB Client | ||
* @param credentialAlias credential alias to find | ||
* @return true if the did was found | ||
*/ | ||
fun credentialAliasExists(walletName: String, credentialAlias: String): Boolean { | ||
val wallet = walletCollection.findOne("{_id:'$walletName','credentials':{${MongoOperator.elemMatch}: {'alias':'$credentialAlias'}}}") | ||
return wallet != null | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/rootsid/wal/library/mongoimpl/config/DefaultMongoDbConn.kt
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,22 @@ | ||
package com.rootsid.wal.library.mongoimpl.config | ||
|
||
import com.mongodb.client.MongoClient | ||
import com.mongodb.client.MongoDatabase | ||
import com.rootsid.wal.library.Constant | ||
import org.litote.kmongo.KMongo | ||
|
||
object DefaultMongoDbConn : MongoDbConn { | ||
private var db: MongoDatabase = openDb() | ||
|
||
fun open(): MongoDatabase { | ||
return db | ||
} | ||
|
||
override fun getDbName(): String { | ||
return Constant.DB_NAME | ||
} | ||
|
||
override fun getMongoClient(): MongoClient { | ||
return KMongo.createClient() | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/rootsid/wal/library/mongoimpl/config/MongoDbConn.kt
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,22 @@ | ||
package com.rootsid.wal.library.mongoimpl.config | ||
|
||
import com.mongodb.client.MongoClient | ||
import com.mongodb.client.MongoDatabase | ||
|
||
interface MongoDbConn { | ||
fun getDbName(): String | ||
|
||
fun getMongoClient() : MongoClient | ||
|
||
/** | ||
* Open db | ||
* | ||
* @return a MongoDatabase representing 'wal' database | ||
*/ | ||
fun openDb(): MongoDatabase { | ||
// TODO: make this configurable so it can use other connection settings | ||
val client = getMongoClient() // get com.mongodb.MongoClient new instance | ||
// TODO: make databaseName configurable | ||
return client.getDatabase(getDbName()) // normal java driver usage | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/rootsid/wal/library/wallet/storage/BlockchainTxLogStorage.kt
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,9 @@ | ||
package com.rootsid.wal.library.wallet.storage | ||
|
||
import com.rootsid.wal.library.wallet.model.BlockchainTxLog | ||
|
||
interface BlockchainTxLogStorage { | ||
fun insert(txLog: BlockchainTxLog): BlockchainTxLog | ||
|
||
fun list(): List<BlockchainTxLog> | ||
} |
7 changes: 0 additions & 7 deletions
7
src/main/kotlin/com/rootsid/wal/library/wallet/storage/Storage.kt
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/rootsid/wal/library/wallet/storage/WalletStorage.kt
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,15 @@ | ||
package com.rootsid.wal.library.wallet.storage | ||
|
||
import com.rootsid.wal.library.wallet.model.Wallet | ||
|
||
interface WalletStorage { | ||
fun insert(wallet: Wallet): Wallet | ||
|
||
fun update(wallet: Wallet): Boolean | ||
|
||
fun findByName(name: String): Wallet | ||
|
||
fun exists(name: String): Boolean | ||
|
||
fun list(): List<Wallet> | ||
} |
Oops, something went wrong.