/** * 写入数据 * * @param obj */ public static void insert(Object obj) { if (!Constants.IN_BDB) { MonitorLog.addStat( Constants.DATA_PERSISTENCE_LOG, new String[] {"Bdb Ingore"}, new Long[] {1l}); return; } Long kIndex = keyIndex.getAndIncrement(); try { byte[] vbytes = serialData(obj); byte[] kbytes = serialData("j" + kIndex); DatabaseEntry keyEntry = new DatabaseEntry(kbytes); DatabaseEntry valueEntry = new DatabaseEntry(vbytes); OperationStatus rtn = database.put(null, keyEntry, valueEntry); if (rtn != OperationStatus.SUCCESS) { logger.warn("write to bdb fail" + rtn.name()); MonitorLog.addStat( Constants.DATA_PERSISTENCE_LOG, new String[] {"Bdb Write Fail"}, new Long[] {1l}); } else { MonitorLog.addStat( Constants.DATA_PERSISTENCE_LOG, new String[] {"Bdb Success"}, new Long[] {1l}); } } catch (Exception e) { logger.error("write to bdb exception", e); MonitorLog.addStat( Constants.DATA_PERSISTENCE_LOG, new String[] {"Bdb Write Exception"}, new Long[] {1l}); } }
protected OperationStatus getNextNearestItem(DatabaseEntry headKey, DatabaseEntry result) throws DatabaseException { Cursor cursor = null; OperationStatus status; try { cursor = this.pendingUrisDB.openCursor(null, null); // get cap; headKey at this point should always point to // a queue-beginning cap entry (zero-length value) status = cursor.getSearchKey(headKey, result, null); if (status != OperationStatus.SUCCESS) { LOGGER.severe( "bdb queue cap missing: " + status.toString() + " " + new String(headKey.getData())); return status; } if (result.getData().length > 0) { LOGGER.severe("bdb queue has nonzero size: " + result.getData().length); return OperationStatus.KEYEXIST; } // get next item (real first item of queue) status = cursor.getNext(headKey, result, null); } finally { if (cursor != null) { cursor.close(); } } return status; }
@Override public synchronized void putDecision(Long instance, Decision decision) { keyBinding.objectToEntry(instance, key); dataBinding.objectToEntry(decision, data); OperationStatus status = db.put(null, key, data); if (logger.isDebugEnabled()) { logger.debug("DB put " + decision + " " + status.name()); } }
@Override public synchronized void putBallot(Long instance, int ballot) { keyBinding.objectToEntry(instance, key); ballotBinding.objectToEntry(ballot, ballot_data); OperationStatus status = db.put(null, key, ballot_data); if (logger.isDebugEnabled()) { logger.debug("DB put ballot " + ballot + " for instance " + instance + " " + status.name()); } }
@Override public synchronized boolean containsDecision(Long instance) { boolean found = false; keyBinding.objectToEntry(instance, key); OperationStatus status = db.get(null, key, data, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { found = true; } if (logger.isDebugEnabled()) { logger.debug("DB contains " + instance + " " + found + " (" + status.name() + ")"); } return found; }
@Override public synchronized Decision getDecision(Long instance) { keyBinding.objectToEntry(instance, key); Decision decision = null; OperationStatus status = db.get(null, key, data, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { decision = dataBinding.entryToObject(data); } if (logger.isDebugEnabled()) { logger.debug("DB get " + decision + " " + status.name()); } return decision; }
@Override public synchronized int getBallot(Long instance) { keyBinding.objectToEntry(instance, key); Integer ballot = null; OperationStatus status = db.get(null, key, ballot_data, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) { ballot = ballotBinding.entryToObject(ballot_data); } if (logger.isDebugEnabled()) { logger.debug("DB get ballot " + ballot + " for instance " + instance + " " + status.name()); } return ballot.intValue(); }
/** * todo:触发更新二级索引 * * @param key * @param value * @throws TddlException */ @Override public void put( ExecutionContext context, CloneableRecord key, CloneableRecord value, IndexMeta indexMeta, String dbName) throws TddlException { DatabaseEntry keyEntry = new DatabaseEntry(); DatabaseEntry valueEntry = new DatabaseEntry(); keyEntry.setData(indexCodecMap.get(indexMeta.getName()).getKey_codec().encode(key)); // 当临时表排序时候可能会用到临时表join,主键插入值为null // if (keyEntry.getData().length == 1 && !isTempTable) { // throw new RuntimeException("pk must not null."); // } if (value != null) { valueEntry.setData(indexCodecMap.get(indexMeta.getName()).getValue_codec().encode(value)); } else { valueEntry = emptyValueEntry; } try { ITransaction transaction = context.getTransaction(); com.sleepycat.je.Transaction txn = null; if (transaction != null && transaction instanceof JE_Transaction) { txn = ((JE_Transaction) transaction).txn; } OperationStatus operationStatus = getDatabase(dbName).put(txn, keyEntry, valueEntry); if (operationStatus.equals(OperationStatus.SUCCESS)) { return; } } catch (LockTimeoutException ex) { throw ex; } catch (ReplicaWriteException ex) { throw new TddlException(ExceptionErrorCodeUtils.Read_only, ex); } }