Skip to content

Commit

Permalink
add WithLeaves
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Sep 24, 2019
1 parent 9cce104 commit 4284981
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 49 deletions.
81 changes: 49 additions & 32 deletions trie/nodes.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions trie/nodes.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ message Bitmap {
// Since 0.5.10
message Nodes {

// WithLeaves indicates whether to store leaf values(as an index).
// Otherwise, it is a "filter" which can only provide existence information
// but not location information.
//
// Since 0.5.10
bool WithLeaves = 8;

// WithPrefixContent indicates whether full inner prefixes are stored or only the
// lengths of inner prefix are stored.
Expand Down Expand Up @@ -97,6 +103,7 @@ message Nodes {
int32 InnerPrefixCnt = 16;



// NodeTypeBM is a bitmap in which a "1" indicates the i-th node is an inner
// node, otherwise it is a leaf.
//
Expand Down
35 changes: 19 additions & 16 deletions trie/slimtrie_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type creator struct {
bigCnt int32
nodeCnt int32

withLeaves bool

// options

option Opt
Expand Down Expand Up @@ -68,14 +70,16 @@ type creator struct {
innerBMCnt []map[uint64]int32
}

func newCreator(n int, opt Opt) *creator {
func newCreator(n int, withLeaves bool, opt Opt) *creator {

c := &creator{

isBig: true,
bigCnt: 0,
nodeCnt: 0,

withLeaves: withLeaves,

option: opt,

innerIndexes: make([]int32, 0, n),
Expand Down Expand Up @@ -205,7 +209,10 @@ func (c *creator) addLeaf(nid int32, v interface{}) {

c.nodeCnt++

c.leaves = append(c.leaves, v)
if c.withLeaves {
c.leaves = append(c.leaves, v)
}

}

// counterElt stores an at most 17 bit bitmap and how many times it is used.
Expand Down Expand Up @@ -303,6 +310,7 @@ func (c *creator) build(e encode.Encoder) *SlimTrie {
BigInnerCnt: c.bigCnt,
WithPrefixContent: c.option.CompleteInner,
WithLeafPrefix: c.option.CompleteLeaf,
WithLeaves: c.withLeaves,
}

// Mapping most used 17-bit bitmap inner node to short inner node.
Expand Down Expand Up @@ -376,7 +384,9 @@ func (c *creator) build(e encode.Encoder) *SlimTrie {
}

// TODO separate leaf init
ns.initLeaves(c.leaves, e)
if c.withLeaves {
ns.initLeaves(c.leaves, e)
}

if c.option.CompleteLeaf {
ns.LeafPrefixBM = newBM(c.leafPrefixIndexes, int32(len(c.leaves)), "r64")
Expand All @@ -395,11 +405,6 @@ func (c *creator) build(e encode.Encoder) *SlimTrie {
// TODO filter mode: CompleteInner
func newSlimTrie(e encode.Encoder, keys []string, values interface{}, opt Opt) (*SlimTrie, error) {

// filter mode
if values == nil {
e = encode.Dummy{}
}

n := len(keys)
if n == 0 {
return &SlimTrie{encoder: e, nodes: &Nodes{}}, nil
Expand Down Expand Up @@ -429,7 +434,7 @@ func newSlimTrie(e encode.Encoder, keys []string, values interface{}, opt Opt) (
})

sb := sigbits.New(keys)
c := newCreator(n, opt)
c := newCreator(n, values != nil, opt)

queue := make([]subset, 0, n*2)
queue = append(queue, subset{0, int32(n), 0})
Expand All @@ -442,13 +447,11 @@ func newSlimTrie(e encode.Encoder, keys []string, values interface{}, opt Opt) (
// single key, it is a leaf
if e-s == 1 {
must.Be.True(tokeep[s])
var v interface{}
if values == nil {
v = nil
c.addLeaf(nid, nil)
} else {
v = getV(rvals, int(s))
c.addLeaf(nid, getV(rvals, s))
}
c.addLeaf(nid, v)
c.setLeafPrefix(nid, keys[s], o.fromKeyBit)
continue
}
Expand Down Expand Up @@ -579,7 +582,7 @@ func newValueToKeep(keys []string, values interface{}) []bool {
prev := getV(rvals, 0)

for i := 1; i < n; i++ {
v := getV(rvals, i)
v := getV(rvals, int32(i))
tokeep[i] = prev != v
prev = v
}
Expand All @@ -588,11 +591,11 @@ func newValueToKeep(keys []string, values interface{}) []bool {
return tokeep
}

func getV(reflectSlice reflect.Value, i int) interface{} {
func getV(reflectSlice reflect.Value, i int32) interface{} {
if reflectSlice.IsNil() {
return nil
}
return reflectSlice.Index(i).Interface()
return reflectSlice.Index(int(i)).Interface()
}

func (ns *Nodes) initLeaves(elts interface{}, e encode.Encoder) {
Expand Down
2 changes: 1 addition & 1 deletion trie/slimtrie_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func before000510ToNewChildrenArray(st *SlimTrie, ver string, ch *array.Bitmap16
}

// before 0.5.10 it stores steps only, no prefix
c := newCreator(64, Opt{})
c := newCreator(64, true, Opt{})

// before 0.5.10 there is no big inner
c.isBig = false
Expand Down
4 changes: 4 additions & 0 deletions trie/slimtrie_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ func (st *SlimTrie) getLeaf(nodeid int32) interface{} {

func (st *SlimTrie) getIthLeaf(ith int32) interface{} {

if !st.nodes.WithLeaves {
return nil
}

eltsize := st.encoder.GetEncodedSize(nil)
stIdx := ith * int32(eltsize)

Expand Down

0 comments on commit 4284981

Please sign in to comment.