~ubuntu-branches/ubuntu/utopic/hockeypuck/utopic-proposed

« back to all changes in this revision

Viewing changes to build/src/github.com/syndtr/goleveldb/leveldb/key.go

  • Committer: Package Import Robot
  • Author(s): Casey Marshall
  • Date: 2014-04-13 20:06:01 UTC
  • Revision ID: package-import@ubuntu.com-20140413200601-oxdlqn1gy0x8m55u
Tags: 1.0~rel20140413+7a1892a~trusty
Hockeypuck 1.0 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
 
2
// All rights reserved.
 
3
//
 
4
// Use of this source code is governed by a BSD-style license that can be
 
5
// found in the LICENSE file.
 
6
 
 
7
package leveldb
 
8
 
 
9
import (
 
10
        "encoding/binary"
 
11
        "fmt"
 
12
)
 
13
 
 
14
type vType int
 
15
 
 
16
func (t vType) String() string {
 
17
        switch t {
 
18
        case tDel:
 
19
                return "d"
 
20
        case tVal:
 
21
                return "v"
 
22
        }
 
23
        return "x"
 
24
}
 
25
 
 
26
// Value types encoded as the last component of internal keys.
 
27
// Don't modify; this value are saved to disk.
 
28
const (
 
29
        tDel vType = iota
 
30
        tVal
 
31
)
 
32
 
 
33
// tSeek defines the vType that should be passed when constructing an
 
34
// internal key for seeking to a particular sequence number (since we
 
35
// sort sequence numbers in decreasing order and the value type is
 
36
// embedded as the low 8 bits in the sequence number in internal keys,
 
37
// we need to use the highest-numbered ValueType, not the lowest).
 
38
const tSeek = tVal
 
39
 
 
40
const (
 
41
        // Maximum value possible for sequence number; the 8-bits are
 
42
        // used by value type, so its can packed together in single
 
43
        // 64-bit integer.
 
44
        kMaxSeq uint64 = (uint64(1) << 56) - 1
 
45
        // Maximum value possible for packed sequence number and type.
 
46
        kMaxNum uint64 = (kMaxSeq << 8) | uint64(tSeek)
 
47
)
 
48
 
 
49
// Maximum number encoded in bytes.
 
50
var kMaxNumBytes = make([]byte, 8)
 
51
 
 
52
func init() {
 
53
        binary.LittleEndian.PutUint64(kMaxNumBytes, kMaxNum)
 
54
}
 
55
 
 
56
type iKey []byte
 
57
 
 
58
func newIKey(ukey []byte, seq uint64, t vType) iKey {
 
59
        if seq > kMaxSeq || t > tVal {
 
60
                panic("invalid seq number or value type")
 
61
        }
 
62
 
 
63
        b := make(iKey, len(ukey)+8)
 
64
        copy(b, ukey)
 
65
        binary.LittleEndian.PutUint64(b[len(ukey):], (seq<<8)|uint64(t))
 
66
        return b
 
67
}
 
68
 
 
69
func parseIkey(p []byte) (ukey []byte, seq uint64, t vType, ok bool) {
 
70
        if len(p) < 8 {
 
71
                return
 
72
        }
 
73
        num := binary.LittleEndian.Uint64(p[len(p)-8:])
 
74
        seq, t = uint64(num>>8), vType(num&0xff)
 
75
        if t > tVal {
 
76
                return
 
77
        }
 
78
        ukey = p[:len(p)-8]
 
79
        ok = true
 
80
        return
 
81
}
 
82
 
 
83
func (p iKey) assert() {
 
84
        if p == nil {
 
85
                panic("nil iKey")
 
86
        }
 
87
        if len(p) < 8 {
 
88
                panic(fmt.Sprintf("invalid iKey %q, len=%d", []byte(p), len(p)))
 
89
        }
 
90
}
 
91
 
 
92
func (p iKey) ukey() []byte {
 
93
        p.assert()
 
94
        return p[:len(p)-8]
 
95
}
 
96
 
 
97
func (p iKey) num() uint64 {
 
98
        p.assert()
 
99
        return binary.LittleEndian.Uint64(p[len(p)-8:])
 
100
}
 
101
 
 
102
func (p iKey) parseNum() (seq uint64, t vType, ok bool) {
 
103
        if p == nil {
 
104
                panic("nil iKey")
 
105
        }
 
106
        if len(p) < 8 {
 
107
                return
 
108
        }
 
109
        num := p.num()
 
110
        seq, t = uint64(num>>8), vType(num&0xff)
 
111
        if t > tVal {
 
112
                return 0, 0, false
 
113
        }
 
114
        ok = true
 
115
        return
 
116
}
 
117
 
 
118
func (p iKey) String() string {
 
119
        if len(p) == 0 {
 
120
                return "<nil>"
 
121
        }
 
122
        if seq, t, ok := p.parseNum(); ok {
 
123
                return fmt.Sprintf("%s,%s%d", shorten(string(p.ukey())), t, seq)
 
124
        }
 
125
        return "<invalid>"
 
126
}