use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.
the class ODistributedOutput method formatLatency.
public static String formatLatency(final OHazelcastPlugin manager, final ODocument distribCfg) {
final List<OIdentifiable> rows = new ArrayList<OIdentifiable>();
final List<ODocument> members = distribCfg.field("members");
final StringBuilder buffer = new StringBuilder();
buffer.append("\nREPLICATION LATENCY AVERAGE (in milliseconds)");
final OTableFormatter table = new OTableFormatter(new OTableFormatter.OTableOutput() {
@Override
public void onMessage(final String text, final Object... args) {
buffer.append(String.format(text, args));
}
});
table.setColumnHidden("#");
if (members != null) {
// BUILD A SORTED SERVER LIST
final List<String> orderedServers = new ArrayList<String>(members.size());
for (ODocument fromMember : members) {
if (fromMember != null) {
String serverName = fromMember.field("name");
orderedServers.add(serverName);
table.setColumnAlignment(formatServerName(manager, serverName), OTableFormatter.ALIGNMENT.RIGHT);
}
}
Collections.sort(orderedServers);
for (String fromServer : orderedServers) {
// SEARCH FOR THE MEMBER
ODocument fromMember = null;
for (ODocument m : members) {
if (fromServer.equals(m.field("name"))) {
fromMember = m;
break;
}
}
if (fromMember == null)
// SKIP IT
continue;
final ODocument row = new ODocument();
rows.add(row);
row.field("Servers", formatServerName(manager, fromServer));
final ODocument latencies = fromMember.field("latencies");
if (latencies == null)
continue;
for (String toServer : orderedServers) {
String value = "";
if (toServer != null && !toServer.equals(fromServer)) {
final ODocument latency = latencies.field(toServer);
if (latency != null) {
value = String.format("%.2f", ((Float) latency.field("average") / 1000000f));
}
}
row.field(formatServerName(manager, toServer), value);
}
}
}
table.writeRecords(rows, -1);
buffer.append("\n");
return buffer.toString();
}
use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.
the class ODistributedOutput method formatMessageBetweenServers.
public static String formatMessageBetweenServers(final OHazelcastPlugin manager, final ODocument distribCfg) {
final List<OIdentifiable> rows = new ArrayList<OIdentifiable>();
final List<ODocument> members = distribCfg.field("members");
final StringBuilder buffer = new StringBuilder();
buffer.append("\nREPLICATION MESSAGE COUNTERS (servers: source on the row and destination on the column)");
final OTableFormatter table = new OTableFormatter(new OTableFormatter.OTableOutput() {
@Override
public void onMessage(final String text, final Object... args) {
buffer.append(String.format(text, args));
}
});
table.setColumnHidden("#");
if (members != null) {
// BUILD A SORTED SERVER LIST
final List<String> orderedServers = new ArrayList<String>(members.size());
for (ODocument fromMember : members) {
if (fromMember != null) {
String serverName = fromMember.field("name");
orderedServers.add(serverName);
table.setColumnAlignment(formatServerName(manager, serverName), OTableFormatter.ALIGNMENT.RIGHT);
}
}
Collections.sort(orderedServers);
final ODocument rowTotals = new ODocument();
for (String fromServer : orderedServers) {
// SEARCH FOR THE MEMBER
ODocument fromMember = null;
for (ODocument m : members) {
if (fromServer.equals(m.field("name"))) {
fromMember = m;
break;
}
}
if (fromMember == null)
// SKIP IT
continue;
final ODocument row = new ODocument();
rows.add(row);
row.field("Servers", formatServerName(manager, fromServer));
final ODocument latencies = fromMember.field("latencies");
if (latencies == null)
continue;
long total = 0;
for (String toServer : orderedServers) {
final String serverLabel = formatServerName(manager, toServer);
if (toServer != null && !toServer.equals(fromServer)) {
final ODocument latency = latencies.field(toServer);
if (latency != null) {
final Long entries = (Long) latency.field("entries");
total += entries;
row.field(serverLabel, String.format("%,d", entries));
// AGGREGATE IN TOTALS
sumTotal(rowTotals, serverLabel, total);
continue;
}
}
row.field(serverLabel, "");
}
row.field("TOTAL", String.format("%,d", total));
sumTotal(rowTotals, "TOTAL", total);
}
// FOOTER WITH THE TOTAL OF ALL THE ROWS
table.setFooter(rowTotals);
rowTotals.field("Servers", "TOTAL");
for (String fromServer : orderedServers) {
fromServer = formatServerName(manager, fromServer);
rowTotals.field(fromServer, String.format("%,d", rowTotals.field(fromServer)));
}
rowTotals.field("TOTAL", String.format("%,d", rowTotals.field("TOTAL")));
table.setColumnAlignment("TOTAL", OTableFormatter.ALIGNMENT.RIGHT);
}
table.writeRecords(rows, -1);
buffer.append("\n");
return buffer.toString();
}
use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.
the class OSQLHelper method parseValue.
/**
* Convert fields from text to real value. Supports: String, RID, Boolean, Float, Integer and NULL.
*
* @param iValue
* Value to convert.
* @return The value converted if recognized, otherwise VALUE_NOT_PARSED
*/
public static Object parseValue(String iValue, final OCommandContext iContext) {
if (iValue == null)
return null;
iValue = iValue.trim();
Object fieldValue = VALUE_NOT_PARSED;
if (iValue.startsWith("'") && iValue.endsWith("'") || iValue.startsWith("\"") && iValue.endsWith("\""))
// STRING
fieldValue = OStringSerializerHelper.decode(OIOUtils.getStringContent(iValue));
else if (iValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.LIST_END) {
// COLLECTION/ARRAY
final List<String> items = OStringSerializerHelper.smartSplit(iValue.substring(1, iValue.length() - 1), OStringSerializerHelper.RECORD_SEPARATOR);
final List<Object> coll = new ArrayList<Object>();
for (String item : items) {
coll.add(parseValue(item, iContext));
}
fieldValue = coll;
} else if (iValue.charAt(0) == OStringSerializerHelper.MAP_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.MAP_END) {
// MAP
final List<String> items = OStringSerializerHelper.smartSplit(iValue.substring(1, iValue.length() - 1), OStringSerializerHelper.RECORD_SEPARATOR);
final Map<Object, Object> map = new HashMap<Object, Object>();
for (String item : items) {
final List<String> parts = OStringSerializerHelper.smartSplit(item, OStringSerializerHelper.ENTRY_SEPARATOR);
if (parts == null || parts.size() != 2)
throw new OCommandSQLParsingException("Map found but entries are not defined as <key>:<value>");
Object key = OStringSerializerHelper.decode(parseValue(parts.get(0), iContext).toString());
Object value = parseValue(parts.get(1), iContext);
if (VALUE_NOT_PARSED == value) {
value = new OSQLPredicate(parts.get(1)).evaluate(iContext);
}
map.put(key, value);
}
if (map.containsKey(ODocumentHelper.ATTRIBUTE_TYPE))
// IT'S A DOCUMENT
// TODO: IMPROVE THIS CASE AVOIDING DOUBLE PARSING
fieldValue = new ODocument().fromJSON(iValue);
else
fieldValue = map;
} else if (iValue.charAt(0) == OStringSerializerHelper.EMBEDDED_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.EMBEDDED_END) {
// SUB-COMMAND
fieldValue = new OCommandSQL(iValue.substring(1, iValue.length() - 1));
((OCommandSQL) fieldValue).getContext().setParent(iContext);
} else if (ORecordId.isA(iValue))
// RID
fieldValue = new ORecordId(iValue.trim());
else {
if (iValue.equalsIgnoreCase("null"))
// NULL
fieldValue = null;
else if (iValue.equalsIgnoreCase("not null"))
// NULL
fieldValue = NOT_NULL;
else if (iValue.equalsIgnoreCase("defined"))
// NULL
fieldValue = DEFINED;
else if (iValue.equalsIgnoreCase("true"))
// BOOLEAN, TRUE
fieldValue = Boolean.TRUE;
else if (iValue.equalsIgnoreCase("false"))
// BOOLEAN, FALSE
fieldValue = Boolean.FALSE;
else if (iValue.startsWith("date(")) {
final OSQLFunctionRuntime func = OSQLHelper.getFunction(null, iValue);
if (func != null) {
fieldValue = func.execute(null, null, null, iContext);
}
} else {
final Object v = parseStringNumber(iValue);
if (v != null)
fieldValue = v;
}
}
return fieldValue;
}
use of com.orientechnologies.orient.core.record.impl.ODocument in project YCSB by brianfrankcooper.
the class OrientDBClient method update.
@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
while (true) {
try (ODatabaseDocumentTx db = databasePool.acquire()) {
final ODictionary<ORecord> dictionary = db.getMetadata().getIndexManager().getDictionary();
final ODocument document = dictionary.get(key);
if (document != null) {
for (Map.Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
document.field(entry.getKey(), entry.getValue());
}
document.save();
return Status.OK;
}
} catch (OConcurrentModificationException cme) {
continue;
} catch (Exception e) {
e.printStackTrace();
return Status.ERROR;
}
}
}
use of com.orientechnologies.orient.core.record.impl.ODocument in project YCSB by brianfrankcooper.
the class OrientDBClientTest method updateTest.
@Test
public void updateTest() {
String preupdateString = "preupdate";
String user0 = "user0";
String user1 = "user1";
String user2 = "user2";
OPartitionedDatabasePool pool = orientDBClient.getDatabasePool();
try (ODatabaseDocumentTx db = pool.acquire()) {
// Manually insert three documents
for (String key : Arrays.asList(user0, user1, user2)) {
ODocument doc = new ODocument(CLASS);
for (int i = 0; i < NUM_FIELDS; i++) {
doc.field(FIELD_PREFIX + i, preupdateString);
}
doc.save();
ODictionary<ORecord> dictionary = db.getDictionary();
dictionary.put(key, doc);
}
}
HashMap<String, ByteIterator> updateMap = new HashMap<>();
for (int i = 0; i < NUM_FIELDS; i++) {
updateMap.put(FIELD_PREFIX + i, new StringByteIterator(buildDeterministicValue(user1, FIELD_PREFIX + i)));
}
orientDBClient.update(CLASS, user1, updateMap);
try (ODatabaseDocumentTx db = pool.acquire()) {
ODictionary<ORecord> dictionary = db.getDictionary();
// Ensure that user0 record was not changed
ODocument result = dictionary.get(user0);
for (int i = 0; i < NUM_FIELDS; i++) {
assertEquals("Assert first row fields contain preupdateString", result.field(FIELD_PREFIX + i), preupdateString);
}
// Check that all the columns have expected values for user1 record
result = dictionary.get(user1);
for (int i = 0; i < NUM_FIELDS; i++) {
assertEquals("Assert updated row fields are correct", result.field(FIELD_PREFIX + i), updateMap.get(FIELD_PREFIX + i).toString());
}
// Ensure that user2 record was not changed
result = dictionary.get(user2);
for (int i = 0; i < NUM_FIELDS; i++) {
assertEquals("Assert third row fields contain preupdateString", result.field(FIELD_PREFIX + i), preupdateString);
}
}
}
Aggregations