re-use selectionReader

master
Jordan Orelli 8 years ago
parent 372d236fe7
commit dbc7bf36c3

@ -38,6 +38,7 @@ type Dict struct {
*Namespace *Namespace
entities []Entity entities []Entity
br *bit.BufReader br *bit.BufReader
sr *selectionReader
// a reference to our string table of entity baseline data. For whatever // a reference to our string table of entity baseline data. For whatever
// reason, the first set of baselines sometimes come in before the classes // reason, the first set of baselines sometimes come in before the classes
@ -50,6 +51,7 @@ func NewDict(sd *stbl.Dict) *Dict {
Namespace: new(Namespace), Namespace: new(Namespace),
entities: make([]Entity, e_limit), entities: make([]Entity, e_limit),
br: new(bit.BufReader), br: new(bit.BufReader),
sr: new(selectionReader),
base: sd.TableForName("instancebaseline"), base: sd.TableForName("instancebaseline"),
} }
sd.WatchTable("instancebaseline", d.updateBaselines) sd.WatchTable("instancebaseline", d.updateBaselines)
@ -72,7 +74,7 @@ func (d *Dict) createEntity(id int) error {
} }
Debug.Printf("create entity id: %d classId: %d className: %v class: %v\n", id, classId, className, class) Debug.Printf("create entity id: %d classId: %d className: %v class: %v\n", id, classId, className, class)
e := class.New() e := class.New()
e.Read(d.br) e.Read(d.br, d.sr)
return nil return nil
} }
@ -90,7 +92,7 @@ func (d *Dict) updateEntity(id int) error {
if e == nil { if e == nil {
return fmt.Errorf("update entity %d refused: no such entity", id) return fmt.Errorf("update entity %d refused: no such entity", id)
} }
e.Read(d.br) e.Read(d.br, d.sr)
return nil return nil
} }
@ -198,7 +200,7 @@ func (d *Dict) syncBaselines() {
d.br.SetSource(e.Value) d.br.SetSource(e.Value)
Debug.Printf("syncBaselines has new baseline for class %v", c) Debug.Printf("syncBaselines has new baseline for class %v", c)
if err := c.baseline.Read(d.br); err != nil { if err := c.baseline.Read(d.br, d.sr); err != nil {
Debug.Printf("syncBaselines failed to read a baseline: %v", err) Debug.Printf("syncBaselines failed to read a baseline: %v", err)
continue continue
} }

@ -10,18 +10,17 @@ type Entity struct {
*Class *Class
} }
func (e *Entity) Read(br bit.Reader) error { func (e *Entity) Read(br bit.Reader, sr *selectionReader) error {
if e.Class == nil { if e.Class == nil {
return fmt.Errorf("unable to read entity: entity has no class") return fmt.Errorf("unable to read entity: entity has no class")
} }
Debug.Printf("entity %v read", e) Debug.Printf("entity %v read", e)
r := newSelectionReader() if err := sr.read(br, htree); err != nil {
if err := r.read(br, htree); err != nil {
return fmt.Errorf("unable to read entity: %v", err) return fmt.Errorf("unable to read entity: %v", err)
} }
for _, s := range r.selections() { for _, s := range sr.selections() {
switch s.count { switch s.count {
case 0: case 0:
Debug.Printf("FUCK!") Debug.Printf("FUCK!")

@ -33,14 +33,10 @@ type selectionReader struct {
all [1024]selection all [1024]selection
} }
func newSelectionReader() *selectionReader { func (r *selectionReader) read(br bit.Reader, n node) error {
r := new(selectionReader)
r.cur.count = 1 r.cur.count = 1
r.cur.vals[0] = -1 r.cur.vals[0] = -1
return r r.count = 0
}
func (r *selectionReader) read(br bit.Reader, n node) error {
for fn := walk(n, br); fn != nil; fn = walk(n, br) { for fn := walk(n, br); fn != nil; fn = walk(n, br) {
if err := br.Err(); err != nil { if err := br.Err(); err != nil {
return fmt.Errorf("unable to read selection: bit reader error: %v", err) return fmt.Errorf("unable to read selection: bit reader error: %v", err)

Loading…
Cancel
Save