duanjian9148 2016-02-03 14:19
浏览 83
已采纳

BoltDB中的键顺序

Which endian should be used for a boltdb's key? Should I get the endian of the machine first and use that?

I need the keys to be ordered properly - just as a sequence of bytes, no specific logic for ordering them. For example which endian should be used here (the Key is a sequential id, like a mongodb's one):

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
    "log"
    "time"

    "github.com/boltdb/bolt"
)

var (
    Endian = binary.BigEndian // Which endian?
)

func main() {
    db, err := bolt.Open("temp.db", 0600, nil)
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    defer time.Sleep(time.Second)

    tempBucket := []byte("TMP")

    db.Update(func(tx *bolt.Tx) error {
        data := Row{
            Key: Key{
                Head:    0x1A1A1A,
                Mark:    0x1010,
                Counter: 0x01,
            },
            At:      time.Now().UTC().Unix(),
            Payload: 10,
        }

        keyBytes, err := marshal(&data.Key)
        if err != nil {
            return erp(err)
        }

        dataBytes, err := marshal(&data)
        if err != nil {
            return erp(err)
        }

        b, err := tx.CreateBucketIfNotExists(tempBucket)
        if err != nil {
            return erp(err)
        }

        err = b.Put(keyBytes, dataBytes)
        if err != nil {
            return erp(err)
        }

        return nil
    })

    db.View(func(tx *bolt.Tx) error {
        b := tx.Bucket(tempBucket)

        c := b.Cursor()

        for k, v := c.First(); k != nil; k, v = c.Next() {
            k, v := k, v
            fmt.Println(k, v)

            dt := Row{}
            err := unmarshal(&dt, v)
            if err != nil {
                return erp(err)
            }
            fmt.Printf("key=%X, value=%v
", k, dt)
        }

        return nil
    })
}

func erp(e error) error {
    log.Println(e)
    return e
}

func marshal(pointerToData interface{}) ([]byte, error) {
    buf := new(bytes.Buffer)
    err := binary.Write(buf, Endian, pointerToData)
    if err != nil {
        return nil, err
    }
    return buf.Bytes(), nil
}

func unmarshal(pointerToData interface{}, bs []byte) error {
    buffer := bytes.NewBuffer(bs)
    err := binary.Read(buffer, Endian, pointerToData)
    if err != nil {
        return err
    }
    return nil
}

type Row struct {
    Key
    At      int64
    Payload int64
}

type Key struct {
    Head    uint32
    Mark    uint16
    Counter uint16
}
  • 写回答

1条回答 默认 最新

  • dsfvsdfv23599 2016-02-03 15:49
    关注

    To have the keys naturally ordered, use BigEndian

    In LittleEndian, the bytes are swapped, which would give it to you out of order.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?