Hyperledger fabric 如何通过链码将文档批量插入couchdb

Hyperledger fabric 如何通过链码将文档批量插入couchdb,hyperledger-fabric,hyperledger,Hyperledger Fabric,Hyperledger,如何通过链码向couchdb批量插入/写入文档?链码垫片库()似乎没有这样的API 对于读取文档,似乎有一个名为“GetQueryResult”()的API。在字符串参数“query”中,我们可以构造批量get请求 但是对于插入/编写文档,是否有用于链码的批量API?提前感谢。如果您的意思是希望在一批中批处理多个事务,则不建议这样做,因为如果它们在查看历史记录时更新相同的值,则只能跟踪最终更改。因此,最好提交单个事务。如果您的意思是希望在一个批处理中批处理多个事务,则不建议这样做,因为如果它们在

如何通过链码向couchdb批量插入/写入文档?链码垫片库()似乎没有这样的API

对于读取文档,似乎有一个名为“GetQueryResult”()的API。在字符串参数“query”中,我们可以构造批量get请求


但是对于插入/编写文档,是否有用于链码的批量API?提前感谢。

如果您的意思是希望在一批中批处理多个事务,则不建议这样做,因为如果它们在查看历史记录时更新相同的值,则只能跟踪最终更改。因此,最好提交单个事务。

如果您的意思是希望在一个批处理中批处理多个事务,则不建议这样做,因为如果它们在查看历史记录时更新相同的值,则只能跟踪最终更改。因此,最好提交单个事务。

当执行链码时,每个PutState()都会向事务的建议写集添加一个键/值。您可以在链码中多次调用PutState(),最终的键/值集将出现在事务的建议写集中。请注意,在链码执行时,没有向CouchDB写入任何内容

只有在提交事务进行排序时,事务才会出现在由所有对等方处理的块中。每个对等方验证事务,然后将块中所有有效事务的写集应用于CouchDB状态数据库。请注意,状态数据库提交实际上使用的是CouchDB批量更新API(HTTP\U bulk\U docs),因此您可以在CouchDB中自动获得所需的批量更新性能


如果块中有许多键/值更新,Fabric在提交到CouchDB时会将它们实际分组为1000个批次(可使用core.yaml maxBatchUpdateSize属性进行配置),以避免任何过大有效负载的问题。最后,为了确保所有写入操作在Fabric中都显示为原子提交,Fabric将每个块的最终保存点写入CouchDB状态数据库,并将CouchDB数据刷新到磁盘。这确保了任何链码执行都能获得状态数据的一致视图,并确保Fabric可以从任何对等崩溃中完全恢复数据完整性。

当链码执行时,每个PutState()都会向事务的建议写集添加一个键/值。您可以在链码中多次调用PutState(),最终的键/值集将出现在事务的建议写集中。请注意,在链码执行时,没有向CouchDB写入任何内容

只有在提交事务进行排序时,事务才会出现在由所有对等方处理的块中。每个对等方验证事务,然后将块中所有有效事务的写集应用于CouchDB状态数据库。请注意,状态数据库提交实际上使用的是CouchDB批量更新API(HTTP\U bulk\U docs),因此您可以在CouchDB中自动获得所需的批量更新性能


如果块中有许多键/值更新,Fabric在提交到CouchDB时会将它们实际分组为1000个批次(可使用core.yaml maxBatchUpdateSize属性进行配置),以避免任何过大有效负载的问题。最后,为了确保所有写入操作在Fabric中都显示为原子提交,Fabric将每个块的最终保存点写入CouchDB状态数据库,并将CouchDB数据刷新到磁盘。这确保了任何链码执行都能获得状态数据的一致视图,并确保Fabric可以从任何对等崩溃中恢复,并具有完整的数据完整性。

不确定这是否回答了您的问题,但通过文档,我们假设您指的是PDF文件。 这有两个函数,读取一个键值对和写入一个。这里没什么特别的

'use strict';
const { Contract } = require('fabric-contract-api');

Class SomeName extends Contract {

    async initLedger(ctx) { }

    async writeDocument(ctx, documentId, documentAsString) {
        await ctx.stub.putState(documentId, Buffer.from(documentAsString));
    };

    async getDocument(ctx, documentId) {
        const docAsBytes = await ctx.stub.getState(documentId);
        return carAsBytes.toString();
    };
}
现在是服务器端代码

