Пример #1
0
func digraphItemset(n *digraph.EmbListNode, attr string) (types.Set, error) {
	dt := n.Dt
	embs, err := n.Embeddings()
	if err != nil {
		return nil, err
	}
	s := set.NewSortedSet(len(embs))
	for _, emb := range embs {
		for _, vid := range emb.Ids {
			err := dt.NodeAttrs.DoFind(
				int32(vid),
				func(_ int32, attrs map[string]interface{}) error {
					if val, has := attrs[attr]; has {
						switch v := val.(type) {
						case string:
							s.Add(types.String(v))
						case int:
							s.Add(types.Int(v))
						default:
							return errors.Errorf("DBSCAN does not yet support attr type %T", val)
						}
					}
					return nil
				})
			if err != nil {
				return nil, err
			}
		}
	}
	return s, nil
}
Пример #2
0
func randstr(length int) types.String {
    if urandom, err := os.Open("/dev/urandom"); err != nil {
        panic(err)
    } else {
        slice := make([]byte, length)
        if _, err := urandom.Read(slice); err != nil {
            panic(err)
        }
        urandom.Close()
        return types.String(slice)
    }
    panic("unreachable")
}
Пример #3
0
func TestAvlPutHasGetRemove(t *testing.T) {

    type record struct {
        key types.String
        value types.String
    }

    records := make([]*record, 400)
    var tree *AvlNode
    var err error
    var val interface{}
    var updated bool

    ranrec := func() *record {
        return &record{ randstr(20), randstr(20) }
    }

    for i := range records {
        r := ranrec()
        records[i] = r
        tree, updated = tree.Put(r.key, types.String(""))
        if updated {
            t.Error("should have not been updated")
        }
        tree, updated = tree.Put(r.key, r.value)
        if !updated {
            t.Error("should have been updated")
        }
        if tree.Size() != (i+1) {
            t.Error("size was wrong", tree.Size(), i+1)
        }
    }

    for _, r := range records {
        if has := tree.Has(r.key); !has {
            t.Error("Missing key")
        }
        if has := tree.Has(randstr(12)); has {
            t.Error("Table has extra key")
        }
        if val, err := tree.Get(r.key); err != nil {
            t.Error(err, val.(types.String), r.value)
        } else if !(val.(types.String)).Equals(r.value) {
            t.Error("wrong value")
        }
    }

    for i, x := range records {
        if tree, val, err = tree.Remove(x.key); err != nil {
            t.Error(err)
        } else if !(val.(types.String)).Equals(x.value) {
            t.Error("wrong value")
        }
        for _, r := range records[i+1:] {
            if has := tree.Has(r.key); !has {
                t.Error("Missing key")
            }
            if has := tree.Has(randstr(12)); has {
                t.Error("Table has extra key")
            }
            if val, err := tree.Get(r.key); err != nil {
                t.Error(err)
            } else if !(val.(types.String)).Equals(r.value) {
                t.Error("wrong value")
            }
        }
        if tree.Size() != (len(records) - (i+1)) {
            t.Error("size was wrong", tree.Size(), (len(records) - (i+1)))
        }
    }
}
Пример #4
0
func randstr(length int) types.String {
	return types.String(bs.ByteSlice(randslice(length)).String()[2:])
}
Пример #5
0
func TestBpMap(t *testing.T) {

	ranrec := func() *record {
		return &record{
			randstr(12),
			randstr(12),
		}
	}

	test := func(table types.MapOperable) {
		recs := make(records, 400)
		for i := range recs {
			r := ranrec()
			recs[i] = r
			err := table.Put(r.key, types.String(""))
			if err != nil {
				t.Error(err)
			}
			err = table.Put(r.key, r.value)
			if err != nil {
				t.Error(err)
			}
			if table.Size() != (i + 1) {
				t.Error("size was wrong", table.Size(), i+1)
			}
		}

		for _, r := range recs {
			if has := table.Has(r.key); !has {
				t.Error(table, "Missing key")
			}
			if has := table.Has(randstr(12)); has {
				t.Error("Table has extra key")
			}
			if val, err := table.Get(r.key); err != nil {
				t.Error(err)
			} else if !(val.(types.String)).Equals(r.value) {
				t.Error("wrong value")
			}
		}

		for i, x := range recs {
			if val, err := table.Remove(x.key); err != nil {
				t.Error(err)
			} else if !(val.(types.String)).Equals(x.value) {
				t.Error("wrong value")
			}
			for _, r := range recs[i+1:] {
				if has := table.Has(r.key); !has {
					t.Error("Missing key")
				}
				if has := table.Has(randstr(12)); has {
					t.Error("Table has extra key")
				}
				if val, err := table.Get(r.key); err != nil {
					t.Error(err)
				} else if !(val.(types.String)).Equals(r.value) {
					t.Error("wrong value")
				}
			}
			if table.Size() != (len(recs) - (i + 1)) {
				t.Error("size was wrong", table.Size(), (len(recs) - (i + 1)))
			}
		}
	}

	test(NewBpMap(23))
}
Пример #6
0
func randstr(length int) types.String {
	return types.String(randslice(length))
}