use of herddb.utils.DataAccessor in project herddb by diennea.
the class SystemTablesTest method testSysTables.
@Test
public void testSysTables() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key auto_increment,n1 int,s1 string)", Collections.emptyList());
execute(manager, "CREATE TABLE tblspace1.tsql2 (k1 string primary key,n1 long,s1 timestamp, b1 blob)", Collections.emptyList());
execute(manager, "CREATE BRIN INDEX index1 on tblspace1.tsql2 (s1,b1)", Collections.emptyList());
execute(manager, "CREATE HASH INDEX index2 on tblspace1.tsql2 (b1)", Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("systables")).findAny().isPresent());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where systemtable=false", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
assertEquals(2, records.size());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where systemtable='false'", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
assertEquals(2, records.size());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where systemtable=true", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
assertFalse(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
assertFalse(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("systables")).findAny().isPresent());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where systemtable='true'", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
assertFalse(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
assertFalse(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("systables")).findAny().isPresent());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systablestats", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql")).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2")).findAny().isPresent());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.sysindexes order by index_name", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
assertEquals(2, records.size());
DataAccessor index1 = records.get(0);
assertEquals(RawString.of("tblspace1"), index1.get("tablespace"));
assertEquals(RawString.of("brin"), index1.get("index_type"));
assertEquals(RawString.of("index1"), index1.get("index_name"));
assertEquals(RawString.of("tsql2"), index1.get("table_name"));
DataAccessor index2 = records.get(1);
assertEquals(RawString.of("tblspace1"), index2.get("tablespace"));
assertEquals(RawString.of("hash"), index2.get("index_type"));
assertEquals(RawString.of("index2"), index2.get("index_name"));
assertEquals(RawString.of("tsql2"), index2.get("table_name"));
}
execute(manager, "BEGIN TRANSACTION 'tblspace1'", Collections.emptyList());
long txid;
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systransactions order by txid", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
assertEquals(1, records.size());
System.out.println("records:" + records);
txid = (Long) records.get(0).get("txid");
}
execute(manager, "COMMIT TRANSACTION 'tblspace1'," + txid, Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systransactions order by txid", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
assertEquals(0, records.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.syscolumns", Collections.emptyList())) {
List<DataAccessor> records = scan.consume();
records.forEach(r -> {
System.out.println("found " + r.toMap());
});
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql") && t.get("column_name").equals("k1") && t.get("data_type").equals("string") && t.get("auto_increment").equals(1)).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql") && t.get("column_name").equals("n1") && t.get("data_type").equals("integer") && t.get("auto_increment").equals(0)).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("s1") && t.get("data_type").equals("timestamp")).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("b1") && t.get("data_type").equals("bytearray")).findAny().isPresent());
assertTrue(records.stream().filter(t -> t.get("table_name").equals("tsql2") && t.get("column_name").equals("n1") && t.get("data_type").equals("long")).findAny().isPresent());
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class MessageUtils method writeEncodedSimpleValue.
@SuppressWarnings("rawtypes")
private static void writeEncodedSimpleValue(ByteBuf output, Object o) {
if (o == null) {
output.writeByte(OPCODE_NULL_VALUE);
} else if (o instanceof String) {
output.writeByte(OPCODE_STRING_VALUE);
writeUTF8String(output, (String) o);
} else if (o instanceof RawString) {
output.writeByte(OPCODE_STRING_VALUE);
writeUTF8String(output, (RawString) o);
} else if (o instanceof java.sql.Timestamp) {
output.writeByte(OPCODE_TIMESTAMP_VALUE);
ByteBufUtils.writeVLong(output, ((java.sql.Timestamp) o).getTime());
} else if (o instanceof java.lang.Byte) {
output.writeByte(OPCODE_BYTE_VALUE);
output.writeByte(((Byte) o));
} else if (o instanceof KeyValue) {
KeyValue kv = (KeyValue) o;
output.writeByte(OPCODE_KEYVALUE_VALUE);
ByteBufUtils.writeArray(output, kv.key);
ByteBufUtils.writeArray(output, kv.value);
} else if (o instanceof Integer) {
int i = (int) o;
if (i < 0) {
if (i < WRITE_MIN_Z_INT_LIMIT) {
output.writeByte(OPCODE_INT_VALUE);
output.writeInt(i);
} else {
output.writeByte(OPCODE_Z_INT_VALUE);
ByteBufUtils.writeZInt(output, i);
}
} else {
if (i > WRITE_MAX_V_INT_LIMIT) {
output.writeByte(OPCODE_INT_VALUE);
output.writeInt(i);
} else {
output.writeByte(OPCODE_V_INT_VALUE);
ByteBufUtils.writeVInt(output, i);
}
}
} else if (o instanceof Long) {
long l = (long) o;
if (l < 0) {
if (l < WRITE_MIN_Z_LONG_LIMIT) {
output.writeByte(OPCODE_LONG_VALUE);
output.writeLong(l);
} else {
output.writeByte(OPCODE_Z_LONG_VALUE);
ByteBufUtils.writeZLong(output, l);
}
} else {
if (l > WRITE_MAX_V_LONG_LIMIT) {
output.writeByte(OPCODE_LONG_VALUE);
output.writeLong(l);
} else {
output.writeByte(OPCODE_V_LONG_VALUE);
ByteBufUtils.writeVLong(output, l);
}
}
} else if (o instanceof Boolean) {
output.writeByte(OPCODE_BOOLEAN_VALUE);
output.writeByte(((Boolean) o).booleanValue() ? 1 : 0);
} else if (o instanceof Double) {
output.writeByte(OPCODE_DOUBLE_VALUE);
ByteBufUtils.writeDouble(output, ((Double) o).doubleValue());
} else if (o instanceof Set) {
Set set = (Set) o;
output.writeByte(OPCODE_SET_VALUE);
ByteBufUtils.writeVInt(output, set.size());
for (Object o2 : set) {
writeEncodedSimpleValue(output, o2);
}
} else if (o instanceof List) {
List set = (List) o;
output.writeByte(OPCODE_LIST_VALUE);
ByteBufUtils.writeVInt(output, set.size());
for (Object o2 : set) {
writeEncodedSimpleValue(output, o2);
}
} else if (o instanceof Map) {
Map set = (Map) o;
output.writeByte(OPCODE_MAP_VALUE);
ByteBufUtils.writeVInt(output, set.size());
for (Map.Entry entry : (Iterable<Map.Entry>) set.entrySet()) {
writeEncodedSimpleValue(output, entry.getKey());
writeEncodedSimpleValue(output, entry.getValue());
}
} else if (o instanceof DataAccessor) {
DataAccessor set = (DataAccessor) o;
output.writeByte(OPCODE_MAP2_VALUE);
// number of entries is not known
set.forEach((key, value) -> {
writeEncodedSimpleValue(output, key);
writeEncodedSimpleValue(output, value);
});
output.writeByte(OPCODE_MAP2_VALUE_END);
} else if (o instanceof TuplesList) {
TuplesList set = (TuplesList) o;
output.writeByte(OPCODE_TUPLELIST_VALUE);
final int numColumns = set.columnNames.length;
ByteBufUtils.writeVInt(output, numColumns);
for (String columnName : set.columnNames) {
writeUTF8String(output, columnName);
}
ByteBufUtils.writeVInt(output, set.tuples.size());
for (DataAccessor da : set.tuples) {
IntHolder currentColumn = new IntHolder();
da.forEach((String key, Object value) -> {
String expectedColumnName = set.columnNames[currentColumn.value];
while (!key.equals(expectedColumnName)) {
// nulls are not returned for some special accessors, lie DataAccessorForFullRecord
writeEncodedSimpleValue(output, null);
currentColumn.value++;
expectedColumnName = set.columnNames[currentColumn.value];
}
writeEncodedSimpleValue(output, value);
currentColumn.value++;
});
// fill with nulls
while (currentColumn.value < numColumns) {
writeEncodedSimpleValue(output, null);
currentColumn.value++;
}
if (currentColumn.value > numColumns) {
throw new RuntimeException("unexpected number of columns " + currentColumn.value + " > " + numColumns);
}
}
} else if (o instanceof byte[]) {
byte[] set = (byte[]) o;
output.writeByte(OPCODE_BYTEARRAY_VALUE);
ByteBufUtils.writeArray(output, set);
} else {
throw new RuntimeException("unsupported class " + o.getClass());
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class MessageUtilsTest method testEncodeMessage.
@Test
public void testEncodeMessage() {
final String clientId = "2331";
System.out.println("encodeMessage");
ByteBuf buffer = Unpooled.buffer();
Map<String, Object> payload = new HashMap<>();
payload.put("string", "value");
payload.put("int", 1234);
payload.put("long", 12345L);
payload.put("rawstring", RawString.of("value"));
payload.put("list", Arrays.asList("foo", "bar"));
payload.put("set", new HashSet<>(Arrays.asList("foo", "bar")));
String[] colNames = { "one", "null", "two", "notfound" };
List<DataAccessor> records = new ArrayList<>();
Map<String, Object> record1 = new HashMap<>();
record1.put("one", 1234);
record1.put("two", RawString.of("test"));
records.add(new MapDataAccessor(record1, colNames));
Map<String, Object> record2 = new HashMap<>();
record2.put("one", 2234);
record2.put("two", RawString.of("test2"));
record2.put("null", null);
records.add(new MapDataAccessor(record2, colNames));
TuplesList tl = new TuplesList(colNames, records);
payload.put("data", tl);
Message m = new Message(clientId, 1234, payload);
m.assignMessageId();
m.setReplyMessageId("2343");
MessageUtils.encodeMessage(buffer, m);
Message read = MessageUtils.decodeMessage(buffer);
assertEquals(read.clientId, m.clientId);
assertEquals(read.messageId, m.messageId);
assertEquals(read.replyMessageId, m.replyMessageId);
assertEquals(read.type, m.type);
assertEquals(read.parameters.size(), m.parameters.size());
read.parameters.forEach((String k, Object v) -> {
Object o = m.parameters.get(k);
assertEquals(o, v);
});
TuplesList tl2 = (TuplesList) read.parameters.get("data");
assertEquals(4, tl2.tuples.get(0).getValues().length);
assertArrayEquals(colNames, tl2.tuples.get(0).getFieldNames());
assertEquals(4, tl2.tuples.get(1).getValues().length);
assertArrayEquals(colNames, tl2.tuples.get(1).getFieldNames());
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class TmpMapImpl method forEachKey.
@Override
public void forEachKey(Sink<K> sink) throws CollectionsException, SinkException {
try (DataScanner dataScanner = tableSpaceManager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION, false, false)) {
while (dataScanner.hasNext()) {
DataAccessor next = dataScanner.next();
Object key = next.get(0);
if (key instanceof RawString) {
key = key.toString();
}
try {
if (!sink.accept((K) key)) {
return;
}
} catch (Exception err) {
throw new SinkException(err);
}
}
} catch (SinkException err) {
throw err;
} catch (HerdDBInternalException | DataScannerException err) {
throw new CollectionsException(err);
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class FileRecordSet method applyLimits.
@Override
public void applyLimits(ScanLimits limits, StatementEvaluationContext context) throws StatementExecutionException {
if (!writeFinished) {
throw new IllegalStateException("RecordSet is still in write mode");
}
if (limits == null) {
return;
}
int offset = limits.computeOffset(context);
if (offset > 0) {
int maxlen = buffer.size();
if (offset >= maxlen) {
buffer.close();
// new empty buffer
buffer = new DiskArrayList<>(Integer.MAX_VALUE, tmpDirectory, new TupleSerializer(columns, fieldNames));
buffer.enableCompression();
buffer.finish();
return;
}
int samplesize = maxlen - offset;
DiskArrayList<DataAccessor> copy = new DiskArrayList<>(buffer.isSwapped() ? -1 : Integer.MAX_VALUE, tmpDirectory, new TupleSerializer(columns, fieldNames));
copy.enableCompression();
int firstIndex = offset;
int lastIndex = offset + samplesize;
int i = 0;
for (DataAccessor t : buffer) {
if (i >= firstIndex && i < lastIndex) {
copy.add(t);
}
i++;
if (i >= lastIndex) {
break;
}
}
buffer.close();
copy.finish();
buffer = copy;
}
int maxRows = limits.computeMaxRows(context);
if (maxRows > 0) {
int maxlen = buffer.size();
if (maxlen < maxRows) {
return;
}
buffer.truncate(maxRows);
}
}
Aggregations