Example #1
0
func ExampleIts() {
	var its []x.Instruction

	for t := 0; t < 10; t++ {
		var i x.Instruction
		i.NanoTs = int64(100 - t)
		its = append(its, i)
	}

	sort.Sort(x.Its(its))
	fmt.Println(its[0].NanoTs)
	// Output: 91
}
Example #2
0
func (l *Leveldb) GetEntity(_, id string) (result []x.Instruction, rerr error) {
	slice := util.BytesPrefix([]byte(id))
	iter := l.db.NewIterator(slice, nil)
	for iter.Next() {
		buf := iter.Value()
		if buf == nil {
			break
		}
		var i x.Instruction
		if err := i.GobDecode(buf); err != nil {
			x.LogErr(log, err).Error("While decoding")
			return result, err
		}
		result = append(result, i)
	}
	iter.Release()
	err := iter.Error()
	if err != nil {
		x.LogErr(log, err).Error("While iterating")
	}
	return result, err
}
Example #3
0
func (l *Leveldb) Iterate(fromId string, num int,
	ch chan x.Entity) (rnum int, rlast x.Entity, rerr error) {
	slice := util.Range{Start: []byte(fromId)}
	iter := l.db.NewIterator(&slice, nil)

	rnum = 0
	handled := make(map[x.Entity]bool)
	for iter.Next() {
		buf := iter.Value()
		if buf == nil {
			break
		}
		var i x.Instruction
		if err := i.GobDecode(buf); err != nil {
			x.LogErr(log, err).Error("While decoding")
			return rnum, rlast, err
		}
		e := x.Entity{Kind: i.SubjectType, Id: i.SubjectId}
		rlast = e
		if _, present := handled[e]; present {
			continue
		}
		ch <- e
		handled[e] = true
		rnum += 1
		if rnum >= num {
			break
		}
	}
	iter.Release()
	err := iter.Error()
	if err != nil {
		x.LogErr(log, err).Error("While iterating")
	}
	return rnum, rlast, err
}
Example #4
0
func ExampleInstruction() {
	var i x.Instruction
	i.SubjectId = "sid"
	i.SubjectType = "stype"

	b, err := i.GobEncode()
	if err != nil {
		panic(err)
	}

	var o x.Instruction
	if err := o.GobDecode(b); err != nil {
		panic(err)
	}

	fmt.Println(o.SubjectId)
	fmt.Println(o.SubjectType)
	// Output:
	// sid
	// stype
}
Example #5
0
func (n *Update) doExecute(c *req.Context, its *[]*x.Instruction) error {
	for pred, val := range n.edges {
		if len(n.source) == 0 {
			return errors.New(fmt.Sprintf(
				"No source specified for id: %v kind: %v", n.id, n.kind))
		}

		i := new(x.Instruction)
		i.SubjectId = n.id
		i.SubjectType = n.kind
		i.Predicate = pred

		if b, err := json.Marshal(val); err != nil {
			return err
		} else {
			i.Object = b
		}
		i.Source = n.source
		i.NanoTs = n.NanoTs
		log.WithField("instruction", i).Debug("Pushing to list")
		*its = append(*its, i)
	}

	if len(n.children) == 0 {
		return nil
	}
	if len(n.source) == 0 {
		return errors.New(fmt.Sprintf(
			"No source specified for id: %v kind: %v", n.id, n.kind))
	}

	// Children can only be added, not deleted via API. But they can be stopped
	// from being retrieved.
	// Scenario: How do I stop childA from being retrieved?
	// Answer:
	// Modify child by adding a 'deleted' edge
	// Get(ChildKind, ChildId).Set("deleted", true).Execute(c)
	//
	// Then for retrieval from parent:
	// NewQuery(ParentKind, ParentId).Collect(ChildKind).FilterOut("deleted")
	// This would remove all children with a 'deleted' edge.
	// Better still
	// Get(ChildKind, ChildId).MarkDeleted().Execute(c)
	// would automatically filter out that child and it's children
	// from being retrieved.

	for _, child := range n.children {
		if len(child.id) > 0 {
			log.WithField("child_id", child.id).Fatal(
				"Child id should be empty for all current use cases")
			return errors.New("Non empty child id")
		}

		for idx := 0; ; idx++ { // Retry loop.
			child.id = x.UniqueString(c.NumCharsUnique)
			log.WithField("id", child.id).Debug("Checking availability of new id")
			if isnew := Get().IsNew(child.id); isnew {
				log.WithField("id", child.id).Debug("New id available")
				break
			}
			if idx >= 30 {
				return errors.New("Unable to find new id")
			}
		}
		// Create edge from parent to child
		i := new(x.Instruction)
		i.SubjectId = n.id
		i.SubjectType = n.kind
		i.Predicate = child.kind
		i.ObjectId = child.id
		i.Source = n.source
		i.NanoTs = n.NanoTs
		log.WithField("instruction", i).Debug("Pushing to list")
		*its = append(*its, i)

		// Create edge from child to parent
		i = new(x.Instruction)
		i.SubjectId = child.id
		i.SubjectType = child.kind
		i.Predicate = "_parent_"
		i.ObjectId = n.id
		i.Source = n.source
		i.NanoTs = n.NanoTs
		log.WithField("instruction", i).Debug("Pushing to list")
		*its = append(*its, i)

		if err := child.doExecute(c, its); err != nil {
			return err
		}
	}
	return nil
}