Skip to content

Commit

Permalink
Merge pull request #20 from project-tsurugi/update-stock-delete
Browse files Browse the repository at this point in the history
update-stock delete old history
  • Loading branch information
hishidama authored Jan 19, 2024
2 parents e29a9ae + 54eb56e commit a7ed3c9
Show file tree
Hide file tree
Showing 14 changed files with 399 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ online.schedule.execute.per.minute.show-quantity=-1
online.schedule.execute.per.minute.show-cost=-1
periodic.schedule.interval.update-stock=0
periodic.schedule.update-stock.split.size=60
periodic.schedule.update-stock.keep.size=4


## online-command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ online.schedule.execute.per.minute.show-quantity=-1
online.schedule.execute.per.minute.show-cost=-1
periodic.schedule.interval.update-stock=0
periodic.schedule.update-stock.split.size=60
periodic.schedule.update-stock.keep.size=4


## online-command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ online.schedule.execute.per.minute.show-quantity=-1
online.schedule.execute.per.minute.show-cost=-1
periodic.schedule.interval.update-stock=0
periodic.schedule.update-stock.split.size=60
periodic.schedule.update-stock.keep.size=4


## online-command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ online.schedule.execute.per.minute.show-quantity=-1
online.schedule.execute.per.minute.show-cost=-1
periodic.schedule.interval.update-stock=0
periodic.schedule.update-stock.split.size=60
periodic.schedule.update-stock.keep.size=4


## online-command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ online.schedule.execute.per.minute.show-quantity=-1
online.schedule.execute.per.minute.show-cost=-1
periodic.schedule.interval.update-stock=0
periodic.schedule.update-stock.split.size=60
periodic.schedule.update-stock.keep.size=4


## online-command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

import com.tsurugidb.benchmark.costaccounting.db.entity.StockHistory;
import com.tsurugidb.benchmark.costaccounting.db.iceaxe.domain.BenchVariable;
import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable;
import com.tsurugidb.benchmark.costaccounting.db.entity.StockHistoryDateTime;

/**
* 在庫履歴DAO
Expand All @@ -16,11 +16,6 @@ public interface StockHistoryDao {

public static final String TABLE_NAME = "stock_history";

public static final String PS_COND_DATE = "s_date = ?";

public static final TgBindVariable<LocalDate> vDate = BenchVariable.ofDate("date");
public static final String TG_COND_DATE = "s_date = " + vDate.sqlName();

/**
* <pre>
* truncate table stock_history
Expand All @@ -45,19 +40,43 @@ public interface StockHistoryDao {

int[] insertBatch(Collection<StockHistory> entityList);

/**
* <pre>
* select distinct s_date, s_time from stock_history
* order by s_date, s_time
* </pre>
*/
List<StockHistoryDateTime> selectDistinctDateTime();

/**
* <pre>
* delete from stock_history
* where (s_date < :date) or (s_date = :date and s_time <= :time)
* </pre>
*/
int deleteOldDateTime(LocalDate date, LocalTime time);

/**
* <pre>
* delete from stock_history
* where ((s_date < :date) or (s_date = :date and s_time <= :time) and s_f_id = :factoryId
* </pre>
*/
int deleteOldDateTime(LocalDate date, LocalTime time, int factoryId);

/**
* <pre>
* insert into stock_history
* select ... from cost_master
* </prE>
* </pre>
*/
void insertSelectFromCostMaster(LocalDate date, LocalTime time);

/**
* <pre>
* insert into stock_history
* select ... from cost_master where c_f_id = :factoryId
* </prE>
* </pre>
*/
void insertSelectFromCostMaster(LocalDate date, LocalTime time, int factoryId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.tsurugidb.benchmark.costaccounting.db.entity;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Objects;

/**
* key(s_date, s_time) for stock_history
*/
public class StockHistoryDateTime implements Comparable<StockHistoryDateTime> {

/** s_date date(8) */
private LocalDate sDate;

/** s_time time(6) */
private LocalTime sTime;

public void setSDate(LocalDate value) {
this.sDate = value;
}

public LocalDate getSDate() {
return this.sDate;
}

public void setSTime(LocalTime value) {
this.sTime = value;
}

public LocalTime getSTime() {
return this.sTime;
}

@Override
public int compareTo(StockHistoryDateTime o) {
int c = sDate.compareTo(o.getSDate());
if (c != 0) {
return c;
}
return sTime.compareTo(o.getSTime());
}

@Override
public int hashCode() {
return Objects.hash(sDate, sTime);
}

@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof StockHistoryDateTime)) {
return false;
}

