Skip to content

Commit a434bdf

Browse files
authored
Merge pull request #239 from ahoppen/timestamp-unit-output-path
Add an API to get the timestamp of a unit by its output path
2 parents 74da814 + 8ecd536 commit a434bdf

File tree

7 files changed

+66
-3
lines changed

7 files changed

+66
-3
lines changed

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,19 @@ public final class IndexStoreDB {
488488
}
489489
return Date(timeIntervalSince1970: Double(timestamp) / 1_000_000_000)
490490
}
491+
492+
/// Returns a modification date of the latest unit that contains the given source file.
493+
///
494+
/// If no unit containing the given source file exists, returns `nil`
495+
public func dateOfUnitFor(outputPath: String) -> Date? {
496+
let timestamp = outputPath.withCString { outputPathCString in
497+
indexstoredb_timestamp_of_unit_for_output_path(impl, outputPathCString)
498+
}
499+
if timestamp == 0 {
500+
return nil
501+
}
502+
return Date(timeIntervalSince1970: Double(timestamp) / 1_000_000_000)
503+
}
491504
}
492505

493506
public protocol IndexStoreLibraryProvider {

Sources/IndexStoreDB_CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,4 +698,21 @@ indexstoredb_timestamp_of_latest_unit_for_file(
698698
return 0;
699699
}
700700

701+
INDEXSTOREDB_PUBLIC uint64_t
702+
indexstoredb_timestamp_of_unit_for_output_path(
703+
_Nonnull indexstoredb_index_t index,
704+
const char *_Nonnull unitOutputPath
705+
) {
706+
auto obj = (Object<std::shared_ptr<IndexSystem>> *)index;
707+
llvm::Optional<llvm::sys::TimePoint<>> timePoint = obj->value->timestampOfUnitForOutputPath(unitOutputPath);
708+
if (timePoint) {
709+
// Up until C++20 the reference date of time_since_epoch is undefined but according to
710+
// https://en.cppreference.com/w/cpp/chrono/system_clock most implementations use Unix Time.
711+
// Since C++20, system_clock is defined to measure time since 1/1/1970.
712+
// We rely on `time_since_epoch` always returning the nanoseconds since 1/1/1970.
713+
return timePoint->time_since_epoch().count();
714+
}
715+
return 0;
716+
}
717+
701718
ObjectBase::~ObjectBase() {}

Sources/IndexStoreDB_CIndexStoreDB/include/IndexStoreDB_CIndexStoreDB/CIndexStoreDB.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,15 @@ indexstoredb_timestamp_of_latest_unit_for_file(
601601
const char *_Nonnull fileName
602602
);
603603

604+
/// Returns a Unix timestamp (nanoseconds since 1/1/1970) of the unit that has the given output path.
605+
///
606+
/// If no unit with this output paths exits, returns 0.
607+
INDEXSTOREDB_PUBLIC uint64_t
608+
indexstoredb_timestamp_of_unit_for_output_path(
609+
_Nonnull indexstoredb_index_t index,
610+
const char *_Nonnull unitOutputPath
611+
);
612+
604613
INDEXSTOREDB_END_DECLS
605614

606615
#endif

Sources/IndexStoreDB_Index/IndexDatastore.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "IndexDatastore.h"
14+
#include "IndexStoreDB_LLVMSupport/llvm_ADT_None.h"
1415
#include "StoreSymbolRecord.h"
1516
#include <IndexStoreDB_Core/Symbol.h>
1617
#include <IndexStoreDB_Index/FilePathIndex.h>
@@ -172,6 +173,7 @@ class IndexDatastoreImpl {
172173

173174
bool isUnitOutOfDate(StringRef unitOutputPath, ArrayRef<StringRef> dirtyFiles);
174175
bool isUnitOutOfDate(StringRef unitOutputPath, llvm::sys::TimePoint<> outOfDateModTime);
176+
llvm::Optional<sys::TimePoint<>> timestampOfUnitForOutputPath(StringRef unitOutputPath);
175177
void checkUnitContainingFileIsOutOfDate(StringRef file);
176178

177179
void addUnitOutFilePaths(ArrayRef<StringRef> filePaths, bool waitForProcessing);
@@ -1101,16 +1103,23 @@ bool IndexDatastoreImpl::isUnitOutOfDate(StringRef unitOutputPath, ArrayRef<Stri
11011103
}
11021104

11031105
bool IndexDatastoreImpl::isUnitOutOfDate(StringRef unitOutputPath, sys::TimePoint<> outOfDateModTime) {
1106+
auto unitModTime = timestampOfUnitForOutputPath(unitOutputPath);
1107+
if (!unitModTime) {
1108+
return true;
1109+
}
1110+
return outOfDateModTime > unitModTime;
1111+
}
1112+
1113+
llvm::Optional<sys::TimePoint<>> IndexDatastoreImpl::timestampOfUnitForOutputPath(StringRef unitOutputPath) {
11041114
SmallString<128> nameBuf;
11051115
IdxStore->getUnitNameFromOutputPath(unitOutputPath, nameBuf);
11061116
StringRef unitName = nameBuf.str();
11071117
std::string error;
11081118
auto optUnitModTime = IdxStore->getUnitModificationTime(unitName, error);
11091119
if (!optUnitModTime)
1110-
return true;
1120+
return llvm::None;
11111121

1112-
auto unitModTime = toTimePoint(optUnitModTime.getValue());
1113-
return outOfDateModTime > unitModTime;
1122+
return toTimePoint(optUnitModTime.getValue());
11141123
}
11151124

11161125
void IndexDatastoreImpl::checkUnitContainingFileIsOutOfDate(StringRef file) {
@@ -1169,6 +1178,10 @@ bool IndexDatastore::isUnitOutOfDate(StringRef unitOutputPath, sys::TimePoint<>
11691178
return IMPL->isUnitOutOfDate(unitOutputPath, outOfDateModTime);
11701179
}
11711180

1181+
llvm::Optional<sys::TimePoint<>> IndexDatastore::timestampOfUnitForOutputPath(StringRef unitOutputPath) {
1182+
return IMPL->timestampOfUnitForOutputPath(unitOutputPath);
1183+
}
1184+
11721185
void IndexDatastore::checkUnitContainingFileIsOutOfDate(StringRef file) {
11731186
return IMPL->checkUnitContainingFileIsOutOfDate(file);
11741187
}

Sources/IndexStoreDB_Index/IndexDatastore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class IndexDatastore {
4646

4747
bool isUnitOutOfDate(StringRef unitOutputPath, ArrayRef<StringRef> dirtyFiles);
4848
bool isUnitOutOfDate(StringRef unitOutputPath, llvm::sys::TimePoint<> outOfDateModTime);
49+
llvm::Optional<llvm::sys::TimePoint<>> timestampOfUnitForOutputPath(StringRef unitOutputPath);
4950

5051
/// Check whether any unit(s) containing \p file are out of date and if so,
5152
/// *synchronously* notify the delegate.

Sources/IndexStoreDB_Index/IndexSystem.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class IndexSystemImpl {
141141

142142
bool isUnitOutOfDate(StringRef unitOutputPath, ArrayRef<StringRef> dirtyFiles);
143143
bool isUnitOutOfDate(StringRef unitOutputPath, llvm::sys::TimePoint<> outOfDateModTime);
144+
llvm::Optional<llvm::sys::TimePoint<>> timestampOfUnitForOutputPath(StringRef unitOutputPath);
144145
void checkUnitContainingFileIsOutOfDate(StringRef file);
145146

146147
void registerMainFiles(ArrayRef<StringRef> filePaths, StringRef productName);
@@ -305,6 +306,10 @@ bool IndexSystemImpl::isUnitOutOfDate(StringRef unitOutputPath, llvm::sys::TimeP
305306
return IndexStore->isUnitOutOfDate(unitOutputPath, outOfDateModTime);
306307
}
307308

309+
llvm::Optional<llvm::sys::TimePoint<>> IndexSystemImpl::timestampOfUnitForOutputPath(StringRef unitOutputPath) {
310+
return IndexStore->timestampOfUnitForOutputPath(unitOutputPath);
311+
}
312+
308313
void IndexSystemImpl::checkUnitContainingFileIsOutOfDate(StringRef file) {
309314
return IndexStore->checkUnitContainingFileIsOutOfDate(file);
310315
}
@@ -686,6 +691,10 @@ bool IndexSystem::isUnitOutOfDate(StringRef unitOutputPath, llvm::sys::TimePoint
686691
return IMPL->isUnitOutOfDate(unitOutputPath, outOfDateModTime);
687692
}
688693

694+
llvm::Optional<llvm::sys::TimePoint<>> IndexSystem::timestampOfUnitForOutputPath(StringRef unitOutputPath) {
695+
return IMPL->timestampOfUnitForOutputPath(unitOutputPath);
696+
}
697+
689698
void IndexSystem::checkUnitContainingFileIsOutOfDate(StringRef file) {
690699
return IMPL->checkUnitContainingFileIsOutOfDate(file);
691700
}

Sources/IndexStoreDB_Index/include/IndexStoreDB_Index/IndexSystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class INDEXSTOREDB_EXPORT IndexSystem {
6767

6868
bool isUnitOutOfDate(StringRef unitOutputPath, ArrayRef<StringRef> dirtyFiles);
6969
bool isUnitOutOfDate(StringRef unitOutputPath, llvm::sys::TimePoint<> outOfDateModTime);
70+
llvm::Optional<llvm::sys::TimePoint<>> timestampOfUnitForOutputPath(StringRef unitOutputPath);
7071

7172
/// Check whether any unit(s) containing \p file are out of date and if so,
7273
/// *synchronously* notify the delegate.

0 commit comments

Comments
 (0)