简体   繁体   English

Hyperledger Fabric V1.0中日期范围的复合密钥格式

[英]Composite Key formation for a date range in Hyperledger Fabric V1.0

With reference to this question, I also have a doubt. 关于这个问题,我也有疑问。
Suppose my composite key is formed with the fields Owner_id~dateOfcreation and my keys are like: 假设我的复合密钥是由Owner_id~dateOfcreation字段构成的,而我的密钥如下:

  • Owner1~11-05-2017 Owner1〜2017年11月5日
  • Owner1~12-05-2017 Owner1〜2017年12月5日
  • Owner1~13-05-2017 Owner1〜13-05-2017
  • ... ...
  • ... ...
  • Owner1~30-05-2017 Owner1〜30-05-2017

Will it be possible for me to get states for a date range for example Owner1~12-05-2017 to Owner~27-05-2017 ? 我能否获得date range ,例如Owner1~12-05-2017 to Owner~27-05-2017

In my understanding stub.GetStateByRange(startKey,endKey) will return the keys which are in lexical order, so it will not return the expected range. 以我的理解, stub.GetStateByRange(startKey,endKey)将返回按词法顺序排列的键,因此不会返回期望的范围。 Correct me if I am wrong. 如果我错了,请纠正我。

I tried to re-arrange the keys like: 我试图像这样重新排列键:

  • Owner1~2017_05_11 Owner1〜2017_05_11
  • Owner1~2017_05_12 Owner1〜2017_05_12
  • Owner1~2017_05_13 Owner1〜2017_05_13
  • ... ...
  • ... ...
  • Owner1~2017_05_30 Owner1〜2017_05_30

In this case if i use 在这种情况下,如果我使用
stub.GetStateByPartialCompositeKey('owner~year~month~day',[]string{"owner1","2017","05"}) it will return all the keys starting with these range. stub.GetStateByPartialCompositeKey('owner~year~month~day',[]string{"owner1","2017","05"}) ,它将返回所有从这些范围开始的键。 is it correct? 这是正确的吗?

But here also I am not getting my exact output for a date range . 但是在这里我也没有得到date range确切输出。

Can anyone suggest me the proper way to implement this. 谁能建议我实施此方法的正确方法。 I think it is a common business scenario in the context of asset sharing, so please help. 我认为这是资产共享环境中的常见业务场景,因此请提供帮助。

Thanks in advance :) 提前致谢 :)

After rereading your question and the comments, I think that you have your own solution for you. 重读您的问题和评论后,我认为您有适合您的解决方案。 What about using the function GetStateByRange? 使用功能GetStateByRange怎么样? This function doesn't return keys, it returns an iterator with the values. 此函数不返回键,而是返回带有值的迭代器。

As you said in your last comment, I want to list the all assets created with in a date range how I will query it from ledger. 正如您在最后一条评论中所说,我想列出在日期范围内创建的所有资产,以及如何从分类帐中查询资产。 So, if I were you, I'd use the GetStateByRange function, passing to it the date range. 因此,如果我是您,则可以使用GetStateByRange函数,并将日期范围传递给它。

Here I paste you an example of a function that I used. 在这里,我向您粘贴一个我使用的函数的示例。

func get_all_components(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    var err error
    fmt.Println("starting get_all_components")

    // input sanitation
    err = sanitize_arguments_len(args, 1)
    if err != nil {
        return shim.Error(err.Error())
    }

    // ---- Get All Components ---- //
    resultsIterator, err := stub.GetStateByRange("c0", "c9999999999999999999")
    if err != nil {
        return shim.Error(err.Error())
    }
    defer resultsIterator.Close()

    // buffer is a JSON array containing QueryRecords
    var buffer bytes.Buffer
    buffer.WriteString("[")

    bArrayMemberAlreadyWritten := false
    for resultsIterator.HasNext() {
        //queryKeyAsStr, queryValAsBytes, err := resultsIterator.Next()
        queryResponse, err := resultsIterator.Next()
        if err != nil {
            return shim.Error(err.Error())
        }

        // Add a comma before array members, suppress it for the first array member
        if bArrayMemberAlreadyWritten == true {
            buffer.WriteString(",")
        }
        buffer.WriteString("{\"Key\":")
        buffer.WriteString("\"")
        buffer.WriteString(queryResponse.Key)
        buffer.WriteString("\"")

        buffer.WriteString(", \"Record\":")
        // Record is a JSON object, so we write as-is
        buffer.WriteString(string(queryResponse.Value))
        buffer.WriteString("}")
        bArrayMemberAlreadyWritten = true
    }
    buffer.WriteString("]")

    fmt.Printf("get_all_components:\n%s\n", buffer.String())

    return shim.Success(buffer.Bytes())
}

You should pass your date range values in the function call, in the args. 您应该在args函数调用中传递date range值。

As with the existing comments, adopted solutions and API specification, it is concluded that stub.GetStateByPartialCompositeKey is best suited API for this requirement. 与现有注释,采用的解决方案和API规范一样,可以得出结论, stub.GetStateByPartialCompositeKey最适合此要求的API。
To fetch data only for a day, it can use rich query with json date_field:value also if it is using CouchDB as its state db. 要仅获取一天的数据,如果它使用CouchDB作为状态数据库,它也可以使用带有json date_field:value 丰富查询 Since it is not a committing transcation no issue to use rich query here. 由于不是提交事务,因此在此处使用富查询没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM