use of io.questdb.std.datetime.DateFormat in project questdb by bluestreak01.
the class ToStrTimestampFunctionFactory method newInstance.
@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) throws SqlException {
Function fmt = args.getQuick(1);
CharSequence format = fmt.getStr(null);
if (format == null) {
throw SqlException.$(argPositions.getQuick(1), "format must not be null");
}
DateFormat timestampFormat = tlCompiler.get().compile(fmt.getStr(null));
Function var = args.getQuick(0);
if (var.isConstant()) {
long value = var.getTimestamp(null);
if (value == Numbers.LONG_NaN) {
return StrConstant.NULL;
}
StringSink sink = tlSink.get();
sink.clear();
timestampFormat.format(value, configuration.getDefaultDateLocale(), "Z", sink);
return new StrConstant(sink);
}
return new ToCharDateFFunc(args.getQuick(0), timestampFormat, configuration.getDefaultDateLocale());
}
use of io.questdb.std.datetime.DateFormat in project questdb by bluestreak01.
the class TableReaderTest method testRemovePartitionByDayCannotDeleteDir.
@Test
public void testRemovePartitionByDayCannotDeleteDir() throws Exception {
TestUtils.assertMemoryLeak(() -> {
int N = 100;
int N_PARTITIONS = 5;
long timestampUs = TimestampFormatUtils.parseTimestamp("2017-12-11T00:00:00.000Z");
long stride = 100;
int bandStride = 1000;
int totalCount = 0;
FilesFacade ff = new FilesFacadeImpl() {
@Override
public int rmdir(Path name) {
if (Chars.endsWith(name, "2017-12-14" + Files.SEPARATOR)) {
return 1;
}
return super.rmdir(name);
}
};
CairoConfiguration configuration = new DefaultCairoConfiguration(root) {
@Override
public FilesFacade getFilesFacade() {
return ff;
}
};
// model table
try (TableModel model = new TableModel(configuration, "w", PartitionBy.DAY).col("l", ColumnType.LONG).timestamp()) {
CairoTestUtils.create(model);
}
try (TableWriter writer = new TableWriter(configuration, "w")) {
for (int k = 0; k < N_PARTITIONS; k++) {
long band = k * bandStride;
for (int i = 0; i < N; i++) {
TableWriter.Row row = writer.newRow(timestampUs);
row.putLong(0, band + i);
row.append();
writer.commit();
timestampUs += stride;
}
timestampUs = Timestamps.addDays(Timestamps.floorDD(timestampUs), 1);
}
Assert.assertEquals(N * N_PARTITIONS, writer.size());
DateFormat fmt = TableWriter.selectPartitionDirFmt(PartitionBy.DAY);
assert fmt != null;
final long timestamp = fmt.parse("2017-12-14", null);
Assert.assertTrue(writer.removePartition(timestamp));
Assert.assertFalse(writer.removePartition(timestamp));
Assert.assertEquals(N * (N_PARTITIONS - 1), writer.size());
}
// now open table reader having partition gap
try (TableReader reader = new TableReader(configuration, "w")) {
Assert.assertEquals(N * (N_PARTITIONS - 1), reader.size());
int previousBand = -1;
int bandCount = 0;
RecordCursor cursor = reader.getCursor();
final Record record = cursor.getRecord();
while (cursor.hasNext()) {
long value = record.getLong(0);
int band = (int) ((value / bandStride) * bandStride);
if (band != previousBand) {
// make sure we don#t pick up deleted partition
Assert.assertNotEquals(3000, band);
if (previousBand != -1) {
Assert.assertEquals(N, bandCount);
}
previousBand = band;
bandCount = 0;
}
bandCount++;
totalCount++;
}
Assert.assertEquals(N, bandCount);
}
Assert.assertEquals(N * (N_PARTITIONS - 1), totalCount);
});
}
use of io.questdb.std.datetime.DateFormat in project questdb by bluestreak01.
the class TableReaderTest method testRemoveActivePartition.
private void testRemoveActivePartition(int partitionBy, NextPartitionTimestampProvider provider, CharSequence partitionNameToDelete) throws Exception {
TestUtils.assertMemoryLeak(() -> {
int N = 100;
int N_PARTITIONS = 5;
long timestampUs = TimestampFormatUtils.parseTimestamp("2017-12-11T00:00:00.000Z");
long stride = 100;
int bandStride = 1000;
int totalCount = 0;
// model table
try (TableModel model = new TableModel(configuration, "w", partitionBy).col("l", ColumnType.LONG).timestamp()) {
CairoTestUtils.create(model);
}
try (TableWriter writer = new TableWriter(configuration, "w")) {
for (int k = 0; k < N_PARTITIONS; k++) {
long band = k * bandStride;
for (int i = 0; i < N; i++) {
TableWriter.Row row = writer.newRow(timestampUs);
row.putLong(0, band + i);
row.append();
writer.commit();
timestampUs += stride;
}
timestampUs = provider.getNext(timestampUs);
}
Assert.assertEquals(500, writer.size());
// now open table reader having partition gap
try (TableReader reader = new TableReader(configuration, "w")) {
Assert.assertEquals(500, reader.size());
RecordCursor cursor = reader.getCursor();
Record record = cursor.getRecord();
while (cursor.hasNext()) {
record.getLong(0);
totalCount++;
}
Assert.assertEquals(500, totalCount);
DateFormat fmt = TableWriter.selectPartitionDirFmt(partitionBy);
assert fmt != null;
Assert.assertFalse(writer.removePartition(fmt.parse(partitionNameToDelete, null)));
Assert.assertEquals(500, writer.size());
reader.reload();
totalCount = 0;
Assert.assertEquals(N * N_PARTITIONS, reader.size());
cursor = reader.getCursor();
record = cursor.getRecord();
while (cursor.hasNext()) {
record.getLong(0);
totalCount++;
}
Assert.assertEquals(N * N_PARTITIONS, totalCount);
}
}
});
}
use of io.questdb.std.datetime.DateFormat in project questdb by bluestreak01.
the class TableBackupTest method setFinalBackupPath.
private void setFinalBackupPath(int n) {
DateFormat timestampFormat = mainConfiguration.getBackupDirTimestampFormat();
finalBackupPath.of(mainConfiguration.getBackupRoot()).slash();
timestampFormat.format(mainConfiguration.getMicrosecondClock().getTicks(), mainConfiguration.getDefaultDateLocale(), null, finalBackupPath);
if (n > 0) {
finalBackupPath.put('.');
finalBackupPath.put(n);
}
finalBackupPath.slash$();
finalBackupPathLen = finalBackupPath.length();
finalBackupPath.trimTo(finalBackupPathLen).concat(PropServerConfiguration.DB_DIRECTORY).slash$();
}
use of io.questdb.std.datetime.DateFormat in project questdb by bluestreak01.
the class TableBackupTest method setup.
@Before
public void setup() throws IOException {
path = new Path();
finalBackupPath = new Path();
mkdirsErrno = -1;
renameErrno = -1;
FilesFacade ff = new FilesFacadeImpl() {
private int nextErrno = -1;
@Override
public int errno() {
if (nextErrno != -1) {
int errno = nextErrno;
nextErrno = -1;
return errno;
}
return super.errno();
}
@Override
public int mkdirs(LPSZ path, int mode) {
if (mkdirsErrno != -1 && --mkdirsErrnoCountDown < 1) {
nextErrno = mkdirsErrno;
mkdirsErrno = -1;
mkdirsErrnoCountDown = 0;
return -1;
}
return super.mkdirs(path, mode);
}
@Override
public boolean rename(LPSZ from, LPSZ to) {
if (renameErrno != -1) {
nextErrno = renameErrno;
renameErrno = -1;
return false;
}
return super.rename(from, to);
}
};
CharSequence root = temp.newFolder(String.format("dbRoot%c%s", Files.SEPARATOR, PropServerConfiguration.DB_DIRECTORY)).getAbsolutePath();
backupRoot = temp.newFolder("dbBackupRoot").getAbsolutePath();
mainConfiguration = new DefaultCairoConfiguration(root) {
@Override
public FilesFacade getFilesFacade() {
return ff;
}
@Override
public CharSequence getBackupRoot() {
return backupRoot;
}
@Override
public DateFormat getBackupDirTimestampFormat() {
return new TimestampFormatCompiler().compile("ddMMMyyyy");
}
};
mainEngine = new CairoEngine(mainConfiguration);
mainCompiler = new SqlCompiler(mainEngine);
mainSqlExecutionContext = new SqlExecutionContextImpl(mainEngine, 1).with(AllowAllCairoSecurityContext.INSTANCE, new BindVariableServiceImpl(mainConfiguration), null, -1, null);
// dummy configuration
File confRoot = new File(PropServerConfiguration.confRoot(root));
Assert.assertTrue(confRoot.mkdirs());
Assert.assertTrue(new File(confRoot, "server.conf").createNewFile());
Assert.assertTrue(new File(confRoot, "mime.types").createNewFile());
Assert.assertTrue(new File(confRoot, "log-file.conf").createNewFile());
Assert.assertTrue(new File(confRoot, "date.formats").createNewFile());
}
Aggregations