const { FileSystemWallet, Gateway } = require('fabric-network');

async main() {
    const walletPath = path.join(process.cwd(), 'wallet');
    const wallet = new FileSystemWallet(walletPath); // can be wherever your path is

    const gateway = new Gateway();
    await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: false }  });
    const network = await gateway.getNetwork('mychannel');
    // Get the contract from the network.
    const contract = network.getContract('SomeName');

    // Now over here lets assume you get the PDF file from a POST request using Multer
    // We get the PDF file as a buffer, then convert to a string and send it
    let pdfFile = req.file;

    await contract.submitTransaction('writeDocument', req.file.buffer.toString('utf-8);
}
如果要检索PDF文件

const getDocument = async (req, res, next) => {
    const walletPath = path.join(process.cwd(), 'wallet');
    const wallet = new FileSystemWallet(walletPath); // can be wherever your path is

    const gateway = new Gateway();
    await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: false }  });
    const network = await gateway.getNetwork('mychannel');
    // Get the contract from the network.
    const contract = network.getContract('SomeName');
    const pdfAsString= await contract.evaluateTransaction('getDocument', req.body.documentId);
    // Now do whatever with this
};
因此,我希望这能回答您关于文档上传的问题,也就是说,在区块链上上传文档不是一个好的做法,因为它会显著降低交易速度。
在这种情况下,您可以将文档存储在本地服务器上,如
mongoDB
,然后在区块链上引用文档的
sha256
散列,以便在稍后阶段交叉检查文档的真实性。

不确定这是否回答了您的问题,但假设您指的是PDF文件。 这有两个函数,读取一个键值对和写入一个。这里没什么特别的

'use strict';
const { Contract } = require('fabric-contract-api');

Class SomeName extends Contract {

    async initLedger(ctx) { }

    async writeDocument(ctx, documentId, documentAsString) {
        await ctx.stub.putState(documentId, Buffer.from(documentAsString));
    };

    async getDocument(ctx, documentId) {
        const docAsBytes = await ctx.stub.getState(documentId);
        return carAsBytes.toString();
    };
}
现在是服务器端代码

const { FileSystemWallet, Gateway } = require('fabric-network');

async main() {
    const walletPath = path.join(process.cwd(), 'wallet');
    const wallet = new FileSystemWallet(walletPath); // can be wherever your path is

    const gateway = new Gateway();
    await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: false }  });
    const network = await gateway.getNetwork('mychannel');
    // Get the contract from the network.
    const contract = network.getContract('SomeName');

    // Now over here lets assume you get the PDF file from a POST request using Multer
    // We get the PDF file as a buffer, then convert to a string and send it
    let pdfFile = req.file;

    await contract.submitTransaction('writeDocument', req.file.buffer.toString('utf-8);
}
如果要检索PDF文件

const getDocument = async (req, res, next) => {
    const walletPath = path.join(process.cwd(), 'wallet');
    const wallet = new FileSystemWallet(walletPath); // can be wherever your path is

    const gateway = new Gateway();
    await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: false }  });
    const network = await gateway.getNetwork('mychannel');
    // Get the contract from the network.
    const contract = network.getContract('SomeName');
    const pdfAsString= await contract.evaluateTransaction('getDocument', req.body.documentId);
    // Now do whatever with this
};
因此,我希望这能回答您关于文档上传的问题,也就是说,在区块链上上传文档不是一个好的做法,因为它会显著降低交易速度。
在这种情况下,您可以将文档存储在本地服务器上,如
mongoDB
,然后在区块链上引用文档的
sha256
散列,以便在稍后阶段交叉检查文档的真实性。

根据couchdb的文档,每次在couchdb中存储或更新文档时,内部B-树被更新。大容量插入通过整合对中间B树节点的许多更新,在存储空间和时间上都提供了效率提升。因此,如果我想向couchdb插入多个不同的文档,批量插入会更有效。这意味着只有一个事务包含多个文档。但是如何在golang chaincode中实现这一点呢?您认为这就像chaincode将数据上传到Coach db一样,但这里不是这样,chaincode生成事务,这些事务被执行、排序、验证并以块的形式存储。实现couchdb的状态数据库只确定资产的状态。您可以使用golang查询直接将文件上载到couchdb,但如果需要这些事务块,建议