Titan: has() doesn't work - titan

I have been trying to query my graph using this query
graph()
.traversal()
.V()
.has(Tokens.FIRSTNAME)
.filter(vt ->
vt.get().property(Tokens.FIRSTNAME).equals("Hank")
);
And that always return no result. When I debugged the code, I noticed that the comparison happens at AbstractElement#equals() and it returns false because the passed value (String) is not Vertex or Edge or VertexProperty
Another way to do the same thing but never works too is
graph()
.traversal()
.V()
.has(Tokens.FIRSTNAME, "Hank")
I'm using titan 0.9.0-M2
Am I doing something wrong?

The query should be:
graph()
.traversal()
.V()
.has(Tokens.FIRSTNAME)
.filter(vt ->
vt.get().value(Tokens.FIRSTNAME).equals("Hank")
);
Or even better:
graph()
.traversal()
.V()
.has(Tokens.FIRSTNAME, "Hank")

Related

Call a PostgreSQL function and get result back with no loop

I have a simple rust program that interacts with a PostgreSQL database.
The actual code is:
for row in &db_client.query("select magic_value from priv.magic_value();", &[]).unwrap()
{
magic_value = row.get("magic_value");
println!("Magic value is = {}", magic_value);
}
And.. it works. But I don't like it: I know this function will return one and only one value.
From the example I found, for example here: https://docs.rs/postgres/latest/postgres/index.html
and here: https://tms-dev-blog.com/postgresql-database-with-rust-how-to/
You always have a recordset to loop on.
Which is the clean way to call a function without looping?
query returns a Result<Vec<Row>, _>. You are already unwrapping the Vec, so you can just use it directly instead of looping. By turning the Vec into an owning iterator yourself, you can even easily obtain a Row instead of a &Row.
magic_value = db_client.query("select magic_value from priv.magic_value();", &[])
.unwrap() // -> Vec<Row>
.into_iter() // -> impl Iterator<Item=Row>
.next() // -> Option<Row>
.unwrap() // -> Row
.get("magic_value");

MongoDB countDocuments() is returning an object, not a number

I'm starting to learn about mongoose/MongoDB aggregation functions, and am having some basic difficulties. For example, I'm trying to do the following:
var myModels= require('./models/myModel');
var myCount = myModels.countDocuments({userID: "A"});
console.log(myCount );
I just want to count the number of documents with userID of "A" but when this prints to the console, it's printing as a whole object, instead of just a numerical count. I've read the answer here but I'm still not able to solve this problem (also, is there a way, unlike in that question, to return the count directly rather than having to predefine a variable and set it in a callback function?)
I'm trying to follow the guide here and don't see where I'm going wrong.
It's because the return value of countDocuments is a promise and not a number.
You either need to wait for that Promise or use callback syntax like so:
var myModels= require('./models/myModel');
// this required the code to be inside an async function
var myCount = await myModels.countDocuments({userID: "A"});
console.log(myCount);
Or:
var myModels= require('./models/myModel');
myModels.countDocuments({userID: "A"})
.then((myCount) =>{console.log(myCount);});

Retrieving single property value in Gremlin query closure works, but retrieving valueMap() fails yields Exception

Using the Titan 1.0.0 Gremlin shell I can retrieve a single property value from an edge from within a closure. But trying to access the valueMap() fails with an exception.
Works:
gremlin> t.E().hasLabel("TRUSTS").has('NOT_VALID_BEFORE').each( { trustEdge -> t.E().has('EDGE_GROUP_ID', trustEdge.value('EDGE_GROUP_ID')).hasNot('NOT_VALID_BEFORE').each({println it.value('EDGE_ID')}) } )
Yields exception (only difference is in the right most closure 'it.valueMap()' vs 'it.value('..')'):
gremlin> t.E().hasLabel("TRUSTS").has('NOT_VALID_BEFORE').each( { trustEdge -> t.E().has('EDGE_GROUP_ID', trustEdge.value('EDGE_GROUP_ID')).hasNot('NOT_VALID_BEFORE').each( { println it.valueMap() } ) } )
No signature of method: com.thinkaurelius.titan.graphdb.relations.StandardEdge.valueMap() is applicable for argument types: () values: []
Possible solutions: value(java.lang.String)
Display stack trace? [yN]
gremlin>
But it is not that in general I would be unable to get to the valueMap of the edge:
gremlin> t.E().hasLabel("TRUSTS").has('NOT_VALID_BEFORE').each( { trustEdge -> t.E().has('EDGE_GROUP_ID', trustEdge.value('EDGE_GROUP_ID')).hasNot('NOT_VALID_BEFORE').each( { println it } ) } )
e[215rmh-oe094-1d05-9i0][40964296-MANAGED->12312]
gremlin> t.E('215rmh-oe094-1d05-9i0').valueMap()
==>[MANAGE_INFORM:false, NOT_VALID_AFTER:1669873006000, MANAGE_MANAGERS:false, MANAGE_AUTHENTICATION_MEANS:true, CREATED_AT:1487683094863, RELATIONSHIP_ROLE:FAMILY_DOCTOR, MANAGE_TRUST:true, UPDATED_AT:1487683094915, MANAGE_REPRESENTATION:false, EDGE_ID:122881049, VERIFIED:true, EDGE_GROUP_ID:122881049]
Is this a bug or am I doing something wrong here?
A little context, just in case the query does not even do what I think it does:
What I think I am doing here is looking up all edges with label "TRUSTS" that have a property NOT_VALID_BEFORE. For each of those edges I look up all edges that share the same edge group ID value and check if they also have a property NOT_VALID_BEFORE, printing those to the console that do not have the property set.
When you start iterating with each(), every item you manipulate inside the closure is "off the traversal" -- that is, you're working with an Edge object, not an GraphTraversal object.
Edge has a value() method similar to the GraphTraversal, but it does not have a valueMap() method. You could use ElementHelper.propertyValueMap() instead.
Here's a quick example:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.E().valueMap()
==>[weight:0.5]
==>[weight:1.0]
==>[weight:0.4]
==>[weight:1.0]
==>[weight:0.4]
==>[weight:0.2]
gremlin> g.E().each{ edge -> println ElementHelper.propertyValueMap(edge) };[]
[weight:0.5]
[weight:1.0]
[weight:0.4]
[weight:1.0]
[weight:0.4]
[weight:0.2]

NHibernate: Can't Select after Skip Take In Certain Scenario

For some reason I am unable to use Select() after a Skip()/Take() unless I do this in a certain way. The following code works and allows me to use result as part of a sub query.
var query = QueryOver.Of<MyType>();
query.Skip(1);
var result = query.Select(myType => myType.Id);
However, if I attempt to create the query on one line as below I can't compile.
var query = QueryOver.Of<MyType>().Skip(1);
var result = query.Select(myType => myType.Id);
It looks like the code in the first results in query being of type QueryOver< MyType, MyType> while the second results in query being of type QueryOver< MyType>.
It also works if written like this.
var query = QueryOver.Of<MyType>().Select(myType => myType.Id).Skip(1);
Any ideas why the second version fails horribly when the first and third versions work? It seems like odd behavior.
You have a typo in the second version...
var query = QueryOver.Of<MyType().Skip(1);
is missing the >
var query = QueryOver.Of<MyType>().Skip(1);
Not sure if thats what you where looking for.

What would be the opposite to hasFields?

I'm using logical deletes by adding a field deletedAt. If I want to get only the deleted documents it would be something like r.table('clients').hasFields('deletedAt'). My method has a withDeletes parameter which determines if deleted documents are excluded or not.
Finally, people at the #rethinkdb IRC channel suggested me to use the filter method and that did the trick:
query = adapter.table(table).filter(filters)
if withDeleted
query = adapter.filter (doc) ->
return doc.hasFields 'deletedAt'
else
query = adapter.filter (doc) ->
return doc.hasFields('deletedAt').not()
query.run connection, (err, results) ->
...
My question is why do I have to use filter and not something like:
query = adapter.table(table).filter(filters)
query = if withDeleted then query.hasFields 'deletedAt' else query.hasFields('deletedAt').not()
...
or something like that.
Thanks in advance.
The hasFields function can be called on both objects and sequences, but not cannot.
This query:
query.hasFields('deletedAt')
Behaves the same as this one (on sequences of objects):
query.filter((doc) -> return doc.hasFields('deletedAt'))
However, this query:
query.hasFields('deletedAt').not()
Behaves like this:
query.filter((doc) -> return doc.hasFields('deletedAt')).not()
But that doesn't make sense. you want the not to be inside the filter, not after it. Like this:
query.filter((doc) -> return doc.hasFields('deletedAt').not())
One nice that about RethinkDB is that because of the way queries are built up in host language it's very easy to define new fluent syntax by just defining functions in your language. For example if you wanted to have a lacksFields function you could define it in Python (sorry I don't really know coffeescript) like so:
def lacks_fields(stream, *args):
res = stream
for arg in args:
res = res.filter(lambda x: ~x.has_fields(arg))
return res
Then you can use a nice fluent syntax like:
lacks_fields(stream, "foo", "bar", "buzz")