package com.orientechnologies.orient.core.sql;

import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.ODatabaseType;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class CreateLightWeightEdgesSQLTest {

  private OrientDB orientDB;

  @Before
  public void before() {
    orientDB = new OrientDB("embedded:", OrientDBConfig.defaultConfig());
    orientDB.create(CreateLightWeightEdgesSQLTest.class.getSimpleName(), ODatabaseType.MEMORY);
  }

  @Test
  public void test() {
    ODatabaseSession session = orientDB.open(CreateLightWeightEdgesSQLTest.class.getSimpleName(), "admin", "admin");

    session.command("ALTER DATABASE CUSTOM useLightweightEdges = true");
    session.command("create vertex v set name='a' ");
    session.command("create vertex v set name='b' ");
    session.command("create edge e from (select from v where name='a') to (select from v where name='a') ");
    try (OResultSet res = session.query("select expand(out()) from v where name='a' ")) {
      assertEquals(res.stream().count(), 1);
    }
  }

  @After
  public void after() {
    orientDB.close();
  }

}

@@ -85,32 +85,17 @@
 core.db.
-ODatabaseSession
+*
 ;%0Aimport
@@ -133,138 +133,50 @@
 ore.
-db.ODatabaseType;%0Aimport com.orientechnologies.orient.core.db.OrientDB;%0Aimport com.orientechnologies.orient.core.db.OrientDBConfig
+exception.OConcurrentModificationException
 ;%0Aim
@@ -312,16 +312,96 @@
 .Test;%0A%0A
+import java.util.concurrent.CountDownLatch;%0Aimport java.util.stream.IntStream;%0A%0A
 import s
@@ -1298,16 +1298,16 @@
 (), 1);%0A
-
     %7D%0A  
@@ -1309,16 +1309,1454 @@
  %7D%0A  %7D%0A%0A
+  @Test%0A  public void mtTest() throws InterruptedException %7B%0A%0A    ODatabasePool pool = new ODatabasePool(orientDB, CreateLightWeightEdgesSQLTest.class.getSimpleName(), %22admin%22, %22admin%22);%0A%0A    ODatabaseSession session = pool.acquire();%0A%0A    session.command(%22ALTER DATABASE CUSTOM useLightweightEdges = true%22);%0A    session.command(%22create vertex v set id = 1 %22);%0A    session.command(%22create vertex v set id = 2 %22);%0A%0A    session.close();%0A%0A    CountDownLatch latch = new CountDownLatch(10);%0A%0A    IntStream.range(0, 10).forEach((i) -%3E %7B%0A      new Thread(() -%3E %7B%0A%0A        ODatabaseSession session1 = pool.acquire();%0A%0A        try %7B%0A          for (int j = 0; j %3C 100; j++) %7B%0A%0A%0A            try %7B%0A              session1.command(%22create edge e from (select from v where id=1) to (select from v where id=2) %22);%0A            %7D catch (OConcurrentModificationException e) %7B%0A%0A            %7D%0A          %7D%0A        %7D finally %7B%0A          latch.countDown();%0A        %7D%0A%0A      %7D).start();%0A    %7D);%0A%0A    latch.await();%0A%0A    session = pool.acquire();%0A    try (OResultSet res = session.query(%22select sum(out().size()) as size from V where id = 1%22);%0A        OResultSet res1 = session.query(%22select sum(in().size()) as size from V where id = 2%22)) %7B%0A%0A      Integer s1 = res.stream().findFirst().get().getProperty(%22size%22);%0A      Integer s2 = res1.stream().findFirst().get().getProperty(%22size%22);%0A      assertEquals(s1, s2);%0A%0A    %7D finally %7B%0A      pool.close();%0A    %7D%0A%0A  %7D%0A%0A
   @After

Test for autotx in case of lightweight edges