Skip to content

Commit

Permalink
fix: make sure to reload correctly view index informations on context…
Browse files Browse the repository at this point in the history
… restart
  • Loading branch information
tglman committed Jul 21, 2023
1 parent d5cf741 commit c14f46f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ public synchronized void forceDatabaseClose(String iDatabaseName) {
OAbstractPaginatedStorage storage = storages.remove(iDatabaseName);
if (storage != null) {
OSharedContext ctx = sharedContexts.remove(iDatabaseName);
ctx.getViewManager().close();
if (ctx != null) {
ctx.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void fromStream() {
this.cfg = new OViewConfig(getName(), query);
this.cfg.setUpdatable(Boolean.TRUE.equals(document.getProperty("updatable")));

if (document.getProperty("indexes") instanceof Map) {
if (document.getProperty("indexes") instanceof List) {
List<Map<String, Object>> idxData = document.getProperty("indexes");
for (Map<String, Object> idx : idxData) {
String type = (String) idx.get("type");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.orientechnologies.orient.core.metadata.schema;

import static org.junit.Assert.assertTrue;

import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import com.orientechnologies.orient.core.db.OrientDBInternal;
import com.orientechnologies.orient.core.db.viewmanager.ViewCreationListener;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import java.util.concurrent.CountDownLatch;
import org.junit.After;
import org.junit.Assert;
Expand All @@ -11,39 +16,78 @@

public class OViewTest {

private ODatabaseDocumentTx db;
private OrientDB orientdb;

@Before
public void setUp() {
db = new ODatabaseDocumentTx("memory:" + OViewTest.class.getSimpleName());
db.create();
orientdb = new OrientDB("embedded:./target/views", OrientDBConfig.defaultConfig());
}

@After
public void after() {
db.drop();
if (orientdb.exists("view_close")) {
orientdb.drop("view_close");
}
orientdb.close();
}

@Test
public void testSimple() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
db.getMetadata()
.getSchema()
.createView(
new OViewConfig("testSimple", "SELECT FROM V"),
new ViewCreationListener() {
@Override
public void afterCreate(ODatabaseSession database, String viewName) {
latch.countDown();
}

@Override
public void onError(String viewName, Exception exception) {}
});
latch.await();

Assert.assertNotNull(db.getMetadata().getSchema().getView("testSimple"));
Assert.assertNull(db.getMetadata().getSchema().getClass("testSimple"));
Assert.assertNull(db.getMetadata().getSchema().getView("V"));
orientdb.execute(
"create database "
+ OViewTest.class.getSimpleName()
+ " memory users (admin identified by 'adminpwd' role admin)");
try (ODatabaseSession db =
orientdb.open(OViewTest.class.getSimpleName(), "admin", "adminpwd")) {

db.getMetadata()
.getSchema()
.createView(
new OViewConfig("testSimple", "SELECT FROM V"),
new ViewCreationListener() {
@Override
public void afterCreate(ODatabaseSession database, String viewName) {
latch.countDown();
}

@Override
public void onError(String viewName, Exception exception) {}
});
latch.await();

Assert.assertNotNull(db.getMetadata().getSchema().getView("testSimple"));
Assert.assertNull(db.getMetadata().getSchema().getClass("testSimple"));
Assert.assertNull(db.getMetadata().getSchema().getView("V"));
}
}

@Test
public void testCloseDatabase() throws InterruptedException {
orientdb.execute(
"create database view_close plocal users (admin identified by 'adminpwd' role admin)");
try (ODatabaseSession session = orientdb.open("view_close", "admin", "adminpwd")) {
session.createClass("test");
session
.command(
"create view test_view from( select name from test ) metadata {updateIntervalSeconds:1, indexes: [{type:'NOTUNIQUE', properties:{name:'STRING'}}]}")
.close();
session.command("insert into test set name='abc'").close();

Thread.sleep(2000);
try (OResultSet result = session.query("select from test_view where name='abc'")) {
String execution = result.getExecutionPlan().get().prettyPrint(0, 0);
assertTrue(execution.contains("FETCH FROM INDEX"));
}
}

OrientDBInternal.extract(orientdb).forceDatabaseClose("view_close");
try (ODatabaseSession session = orientdb.open("view_close", "admin", "adminpwd")) {
Thread.sleep(2000);
try (OResultSet result = session.query("select from test_view where name='abc'")) {
String execution = result.getExecutionPlan().get().prettyPrint(0, 0);
assertTrue(execution.contains("FETCH FROM INDEX"));
}
}
}
}

0 comments on commit c14f46f

Please sign in to comment.