Exemplo n.º 1
0
static bool migrateObjectStores(PassRefPtr<IDBBackingStore> fromBackingStore, int64_t fromDatabaseId, PassRefPtr<IDBBackingStore> toBackingStore, int64_t toDatabaseId)
{
    Vector<int64_t> fromObjStoreIds, toObjStoreIds;
    Vector<String> fromObjStoreNames, toObjStoreNames;
    Vector<String> fromKeyPaths, toKeyPaths;
    Vector<bool> fromAutoIncrement, toAutoIncrement;

    // Migrate objectStores. Check to see if the object store already exists in the target.
    fromBackingStore->getObjectStores(fromDatabaseId, fromObjStoreIds, fromObjStoreNames, fromKeyPaths, fromAutoIncrement);

    toBackingStore->getObjectStores(toDatabaseId, toObjStoreIds, toObjStoreNames, toKeyPaths, toAutoIncrement);

    for (unsigned i = 0; i < fromObjStoreIds.size(); i++) {
        if (toObjStoreNames.contains(fromObjStoreNames[i]))
            continue;

        int64_t assignedObjectStoreId = -1;

        RefPtr<IDBBackingStore::Transaction> trans = toBackingStore->createTransaction();
        trans->begin();

        if (!toBackingStore->createObjectStore(toDatabaseId, fromObjStoreNames[i], fromKeyPaths[i], fromAutoIncrement[i], assignedObjectStoreId))
            return false;

        RefPtr<IDBBackingStore::Cursor> cursor = fromBackingStore->openObjectStoreCursor(fromDatabaseId, fromObjStoreIds[i], 0, IDBCursor::NEXT);
        if (cursor) {
            do {
                RefPtr<IDBKey> key = cursor->key();
                RefPtr<IDBBackingStore::ObjectStoreRecordIdentifier> recordIdentifier = toBackingStore->createInvalidRecordIdentifier();

                if (!toBackingStore->putObjectStoreRecord(toDatabaseId, assignedObjectStoreId, *(key.get()), cursor->value(), recordIdentifier.get()))
                    return false;

            } while (cursor->continueFunction());
        }

        // Populate any/all indexes for this objectstore.
        Vector<int64_t> idxIds;
        Vector<String> idxNames;
        Vector<String> idxKeyPaths;
        Vector<bool> idxUnique;
        fromBackingStore->getIndexes(fromDatabaseId, fromObjStoreIds[i], idxIds, idxNames, idxKeyPaths, idxUnique);
        for (unsigned j = 0; j < idxIds.size(); j++) {
            int64_t indexId = -1;

            if (!toBackingStore->createIndex(toDatabaseId, assignedObjectStoreId, idxNames[j], idxKeyPaths[j], idxUnique[j], indexId))
                return false;

            if (!IDBObjectStoreBackendImpl::populateIndex(*toBackingStore, toDatabaseId, assignedObjectStoreId, indexId, fromKeyPaths[i]))
                return false;
        }

        trans->commit();
    }

    return true;
}