Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "ifmap/ifmap_link_table.h" 6 : 7 : #include <boost/bind/bind.hpp> 8 : 9 : #include "db/db.h" 10 : #include "db/db_graph.h" 11 : #include "db/db_table_partition.h" 12 : #include "ifmap/ifmap_link.h" 13 : #include "ifmap/ifmap_log.h" 14 : #include "ifmap/ifmap_log_types.h" 15 : 16 : using namespace std; 17 : using namespace boost::placeholders; 18 : 19 567 : IFMapLinkTable::IFMapLinkTable(DB *db, const string &name, DBGraph *graph) 20 567 : : DBGraphTable(db, name, graph) { 21 567 : } 22 : 23 0 : void IFMapLinkTable::Input(DBTablePartition *partition, DBClient *client, 24 : DBRequest *req) { 25 0 : assert(false); 26 : } 27 : 28 9744 : std::unique_ptr<DBEntry> IFMapLinkTable::AllocEntry( 29 : const DBRequestKey *key) const { 30 9744 : const RequestKey *rkey = static_cast<const RequestKey *>(key); 31 9744 : unique_ptr<DBEntry> entry(new IFMapLink(rkey->name)); 32 9744 : return entry; 33 : } 34 : 35 : // Generate an unique name for the link node and it should 36 : // be independent of the order in which the right and left nodes are specified 37 9715 : std::string IFMapLinkTable::LinkKey(const string &metadata, 38 : IFMapNode *left, IFMapNode *right) { 39 9715 : ostringstream oss; 40 9715 : if (left->ToString() < right->ToString()) { 41 3879 : oss << metadata << "," << left->ToString() << "," << right->ToString(); 42 : } else { 43 5836 : oss << metadata << "," << right->ToString() << "," << left->ToString(); 44 : } 45 19430 : return oss.str(); 46 9715 : } 47 : 48 4182 : IFMapLink *IFMapLinkTable::AddLink(IFMapNode *left, IFMapNode *right, 49 : const string &metadata, uint64_t sequence_number, 50 : const IFMapOrigin &origin) { 51 : DBTablePartition *partition = 52 4182 : static_cast<DBTablePartition *>(GetTablePartition(0)); 53 : 54 4182 : string link_name = LinkKey(metadata, left, right); 55 4182 : IFMapLink *link = FindLink(link_name); 56 4182 : if (link) { 57 4 : assert(link->IsDeleted()); 58 4 : link->SetLinkRevival(true); 59 4 : link->ClearDelete(); 60 4 : link->set_last_change_at_to_now(); 61 4 : partition->Change(link); 62 : } else { 63 4178 : link = new IFMapLink(link_name); 64 4178 : partition->Add(link); 65 : } 66 4182 : link->SetProperties(left, right, metadata, sequence_number, origin); 67 4182 : assert(dynamic_cast<IFMapNode *>(left)); 68 4182 : assert(dynamic_cast<IFMapNode *>(right)); 69 4182 : return link; 70 4182 : } 71 : 72 5501 : IFMapLink *IFMapLinkTable::FindLink(const string &metadata, IFMapNode *left, IFMapNode *right) { 73 5501 : string link_name = LinkKey(metadata, left, right); 74 11002 : return FindLink(link_name); 75 5501 : } 76 : 77 9744 : IFMapLink *IFMapLinkTable::FindLink(const string &name) { 78 : 79 : DBTablePartition *partition = 80 9744 : static_cast<DBTablePartition *>(GetTablePartition(0)); 81 9744 : RequestKey key; 82 9744 : key.name = name; 83 19488 : return static_cast<IFMapLink *>(partition->Find(&key)); 84 9744 : } 85 : 86 0 : IFMapLink *IFMapLinkTable::FindNextLink(const string &name) { 87 : 88 : DBTablePartition *partition = 89 0 : static_cast<DBTablePartition *>(GetTablePartition(0)); 90 0 : RequestKey key; 91 0 : key.name = name; 92 0 : return static_cast<IFMapLink *>(partition->FindNext(&key)); 93 0 : } 94 : 95 488 : void IFMapLinkTable::DeleteLink(IFMapLink *link) { 96 488 : DBGraphEdge *edge = static_cast<DBGraphEdge *>(link); 97 488 : graph()->Unlink(edge); 98 488 : link->set_last_change_at_to_now(); 99 488 : link->ClearNodes(); 100 : DBTablePartition *partition = 101 488 : static_cast<DBTablePartition *>(GetTablePartition(0)); 102 488 : partition->Delete(edge); 103 488 : } 104 : 105 178 : void IFMapLinkTable::DeleteLink(IFMapLink *link, const IFMapOrigin &origin) { 106 178 : link->RemoveOriginInfo(origin.origin); 107 178 : if (link->is_origin_empty()) { 108 172 : DeleteLink(link); 109 : } 110 178 : } 111 : 112 565 : DBTable *IFMapLinkTable::CreateTable(DB *db, const string &name, 113 : DBGraph *graph) { 114 565 : IFMapLinkTable *table = new IFMapLinkTable(db, name, graph); 115 565 : table->Init(); 116 565 : return table; 117 : } 118 : 119 567 : void IFMapLinkTable::Clear() { 120 : DBTablePartition *partition = static_cast<DBTablePartition *>( 121 567 : GetTablePartition(0)); 122 : 123 567 : assert(!HasListeners()); 124 567 : for (IFMapLink *link = static_cast<IFMapLink *>(partition->GetFirst()), 125 4261 : *next = NULL; link != NULL; link = next) { 126 3694 : next = static_cast<IFMapLink *>(partition->GetNext(link)); 127 3694 : if (link->IsDeleted()) { 128 0 : continue; 129 : } 130 3694 : graph()->Unlink(link); 131 3694 : partition->Delete(link); 132 : } 133 567 : } 134 : 135 565 : void IFMapLinkTable_Init(DB *db, DBGraph *graph) { 136 : DBTable *table = 137 565 : IFMapLinkTable::CreateTable(db, "__ifmap_metadata__.0", graph); 138 565 : db->AddTable(table); 139 565 : } 140 : 141 160 : void IFMapLinkTable_Clear(DB *db) { 142 : IFMapLinkTable *ltable = static_cast<IFMapLinkTable *>( 143 160 : db->FindTable("__ifmap_metadata__.0")); 144 160 : ltable->Clear(); 145 160 : }