Skip to content

Commit 839b399

Browse files
authored
Fixed issue with namespaces in copied requests. (#52)
* Added complex query to namespace test * Re-added original (non-default) namespace test * only invoke default namespace test if kv >= 22.3.32.
1 parent b035253 commit 839b399

File tree

3 files changed

+145
-22
lines changed

3 files changed

+145
-22
lines changed

driver/src/main/java/oracle/nosql/driver/ops/Request.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ public void copyTo(Request other) {
503503
other.setCheckRequestSize(this.checkRequestSize);
504504
other.setCompartmentInternal(this.compartment);
505505
other.setTableNameInternal(this.tableName);
506+
other.setNamespaceInternal(this.namespace);
506507
other.setStartNanos(this.startNanos);
507508
other.setRetryStats(this.retryStats);
508509
other.setReadRateLimiter(this.readRateLimiter);

driver/src/test/java/oracle/nosql/driver/OnPremiseTest.java

Lines changed: 135 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -453,26 +453,119 @@ public void testNamespaces()
453453
/*
454454
* Note: tables will be dropped by a cascading drop of the namespace
455455
*/
456-
final String parentName = "parent";
457-
final String childName = "parent.child";
458-
final String nsParentName = "myns:parent";
459-
final String nsChildName = "myns:parent.child";
456+
457+
final String parentName = "myns:parent";
458+
final String childName = "myns:parent.child";
460459
final int numParent = 30;
461460
final int numChild = 40;
462461

463-
((NoSQLHandleImpl)handle).setDefaultNamespace("myns");
464-
465462
doSysOp(handle, "create namespace myns");
466463

467464
/* parent in myns */
468465
TableRequest treq = new TableRequest().setStatement(
469-
"create table parent(id integer, primary key(id))");
466+
"create table myns:parent(id integer, primary key(id))");
470467
TableResult tres = handle.tableRequest(treq);
471468
tres.waitForCompletion(handle, 100000, 1000);
472469

473470
/* child in myns */
474471
treq = new TableRequest().setStatement(
475-
"create table parent.child(cid integer, name string, " +
472+
"create table myns:parent.child(cid integer, name string, " +
473+
"primary key(cid))");
474+
tres = handle.tableRequest(treq);
475+
tres.waitForCompletion(handle, 100000, 1000);
476+
477+
/* put data in both tables */
478+
PutRequest preq = new PutRequest();
479+
MapValue value = new MapValue();
480+
for (int i = 0; i < numParent; i++) {
481+
value.put("name", "myname"); // ignored in parent
482+
value.put("id", i);
483+
preq.setTableName(parentName).setValue(value);
484+
PutResult pres = handle.put(preq);
485+
assertNotNull("Parent put failed", pres.getVersion());
486+
for (int j = 0; j < numChild; j++) {
487+
value.put("cid", j); // ignored in parent
488+
preq.setTableName(childName).setValue(value);
489+
pres = handle.put(preq);
490+
assertNotNull("Child put failed", pres.getVersion());
491+
assertNoUnits(pres);
492+
}
493+
}
494+
495+
/* get parent */
496+
GetRequest getReq = new GetRequest().setTableName(parentName)
497+
.setKey(new MapValue().put("id", 1));
498+
GetResult getRes = handle.get(getReq);
499+
assertNotNull(getRes.getValue());
500+
501+
/* get child */
502+
getReq = new GetRequest().setTableName(childName)
503+
.setKey(new MapValue().put("id", 1).put("cid", 1));
504+
getRes = handle.get(getReq);
505+
assertNotNull(getRes.getValue());
506+
assertNoUnits(getRes);
507+
508+
try {
509+
/* query parent */
510+
String query = "select * from " + parentName;
511+
List<MapValue> res = doQuery(handle, query);
512+
assertEquals(numParent, res.size());
513+
514+
/* query child */
515+
query = "select * from " + childName;
516+
res = doQuery(handle, query);
517+
assertEquals(numParent * numChild, res.size());
518+
519+
/* prepared query on child */
520+
res = doPreparedQuery(handle, query);
521+
assertEquals(numParent * numChild, res.size());
522+
} catch (RequestTimeoutException rte) {
523+
/*
524+
if (!(rte.getCause() instanceof SystemException)) {
525+
throw rte;
526+
}
527+
*/
528+
/* ignore this exception for 19 for now; known bug */
529+
}
530+
531+
/* test ListTables with namespace */
532+
ListTablesRequest listTables =
533+
new ListTablesRequest().setNamespace("myns");
534+
ListTablesResult lres = handle.listTables(listTables);
535+
assertEquals(2, lres.getTables().length);
536+
}
537+
538+
@Test
539+
public void testDefaultNamespaces()
540+
throws Exception {
541+
542+
/* this only works with versions of KV with default namespace support */
543+
assumeKVVersion("testDefaultNamespaces", 22, 3, 32);
544+
545+
/*
546+
* Note: tables will be dropped by a cascading drop of the namespace
547+
*/
548+
final String parentName = "parent";
549+
final String childName = "parent.child";
550+
final String nsParentName = "mydns:parent";
551+
final String nsChildName = "mydns:parent.child";
552+
final int numParent = 30;
553+
final int numChild = 40;
554+
555+
((NoSQLHandleImpl)handle).setDefaultNamespace("mydns");
556+
557+
doSysOp(handle, "create namespace mydns");
558+
559+
/* parent in mydns */
560+
TableRequest treq = new TableRequest().setStatement(
561+
"create table parent(sid integer, id integer, name string, " +
562+
"salary long, primary key(SHARD(sid), id))");
563+
TableResult tres = handle.tableRequest(treq);
564+
tres.waitForCompletion(handle, 100000, 1000);
565+
566+
/* child in mydns */
567+
treq = new TableRequest().setStatement(
568+
"create table parent.child(cid integer, cname string, " +
476569
"primary key(cid))");
477570
tres = handle.tableRequest(treq);
478571
tres.waitForCompletion(handle, 100000, 1000);
@@ -486,7 +579,7 @@ public void testNamespaces()
486579
assertTrue(lres.getTables().length > 2);
487580

488581
/* test ListTables with explicit namespace */
489-
listTables = new ListTablesRequest().setNamespace("myns");
582+
listTables = new ListTablesRequest().setNamespace("mydns");
490583
lres = handle.listTables(listTables);
491584
assertEquals(2, lres.getTables().length);
492585

@@ -499,13 +592,16 @@ public void testNamespaces()
499592
PutRequest preq = new PutRequest();
500593
MapValue value = new MapValue();
501594
for (int i = 0; i < numParent; i++) {
502-
value.put("name", "myname"); // ignored in parent
595+
value.put("name", "pname");
503596
value.put("id", i);
597+
value.put("sid", i);
598+
value.put("salary", i*1000);
504599
preq.setTableName(parentName).setValue(value);
505600
PutResult pres = handle.put(preq);
506601
assertNotNull("Parent put failed", pres.getVersion());
507602
for (int j = 0; j < numChild; j++) {
508-
value.put("cid", j); // ignored in parent
603+
value.put("cid", j);
604+
value.put("cname", "cname" + j);
509605
preq.setTableName(childName).setValue(value);
510606
pres = handle.put(preq);
511607
assertNotNull("Child put failed", pres.getVersion());
@@ -515,13 +611,13 @@ public void testNamespaces()
515611

516612
/* get parent */
517613
GetRequest getReq = new GetRequest().setTableName(parentName)
518-
.setKey(new MapValue().put("id", 1));
614+
.setKey(new MapValue().put("id", 1).put("sid", 1));
519615
GetResult getRes = handle.get(getReq);
520616
assertNotNull(getRes.getValue());
521617

522618
/* get child */
523619
getReq = new GetRequest().setTableName(childName)
524-
.setKey(new MapValue().put("id", 1).put("cid", 1));
620+
.setKey(new MapValue().put("id", 1).put("sid", 1).put("cid", 1));
525621
getRes = handle.get(getReq);
526622
assertNotNull(getRes.getValue());
527623
assertNoUnits(getRes);
@@ -564,7 +660,7 @@ public void testNamespaces()
564660

565661
/* this should fail */
566662
getReq = new GetRequest().setTableName(parentName)
567-
.setKey(new MapValue().put("id", 1));
663+
.setKey(new MapValue().put("id", 1).put("sid", 1));
568664
try {
569665
getRes = handle.get(getReq);
570666
fail("Get should have failed");
@@ -574,25 +670,42 @@ public void testNamespaces()
574670

575671
/* This should pass */
576672
getReq = new GetRequest().setTableName(parentName)
577-
.setNamespace("myns")
578-
.setKey(new MapValue().put("id", 1));
673+
.setNamespace("mydns")
674+
.setKey(new MapValue().put("id", 1).put("sid", 1));
579675
getRes = handle.get(getReq);
580676
assertNotNull(getRes.getValue());
581677

582678
/* This should pass */
583679
((NoSQLHandleImpl)handle).setDefaultNamespace("invalid");
584680
getReq = new GetRequest().setTableName(parentName)
585-
.setNamespace("myns")
586-
.setKey(new MapValue().put("id", 1));
681+
.setNamespace("mydns")
682+
.setKey(new MapValue().put("id", 1).put("sid", 1));
587683
getRes = handle.get(getReq);
588684
assertNotNull(getRes.getValue());
589685

686+
687+
/* test complex query (exercises internal request copying) */
688+
String query = "select sid, count(*) as cnt, sum(salary) as sum " +
689+
"from " + parentName + " group by sid";
690+
/* this should pass */
691+
List<MapValue> res = doQuery(handle, query, "mydns");
692+
assertEquals(numParent, res.size());
693+
694+
/* this should fail */
695+
try {
696+
res = doQuery(handle, query, "invalid");
697+
fail("query should have failed");
698+
} catch (TableNotFoundException e) {
699+
// expected
700+
}
701+
702+
590703
/* verify table name overrides request namespace */
591704

592705
/* this should fail */
593706
getReq = new GetRequest().setTableName(parentName)
594707
.setNamespace("invalid")
595-
.setKey(new MapValue().put("id", 1));
708+
.setKey(new MapValue().put("id", 1).put("sid", 1));
596709
try {
597710
getRes = handle.get(getReq);
598711
fail("Get should have failed");
@@ -603,19 +716,19 @@ public void testNamespaces()
603716
/* This should pass */
604717
getReq = new GetRequest().setTableName(nsParentName)
605718
.setNamespace("invalid")
606-
.setKey(new MapValue().put("id", 1));
719+
.setKey(new MapValue().put("id", 1).put("sid", 1));
607720
getRes = handle.get(getReq);
608721
assertNotNull(getRes.getValue());
609722

610723
/* drop table with namespace in request */
611724
treq = new TableRequest()
612725
.setStatement("drop table parent.child")
613-
.setNamespace("myns");
726+
.setNamespace("mydns");
614727
tres = handle.tableRequest(treq);
615728
tres.waitForCompletion(handle, 100000, 1000);
616729

617730
/* drop namespace - use cascade to remove tables */
618-
doSysOp(handle, "drop namespace myns cascade");
731+
doSysOp(handle, "drop namespace mydns cascade");
619732
}
620733

621734
/**

driver/src/test/java/oracle/nosql/driver/ProxyTestBase.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,17 @@ static private ArrayValue createLargeStringArray(int size) {
581581
}
582582

583583
protected static List<MapValue> doQuery(NoSQLHandle qHandle, String query) {
584+
return doQuery(qHandle, query, null);
585+
}
586+
587+
protected static List<MapValue> doQuery(NoSQLHandle qHandle,
588+
String query,
589+
String namespace) {
584590
try( QueryRequest queryRequest = new QueryRequest()) {
585591
queryRequest.setStatement(query);
592+
if (namespace != null) {
593+
queryRequest.setNamespace(namespace);
594+
}
586595
List<MapValue> results = new ArrayList<MapValue>();
587596

588597
do {

0 commit comments

Comments
 (0)