Пример #1
0
func (fo *frameOpen) createRollClientTxn() (*cmsgs.ClientTxn, map[common.VarUUId]*common.Positions) {
	var origWrite *msgs.Action
	vUUIdBytes := fo.v.UUId[:]
	for idx, l := 0, fo.frameTxnActions.Len(); idx < l; idx++ {
		action := fo.frameTxnActions.At(idx)
		if bytes.Equal(action.VarId(), vUUIdBytes) {
			origWrite = &action
			break
		}
	}
	seg := capn.NewBuffer(nil)
	ctxn := cmsgs.NewClientTxn(seg)
	ctxn.SetRetry(false)
	actions := cmsgs.NewClientActionList(seg, 1)
	ctxn.SetActions(actions)
	action := actions.At(0)
	action.SetVarId(fo.v.UUId[:])
	action.SetRoll()
	roll := action.Roll()
	roll.SetVersion(fo.frameTxnId[:])
	var refs msgs.VarIdPos_List
	switch origWrite.Which() {
	case msgs.ACTION_WRITE:
		ow := origWrite.Write()
		roll.SetValue(ow.Value())
		refs = ow.References()
	case msgs.ACTION_READWRITE:
		owr := origWrite.Readwrite()
		roll.SetValue(owr.Value())
		refs = owr.References()
	case msgs.ACTION_CREATE:
		oc := origWrite.Create()
		roll.SetValue(oc.Value())
		refs = oc.References()
	case msgs.ACTION_ROLL:
		owr := origWrite.Roll()
		roll.SetValue(owr.Value())
		refs = owr.References()
	default:
		panic(fmt.Sprintf("%v unexpected action type when building roll: %v", fo.frame, origWrite.Which()))
	}
	posMap := make(map[common.VarUUId]*common.Positions)
	posMap[*fo.v.UUId] = fo.v.positions
	refVarList := seg.NewDataList(refs.Len())
	roll.SetReferences(refVarList)
	for idx, l := 0, refs.Len(); idx < l; idx++ {
		ref := refs.At(idx)
		vUUId := common.MakeVarUUId(ref.Id())
		pos := common.Positions(ref.Positions())
		posMap[*vUUId] = &pos
		refVarList.Set(idx, vUUId[:])
	}
	return &ctxn, posMap
}
Пример #2
0
func (s *store) LoadTopology() error {
	res, err := s.db.ReadonlyTransaction(func(rtxn *mdbs.RTxn) interface{} {
		bites, err := rtxn.Get(s.db.Vars, configuration.TopologyVarUUId[:])
		if err != nil {
			rtxn.Error(err)
			return nil
		}
		seg, _, err := capn.ReadFromMemoryZeroCopy(bites)
		if err != nil {
			rtxn.Error(err)
			return nil
		}
		varCap := msgs.ReadRootVar(seg)
		txnId := common.MakeTxnId(varCap.WriteTxnId())
		bites = s.db.ReadTxnBytesFromDisk(rtxn, txnId)
		if bites == nil {
			rtxn.Error(fmt.Errorf("Unable to find txn for topology: %v", txnId))
			return nil
		}
		seg, _, err = capn.ReadFromMemoryZeroCopy(bites)
		if err != nil {
			rtxn.Error(err)
			return nil
		}
		txnCap := msgs.ReadRootTxn(seg)
		actions := txnCap.Actions()
		if actions.Len() != 1 {
			rtxn.Error(fmt.Errorf("Topology txn has %v actions; expected 1", actions.Len()))
			return nil
		}
		action := actions.At(0)
		var refs msgs.VarIdPos_List
		switch action.Which() {
		case msgs.ACTION_WRITE:
			w := action.Write()
			bites = w.Value()
			refs = w.References()
		case msgs.ACTION_READWRITE:
			rw := action.Readwrite()
			bites = rw.Value()
			refs = rw.References()
		case msgs.ACTION_CREATE:
			c := action.Create()
			bites = c.Value()
			refs = c.References()
		default:
			rtxn.Error(fmt.Errorf("Expected topology txn action to be w, rw, or c; found %v", action.Which()))
			return nil
		}

		if refs.Len() != 1 {
			rtxn.Error(fmt.Errorf("Topology txn action has %v references; expected 1", refs.Len()))
			return nil
		}
		rootRef := refs.At(0)

		seg, _, err = capn.ReadFromMemoryZeroCopy(bites)
		if err != nil {
			rtxn.Error(err)
			return nil
		}
		topology, err := configuration.TopologyFromCap(txnId, &rootRef, bites)
		if err != nil {
			rtxn.Error(err)
			return nil
		}
		return topology
	}).ResultError()
	if err != nil {
		return err
	}
	s.topology = res.(*configuration.Topology)
	return nil
}