public void testReallocateWrongPlacedReplicasStateOnlyIdToStartInDB() { final ONodeId idToStart = ONodeId.generateUniqueId(); final ONodeId nodeId = ONodeId.generateUniqueId(); final ONodeAddressStub nodeAddressStub = new ONodeAddressStub(nodeId); when(nodeLocal.state()).thenReturn(ODHTNode.NodeState.PRODUCTION); when(nodeLocal.getNodeAddress()).thenReturn(nodeAddressStub); final ORID startRecordId = new ORecordId(1, new OClusterPositionNodeId(idToStart)); final Iterator<ORecordMetadata> metadataIterator = mock(Iterator.class); when(nodeLocal.getLocalRingIterator(STORAGE_NAME, startRecordId.nextRid(), startRecordId)) .thenReturn(metadataIterator); when(metadataIterator.hasNext()).thenReturn(true); when(metadataIterator.next()) .thenReturn(new ORecordMetadata(startRecordId, new ODistributedVersion(0))); final ONodeId result = globalMaintenanceProtocol.reallocateWrongPlacedReplicas( STORAGE_NAME, CLUSTER_ID, nodeLocal, idToStart, 1, 1); Assert.assertEquals(result, nodeId); verify(nodeLocal).state(); verify(nodeLocal).getNodeAddress(); verify(nodeLocal).getLocalRingIterator(STORAGE_NAME, startRecordId.nextRid(), startRecordId); verifyZeroInteractions(replicaDistributionStrategy, nodeLookup); verifyNoMoreInteractions(nodeLocal); }
protected ORID[] getRange() { final ORID beginRange; final ORID endRange; final OSQLFilterCondition rootCondition = compiledFilter == null ? null : compiledFilter.getRootCondition(); if (compiledFilter == null || rootCondition == null) { if (request instanceof OSQLSynchQuery) beginRange = ((OSQLSynchQuery<ODocument>) request).getNextPageRID(); else beginRange = null; endRange = null; } else { final ORID conditionBeginRange = rootCondition.getBeginRidRange(); final ORID conditionEndRange = rootCondition.getEndRidRange(); final ORID nextPageRid; if (request instanceof OSQLSynchQuery) nextPageRid = ((OSQLSynchQuery<ODocument>) request).getNextPageRID(); else nextPageRid = null; if (conditionBeginRange != null && nextPageRid != null) beginRange = conditionBeginRange.compareTo(nextPageRid) > 0 ? conditionBeginRange : nextPageRid; else if (conditionBeginRange != null) beginRange = conditionBeginRange; else beginRange = nextPageRid; endRange = conditionEndRange; } return new ORID[] {beginRange, endRange}; }
public Object jsFunction_objectToDocument(Object object) { if (object instanceof NativeJavaObject) object = ((NativeJavaObject) object).unwrap(); ORID id = null; ODocument doc = null; if (object instanceof DbObject) id = (ORID) ((DbObject) object).getId(); // we have to convert java object to ODocument in a proper way if (id != null && !id.isNew()) { String className = ((DbObject) object).getClassName(); List<ODocument> result = getDb() .command( new OSQLSynchQuery<ODocument>( String.format("select from %1$s where @rid = ?", className))) .execute(id); if (result != null && result.size() > 0) doc = result.get(0); } else doc = (ODocument) Application.getDb().getRecordByUserObject(object, false); // reset thread local db getDb(); return wrap(doc); }
public int compareTo(final OIdentifiable iOther) { if (iOther == null) return 1; final ORID myRID = getIdentity(); final ORID otherRID = iOther.getIdentity(); if (myRID == null && otherRID == null) return 0; return myRID.compareTo(otherRID); }
protected boolean checkDeletedInTx() { OrientBaseGraph curGraph = getGraph(); if (curGraph == null) return false; ORID id; if (getRecord() != null) id = getRecord().getIdentity(); else return false; final ORecordOperation oper = curGraph.getRawGraph().getTransaction().getRecordEntry(id); if (oper == null) return id.isTemporary(); else return oper.type == ORecordOperation.DELETED; }
@Override public ORID getIdentity() { if (rawElement == null) return ORecordId.EMPTY_RECORD_ID; final ORID rid = rawElement.getIdentity(); if (!rid.isValid() && !isDetached()) { // SAVE THE RECORD TO OBTAIN A VALID RID graph.setCurrentGraphInThreadLocal(); graph.autoStartTransaction(); save(); } return rid; }
public static void addUserToRole(String username, String role) { boolean admin = true; if (!DbHelper.currentUsername().equals(BBConfiguration.getBaasBoxAdminUsername())) { DbHelper.reconnectAsAdmin(); admin = false; } String sqlAdd = "update ouser add roles = {TO_ROLE} where name = ?"; ORole toRole = RoleDao.getRole(role); ORID toRID = toRole.getDocument().getRecord().getIdentity(); sqlAdd = sqlAdd.replace("{TO_ROLE}", toRID.toString()); GenericDao.getInstance().executeCommand(sqlAdd, new String[] {username}); if (!admin) { DbHelper.reconnectAsAuthenticatedUser(); } }
public static void removeUserFromRole(String username, String role) { boolean admin = false; if (!DbHelper.currentUsername().equals(BBConfiguration.getBaasBoxAdminUsername())) { DbHelper.reconnectAsAdmin(); admin = true; } String sqlRemove = "update ouser remove roles = {FROM_ROLE} where roles contains {FROM_ROLE} and name = ?"; ORole fromRole = RoleDao.getRole(role); ORID fromRID = fromRole.getDocument().getRecord().getIdentity(); sqlRemove = sqlRemove.replace("{FROM_ROLE}", fromRID.toString()); GenericDao.getInstance().executeCommand(sqlRemove, new String[] {username}); if (admin) { DbHelper.reconnectAsAuthenticatedUser(); } }
public ORecordInternal<?> loadRecord( final ORID iRid, final ORecordInternal<?> iRecord, final String iFetchPlan, boolean ignonreCache, boolean loadTombstone) { if (iRid.isNew()) return null; return database.executeReadRecord( (ORecordId) iRid, iRecord, iFetchPlan, ignonreCache, loadTombstone); }
private void doDeserialization() { if (deserialized) return; int offset = 0; int entriesSize = OIntegerSerializer.INSTANCE.deserialize(serializedContent, offset); offset += OIntegerSerializer.INT_SIZE; for (int i = 0; i < entriesSize; i++) { ORID rid = OLinkSerializer.INSTANCE.deserialize(serializedContent, offset); offset += OLinkSerializer.RID_SIZE; OIdentifiable identifiable; if (rid.isTemporary()) identifiable = rid.getRecord(); else identifiable = rid; addEntry(identifiable); } deserialized = true; }
protected Object wrap(ODocument document) { Context context = Context.getCurrentContext(); Scriptable scope = getParentScope(); ScriptableObject current = (ScriptableObject) context.newObject(scope); for (String key : document.fieldNames()) { if (!key.startsWith("_")) { Object value = document.field(key); if (value instanceof ODocument) value = wrap((ODocument) value); else if (value instanceof List) value = wrap((List<?>) value); else value = Context.javaToJS(value, scope); current.put(key, current, value); } } ORID id = document.getIdentity(); if (!id.isNew()) current.put("id", current, id.toString()); // if (proxy == null) { String function; try { function = Files.toString(new File("scripts/me/o_nix/db/DB.template"), Charsets.UTF_8); } catch (IOException e) { throw new RuntimeException("DB script file not found.", e); } proxy = context.compileFunction(current, function, getClass().getSimpleName(), 0, null); // } // proxy.setParentScope(current); current.putConst("__noSuchMethod__", current, proxy); current.putConst("__original__", current, Context.javaToJS(document, scope)); return current; }
protected static boolean comparesValues( final Object iValue, final ORecord<?> iRecord, final boolean iConsiderIn) { // ORID && RECORD final ORID other = ((ORecord<?>) iRecord).getIdentity(); if (!other.isPersistent() && iRecord instanceof ODocument) { // ODOCUMENT AS RESULT OF SUB-QUERY: GET THE FIRST FIELD IF ANY final String[] firstFieldName = ((ODocument) iRecord).fieldNames(); if (firstFieldName.length > 0) { Object fieldValue = ((ODocument) iRecord).field(firstFieldName[0]); if (fieldValue != null) { if (iConsiderIn && OMultiValue.isMultiValue(fieldValue)) { for (Object o : OMultiValue.getMultiValueIterable(fieldValue)) { if (o != null && o.equals(iValue)) return true; } } return fieldValue.equals(iValue); } } return false; } return other.equals(iValue); }
protected void setIndex(final INDEX_TYPE iType, final ORID iRID) { // LOAD THE INDEX if (iRID != null && iRID.isValid()) { switch (iType) { case UNIQUE: index = new OPropertyIndexUnique(document.getDatabase(), this, iRID); break; case NOT_UNIQUE: index = new OPropertyIndexNotUnique(document.getDatabase(), this, iRID); break; case FULLTEXT: index = new OFullTextIndex(document.getDatabase(), this, iRID); break; } } }
public static void moveUsersToRole(String from, String to) { String sqlAdd = "update ouser add roles = {TO_ROLE} where roles contains {FROM_ROLE}"; String sqlRemove = "update ouser remove roles = {FROM_ROLE} where roles contains {FROM_ROLE}"; ORole fromRole = RoleDao.getRole(from); ORole toRole = RoleDao.getRole(to); ORID fromRID = fromRole.getDocument().getRecord().getIdentity(); ORID toRID = toRole.getDocument().getRecord().getIdentity(); sqlAdd = sqlAdd.replace("{TO_ROLE}", toRID.toString()).replace("{FROM_ROLE}", fromRID.toString()); sqlRemove = sqlRemove.replace("{TO_ROLE}", toRID.toString()).replace("{FROM_ROLE}", fromRID.toString()); GenericDao.getInstance().executeCommand(sqlAdd, new String[] {}); GenericDao.getInstance().executeCommand(sqlRemove, new String[] {}); }
public ORecordInternal<?> loadRecord( final ORID iRid, final ORecordInternal<?> iRecord, final String iFetchPlan, final boolean ignoreCache, final boolean loadTombstone, final OStorage.LOCKING_STRATEGY iLockingStrategy) { checkTransaction(); final ORecordInternal<?> txRecord = getRecord(iRid); if (txRecord == OTransactionRealAbstract.DELETED_RECORD) // DELETED IN TX return null; if (txRecord != null) { if (iRecord != null && txRecord != iRecord) OLogManager.instance() .warn( this, "Found record in transaction with the same RID %s but different instance. " + "Probably the record has been loaded from another transaction and reused on the current one: reload it " + "from current transaction before to update or delete it", iRecord.getIdentity()); return txRecord; } if (iRid.isTemporary()) return null; // DELEGATE TO THE STORAGE, NO TOMBSTONES SUPPORT IN TX MODE final ORecordInternal<?> record = database.executeReadRecord( (ORecordId) iRid, iRecord, iFetchPlan, ignoreCache, false, iLockingStrategy); if (record != null) addRecord(record, ORecordOperation.LOADED, null); return record; }
public void testReallocateWrongPlacedReplicasStateNodeRecordsTenRecordsTwoRecordsIsOutOfDate() { final ONodeId idToStart = ONodeId.generateUniqueId(); final ONodeId nodeId = ONodeId.generateUniqueId(); final ONodeId successorNodeId = ONodeId.generateUniqueId(); final ONodeAddressStub nodeAddressStub = new ONodeAddressStub(nodeId); when(nodeLocal.state()).thenReturn(ODHTNode.NodeState.PRODUCTION); when(nodeLocal.getNodeAddress()).thenReturn(nodeAddressStub); final ORID startRecordId = new ORecordId(1, new OClusterPositionNodeId(idToStart)); final ORID nextRecordId = startRecordId.nextRid(); final Iterator<ORecordMetadata> metadataIterator = mock(Iterator.class); when(nodeLocal.getLocalRingIterator(STORAGE_NAME, startRecordId.nextRid(), startRecordId)) .thenReturn(metadataIterator); when(metadataIterator.hasNext()).thenReturn(true); when(metadataIterator.next()) .thenReturn(new ORecordMetadata(nextRecordId, new ODistributedVersion(0))); when(nodeLocal.findSuccessor( ((OClusterPositionNodeId) nextRecordId.getClusterPosition()).getNodeId())) .thenReturn(new ONodeAddressStub(successorNodeId)); final ODHTNode successorNode = mock(ODHTNode.class); when(nodeLookup.findById(new ONodeAddressStub(successorNodeId))).thenReturn(successorNode); final ONodeAddress[] recordSuccessors = new ONodeAddress[] { new ONodeAddressStub(ONodeId.generateUniqueId()), new ONodeAddressStub(ONodeId.generateUniqueId()), new ONodeAddressStub(ONodeId.generateUniqueId()) }; when(successorNode.getSuccessors()).thenReturn(recordSuccessors); when(replicaDistributionStrategy.chooseReplicas(recordSuccessors, 2, 1)) .thenReturn( new Set[] { new HashSet<ONodeAddress>(Arrays.asList(recordSuccessors[0])), new HashSet<ONodeAddress>(Arrays.asList(recordSuccessors[1])) }); final ArrayList<ODocument> missedRecords = new ArrayList<ODocument>(); final List<ORecordMetadata> missedMetadata = new ArrayList<ORecordMetadata>(); for (int i = 0; i < 10; i++) { final ORecordId missedRid = new ORecordId(1, new OClusterPositionNodeId(ONodeId.generateUniqueId())); final ODocument doc = new ODocument(); doc.setIdentity(missedRid); doc.field("value", "data"); missedRecords.add(doc); missedMetadata.add(new ORecordMetadata(missedRid, doc.getRecordVersion())); } when(nodeLocal.getLocalRingIterator( STORAGE_NAME, startRecordId, new ORecordId(1, new OClusterPositionNodeId(successorNodeId)))) .thenReturn(missedMetadata.iterator()); final ODHTNode firstReplicaHolder = mock(ODHTNode.class); final ODHTNode secondReplicaHolder = mock(ODHTNode.class); when(nodeLookup.findById(recordSuccessors[0])).thenReturn(firstReplicaHolder); when(nodeLookup.findById(recordSuccessors[1])).thenReturn(secondReplicaHolder); final ORID[] missedIDs = new ORID[10]; for (int i = 0; i < 10; i++) missedIDs[i] = missedMetadata.get(i).getRid(); when(successorNode.findMissedRecords( STORAGE_NAME, missedMetadata.toArray(new ORecordMetadata[0]))) .thenReturn(missedIDs); when(firstReplicaHolder.findMissedRecords( STORAGE_NAME, missedMetadata.toArray(new ORecordMetadata[0]))) .thenReturn(missedIDs); when(secondReplicaHolder.findMissedRecords( STORAGE_NAME, missedMetadata.toArray(new ORecordMetadata[0]))) .thenReturn(missedIDs); for (ODocument missedRecord : missedRecords) when((ORecordInternal) nodeLocal.readRecordLocal(STORAGE_NAME, missedRecord.getIdentity())) .thenReturn(missedRecord); final ODocument outOfDateRecordOne = missedRecords.get(2); final ODocument outOfDateRecordTwo = missedRecords.get(3); doThrow( new OConcurrentModificationException( outOfDateRecordOne.getIdentity(), new ODistributedVersion(0), outOfDateRecordOne.getRecordVersion(), 0)) .when(nodeLocal) .cleanOutRecord( STORAGE_NAME, outOfDateRecordOne.getIdentity(), outOfDateRecordOne.getRecordVersion()); doThrow( new OConcurrentModificationException( outOfDateRecordTwo.getIdentity(), new ODistributedVersion(0), outOfDateRecordTwo.getRecordVersion(), 0)) .when(nodeLocal) .cleanOutRecord( STORAGE_NAME, outOfDateRecordTwo.getIdentity(), outOfDateRecordTwo.getRecordVersion()); final ONodeId result = globalMaintenanceProtocol.reallocateWrongPlacedReplicas( STORAGE_NAME, CLUSTER_ID, nodeLocal, idToStart, 2, 1); Assert.assertEquals(result, successorNodeId); for (ODocument record : missedRecords) { verify(successorNode).updateReplica(STORAGE_NAME, record, false); verify(firstReplicaHolder).updateReplica(STORAGE_NAME, record, false); verify(secondReplicaHolder).updateReplica(STORAGE_NAME, record, false); verify(nodeLocal) .cleanOutRecord(STORAGE_NAME, record.getIdentity(), record.getRecordVersion()); } }
public void testReallocateWrongPlacedReplicasStateNodeRecordsNodeIsAsyncReplicaHolder() { final ONodeId idToStart = ONodeId.generateUniqueId(); final ONodeId nodeId = ONodeId.generateUniqueId(); final ONodeId successorNodeId = ONodeId.generateUniqueId(); final ONodeAddressStub nodeAddressStub = new ONodeAddressStub(nodeId); when(nodeLocal.state()).thenReturn(ODHTNode.NodeState.PRODUCTION); when(nodeLocal.getNodeAddress()).thenReturn(nodeAddressStub); final ORID startRecordId = new ORecordId(1, new OClusterPositionNodeId(idToStart)); final ORID nextRecordId = startRecordId.nextRid(); final Iterator<ORecordMetadata> metadataIterator = mock(Iterator.class); when(nodeLocal.getLocalRingIterator(STORAGE_NAME, startRecordId.nextRid(), startRecordId)) .thenReturn(metadataIterator); when(metadataIterator.hasNext()).thenReturn(true); when(metadataIterator.next()) .thenReturn(new ORecordMetadata(nextRecordId, new ODistributedVersion(0))); when(nodeLocal.findSuccessor( ((OClusterPositionNodeId) nextRecordId.getClusterPosition()).getNodeId())) .thenReturn(new ONodeAddressStub(successorNodeId)); final ODHTNode successorNode = mock(ODHTNode.class); when(nodeLookup.findById(new ONodeAddressStub(successorNodeId))).thenReturn(successorNode); final ONodeAddress[] recordSuccessors = new ONodeAddress[] { new ONodeAddressStub(nodeId), new ONodeAddressStub(ONodeId.generateUniqueId()), new ONodeAddressStub(ONodeId.generateUniqueId()) }; when(successorNode.getSuccessors()).thenReturn(recordSuccessors); when(replicaDistributionStrategy.chooseReplicas(recordSuccessors, 1, 1)) .thenReturn( new Set[] { new HashSet<ONodeAddress>(Arrays.asList(recordSuccessors[1])), new HashSet<ONodeAddress>(Arrays.asList(recordSuccessors[0])) }); final ONodeId result = globalMaintenanceProtocol.reallocateWrongPlacedReplicas( STORAGE_NAME, CLUSTER_ID, nodeLocal, idToStart, 1, 1); Assert.assertEquals(result, nodeId); verify(nodeLocal).state(); verify(nodeLocal).getNodeAddress(); verify(nodeLocal).getLocalRingIterator(STORAGE_NAME, startRecordId.nextRid(), startRecordId); verify(nodeLocal) .findSuccessor(((OClusterPositionNodeId) nextRecordId.getClusterPosition()).getNodeId()); verify(nodeLookup).findById(new ONodeAddressStub(successorNodeId)); verifyZeroInteractions(nodeLocal); verifyNoMoreInteractions(nodeLocal); verifyNoMoreInteractions(nodeLookup); }
public void writeRID(final ORID iRID) throws IOException { writeShort((short) iRID.getClusterId()); writeLong(iRID.getClusterPosition()); }
public ORecordInternal<?> loadRecord( final ORID iRid, final ORecordInternal<?> iRecord, final String iFetchPlan) { if (iRid.isNew()) return null; return database.executeReadRecord((ORecordId) iRid, iRecord, iFetchPlan, false); }
public final Object getORIDAsString() { final ORID aOrid = getORID(); if (aOrid == null) return null; return aOrid.toString(); }