NOTE: may take more than 100ms
| 806 | |
| 807 | // NOTE: may take more than 100ms |
| 808 | leveldb::Status SpanDB::RemoveSpansBefore(int64_t tm) { |
| 809 | if (id_db == NULL || time_db == NULL) { |
| 810 | return leveldb::Status::InvalidArgument(leveldb::Slice("NULL param")); |
| 811 | } |
| 812 | leveldb::Status rc; |
| 813 | leveldb::WriteOptions options; |
| 814 | options.sync = false; |
| 815 | leveldb::Iterator* it = time_db->NewIterator(leveldb::ReadOptions()); |
| 816 | for (it->SeekToFirst(); it->Valid(); it->Next()) { |
| 817 | if (it->key().size() != 8) { |
| 818 | LOG(ERROR) << "Invalid key size: " << it->key().size(); |
| 819 | continue; |
| 820 | } |
| 821 | const int64_t realtime = |
| 822 | ToLittleEndian((const uint32_t*)it->key().data()); |
| 823 | if (realtime >= tm) { // removal is done. |
| 824 | break; |
| 825 | } |
| 826 | BriefSpan brief; |
| 827 | if (brief.ParseFromArray(it->value().data(), it->value().size())) { |
| 828 | uint32_t key_data[4]; |
| 829 | ToBigEndian(brief.trace_id(), key_data); |
| 830 | ToBigEndian(brief.span_id(), key_data + 2); |
| 831 | leveldb::Slice key((char*)key_data, sizeof(key_data)); |
| 832 | rc = id_db->Delete(options, key); |
| 833 | if (!rc.ok()) { |
| 834 | LOG(ERROR) << "Fail to delete from id_db"; |
| 835 | break; |
| 836 | } |
| 837 | } else { |
| 838 | LOG(ERROR) << "Fail to parse value"; |
| 839 | } |
| 840 | rc = time_db->Delete(options, it->key()); |
| 841 | if (!rc.ok()) { |
| 842 | LOG(ERROR) << "Fail to delete from time_db"; |
| 843 | break; |
| 844 | } |
| 845 | } |
| 846 | delete it; |
| 847 | return rc; |
| 848 | } |
| 849 | |
| 850 | // Write span into leveldb. |
| 851 | void Span::dump_to_db() { |
no test coverage detected