Blockchain 检索corda中的特定状态

Blockchain 检索corda中的特定状态,blockchain,corda,Blockchain,Corda,在我的Corda项目中,我需要根据state类的某个特定字段检索特定状态,我已经为每个状态设置了唯一字段。在科尔达怎么做?国家执行情况如下: @BelongsToContract(OfferContract.class) public class OfferState implements LinearState { private final UniqueIdentifier linearID; private AnonymousParty sender; privat

在我的Corda项目中,我需要根据state类的某个特定字段检索特定状态,我已经为每个状态设置了唯一字段。在科尔达怎么做?国家执行情况如下:

@BelongsToContract(OfferContract.class)
public class OfferState implements LinearState {
    private final UniqueIdentifier linearID;
    private AnonymousParty sender;
    private AnonymousParty receiver;
    private final String policyID;
    private final double faceValue;
    private double offeredAmount;

    private boolean isActive;

    public OfferState(UniqueIdentifier linearID, AnonymousParty sender, AnonymousParty receiver, String policyID, double faceValue, double offeredAmount, boolean isActive) {
        this.linearID = linearID;
        this.sender = sender;
        this.receiver = receiver;
        this.policyID = policyID;
        this.faceValue = faceValue;
        this.offeredAmount = offeredAmount;
        this.isActive = isActive;
    }

    public UniqueIdentifier getLinearID() {
        return linearID;
    }

    public AnonymousParty getSender() {
        return sender;
    }

    public AnonymousParty getReceiver() {
        return receiver;
    }

    public String getPolicyID() {
        return policyID;
    }

    public double getFaceValue() {
        return faceValue;
    }

    public double getOfferedAmount() {
        return offeredAmount;
    }

    public boolean isActive() {
        return isActive;
    }

    @NotNull
    @Override
    public UniqueIdentifier getLinearId() {
        return linearID;
    }

    @NotNull
    @Override
    public List<AbstractParty> getParticipants() {
        return Arrays.asList(sender,receiver);
    }

    @Override
    public String toString() {
        return super.toString();
    }
}
@BelongsToContract(OfferContract.class)
公共类OfferState实现LinearState{
专用最终唯一标识符;
私人匿名发送方;
私人接收方;
私有最终字符串policyID;
私人最终双面价值;
私人双重报价金额;
私有布尔非活动;
public OfferState(UniqueIdentifier linearID、AnonymousParty发送方、AnonymousParty接收方、字符串policyID、double faceValue、double OfferAmount、boolean isActive){
this.linearID=linearID;
this.sender=发送方;
this.receiver=接收器;
this.policyID=policyID;
this.faceValue=faceValue;
this.offeredAmount=offeredAmount;
this.isActive=isActive;
}
公共唯一标识符getLinearID(){
回归线性化;
}
公共匿名方getSender(){
发信人;
}
公共匿名方getReceiver(){
返回接收器;
}
公共字符串getPolicyID(){
返回policyID;
}
公共双getFaceValue(){
返回faceValue;
}
公共双getOfferedAmount(){
退还报价金额;
}
公共布尔isActive(){
回报是积极的;
}
@NotNull
@凌驾
公共唯一标识符getLinearId(){
回归线性化;
}
@NotNull
@凌驾
公开名单{
返回数组.asList(发送方、接收方);
}
@凌驾
公共字符串toString(){
返回super.toString();
}
}
我使用以下代码在API中进行查询,但在postman输出中显示了类似[]的空括号

@GetMapping(value = "/getOfferWithID",headers = "Content-Type=application/x-www-form-urlencoded")
    private List<StateAndRef<OfferState>> getOfferWithID(HttpServletRequest request) throws IllegalArgumentException {
        String s  = request.getParameter("PolicyID");
        return proxy.vaultQuery(OfferState.class).getStates().stream().filter(it->it.getState().getData().getPolicyID().equalsIgnoreCase(s)).collect(Collectors.toList());
    }
@GetMapping(value=“/getOfferWithID”,headers=“Content Type=application/x-www-form-urlencoded”)
私有列表getOfferWithID(HttpServletRequest请求)引发IllegalArgumentException{
字符串s=request.getParameter(“PolicyID”);
返回proxy.vaultQuery(OfferState.class).getStates().stream().filter(it->it.getState().getData().getPolicyID().equalsIgnoreCase)).collect(collector.toList());
}
您能告诉我,在查询时如何过滤状态,这里出了什么问题?
是否有任何直接查询方法,以便我可以在rpc调用本身中对其进行过滤,或者我真的需要为其创建一个流?

代码在第一次查看时看起来很好,不确定出了什么问题。传递给查询条件的
linearId
可能有问题

作为参考,您可以查看以下工作正常的代码:


为了能够通过其中一个属性查询您的状态,您必须:

  • 为您的州创建自定义架构,有关示例,请参见和
  • 使用
    VaultCustomQueryCriteria
    查询您的属性
    policyId
    。阅读
    VaultCustomQueryCriteria
    (搜索该术语,并查找示例代码)
您当前的方法(使用
.filter(it->it.getState().getData().getPolicyID().equalsIgnoreCase))
)非常糟糕,本质上,您带来了一个所有状态的列表,然后过滤该列表。
当您的查询返回1000000个状态时,您将做什么?!您正在创建一个包含1000000个条目的列表,然后在该列表中循环查找包含您的
policyId
!!!这只会耗尽Java堆,降低应用程序的速度,并使其崩溃


始终将搜索/查询委托给数据库,方法是在Corda中构造查询,然后在DB中运行它(使用我已经概述的方法),您当前正在做的是从一些表中创建一个
select*,然后循环所有结果,我的方法是从policyId=xxx的某个表中选择*,该表将返回一行。

他想通过
policyId
而不是
linearId
进行查询;因此,他需要一个定制的模式,而且他获取列表中的所有状态,然后过滤该列表的方法是不好的。