From 42849813d391c83dc20e07ec9b1b43b4294700d7 Mon Sep 17 00:00:00 2001 From: drdr xp Date: Tue, 24 Sep 2019 17:33:40 +0800 Subject: [PATCH] add WithLeaves --- trie/nodes.pb.go | 81 ++++++++++++++++++++++++---------------- trie/nodes.proto | 7 ++++ trie/slimtrie_create.go | 35 +++++++++-------- trie/slimtrie_marshal.go | 2 +- trie/slimtrie_query.go | 4 ++ 5 files changed, 80 insertions(+), 49 deletions(-) diff --git a/trie/nodes.pb.go b/trie/nodes.pb.go index 7ab6600f..a78d9f37 100644 --- a/trie/nodes.pb.go +++ b/trie/nodes.pb.go @@ -43,7 +43,7 @@ func (m *Bitmap) Reset() { *m = Bitmap{} } func (m *Bitmap) String() string { return proto.CompactTextString(m) } func (*Bitmap) ProtoMessage() {} func (*Bitmap) Descriptor() ([]byte, []int) { - return fileDescriptor_nodes_182a0136b0eebde3, []int{0} + return fileDescriptor_nodes_da8a55c5c5cffbb1, []int{0} } func (m *Bitmap) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Bitmap.Unmarshal(m, b) @@ -90,12 +90,21 @@ func (m *Bitmap) GetSelectIndex() []int32 { // // Since 0.5.10 type Nodes struct { + // 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 + WithLeaves bool `protobuf:"varint,8,opt,name=WithLeaves,proto3" json:"WithLeaves,omitempty"` // WithPrefixContent indicates whether full inner prefixes are stored or only the // lengths of inner prefix are stored. // // Since 0.5.10 WithPrefixContent bool `protobuf:"varint,9,opt,name=WithPrefixContent,proto3" json:"WithPrefixContent,omitempty"` - WithLeafPrefix bool `protobuf:"varint,10,opt,name=WithLeafPrefix,proto3" json:"WithLeafPrefix,omitempty"` + // WithLeafPrefix indicates whether to store prefix of leaves. + // + // Since 0.5.10 + WithLeafPrefix bool `protobuf:"varint,10,opt,name=WithLeafPrefix,proto3" json:"WithLeafPrefix,omitempty"` // BigInnerCnt is number of big (267 bit) inner node. // // Since 0.5.10 @@ -226,7 +235,7 @@ func (m *Nodes) Reset() { *m = Nodes{} } func (m *Nodes) String() string { return proto.CompactTextString(m) } func (*Nodes) ProtoMessage() {} func (*Nodes) Descriptor() ([]byte, []int) { - return fileDescriptor_nodes_182a0136b0eebde3, []int{1} + return fileDescriptor_nodes_da8a55c5c5cffbb1, []int{1} } func (m *Nodes) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Nodes.Unmarshal(m, b) @@ -246,6 +255,13 @@ func (m *Nodes) XXX_DiscardUnknown() { var xxx_messageInfo_Nodes proto.InternalMessageInfo +func (m *Nodes) GetWithLeaves() bool { + if m != nil { + return m.WithLeaves + } + return false +} + func (m *Nodes) GetWithPrefixContent() bool { if m != nil { return m.WithPrefixContent @@ -391,35 +407,36 @@ func init() { proto.RegisterType((*Nodes)(nil), "Nodes") } -func init() { proto.RegisterFile("nodes.proto", fileDescriptor_nodes_182a0136b0eebde3) } +func init() { proto.RegisterFile("nodes.proto", fileDescriptor_nodes_da8a55c5c5cffbb1) } -var fileDescriptor_nodes_182a0136b0eebde3 = []byte{ - // 432 bytes of a gzipped FileDescriptorProto +var fileDescriptor_nodes_da8a55c5c5cffbb1 = []byte{ + // 443 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x93, 0x4f, 0x6f, 0xd3, 0x40, - 0x10, 0xc5, 0x65, 0x25, 0x71, 0xe9, 0x24, 0x69, 0xe9, 0x28, 0x87, 0x39, 0xa0, 0x74, 0x89, 0x10, - 0xac, 0x04, 0xf4, 0xd0, 0xf2, 0xe7, 0xc2, 0x1f, 0xc9, 0x3d, 0x55, 0xaa, 0x01, 0x39, 0x95, 0x2a, - 0x71, 0x40, 0x72, 0xc9, 0x84, 0x5a, 0x2d, 0xeb, 0xc8, 0xbb, 0x48, 0x2d, 0x9f, 0x86, 0x8f, 0x8a, - 0x76, 0xdc, 0xe0, 0xdd, 0x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 0xde, 0x8a, 0xa3, + 0x05, 0xf8, 0x0c, 0x86, 0xed, 0x43, 0x4a, 0x33, 0x7a, 0x17, 0x8f, 0x8d, 0x44, 0x7c, 0x05, 0xfb, + 0x6d, 0xbd, 0x0e, 0xfa, 0x3e, 0x3e, 0x71, 0xdf, 0x81, 0xd3, 0x70, 0x06, 0x5b, 0xfa, 0x20, 0x21, + 0x22, 0x96, 0x26, 0x5f, 0x7a, 0xae, 0x2e, 0xf9, 0x32, 0x91, 0x2f, 0xf6, 0xf8, 0x6f, 0x00, 0x00, + 0x00, 0xff, 0xff, 0x0d, 0xa4, 0xe0, 0xae, 0xc0, 0x03, 0x00, 0x00, } diff --git a/trie/nodes.proto b/trie/nodes.proto index 985547f0..8bf67aa4 100644 --- a/trie/nodes.proto +++ b/trie/nodes.proto @@ -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. @@ -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. // diff --git a/trie/slimtrie_create.go b/trie/slimtrie_create.go index 7ff643d6..13600ec2 100644 --- a/trie/slimtrie_create.go +++ b/trie/slimtrie_create.go @@ -36,6 +36,8 @@ type creator struct { bigCnt int32 nodeCnt int32 + withLeaves bool + // options option Opt @@ -68,7 +70,7 @@ 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{ @@ -76,6 +78,8 @@ func newCreator(n int, opt Opt) *creator { bigCnt: 0, nodeCnt: 0, + withLeaves: withLeaves, + option: opt, innerIndexes: make([]int32, 0, n), @@ -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. @@ -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. @@ -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") @@ -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 @@ -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}) @@ -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 } @@ -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 } @@ -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) { diff --git a/trie/slimtrie_marshal.go b/trie/slimtrie_marshal.go index 0dc86e1b..70954031 100644 --- a/trie/slimtrie_marshal.go +++ b/trie/slimtrie_marshal.go @@ -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 diff --git a/trie/slimtrie_query.go b/trie/slimtrie_query.go index d25a1355..d19c3b4a 100644 --- a/trie/slimtrie_query.go +++ b/trie/slimtrie_query.go @@ -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)