Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jjjachyty opened this issue Aug 11, 2017 · 6 comments
Closed

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

jjjachyty opened this issue Aug 11, 2017 · 6 comments

Comments

@jjjachyty
Copy link

jjjachyty commented Aug 11, 2017

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 ?

@jjjachyty jjjachyty changed the title How to set the key's index and Ascend? How to set the key's index and iterator? Aug 11, 2017
@tidwall
Copy link
Owner

tidwall commented Aug 11, 2017

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
Copy link
Author

jjjachyty commented Aug 14, 2017

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

@tidwall
Copy link
Owner

tidwall commented Aug 14, 2017

db.CreateIndex("index_name", "*", buntdb.IndexJSON("product"), buntdb.IndexJSON("level"))

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

@jjjachyty
Copy link
Author

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

@tidwall
Copy link
Owner

tidwall commented Aug 15, 2017

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
Copy link
Author

jjjachyty commented Aug 16, 2017

@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

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants