ConstraintViolationException when create node using UniqueNodeFactory - neo4j

this test fails with error and I don't understand why..
I thought that UniqueNodeFactory create node only if it's not exist.
Of course I can do the same using Cypher but I want to understand what happens here..
Could somebody explain?
I am using neo4j 2.3.1.
public class SimpleTest {
private GraphDatabaseService graphService;
#Before
public void setUp() throws Exception {
graphService = new TestGraphDatabaseFactory().newImpermanentDatabase();
graphService.execute("CREATE CONSTRAINT ON (user:User) ASSERT user.userId IS UNIQUE");
}
#After
public void tearDown() throws Exception {
graphService.shutdown();
}
public static UniqueFactory.UniqueNodeFactory createUserFactory(GraphDatabaseService graphDatabaseService) {
return new UniqueFactory.UniqueNodeFactory(graphDatabaseService, "User") {
#Override
protected void initialize(Node created, Map<String, Object> properties) {
created.addLabel(DynamicLabel.label("User"));
created.setProperty("userId", properties.get("userId"));
}
};
}
#Test
public void testCreateUser() throws Exception {
try (Transaction tx = graphService.beginTx()) {
Node node = graphService.createNode(DynamicLabel.label("User"));
node.setProperty("userId", 100L);
tx.success();
}
try (Transaction tx = graphService.beginTx()) {
UniqueFactory.UniqueNodeFactory uniqueFactory = createUserFactory(graphService);
uniqueFactory.getOrCreate("userId", 100L);
tx.success();
}
}
}
error :
Caused by: org.neo4j.kernel.api.exceptions.schema.UniquePropertyConstraintViolationKernelException: Node 0 already exists with label 0 and property 0=100
at org.neo4j.kernel.impl.api.ConstraintEnforcingEntityOperations.validateNoExistingNodeWithLabelAndProperty(ConstraintEnforcingEntityOperations.java:165)
at org.neo4j.kernel.impl.api.ConstraintEnforcingEntityOperations.nodeSetProperty(ConstraintEnforcingEntityOperations.java:140)
at org.neo4j.kernel.impl.api.LockingStatementOperations.nodeSetProperty(LockingStatementOperations.java:453)
at org.neo4j.kernel.impl.api.OperationsFacade.nodeSetProperty(OperationsFacade.java:896)
at org.neo4j.kernel.impl.core.NodeProxy.setProperty(NodeProxy.java:293)
... 33 more

Ok, now I see the reason. I created schema index with Cypher and UniqueNodeFactory use legacy index. To fix the problem you need to create nodes only using UniqueNodeFactory.

Related

variable depth persistence and relationships of type Set

I have the following relationships
Diagram -has-many- Connection
Connection -has-many MoveablePoints
I am using a neo4j omg repository interface v3.1.3 of neo4j to persist addition and removal of connections and moveable points. My unit tests are working fine, however if I use it in a web environment, the removal of the connections is not being applied.
I am using a variable depth persistence of 5. I feel this has something to do with the omg session but I am not sure. Any ideas would be welcome.
service code
#Transactional
#Override
#Retryable(value = TransientException.class,exceptionExpression="#{message.contains('RWLock')}", maxAttempts = 5)
public Diagram update(Diagram diagram) throws GUMLException {
// for (Connection connection : diagram.getConnections())
// if (connection.getId() != null)
// connectionService.deleteMoveablePoints(connection.getId());
//
// for (DiagramElement de : diagram.getDiagramElements()) {
// diagramElementService.save(de);
// }
return umlDiagramRepository.save(diagram, 5);
}
domain classes
#NodeEntity
public class Diagram {
public Set<Connection> getConnections() {
if (connections == null)
connections = new HashSet<Connection>();
return connections;
}
public void setConnections(Set<Connection> connections) {
this.connections = connections;
}
#org.neo4j.ogm.annotation.Relationship(type = "HasConnections", direction = org.neo4j.ogm.annotation.Relationship.OUTGOING)
Set<Connection> connections;
}
#NodeEntity
public class Connection implements IConnector {
public Set<MoveablePoint> getMoveablePoints() {
return moveablePoints;
}
public void setMoveablePoints(Set<MoveablePoint> moveablePoints) {
this.moveablePoints = moveablePoints;
}
#org.neo4j.ogm.annotation.Relationship(type = "HasMoveablePoints", direction = org.neo4j.ogm.annotation.Relationship.OUTGOING)
private Set<MoveablePoint> moveablePoints;
}
When I change my service code to retrieve the object from the session first and then overwrite, it seems to work. This does not seem right to me though.
Diagram d2 = umlDiagramRepository.findById(diagram.getId()).get();
d2 = diagram;
return umlDiagramRepository.save(d2, 5);

Getting "unknown function" for inhouse developed user defined function

