@Override public Object generate(TableGeneratorDiscriptor discriptor, ClientBase client, String dataType) { try { HTableInterface hTable = ((HBaseDataHandler) ((HBaseClient) client).handler).gethTable(discriptor .getSchema()); Long latestCount = hTable.incrementColumnValue(discriptor.getPkColumnValue().getBytes(), discriptor .getTable().getBytes(), discriptor.getValueColumnName().getBytes(), 1); if (latestCount == 1) { return (long) discriptor.getInitialValue(); } else if (discriptor.getAllocationSize() == 1) { return latestCount + discriptor.getInitialValue(); } else { return (latestCount - 1) * discriptor.getAllocationSize() + discriptor.getInitialValue(); } } catch (IOException ioex) { log.error("Error while generating id for entity, Caused by: .", ioex); throw new KunderaException(ioex); } } }
/** * Builds the select query. * * @param descriptor * the descriptor * @return the string builder */ public StringBuilder buildSelectQuery(TableGeneratorDiscriptor descriptor) { StringBuilder builder = new StringBuilder("Select "); ensureCase(builder, descriptor.getValueColumnName(), false).append(" from "); ensureCase(builder, descriptor.getTable(), false).append(" where "); ensureCase(builder, descriptor.getPkColumnName(), false).append(" = '").append( descriptor.getPkColumnValue() + "'"); return builder; }
@Override public Object generate(TableGeneratorDiscriptor discriptor, ClientBase client, String dataType) { Cassandra.Client conn = ((CassandraClientBase) client).getRawClient(discriptor.getSchema()); long latestCount = 0l; Random random = new Random(); if (latestCount == 0) { return (long) discriptor.getInitialValue(); } else { latestCount = random.nextLong(); return (latestCount + 1) * discriptor.getAllocationSize(); } }
Assert.assertNull(keyValue.getSequenceDiscriptor()); Assert.assertNotNull(keyValue.getTableDiscriptor()); Assert.assertEquals(50, keyValue.getTableDiscriptor().getAllocationSize()); Assert.assertEquals(1, keyValue.getTableDiscriptor().getInitialValue()); Assert.assertEquals("KunderaTest", keyValue.getTableDiscriptor().getSchema()); Assert.assertEquals("kundera_sequences", keyValue.getTableDiscriptor().getTable()); Assert.assertEquals("sequence_name", keyValue.getTableDiscriptor().getPkColumnName()); Assert.assertEquals("GeneratedIdStrategyTable", keyValue.getTableDiscriptor().getPkColumnValue()); Assert.assertEquals("sequence_value", keyValue.getTableDiscriptor().getValueColumnName()); Assert.assertNull(keyValue.getTableDiscriptor().getCatalog()); Assert.assertNull(keyValue.getTableDiscriptor().getUniqueConstraints()); keyValue = null; Assert.assertNull(keyValue.getSequenceDiscriptor()); Assert.assertNotNull(keyValue.getTableDiscriptor()); Assert.assertEquals(30, keyValue.getTableDiscriptor().getAllocationSize()); Assert.assertEquals(100, keyValue.getTableDiscriptor().getInitialValue()); Assert.assertEquals("KunderaTest", keyValue.getTableDiscriptor().getSchema()); Assert.assertEquals("kundera", keyValue.getTableDiscriptor().getTable()); Assert.assertEquals("sequence", keyValue.getTableDiscriptor().getPkColumnName()); Assert.assertEquals("kk", keyValue.getTableDiscriptor().getPkColumnValue()); Assert.assertEquals("sequenceValue", keyValue.getTableDiscriptor().getValueColumnName()); Assert.assertNull(keyValue.getTableDiscriptor().getCatalog()); Assert.assertNull(keyValue.getTableDiscriptor().getUniqueConstraints()); keyValue = null; Assert.assertNull(keyValue.getSequenceDiscriptor()); Assert.assertNotNull(keyValue.getTableDiscriptor()); Assert.assertEquals(50, keyValue.getTableDiscriptor().getAllocationSize());
/** * Add tableGenerator to table info. * * @param appMetadata * @param persistenceUnit * @param tableInfos * @param entityMetadata * @param isCompositeId */ private void addTableGenerator(ApplicationMetadata appMetadata, String persistenceUnit, List<TableInfo> tableInfos, EntityMetadata entityMetadata) { Metamodel metamodel = appMetadata.getMetamodel(persistenceUnit); IdDiscriptor keyValue = ((MetamodelImpl) metamodel).getKeyValue(entityMetadata.getEntityClazz().getName()); if (keyValue != null && keyValue.getTableDiscriptor() != null) { TableInfo tableGeneratorDiscriptor = new TableInfo(keyValue.getTableDiscriptor().getTable(), "CounterColumnType", String.class, keyValue.getTableDiscriptor().getPkColumnName()); if (!tableInfos.contains(tableGeneratorDiscriptor)) { tableGeneratorDiscriptor.addColumnInfo(getJoinColumn(tableGeneratorDiscriptor, keyValue.getTableDiscriptor().getValueColumnName(), Long.class)); tableInfos.add(tableGeneratorDiscriptor); } } }
@Override public Object generate(TableGeneratorDiscriptor discriptor, ClientBase client, String dataType) { String tableName = HBaseUtils.getHTableName(discriptor.getSchema(), discriptor.getPkColumnValue()); try { Table hTable = ((HBaseDataHandler) ((HBaseClient) client).handler).gethTable(tableName); Long latestCount = hTable.incrementColumnValue(HBaseUtils.AUTO_ID_ROW.getBytes(), discriptor .getPkColumnValue().getBytes(), discriptor.getValueColumnName().getBytes(), 1); if (latestCount == 1) { return (long) discriptor.getInitialValue(); } else if (discriptor.getAllocationSize() == 1) { return latestCount + discriptor.getInitialValue(); } else { return (latestCount - 1) * discriptor.getAllocationSize() + discriptor.getInitialValue(); } } catch (IOException ioex) { log.error("Error while generating id for entity, Caused by: .", ioex); throw new KunderaException(ioex); } } }
/** * Builds the update query. * * @param descriptor * the descriptor * @return the string builder */ public StringBuilder buildUpdateQuery(TableGeneratorDiscriptor descriptor) { StringBuilder builder = new StringBuilder("Update "); ensureCase(builder, descriptor.getTable(), false).append(" set "); ensureCase(builder, descriptor.getValueColumnName(), false).append(" = "); ensureCase(builder, descriptor.getValueColumnName(), false).append(" + ").append(1).append(" where "); ensureCase(builder, descriptor.getPkColumnName(), false).append(" = '").append( descriptor.getPkColumnValue() + "'"); return builder; } }
Cassandra.Client conn = ((CassandraClientBase) client).getRawClient(discriptor.getSchema()); long latestCount = 0l; try conn.set_keyspace(discriptor.getSchema()); ColumnPath columnPath = new ColumnPath(discriptor.getTable()); columnPath.setColumn(discriptor.getValueColumnName().getBytes()); latestCount = conn.get(ByteBuffer.wrap(discriptor.getPkColumnValue().getBytes()), columnPath, ((CassandraClientBase) client).getConsistencyLevel()).counter_column.value; log.warn("Counter value not found for {}, resetting it to zero.", discriptor.getPkColumnName()); latestCount = 0; ColumnParent columnParent = new ColumnParent(discriptor.getTable()); CounterColumn counterColumn = new CounterColumn(ByteBuffer.wrap(discriptor.getValueColumnName() .getBytes()), 1); conn.add(ByteBuffer.wrap(discriptor.getPkColumnValue().getBytes()), columnParent, counterColumn, ((CassandraClientBase) client).getConsistencyLevel()); return (long) discriptor.getInitialValue(); return (latestCount + 1) * discriptor.getAllocationSize(); log.error("Error while reading counter value from table{}, Caused by: .", discriptor.getTable(), e); throw new KunderaException(e);
@Override public Object generate(TableGeneratorDiscriptor discriptor, ClientBase client, String dataType) { try { HTableInterface hTable = ((HBaseDataHandler) ((HBaseClient) client).handler).gethTable(discriptor .getSchema()); Long latestCount = hTable.incrementColumnValue(discriptor.getPkColumnValue().getBytes(), discriptor .getTable().getBytes(), discriptor.getValueColumnName().getBytes(), 1); if (latestCount == 1) { return (long) discriptor.getInitialValue(); } else if (discriptor.getAllocationSize() == 1) { return latestCount + discriptor.getInitialValue(); } else { return (latestCount - 1) * discriptor.getAllocationSize() + discriptor.getInitialValue(); } } catch (IOException ioex) { log.error("Error while generating id for entity, Caused by: .", ioex); throw new KunderaException(ioex); } } }
@Override public Long generate(TableGeneratorDiscriptor discriptor) { try { HTableInterface hTable = ((HBaseDataHandler) handler).gethTable(discriptor.getSchema()); Long latestCount = hTable.incrementColumnValue(discriptor.getPkColumnValue().getBytes(), discriptor .getTable().getBytes(), discriptor.getValueColumnName().getBytes(), 1); if (latestCount == 1) { return (long) discriptor.getInitialValue(); } else { return (latestCount - 1) * discriptor.getAllocationSize(); } } catch (IOException ioex) { log.error("Error while generating id for entity, Caused by: .", ioex); throw new KunderaException(ioex); } }
Cassandra.Client conn = getRawClient(pu, descriptor.getSchema()); try conn.set_keyspace(descriptor.getSchema()); ColumnPath columnPath = new ColumnPath(descriptor.getTable()); columnPath.setColumn(descriptor.getValueColumnName().getBytes()); long latestCount = 0l; latestCount = conn.get(ByteBuffer.wrap(descriptor.getPkColumnValue().getBytes()), columnPath, getConsistencyLevel()).counter_column.value; log.warn("Counter value not found for {}, resetting it to zero.", descriptor.getPkColumnName()); latestCount = 0; ColumnParent columnParent = new ColumnParent(descriptor.getTable()); ByteBuffer.wrap(descriptor.getValueColumnName().getBytes()), 1); conn.add(ByteBuffer.wrap(descriptor.getPkColumnValue().getBytes()), columnParent, counterColumn, getConsistencyLevel()); return (long) descriptor.getInitialValue(); return (latestCount + 1) * descriptor.getAllocationSize(); log.error("Error while reading counter value from table{}, Caused by: .", descriptor.getTable(), e); throw new KunderaException(e); log.error("Error while reading counter value from table{}, Caused by: .", descriptor.getTable(), e);