var other = (StockHistoryDateTime) obj;
return Objects.equals(sDate, other.sDate) && Objects.equals(sTime, other.sTime);
}

@Override
public String toString() {
return "(s_date=" + sDate + ", s_time=" + sTime + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import com.tsurugidb.benchmark.costaccounting.db.dao.StockHistoryDao;
import com.tsurugidb.benchmark.costaccounting.db.entity.StockHistory;
import com.tsurugidb.benchmark.costaccounting.db.entity.StockHistoryDateTime;
import com.tsurugidb.benchmark.costaccounting.db.iceaxe.CostBenchDbManagerIceaxe;
import com.tsurugidb.benchmark.costaccounting.db.iceaxe.domain.BenchVariable;
import com.tsurugidb.benchmark.costaccounting.util.BenchConst;
import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters;
import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable;
import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableInteger;
import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping;
import com.tsurugidb.iceaxe.sql.result.TgResultMapping;

public class StockHistoryDaoIceaxe extends IceaxeDao<StockHistory> implements StockHistoryDao {

public static final TgBindVariable<LocalDate> vDate = BenchVariable.ofDate("date");
public static final TgBindVariable<LocalTime> vTime = BenchVariable.ofTime("time");
public static final TgBindVariableInteger vFactory = BenchVariable.ofInt("fId");

private static final List<IceaxeColumn<StockHistory, ?>> COLUMN_LIST;
static {
List<IceaxeColumn<StockHistory, ?>> list = new ArrayList<>();
Expand Down Expand Up @@ -51,6 +63,59 @@ public int[] insertBatch(Collection<StockHistory> entityList) {
return doInsert(entityList, false);
}

@Override
public List<StockHistoryDateTime> selectDistinctDateTime() {
var ps = selectDistinctDateTimeCache.get();
var list = executeAndGetList(ps);
if (BenchConst.WORKAROUND) {
Collections.sort(list);
}
return list;
}

private final CacheQuery<StockHistoryDateTime> selectDistinctDateTimeCache = new CacheQuery<>() {
@Override
protected void initialize() {
this.sql = "select distinct s_date, s_time from " + TABLE_NAME //
+ " order by s_date, s_time";
this.resultMapping = TgResultMapping.of(StockHistoryDateTime::new) //
.addDate("s_date", StockHistoryDateTime::setSDate) //
.addTime("s_time", StockHistoryDateTime::setSTime);
}
};

@Override
public int deleteOldDateTime(LocalDate date, LocalTime time) {
var ps = deleteOldDateTimeCache.get();
var parameter = TgBindParameters.of(vDate.bind(date), vTime.bind(time));
return executeAndGetCount(ps, parameter);
}

private final CachePreparedStatement<TgBindParameters> deleteOldDateTimeCache = new CachePreparedStatement<>() {
@Override
protected void initialize() {
this.sql = "delete from " + TABLE_NAME //
+ " where (s_date < " + vDate + ") or (s_date = " + vDate + " and s_time <= " + vTime + ")";
this.parameterMapping = TgParameterMapping.of(vDate, vTime);
}
};

@Override
public int deleteOldDateTime(LocalDate date, LocalTime time, int factoryId) {
var ps = deleteOldDateTimeFactoryCache.get();
var parameter = TgBindParameters.of(vDate.bind(date), vTime.bind(time), vFactory.bind(factoryId));
return executeAndGetCount(ps, parameter);
}

private final CachePreparedStatement<TgBindParameters> deleteOldDateTimeFactoryCache = new CachePreparedStatement<>() {
@Override
protected void initialize() {
this.sql = "delete from " + TABLE_NAME //
+ " where ((s_date < " + vDate + ") or (s_date = " + vDate + " and s_time <= " + vTime + ")) and s_f_id = " + vFactory;
this.parameterMapping = TgParameterMapping.of(vDate, vTime, vFactory);
}
};

@Override
public void insertSelectFromCostMaster(LocalDate date, LocalTime time) {
throw new UnsupportedOperationException("not yet impl");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.tsurugidb.benchmark.costaccounting.db.dao.CostMasterDao;
import com.tsurugidb.benchmark.costaccounting.db.dao.StockHistoryDao;
import com.tsurugidb.benchmark.costaccounting.db.entity.StockHistory;
import com.tsurugidb.benchmark.costaccounting.db.entity.StockHistoryDateTime;
import com.tsurugidb.benchmark.costaccounting.db.jdbc.CostBenchDbManagerJdbc;

public class StockHistoryDaoJdbc extends JdbcDao<StockHistory> implements StockHistoryDao {
Expand Down Expand Up @@ -54,6 +55,44 @@ public int[] insertBatch(Collection<StockHistory> entityList) {
return doInsert(entityList);
}

@Override
public List<StockHistoryDateTime> selectDistinctDateTime() {
String sql = "select distinct s_date, s_time from " + TABLE_NAME //
+ " order by s_date, s_time";
return executeQueryList(sql, ps -> {
}, rs -> {
var entity = new StockHistoryDateTime();
entity.setSDate(JdbcUtil.getDate(rs, "s_date"));
entity.setSTime(JdbcUtil.getTime(rs, "s_time"));
return entity;
});
}

@Override
public int deleteOldDateTime(LocalDate date, LocalTime time) {
String sql = "delete from " + TABLE_NAME //
+ " where (s_date < ?) or (s_date = ? and s_time <= ?)";
return executeUpdate(sql, ps -> {
int i = 1;
JdbcUtil.setDate(ps, i++, date);
JdbcUtil.setDate(ps, i++, date);
JdbcUtil.setTime(ps, i++, time);
});
}

@Override
public int deleteOldDateTime(LocalDate date, LocalTime time, int factoryId) {
String sql = "delete from " + TABLE_NAME //
+ " where ((s_date < ?) or (s_date = ? and s_time <= ?)) and s_f_id = ?";
return executeUpdate(sql, ps -> {
int i = 1;
JdbcUtil.setDate(ps, i++, date);
JdbcUtil.setDate(ps, i++, date);
JdbcUtil.setTime(ps, i++, time);
JdbcUtil.setInt(ps, i++, factoryId);
});
}

private static final String INSERT_SELECT_FROM_COST_MASTER_SQL = "insert into " + TABLE_NAME //
+ "(" + COLUMN_LIST.stream().map(c -> c.getName()).collect(Collectors.joining(", ")) + ")" //
+ " select ?, ?, c_f_id, c_i_id, c_stock_unit, c_stock_quantity, c_stock_amount from " + CostMasterDao.TABLE_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.tsurugidb.benchmark.costaccounting.db.dao.StockHistoryDao;
import com.tsurugidb.benchmark.costaccounting.db.entity.StockHistory;
import com.tsurugidb.benchmark.costaccounting.db.entity.StockHistoryDateTime;
import com.tsurugidb.benchmark.costaccounting.db.tsubakuro.CostBenchDbManagerTsubakuro;
import com.tsurugidb.sql.proto.SqlCommon.AtomType;

Expand Down Expand Up @@ -53,6 +54,21 @@ public int[] insertBatch(Collection<StockHistory> entityList) {
return doInsert(entityList, false);
}

@Override
public List<StockHistoryDateTime> selectDistinctDateTime() {
throw new UnsupportedOperationException("not yet impl");
}

@Override
public int deleteOldDateTime(LocalDate date, LocalTime time) {
throw new UnsupportedOperationException("not yet impl");
}

@Override
public int deleteOldDateTime(LocalDate date, LocalTime time, int factoryId) {
throw new UnsupportedOperationException("not yet impl");
}

@Override
public void insertSelectFromCostMaster(LocalDate date, LocalTime time) {
throw new UnsupportedOperationException("not yet impl");
Expand Down
Loading

0 comments on commit a7ed3c9

Please sign in to comment.