// // writeDateTime takes an interface{} value that must be of type: // - time.Time // - int or int64, representing milliseconds since Epoch // // NOTE: format for OrientDB DATETIME: // Golang formatted date: 2006-01-02 03:04:05 // Example: 2014-11-25 09:14:54 // // OrientDB server converts a DATETIME type to millisecond unix epoch and // stores it as the type LONG. It is written as a varint long to the // obuf.WriteBuf passed in. // func writeDateTime(buf *obuf.WriteBuf, value interface{}) error { var millisEpoch int64 switch value.(type) { case int: millisEpoch = int64(value.(int)) case int64: millisEpoch = value.(int64) case time.Time: // UnixNano returns t as a Unix time, the number of nanoseconds elapsed // since January 1, 1970 UTC. tm := value.(time.Time) tt := tm.Round(time.Millisecond) millisEpoch = tt.UnixNano() / (1000 * 1000) default: return oerror.ErrDataTypeMismatch{ ExpectedDataType: oschema.DATETIME, ExpectedGoType: "time.Time | int64 | int", ActualValue: value, } } err := varint.EncodeAndWriteVarInt64(buf, millisEpoch) if err != nil { return oerror.NewTrace(err) } return nil }
// // +-----------------+-----------------+ // |clusterId:varint | recordId:varInt | // +-----------------+-----------------+ // func (serde ORecordSerializerV0) writeLink(buf *obuf.WriteBuf, lnk *oschema.OLink) error { err := varint.EncodeAndWriteVarInt32(buf, int32(lnk.RID.ClusterID)) if err != nil { return oerror.NewTrace(err) } err = varint.EncodeAndWriteVarInt64(buf, lnk.RID.ClusterPos) if err != nil { return oerror.NewTrace(err) } return nil }
// // writeDateTime takes an interface{} value that must be of type time.Time // // NOTE: format for OrientDB DATETIME: // Golang formatted date: 2006-01-02 03:04:05 // Example: 2014-11-25 09:14:54 // // OrientDB server converts a DATETIME type to millisecond unix epoch and // stores it as the type LONG. It is written as a varint long to the // obuf.WriteBuf passed in. // // From the OrientDB schemaless serialization spec on DATE: // The date is converted to second unix epoch,moved at midnight UTC+0, // divided by 86400(seconds in a day) and stored as the type LONG // func writeDate(buf *obuf.WriteBuf, value interface{}) error { tm, ok := value.(time.Time) if !ok { return oerror.ErrDataTypeMismatch{ ExpectedDataType: oschema.DATE, ExpectedGoType: "time.Time", ActualValue: value, } } tmMidnightUTC := time.Date(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0, time.FixedZone("UTC", 0)) secondsEpoch := tmMidnightUTC.Unix() dateAfterDiv := secondsEpoch / int64(86400) err := varint.EncodeAndWriteVarInt64(buf, dateAfterDiv) if err != nil { return oerror.NewTrace(err) } return nil }
// // writeDataValue is part of the Serialize functionality // TODO: change name to writeSingleValue ? // func (serde ORecordSerializerV0) writeDataValue(buf *obuf.WriteBuf, value interface{}, datatype oschema.ODataType) (err error) { switch datatype { case oschema.STRING: err = varint.WriteString(buf, value.(string)) ogl.Debugf("DEBUG STR: -writeDataVal val: %v\n", value.(string)) // DEBUG case oschema.BOOLEAN: err = rw.WriteBool(buf, value.(bool)) ogl.Debugf("DEBUG BOOL: -writeDataVal val: %v\n", value.(bool)) // DEBUG case oschema.INTEGER: var i32val int32 i32val, err = toInt32(value) if err == nil { err = varint.EncodeAndWriteVarInt32(buf, i32val) // TODO: are serialized integers ALWAYS varint encoded? ogl.Debugf("DEBUG INT: -writeDataVal val: %v\n", i32val) // DEBUG } case oschema.SHORT: // TODO: needs toInt16 conversion fn err = varint.EncodeAndWriteVarInt32(buf, int32(value.(int16))) ogl.Debugf("DEBUG SHORT: -writeDataVal val: %v\n", value.(int16)) // DEBUG case oschema.LONG: var i64val int64 i64val, err = toInt64(value) if err == nil { err = varint.EncodeAndWriteVarInt64(buf, i64val) // TODO: are serialized longs ALWAYS varint encoded? ogl.Debugf("DEBUG LONG: -writeDataVal val: %v\n", i64val) // DEBUG } case oschema.FLOAT: var f32 float32 f32, err = toFloat32(value) if err == nil { err = rw.WriteFloat(buf, f32) } ogl.Debugf("DEBUG FLOAT: -writeDataVal val: %v\n", value) // DEBUG case oschema.DOUBLE: var f64 float64 f64, err = toFloat64(value) if err == nil { err = rw.WriteDouble(buf, f64) } ogl.Debugf("DEBUG DOUBLE: -writeDataVal val: %v\n", value.(float64)) // DEBUG case oschema.DATETIME: err = writeDateTime(buf, value) ogl.Debugf("DEBUG DATETIME: -writeDataVal val: %v\n", value) // DEBUG case oschema.DATE: err = writeDate(buf, value) ogl.Debugf("DEBUG DATE: -writeDataVal val: %v\n", value) // DEBUG case oschema.BINARY: err = varint.WriteBytes(buf, value.([]byte)) ogl.Debugf("DEBUG BINARY: -writeDataVal val: %v\n", value.([]byte)) // DEBUG case oschema.EMBEDDED: err = serde.serializeDocument(buf, value.(*oschema.ODocument)) ogl.Debugf("DEBUG EMBEDDED: -writeDataVal val: %v\n", value) // DEBUG case oschema.EMBEDDEDLIST: err = serde.serializeEmbeddedCollection(buf, value.(oschema.OEmbeddedList)) ogl.Debugf("DEBUG EMBD-LIST: -writeDataVal val: %v\n", value) // DEBUG case oschema.EMBEDDEDSET: err = serde.serializeEmbeddedCollection(buf, value.(oschema.OEmbeddedList)) ogl.Debugf("DEBUG EMBD-SET: -writeDataVal val: %v\n", value) // DEBUG case oschema.EMBEDDEDMAP: err = serde.writeEmbeddedMap(buf, value.(oschema.OEmbeddedMap)) ogl.Debugf("DEBUG EMBEDDEDMAP: val %v\n", value.(oschema.OEmbeddedMap)) case oschema.LINK: err = serde.writeLink(buf, value.(*oschema.OLink)) ogl.Debugf("DEBUG LINK: val %v\n", value) // DEBUG case oschema.LINKLIST: err = serde.writeLinkList(buf, value.([]*oschema.OLink)) ogl.Debugf("DEBUG LINKLIST: val %v\n", value) // DEBUG case oschema.LINKSET: err = serde.writeLinkList(buf, value.([]*oschema.OLink)) ogl.Debugf("DEBUG LINKSET: val %v\n", value) // DEBUG case oschema.LINKMAP: err = serde.writeLinkMap(buf, value.(map[string]*oschema.OLink)) ogl.Debugf("DEBUG LINKMAP: val %v\n", value) // DEBUG case oschema.BYTE: err = rw.WriteByte(buf, value.(byte)) ogl.Debugf("DEBUG BYTE: -writeDataVal val: %v\n", value.(byte)) // DEBUG case oschema.DECIMAL: // TODO: impl me -> Java client uses BigDecimal for this panic("ORecordSerializerV0#writeDataValue DECIMAL NOT YET IMPLEMENTED") case oschema.CUSTOM: // TODO: impl me panic("ORecordSerializerV0#writeDataValue CUSTOM NOT YET IMPLEMENTED") case oschema.LINKBAG: panic("ORecordSerializerV0#writeDataValue LINKBAG NOT YET IMPLEMENTED") default: // ANY and TRANSIENT are do nothing ops } return err }