Exemplo n.º 1
0
func (f binaryRecordFormatV0) writeOptimizedLink(w *rw.Writer, ide orient.OIdentifiable) (int, error) {
	// TODO: link = recursiveLinkSave(link)
	rid := ide.GetIdentity()
	if !rid.IsValid() {
		return 0, fmt.Errorf("cannot serialize invalid link")
	}
	n1, _ := w.WriteVarint(int64(rid.ClusterID))
	n2, _ := w.WriteVarint(int64(rid.ClusterPos))
	return n1 + n2, w.Err()
}
Exemplo n.º 2
0
func (f binaryRecordFormatV0) writeLinkMap(w *rw.Writer, o interface{}) error {
	m := o.(map[string]orient.OIdentifiable) // TODO: can use reflect to support map[Stringer]orient.OIdentifiable
	w.WriteVarint(int64(len(m)))
	for k, v := range m {
		// TODO @orient: check skip of complex types
		// FIXME @orient: changed to support only string key on map
		f.writeOType(w, orient.STRING)
		f.writeString(w, k)
		if v == nil {
			f.writeNullLink(w)
		} else {
			if _, err := f.writeOptimizedLink(w, v); err != nil {
				return err
			}
		}
	}
	return w.Err()
}
Exemplo n.º 3
0
func (f binaryRecordFormatV0) writeLinkCollection(w *rw.Writer, o interface{}) error {
	switch col := o.(type) {
	case []orient.RID:
		w.WriteVarint(int64(len(col)))
		for _, rid := range col {
			if rid == nilRID {
				f.writeNullLink(w)
			} else {
				if _, err := f.writeOptimizedLink(w, rid); err != nil {
					return err
				}
			}
		}
	case []orient.OIdentifiable:
		w.WriteVarint(int64(len(col)))
		for _, item := range col {
			if item.GetIdentity() == nilRID {
				f.writeNullLink(w)
			} else {
				if _, err := f.writeOptimizedLink(w, item); err != nil {
					return err
				}
			}
		}
	case orient.OIdentifiableCollection:
		// TODO: assert (!(value instanceof OMVRBTreeRIDSet))
		w.WriteVarint(int64(col.Len()))
		for item := range col.OIdentifiableIterator() {
			if item == nil {
				f.writeNullLink(w)
			} else {
				if _, err := f.writeOptimizedLink(w, item); err != nil {
					return err
				}
			}
		}
	default:
		panic(fmt.Errorf("not a link collection: %T", o))
	}
	return w.Err()
}
Exemplo n.º 4
0
func (f binaryRecordFormatV0) writeSingleValue(w *rw.Writer, off int, o interface{}, tp, linkedType orient.OType) (err error) {
	defer func() {
		if r := recover(); r != nil {
			if ic, ok := r.(*runtime.TypeAssertionError); ok {
				err = fmt.Errorf("writeSingleValue(%T -> %v): %v", o, tp, ic)
			} else {
				panic(r)
			}
		}
	}()
	switch tp {
	case orient.BYTE:
		w.WriteByte(toByte(o))
	case orient.BOOLEAN:
		w.WriteBool(toBool(o))
	case orient.SHORT:
		w.WriteVarint(int64(toInt16(o)))
	case orient.INTEGER:
		w.WriteVarint(int64(toInt32(o)))
	case orient.LONG:
		w.WriteVarint(int64(toInt64(o)))
	case orient.STRING:
		f.writeString(w, toString(o))
	case orient.FLOAT:
		w.WriteFloat(o.(float32))
	case orient.DOUBLE:
		w.WriteDouble(o.(float64))
	case orient.DATETIME: // unix time in milliseconds
		if t, ok := o.(int64); ok {
			w.WriteVarint(t)
		} else {
			t := o.(time.Time)
			it := t.Unix()*1000 + int64(t.Nanosecond())/1e6
			w.WriteVarint(it)
		}
	case orient.DATE:
		if t, ok := o.(int64); ok {
			w.WriteVarint(t)
		} else {
			t := o.(time.Time)
			it := t.Unix()*1000 + int64(t.Nanosecond())/1e6
			var offset int64
			// TODO: int offset = ODateHelper.getDatabaseTimeZone().getOffset(dateValue)
			w.WriteVarint((it + offset) / millisecPerDay)
		}
	case orient.EMBEDDED:
		var edoc *orient.Document
		switch d := o.(type) {
		case orient.Document:
			edoc = &d
		case *orient.Document:
			edoc = d
		case orient.DocumentSerializable:
			edoc, err = o.(orient.DocumentSerializable).ToDocument()
			if err != nil {
				return
			}
			edoc.SetField(documentSerializableClassName, edoc.ClassName()) // TODO: pass empty value as nil?
		default:
			edoc = orient.NewEmptyDocument()
			if err = edoc.From(o); err != nil {
				return err
			}
			// TODO: set classname of struct?
		}
		err = f.Serialize(edoc, w, off, false)
	case orient.EMBEDDEDSET, orient.EMBEDDEDLIST:
		err = f.writeEmbeddedCollection(w, off, o, linkedType)
	case orient.DECIMAL:
		f.writeDecimal(w, o)
	case orient.BINARY:
		_, err = f.writeBinary(w, o.([]byte))
	case orient.LINKSET, orient.LINKLIST:
		err = f.writeLinkCollection(w, o)
	case orient.LINK:
		_, err = f.writeOptimizedLink(w, o.(orient.OIdentifiable))
	case orient.LINKMAP:
		err = f.writeLinkMap(w, o)
	case orient.EMBEDDEDMAP:
		err = f.writeEmbeddedMap(w, off, o)
	case orient.LINKBAG:
		err = o.(*orient.RidBag).ToStream(w)
	case orient.CUSTOM:
		val := o.(orient.CustomSerializable)
		f.writeString(w, val.GetClassName())
		err = val.ToStream(w)
	case orient.TRANSIENT, orient.ANY:
	default:
		panic(fmt.Errorf("unknown type: %v", tp))
	}
	if err == nil {
		err = w.Err()
	}
	return
}
Exemplo n.º 5
0
func (f binaryRecordFormatV0) writeNullLink(w *rw.Writer) int {
	n1, _ := w.WriteVarint(int64(nilRID.ClusterID))
	n2, _ := w.WriteVarint(int64(nilRID.ClusterPos))
	return n1 + n2
}