use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class JDBCCompositeCache method loadObjects.
protected synchronized void loadObjects(DBRProgressMonitor monitor, OWNER owner, PARENT forParent) throws DBException {
synchronized (objectCache) {
if ((forParent == null && isFullyCached()) || (forParent != null && (!forParent.isPersisted() || objectCache.containsKey(forParent)))) {
return;
}
}
// Load tables and columns first
if (forParent == null) {
parentCache.loadObjects(monitor, owner);
parentCache.loadChildren(monitor, owner, null);
}
Map<PARENT, Map<String, ObjectInfo>> parentObjectMap = new LinkedHashMap<>();
// Load index columns
DBPDataSource dataSource = owner.getDataSource();
assert (dataSource != null);
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Load composite objects")) {
JDBCStatement dbStat = prepareObjectsStatement(session, owner, forParent);
dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
try {
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
if (dbResult != null)
try {
while (dbResult.next()) {
if (monitor.isCanceled()) {
break;
}
String parentName = parentColumnName instanceof Number ? JDBCUtils.safeGetString(dbResult, ((Number) parentColumnName).intValue()) : JDBCUtils.safeGetString(dbResult, parentColumnName.toString());
String objectName = objectColumnName instanceof Number ? JDBCUtils.safeGetString(dbResult, ((Number) objectColumnName).intValue()) : JDBCUtils.safeGetString(dbResult, objectColumnName.toString());
if (CommonUtils.isEmpty(objectName)) {
// Use default name
objectName = getDefaultObjectName(dbResult, parentName);
}
if (forParent == null && CommonUtils.isEmpty(parentName)) {
// No parent - can't evaluate it
log.debug("Empty parent name in " + this);
continue;
}
PARENT parent = forParent;
if (parent == null) {
parent = parentCache.getObject(monitor, owner, parentName, parentType);
if (parent == null) {
log.debug("Object '" + objectName + "' owner '" + parentName + "' not found");
continue;
}
}
synchronized (objectCache) {
if (objectCache.containsKey(parent)) {
// Already cached
continue;
}
}
// Add to map
Map<String, ObjectInfo> objectMap = parentObjectMap.get(parent);
if (objectMap == null) {
objectMap = new TreeMap<>();
parentObjectMap.put(parent, objectMap);
}
ObjectInfo objectInfo = objectMap.get(objectName);
if (objectInfo == null) {
OBJECT object = fetchObject(session, owner, parent, objectName, dbResult);
if (object == null) {
// Can't fetch object
continue;
}
objectName = object.getName();
objectInfo = new ObjectInfo(object);
objectMap.put(objectName, objectInfo);
}
ROW_REF[] rowRef = fetchObjectRow(session, parent, objectInfo.object, dbResult);
if (rowRef == null || rowRef.length == 0) {
// At least one of rows is broken.
// So entire object is broken, let's just skip it.
objectInfo.broken = true;
//log.debug("Object '" + objectName + "' metadata corrupted - NULL child returned");
continue;
}
Collections.addAll(objectInfo.rows, rowRef);
}
} finally {
dbResult.close();
}
} finally {
dbStat.close();
}
} catch (SQLException ex) {
throw new DBException(ex, dataSource);
}
if (monitor.isCanceled()) {
return;
}
// Fill global cache
synchronized (this) {
synchronized (objectCache) {
if (forParent != null || !parentObjectMap.isEmpty()) {
if (forParent == null) {
// Cache global object list
List<OBJECT> globalCache = new ArrayList<>();
for (Map<String, ObjectInfo> objMap : parentObjectMap.values()) {
if (objMap != null) {
for (ObjectInfo info : objMap.values()) {
if (!info.broken) {
globalCache.add(info.object);
}
}
}
}
// Save precached objects in global cache
for (List<OBJECT> objects : objectCache.values()) {
globalCache.addAll(objects);
}
// Add precached objects to global cache too
super.setCache(globalCache);
this.invalidateObjects(monitor, owner, new CacheIterator());
}
}
// All objects are read. Now assign them to parents
for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
if (colEntry.getValue() == null || objectCache.containsKey(colEntry.getKey())) {
// Do not overwrite this object's cache
continue;
}
Collection<ObjectInfo> objectInfos = colEntry.getValue().values();
ArrayList<OBJECT> objects = new ArrayList<>(objectInfos.size());
for (ObjectInfo objectInfo : objectInfos) {
objectInfo.needsCaching = true;
objects.add(objectInfo.object);
}
objectCache.put(colEntry.getKey(), objects);
}
// Now set empty object list for other parents
if (forParent == null) {
for (PARENT tmpParent : parentCache.getTypedObjects(monitor, owner, parentType)) {
if (!parentObjectMap.containsKey(tmpParent) && !objectCache.containsKey(tmpParent)) {
objectCache.put(tmpParent, new ArrayList<OBJECT>());
}
}
} else if (!parentObjectMap.containsKey(forParent) && !objectCache.containsKey(forParent)) {
objectCache.put(forParent, new ArrayList<OBJECT>());
}
}
// Cache children lists (we do it in the end because children caching may operate with other model objects)
for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
for (ObjectInfo objectInfo : colEntry.getValue().values()) {
if (objectInfo.needsCaching) {
cacheChildren(monitor, objectInfo.object, objectInfo.rows);
}
}
}
for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
for (ObjectInfo objectInfo : colEntry.getValue().values()) {
if (objectInfo.needsCaching) {
cacheChildren2(monitor, objectInfo.object, objectInfo.rows);
}
}
}
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class JDBCObjectCache method loadObjects.
protected synchronized void loadObjects(DBRProgressMonitor monitor, OWNER owner) throws DBException {
if (isFullyCached() || monitor.isCanceled()) {
return;
}
List<OBJECT> tmpObjectList = new ArrayList<>();
DBPDataSource dataSource = owner.getDataSource();
if (dataSource == null) {
throw new DBException("Not connected to database");
}
try {
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Load objects from " + owner.getName())) {
try (JDBCStatement dbStat = prepareObjectsStatement(session, owner)) {
monitor.subTask("Execute query");
dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
if (dbResult != null) {
try {
while (dbResult.next()) {
if (monitor.isCanceled()) {
break;
}
OBJECT object = fetchObject(session, owner, dbResult);
if (object == null) {
continue;
}
tmpObjectList.add(object);
//monitor.subTask(object.getName());
if (tmpObjectList.size() == maximumCacheSize) {
log.warn("Maximum cache size exceeded (" + maximumCacheSize + ") in " + this);
break;
}
}
} finally {
dbResult.close();
}
}
}
} catch (SQLException ex) {
throw new DBException(ex, dataSource);
}
} catch (DBException e) {
if (!handleCacheReadError(e)) {
throw e;
}
}
Comparator<OBJECT> comparator = getListOrderComparator();
if (comparator != null) {
Collections.sort(tmpObjectList, comparator);
}
detectCaseSensitivity(owner);
mergeCache(tmpObjectList);
this.invalidateObjects(monitor, owner, new CacheIterator());
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class JDBCStructLookupCache method reloadObject.
protected OBJECT reloadObject(@NotNull DBRProgressMonitor monitor, @NotNull OWNER owner, @Nullable OBJECT object, @Nullable String objectName) throws DBException {
DBPDataSource dataSource = owner.getDataSource();
if (dataSource == null) {
throw new DBException("Not connected to database");
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, object == null ? "Load object '" + objectName + "' from " + owner.getName() : "Reload object '" + object + "' from " + owner.getName())) {
try (JDBCStatement dbStat = prepareLookupStatement(session, owner, object, objectName)) {
dbStat.setFetchSize(1);
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
if (dbResult != null) {
try {
if (dbResult.next()) {
return fetchObject(session, owner, dbResult);
}
} finally {
dbResult.close();
}
}
return null;
}
} catch (SQLException ex) {
throw new DBException("Error loading object metadata from database", ex, dataSource);
}
}
Aggregations