I'm trying to test user defined functions in neo4j 3.1. So I wrote this:
public class Udf
{
#Context
public GraphDatabaseService db;
#Context
public Log log;
#UserFunction("test.id")
public Long id(#Name("node") Node node)
{
return node.getId();
}
}
and a test function like this:
public class UdfTest
{
#Rule
public Neo4jRule neo4j = new Neo4jRule()
.withProcedure(Udf.class);
#Test
public void shouldBeAbleToExtractIdProperty() throws Throwable
{
try (Driver driver = GraphDatabase.driver(neo4j.boltURI() , Config.build().withEncryptionLevel(Config.EncryptionLevel.NONE).toConfig()))
{
Session session = driver.session();
long nodeId = session.run("CREATE (p) RETURN test.id(p)")
.single()
.get(0).asLong();
assertEquals(nodeId, 0);
}
}
}
And when I run the test it prompts:
org.neo4j.driver.v1.exceptions.ClientException: Unknown function 'test.id' (line 1, column 19 (offset: 18))
"CREATE (p) RETURN test.id(p)"
^
When I change the #UserFunction to #Procedure and a bunch of other changes, I can call the exact same method using CALL .. YIELD clause.
Can someone please tell me what I'm doing wrong?
You are using the withProdcedure method instead of the withFunction method on Neo4jRule in your test. Change that line to:
#Rule
public Neo4jRule neo4j = new Neo4jRule()
.withFunction(Udf.class);

Chaining InputOperations in Rhino-Etl

I've just recently started using Rhino-Etl for very simple ETL processes and have had great success with it. I have a slightly more complicated scenario to address now and I didn't find the ConventionInputCommandOperation behaving the way I expected.
I've done up a very simplified example of what I'm trying to do. Basically I have two systems involved and I don't know what I want to get from system 2 until I first query system 1. I thought registering an InputOperation immediately after another InputOperation would behave like a loop. So that each row in operation 1 would be fed to operation 2. The below code fails with "Failed to execute operation DetailReader: Must declare the scalar variable #PlanetAbbrv." So my question is how are you meant to handle situations where the input operation is dependent a previous input operation?
Thanks,
Brian
using System;
using Rhino.Etl.Core;
using Rhino.Etl.Core.ConventionOperations;
namespace ETLTest
{
class Program
{
static void Main()
{
new MainProcess().Execute();
Console.ReadLine();
}
}
public class MainProcess : EtlProcess
{
protected override void Initialize()
{
Register(new MainReader());
Register(new DetailReader());
}
protected override void PostProcessing()
{
foreach (var exception in GetAllErrors())
{
throw exception;
}
}
}
public class MainReader : ConventionInputCommandOperation
{
public MainReader() : base("Galactic1")
{
Command = #"select * from Planet";
}
}
public class DetailReader : ConventionInputCommandOperation
{
public DetailReader() : base("Galactic2")
{
Command = #"select * from Delivery where DeliveryPlanetAbbrv = #PlanetAbbrv";
}
}
}
You'll need to have your DetailReader select all rows (take out the where operation).
Then use a JoinOperation to match the details to the main information.
Register(new JoinPlanets()
.Right(new MainReader())
.Left(new DetailReader()));
public class JoinPlanets: JoinOperation
{
protected override Row MergeRows(Row leftRow, Row rightRow)
{
Row row = leftRow.Clone();
foreach (var column in leftRow.Columns)
row[column] = leftRow[column];
return row;
}
protected override void SetupJoinConditions()
{
FullOuterJoin.Left("PlanetAbbrv")
.Right("DeliveryPlanetAbbrv");
}
}

Can't recreate just deleted node with UniqueNodeFactory

I just created a node with UniqueNodeFactory and his relationship with UniqueRelationshipFactory. I deleted the the node with the NeoEclipse and then I tried to recreate the same node and I get no exception and the node it's not recreated again. Anyone knows this is happening?
public Node getOrCreateNodeWithUniqueFactory(final Index<Node> nodeIndex, final String indexableKey,final String indexableValue) {
UniqueFactory<Node> factory = new UniqueFactory.UniqueNodeFactory( Global.graphDB.getGraphDbService(), nodeIndex.getName())
{
#Override
protected void initialize(Node created, Map<String, Object> properties) {
created.setProperty(indexableKey, properties.get(indexableKey));
}
};
return factory.getOrCreate( indexableKey, indexableValue );
}
public Relationship getOrCreateRelationshipTypeWithUniqueFactory(Index<Relationship> index, String indexableKey, final String indexableValue,
final RelationshipType type, final Node start, final Node end) {
UniqueFactory<Relationship> factory = new UniqueFactory.UniqueRelationshipFactory(index) {
#Override
protected Relationship create(Map<String, Object> properties) {
Relationship r = start.createRelationshipTo(end, type);
return r;
}
};
return factory.getOrCreate(indexableKey, indexableValue);
}
I can't reproduce your issue. What I get is that there's a new node created the second time around. But it would help with the full source code. And also try with the method "getOrCreateWithOutcome" on UniqueNodeFactory to see whether it was created or not.

db4o Tranparent Persistence doesn't store later objects in my own ActivatableCollection<T>

I'm rolling my own ActivatableCollection<T> for db4o but cribbing heavily from the builtin ActivatableList<T> implementation. I'm running into the problem where transparent persistence doesn't seem to be working correctly. In the test code below:
[Fact]
void CanStoreActivatableCollection()
{
var planets = new ActivatableCollection<Planet>();
var pagingMemoryStorage = new PagingMemoryStorage();
var config = Db4oEmbedded.NewConfiguration();
config.Common.Add(new TransparentActivationSupport());
config.Common.Add(new TransparentPersistenceSupport());
config.File.Storage = pagingMemoryStorage;
var objectContainer = Db4oEmbedded.OpenFile(config, "Memory.yap");
planets.Add(new Planet("Mercury"));
objectContainer.Store(planets);
planets.Add(new Planet("Venus"));
planets.Add(new Planet("Earth"));
objectContainer.Commit();
objectContainer.Close();
config = Db4oEmbedded.NewConfiguration();
config.Common.Add(new TransparentActivationSupport());
config.Common.Add(new TransparentPersistenceSupport());
config.File.Storage = pagingMemoryStorage;
objectContainer = Db4oEmbedded.OpenFile(config, "Memory.yap");
planets = objectContainer.Query<ActivatableCollection<Planet>>().FirstOrDefault();
Assert.NotNull(planets);
Assert.Equal(3, planets.Count);
objectContainer.Close();
}
The planet "Mercury" is stored, but not "Venus" and "Earth". If I change from ActivatableCollection to ActivatableList, then all 3 planets are stored.
What am I missing? My ActivatableCollection is just minimal implementation of ActivatableList as best as I can tell.
Below is my implementation of ActivatableCollection:
public class ActivatableCollection<T>
: ICollection<T>
, IActivatable
, INotifyCollectionChanged
{
List<T> _list;
List<T> List
{
get
{
if (_list == null)
_list = new List<T>();
return _list;
}
}
public ActivatableCollection()
{
}
public int Count
{
get
{
ActivateForRead();
return List.Count;
}
}
public bool IsReadOnly
{
get
{
ActivateForRead();
return ((IList) List).IsReadOnly;
}
}
public void Add(T t)
{
ActivateForWrite();
List.Add(t);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, t));
}
public void Clear()
{
ActivateForWrite();
List.Clear();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public bool Contains(T t)
{
ActivateForRead();
return List.Contains(t);
}
public void CopyTo(T[] array, int index)
{
ActivateForRead();
List.CopyTo(array, index);
}
public IEnumerator<T> GetEnumerator()
{
ActivateForRead();
return List.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool Remove(T t)
{
ActivateForWrite();
bool removed = List.Remove(t);
if (removed)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, t));
return removed;
}
[Transient]
private IActivator _activator;
public virtual void Bind(IActivator activator)
{
if (_activator == activator)
return;
if (activator != null && _activator != null)
throw new InvalidOperationException();
_activator = activator;
}
public virtual void Activate(ActivationPurpose purpose)
{
if (_activator == null)
return;
_activator.Activate(purpose);
}
protected virtual void ActivateForRead()
{
Activate(ActivationPurpose.Read);
}
protected virtual void ActivateForWrite()
{
Activate(ActivationPurpose.Write);
}
[Transient]
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
CollectionChanged(this, e);
}
}
I've also tried copying the code from GenericTypeHandlerPredicate and registering my ActivatableCollection to use the GenericCollectionTypeHandler. That results in a crash in GenericTypeFor() throwing an InvalidOperationException() when "Mercury" is being stored.
Just want to mention my answers from the db4o forums also here, for people with a similar problem:
First part of the issue:
From db4o's point of view nothing has changed in the 'ActivatableCollection' object and therefore no changes are stored. This is what is happening:
When you add the items, the ActivatableCollection is marked as changed.
When you commit the changes are stored. However the ' ActivatableCollection' holds the reference to the same object. db4o only stores the changes in the ActivatableCollection-object, which is the reference to the List. Since it is the same, no actual change is stored.
The List of the ActivatableCollection is never updated, because it wasn't marked as 'changed'
So the transparent activation doesn't see the changes in the list. You can fix your issue simply by using an ActivatableList in you're ActivatableCollection implementation. Just change the List with a IList interface and instantiate a ActivatableList instead of an List.
The second part of the issue: Why doesn't it work even when registering the GenericCollectionTypeHandler for this type? Here we hit a implementation detail. The GenericCollectionTypeHandler has an internal list of supported types, which doesn't include the self made 'ActivatableCollection'. GenericCollectionTypeHandler is not really part of the public API and intendet for internal use only.
Workaround / Fix
Just use an ActivatableList<T> instead of a List<T>. then everything works fine.

Resources