1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package age
import (
"bytes"
"fmt"
"math/big"
"reflect"
)
// GTYPE representing entity types for AGE result data : Vertex, Edge, Path and SimpleEntity
type GTYPE uint8
const (
G_OTHER GTYPE = 1 + iota
G_VERTEX
G_EDGE
G_PATH
G_MAP_PATH
G_STR
G_INT
G_INTBIG
G_FLOAT
G_FLOATBIG
G_BOOL
G_NULL
G_MAP
G_ARR
)
var _TpV = reflect.TypeOf(&Vertex{})
var _TpE = reflect.TypeOf(&Edge{})
var _TpP = reflect.TypeOf(&Path{})
var _TpMP = reflect.TypeOf(&MapPath{})
var _TpStr = reflect.TypeOf(string(""))
var _TpInt = reflect.TypeOf(int64(0))
var _TpIntBig = reflect.TypeOf(big.NewInt(0))
var _TpFloat = reflect.TypeOf(float64(0))
var _TpFloatBig = reflect.TypeOf(big.NewFloat(0))
var _TpBool = reflect.TypeOf(bool(false))
var _TpMap = reflect.TypeOf(map[string]interface{}{})
var _TpArr = reflect.TypeOf([]interface{}{})
// Entity object interface for parsed AGE result data : Vertex, Edge, Path and SimpleEntity
type Entity interface {
GType() GTYPE
String() string
}
func IsEntity(v interface{}) bool {
_, ok := v.(Entity)
return ok
}
type SimpleEntity struct {
Entity
typ GTYPE
value interface{}
}
func NewSimpleEntity(value interface{}) *SimpleEntity {
if value == nil {
return &SimpleEntity{typ: G_NULL, value: nil}
}
switch value.(type) {
case string:
return &SimpleEntity{typ: G_STR, value: value}
case int64:
return &SimpleEntity{typ: G_INT, value: value}
case *big.Int:
return &SimpleEntity{typ: G_INTBIG, value: value}
case float64:
return &SimpleEntity{typ: G_FLOAT, value: value}
case *big.Float:
return &SimpleEntity{typ: G_FLOATBIG, value: value}
case bool:
return &SimpleEntity{typ: G_BOOL, value: value}
case map[string]interface{}:
return &SimpleEntity{typ: G_MAP, value: value}
case []interface{}:
return &SimpleEntity{typ: G_ARR, value: value}
default:
return &SimpleEntity{typ: G_OTHER, value: value}
}
}
func (e *SimpleEntity) GType() GTYPE {
return e.typ
}
func (e *SimpleEntity) IsNull() bool {
return e.value == nil
}
func (e *SimpleEntity) Value() interface{} {
return e.value
}
func (e *SimpleEntity) String() string {
return fmt.Sprintf("%v", e.value)
}
func (e *SimpleEntity) AsStr() string {
return e.value.(string)
}
func (e *SimpleEntity) AsInt() int {
return e.value.(int)
}
func (e *SimpleEntity) AsInt64() int64 {
return e.value.(int64)
}
func (e *SimpleEntity) AsBigInt() *big.Int {
return e.value.(*big.Int)
}
func (e *SimpleEntity) AsFloat() float64 {
return e.value.(float64)
}
func (e *SimpleEntity) AsBigFloat() *big.Float {
return e.value.(*big.Float)
}
func (e *SimpleEntity) AsBool() bool {
return e.value.(bool)
}
func (e *SimpleEntity) AsMap() map[string]interface{} {
return e.value.(map[string]interface{})
}
func (e *SimpleEntity) AsArr() []interface{} {
return e.value.([]interface{})
}
type LabeledEntity struct {
Entity
id int64
label string
props map[string]interface{}
}
func newLabeledEntity(id int64, label string, props map[string]interface{}) *LabeledEntity {
return &LabeledEntity{id: id, label: label, props: props}
}
func (n *LabeledEntity) Id() int64 {
return n.id
}
func (n *LabeledEntity) Label() string {
return n.label
}
func (n *LabeledEntity) Prop(key string) interface{} {
return n.props[key]
}
// return properties
func (n *LabeledEntity) Props() map[string]interface{} {
return n.props
}
type Vertex struct {
*LabeledEntity
}
func NewVertex(id int64, label string, props map[string]interface{}) *Vertex {
return &Vertex{newLabeledEntity(id, label, props)}
}
func (v *Vertex) GType() GTYPE {
return G_VERTEX
}
func (v *Vertex) String() string {
return fmt.Sprintf("V{id:%d, label:%s, props:%v}", v.id, v.label, v.props)
}
type Edge struct {
*LabeledEntity
start_id int64
end_id int64
}
func NewEdge(id int64, label string, start int64, end int64, props map[string]interface{}) *Edge {
return &Edge{LabeledEntity: newLabeledEntity(id, label, props), start_id: start, end_id: end}
}
func (e *Edge) GType() GTYPE {
return G_EDGE
}
func (e *Edge) StartId() int64 {
return e.start_id
}
func (e *Edge) EndId() int64 {
return e.end_id
}
func (e *Edge) String() string {
return fmt.Sprintf("E{id:%d, label:%s, start:%d, end:%d, props:%v}",
e.id, e.label, e.start_id, e.end_id, e.props)
}
type Path struct {
Entity
entities []Entity
}
func NewPath(entities []Entity) *Path {
return &Path{entities: entities}
}
func (e *Path) GType() GTYPE {
return G_PATH
}
func (e *Path) Size() int {
return len(e.entities)
}
func (e *Path) Get(index int) Entity {
if index < 0 && index >= len(e.entities) {
panic(fmt.Errorf("Entity index[%d] is out of range (%d) ", index, len(e.entities)))
}
return e.entities[index]
}
func (e *Path) GetAsVertex(index int) *Vertex {
v := e.Get(index)
if v.GType() != G_VERTEX {
panic(fmt.Errorf("Entity[%d] is not Vertex", index))
}
return v.(*Vertex)
}
func (e *Path) GetAsEdge(index int) *Edge {
v := e.Get(index)
if v.GType() != G_EDGE {
panic(fmt.Errorf("Entity[%d] is not Edge", index))
}
return v.(*Edge)
}
func (p *Path) String() string {
var buf bytes.Buffer
buf.WriteString("P[")
for _, e := range p.entities {
buf.WriteString(e.String())
buf.WriteString(",")
}
buf.WriteString("]")
return buf.String()
}
type MapPath struct {
Entity
entities []interface{}
}
func NewMapPath(entities []interface{}) *MapPath {
return &MapPath{entities: entities}
}
func (e *MapPath) GType() GTYPE {
return G_MAP_PATH
}
func (e *MapPath) Size() int {
return len(e.entities)
}
func (e *MapPath) Get(index int) interface{} {
if index < 0 && index >= len(e.entities) {
panic(fmt.Errorf("Entity index[%d] is out of range (%d) ", index, len(e.entities)))
}
return e.entities[index]
}
func (p *MapPath) String() string {
var buf bytes.Buffer
buf.WriteString("P[")
for _, e := range p.entities {
buf.WriteString(fmt.Sprintf("%v", e))
buf.WriteString(",")
}
buf.WriteString("]")
return buf.String()
}
|