use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.
the class ImmutableTablePropertiesIT method testImmutableProperty.
@Test
public void testImmutableProperty() throws Exception {
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
String immutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
String mutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
Statement stmt = conn.createStatement();
// create table with immutable table property set to true
String ddl = "CREATE TABLE " + immutableDataTableFullName + " (a_string varchar not null, col1 integer" + " CONSTRAINT pk PRIMARY KEY (a_string)) IMMUTABLE_ROWS=true";
stmt.execute(ddl);
// create table with immutable table property set to false
ddl = "CREATE TABLE " + mutableDataTableFullName + " (a_string varchar not null, col1 integer" + " CONSTRAINT pk PRIMARY KEY (a_string)) IMMUTABLE_ROWS=false";
stmt.execute(ddl);
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
PTable immutableTable = phxConn.getTable(new PTableKey(null, immutableDataTableFullName));
assertTrue("IMMUTABLE_ROWS should be set to true", immutableTable.isImmutableRows());
PTable mutableTable = phxConn.getTable(new PTableKey(null, mutableDataTableFullName));
assertFalse("IMMUTABLE_ROWS should be set to false", mutableTable.isImmutableRows());
}
}
use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.
the class ParameterizedTransactionIT method testNonTxToTxTable.
@Test
public void testNonTxToTxTable() throws Exception {
String nonTxTableName = generateUniqueName();
Connection conn = DriverManager.getConnection(getUrl());
conn.createStatement().execute("CREATE TABLE " + nonTxTableName + "(k INTEGER PRIMARY KEY, v VARCHAR)" + tableDDLOptions);
conn.createStatement().execute("UPSERT INTO " + nonTxTableName + " VALUES (1)");
conn.createStatement().execute("UPSERT INTO " + nonTxTableName + " VALUES (2, 'a')");
conn.createStatement().execute("UPSERT INTO " + nonTxTableName + " VALUES (3, 'b')");
conn.commit();
String index = generateUniqueName();
conn.createStatement().execute("CREATE INDEX " + index + " ON " + nonTxTableName + "(v)");
// Reset empty column value to an empty value like it is pre-transactions
HTableInterface htable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes(nonTxTableName));
List<Put> puts = Lists.newArrayList(new Put(PInteger.INSTANCE.toBytes(1)), new Put(PInteger.INSTANCE.toBytes(2)), new Put(PInteger.INSTANCE.toBytes(3)));
for (Put put : puts) {
put.add(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES, ByteUtil.EMPTY_BYTE_ARRAY);
}
htable.put(puts);
conn.createStatement().execute("ALTER TABLE " + nonTxTableName + " SET TRANSACTIONAL=true");
htable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes(nonTxTableName));
assertTrue(htable.getTableDescriptor().getCoprocessors().contains(PhoenixTransactionalProcessor.class.getName()));
htable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes(index));
assertTrue(htable.getTableDescriptor().getCoprocessors().contains(PhoenixTransactionalProcessor.class.getName()));
conn.createStatement().execute("UPSERT INTO " + nonTxTableName + " VALUES (4, 'c')");
ResultSet rs = conn.createStatement().executeQuery("SELECT /*+ NO_INDEX */ k FROM " + nonTxTableName + " WHERE v IS NULL");
assertTrue(conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, nonTxTableName)).isTransactional());
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
conn.commit();
conn.createStatement().execute("UPSERT INTO " + nonTxTableName + " VALUES (5, 'd')");
rs = conn.createStatement().executeQuery("SELECT k FROM " + nonTxTableName);
assertTrue(conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, index)).isTransactional());
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
assertTrue(rs.next());
assertEquals(4, rs.getInt(1));
assertTrue(rs.next());
assertEquals(5, rs.getInt(1));
assertFalse(rs.next());
conn.rollback();
rs = conn.createStatement().executeQuery("SELECT k FROM " + nonTxTableName);
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
assertTrue(rs.next());
assertEquals(4, rs.getInt(1));
assertFalse(rs.next());
}
use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.
the class ConnectionQueryServicesImpl method addFunction.
@Override
public void addFunction(PFunction function) throws SQLException {
synchronized (latestMetaDataLock) {
try {
throwConnectionClosedIfNullMetaData();
// If existing table isn't older than new table, don't replace
// If a client opens a connection at an earlier timestamp, this can happen
PFunction existingFunction = latestMetaData.getFunction(new PTableKey(function.getTenantId(), function.getFunctionName()));
if (existingFunction.getTimeStamp() >= function.getTimeStamp()) {
return;
}
} catch (FunctionNotFoundException e) {
}
latestMetaData.addFunction(function);
latestMetaDataLock.notifyAll();
}
}
use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.
the class ConnectionlessQueryServicesImpl method getSchema.
@Override
public MetaDataMutationResult getSchema(String schemaName, long clientTimestamp) throws SQLException {
try {
PSchema schema = metaData.getSchema(new PTableKey(null, schemaName));
new MetaDataMutationResult(MutationCode.SCHEMA_ALREADY_EXISTS, schema, 0);
} catch (SchemaNotFoundException e) {
}
return new MetaDataMutationResult(MutationCode.SCHEMA_NOT_FOUND, 0, null);
}
use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.
the class ConnectionlessQueryServicesImpl method updateIndexState.
@Override
public MetaDataMutationResult updateIndexState(List<Mutation> tableMetadata, String parentTableName) throws SQLException {
byte[][] rowKeyMetadata = new byte[3][];
SchemaUtil.getVarChars(tableMetadata.get(0).getRow(), rowKeyMetadata);
Mutation m = MetaDataUtil.getTableHeaderRow(tableMetadata);
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
if (!MetaDataUtil.getMutationValue(m, INDEX_STATE_BYTES, kvBuilder, ptr)) {
throw new IllegalStateException();
}
PIndexState newState = PIndexState.fromSerializedValue(ptr.get()[ptr.getOffset()]);
byte[] tenantIdBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
String schemaName = Bytes.toString(rowKeyMetadata[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX]);
String indexName = Bytes.toString(rowKeyMetadata[PhoenixDatabaseMetaData.TABLE_NAME_INDEX]);
String indexTableName = SchemaUtil.getTableName(schemaName, indexName);
PName tenantId = tenantIdBytes.length == 0 ? null : PNameFactory.newName(tenantIdBytes);
PTable index = metaData.getTableRef(new PTableKey(tenantId, indexTableName)).getTable();
index = PTableImpl.makePTable(index, newState == PIndexState.USABLE ? PIndexState.ACTIVE : newState == PIndexState.UNUSABLE ? PIndexState.INACTIVE : newState);
return new MetaDataMutationResult(MutationCode.TABLE_ALREADY_EXISTS, 0, index);
}
Aggregations