tidwall / buntdb

BuntDB is an embeddable, in-memory key/value database for Go with custom indexing and geospatial support
MIT License
4.57k stars 289 forks source link

How to set the key's index and iterator? #25

Closed jjjachyty closed 7 years ago

jjjachyty commented 7 years ago

My key was timestamps ,and i want use the timestamps key iterator,Hao to do it? however the Index can only be set the key of values ?

tidwall commented 7 years ago

Indexes can only be assigned to values. You cannot add an index to a key. The key is always ordered by it's binary value.

You will need to format your timestamp so it's sortable.

For example:

 2017-09-11T10:35:21.9082
 2017-09-11T10:35:28.8293
 2017-09-12T08:05:13.0012

Or if you want to make the best use of space you can store the nanoseconds representation an int64 or time.Time.

The following serializes a time.Time object into an 8 byte key with nanosecond precision.

key := make([]byte, 8)
binary.LittleEndian.PutUint64(key, timestamp.UnixNano())

Then add the key/value to the database.

tx.Set(string(key), "some value", nil)
jjjachyty commented 7 years ago

@tidwall i want use Multi Value Index with json,like this {"product":"RPM","level":"2"} ,i want equal this pattern ,how todo it?

tidwall commented 7 years ago
db.CreateIndex("index_name", "*", buntdb.IndexJSON("product"), buntdb.IndexJSON("level"))

please see https://github.com/tidwall/buntdb#multi-value-index

jjjachyty commented 7 years ago

@tidwall i see set Multi Value Index,but how can ascend equal this two conditions?

tidwall commented 7 years ago

See #18 for an example of how to ascend specialized JSON queries. Please attach any code that you've tried and I'll do my best to help diagnose the issue.

jjjachyty commented 7 years ago

@tidwall I'm very sorry ,I have a lot of questions,Thank you for your answer patiently。 i have this data

*3
$3
set
$10
1502867961
$357
{
    "product": "RPM",
    "level": "6",
    "modular": "DP",
    "event": "1",
    "origValue": "",
    "currentValue": "测试",
    "operatorKey": "9527",
    "operatorAt":1502676101,
    "operatorName": "周星星",
    "operatorIP": "172.168.171.252",
    "operatorClient": "1",
    "sqlSentence": "select * from dual",
    "sqlParameter": "[1,2,3]"
}
*3
$3
set
$10
1502867965
$357
{
    "product": "RPM",
    "level": "5",
    "modular": "DP",
    "event": "1",
    "origValue": "",
    "currentValue": "测试",
    "operatorKey": "9527",
    "operatorAt":1502676102,
    "operatorName": "周星星",
    "operatorIP": "172.168.171.252",
    "operatorClient": "1",
    "sqlSentence": "select * from dual",
    "sqlParameter": "[1,2,3]"
}
*3
$3
set
$10
1502867971
$357
{
    "product": "RPM",
    "level": "4",
    "modular": "DP",
    "event": "1",
    "origValue": "",
    "currentValue": "测试",
    "operatorKey": "9527",
    "operatorAt":1502676103,
    "operatorName": "周星星",
    "operatorIP": "172.168.171.252",
    "operatorClient": "1",
    "sqlSentence": "select * from dual",
    "sqlParameter": "[1,2,3]"
}
*3
$3
set
$10
1502867975
$357
{
    "product": "RPM",
    "level": "3",
    "modular": "DP",
    "event": "1",
    "origValue": "",
    "currentValue": "测试",
    "operatorKey": "9527",
    "operatorAt":1502676104,
    "operatorName": "周星星",
    "operatorIP": "172.168.171.252",
    "operatorClient": "1",
    "sqlSentence": "select * from dual",
    "sqlParameter": "[1,2,3]"
}
*3
$3
set
$10
1502867982
$357
{
    "product": "RPM",
    "level": "2",
    "modular": "DP",
    "event": "1",
    "origValue": "",
    "currentValue": "测试",
    "operatorKey": "9527",
    "operatorAt":1502676105,
    "operatorName": "周星星",
    "operatorIP": "172.168.171.252",
    "operatorClient": "1",
    "sqlSentence": "select * from dual",
    "sqlParameter": "[1,2,3]"
}
*3
$3
set
$10
1502867987
$357
{
    "product": "RPM",
    "level": "1",
    "modular": "DP",
    "event": "1",
    "origValue": "",
    "currentValue": "测试",
    "operatorKey": "9527",
    "operatorAt":1502676106,
    "operatorName": "周星星",
    "operatorIP": "172.168.171.252",
    "operatorClient": "1",
    "sqlSentence": "select * from dual",
    "sqlParameter": "[1,2,3]"
}

i want to query use product&level&modular&event fields and use operatorAt field Descending order

db.BuntdbDB.CreateIndex("buntdb", "*", buntdb.IndexJSON("level"),buntdb.Desc(buntdb.IndexJSON("operatorAt")))

    err := db.BuntdbDB.View(func(tx *buntdb.Tx) error {
        err := tx.DescendGreaterThan("buntdb", {"level":"3"}, func(key, value string) bool {
                fmt.Println("\n\n\n", key, value, "\n\n\n")
            return true
        })

        return err
    })

output:

[{
    "product": "RPM",
    "level": "6",
    "modular": "DP",
    "event": "1",
    "origValue": "",
    "currentValue": "测试",
    "operatorKey": "9527",
    "operatorAt":1502676101,
    "operatorName": "周星星",
    "operatorIP": "172.168.171.252",
    "operatorClient": "1",
    "sqlSentence": "select * from dual",
    "sqlParameter": "[1,2,3]"
},{
    "product": "RPM",
    "level": "5",
    "modular": "DP",
    "event": "1",
    "origValue": "",
    "currentValue": "测试",
    "operatorKey": "9527",
    "operatorAt":1502676102,
    "operatorName": "周星星",
    "operatorIP": "172.168.171.252",
    "operatorClient": "1",
    "sqlSentence": "select * from dual",
    "sqlParameter": "[1,2,3]"
}{
    "product": "RPM",
    "level": "4",
    "modular": "DP",
    "event": "1",
    "origValue": "",
    "currentValue": "测试",
    "operatorKey": "9527",
    "operatorAt":1502676103,
    "operatorName": "周星星",
    "operatorIP": "172.168.171.252",
    "operatorClient": "1",
    "sqlSentence": "select * from dual",
    "sqlParameter": "[1,2,3]"
}]

Not descending by operatorAt